|
@@ -5,6 +5,7 @@ import (
|
|
"eta/eta_api/utils"
|
|
"eta/eta_api/utils"
|
|
"fmt"
|
|
"fmt"
|
|
"math"
|
|
"math"
|
|
|
|
+ "strings"
|
|
"time"
|
|
"time"
|
|
)
|
|
)
|
|
|
|
|
|
@@ -30,9 +31,9 @@ func CheckAnalysisTableExtraConfig(extraConfig tradeAnalysisModel.TableExtraConf
|
|
}
|
|
}
|
|
classifyTotal += len(v.ClassifyNames)
|
|
classifyTotal += len(v.ClassifyNames)
|
|
}
|
|
}
|
|
- // TODO:品种选择上限
|
|
|
|
- if classifyTotal > 5 {
|
|
|
|
- tips = "选择品种不超过5个"
|
|
|
|
|
|
+ // 品种选择加个上限吧,过多SQL会很慢
|
|
|
|
+ if classifyTotal > 20 {
|
|
|
|
+ tips = "选择品种不超过20个"
|
|
return
|
|
return
|
|
}
|
|
}
|
|
typeArr := []int{tradeAnalysisModel.ContractQueryTypeTop, tradeAnalysisModel.ContractQueryTypeTop2, tradeAnalysisModel.ContractQueryTypeTop3, tradeAnalysisModel.ContractQueryTypeAll, tradeAnalysisModel.ContractQueryTypeTotal}
|
|
typeArr := []int{tradeAnalysisModel.ContractQueryTypeTop, tradeAnalysisModel.ContractQueryTypeTop2, tradeAnalysisModel.ContractQueryTypeTop3, tradeAnalysisModel.ContractQueryTypeAll, tradeAnalysisModel.ContractQueryTypeTotal}
|
|
@@ -154,10 +155,9 @@ func GetTableRowsDataByConfig(tableConfig tradeAnalysisModel.TableExtraConfig) (
|
|
return
|
|
return
|
|
}
|
|
}
|
|
|
|
|
|
- // 查询基准日期TOP20合约排名, 根据类型取出合约数
|
|
|
|
- exchangeContracts := make(map[string][]*tradeAnalysisModel.ContractTopRankData) // 交易所最终取的合约
|
|
|
|
- var contractMax int // 需要取出的品种对应的最大合约数
|
|
|
|
- classifyMax := make(map[string]int) // 品种对应的合约数
|
|
|
|
|
|
+ // 根据类型取出合约数
|
|
|
|
+ var contractMax int // 需要取出的品种对应的最大合约数
|
|
|
|
+ //classifyMax := make(map[string]int) // 品种对应的合约数
|
|
switch tableConfig.ContractType {
|
|
switch tableConfig.ContractType {
|
|
case tradeAnalysisModel.ContractQueryTypeTop:
|
|
case tradeAnalysisModel.ContractQueryTypeTop:
|
|
contractMax = 1
|
|
contractMax = 1
|
|
@@ -168,46 +168,70 @@ func GetTableRowsDataByConfig(tableConfig tradeAnalysisModel.TableExtraConfig) (
|
|
case tradeAnalysisModel.ContractQueryTypeAll, tradeAnalysisModel.ContractQueryTypeTotal:
|
|
case tradeAnalysisModel.ContractQueryTypeAll, tradeAnalysisModel.ContractQueryTypeTotal:
|
|
contractMax = 999
|
|
contractMax = 999
|
|
}
|
|
}
|
|
- // 遍历交易所, 查询各品种下的合约排名情况及TOP当日的多空单数据
|
|
|
|
- var sortContract []string
|
|
|
|
- for _, v := range tableConfig.ClassifyList {
|
|
|
|
- contractRanks, e := GetTopContractRank(v.Exchange, v.ClassifyNames, baseDate)
|
|
|
|
- if e != nil {
|
|
|
|
- err = fmt.Errorf("获取基准日期合约排名失败, %v", e)
|
|
|
|
- return
|
|
|
|
- }
|
|
|
|
- if len(contractRanks) == 0 {
|
|
|
|
- continue
|
|
|
|
- }
|
|
|
|
|
|
|
|
- // ps.正常来讲这里查出来的合约是唯一的, 根据品种分组, 取出ContractType所需的合约数
|
|
|
|
- for _, rd := range contractRanks {
|
|
|
|
- if classifyMax[rd.ClassifyName] >= contractMax {
|
|
|
|
|
|
+ var sortRules []string // 最终排序(合约加总以品种排序,其他以为合约排序)
|
|
|
|
+ classifyContractsData := make(map[string][]*tradeAnalysisModel.ContractTopRankData) // 根据品种分组的合约数据
|
|
|
|
+ classifyContracts := make(map[string][]string) // 品种合约
|
|
|
|
+ for _, v := range tableConfig.ClassifyList {
|
|
|
|
+ for _, classify := range v.ClassifyNames {
|
|
|
|
+ // 获取合约排名及持仓数据
|
|
|
|
+ contractRanks, e := GetTopContractRank(v.Exchange, []string{classify}, baseDate)
|
|
|
|
+ if e != nil {
|
|
|
|
+ err = fmt.Errorf("获取基准日期合约排名失败, %v", e)
|
|
|
|
+ return
|
|
|
|
+ }
|
|
|
|
+ if len(contractRanks) == 0 {
|
|
continue
|
|
continue
|
|
}
|
|
}
|
|
- classifyMax[rd.ClassifyName] += 1
|
|
|
|
- if exchangeContracts[rd.Exchange] == nil {
|
|
|
|
- exchangeContracts[rd.Exchange] = make([]*tradeAnalysisModel.ContractTopRankData, 0)
|
|
|
|
|
|
+ flag := fmt.Sprintf("%s-%s", v.Exchange, classify)
|
|
|
|
+
|
|
|
|
+ // 取出指定数量的合约
|
|
|
|
+ var contractNum int
|
|
|
|
+ for _, rd := range contractRanks {
|
|
|
|
+ if contractNum >= contractMax {
|
|
|
|
+ continue
|
|
|
|
+ }
|
|
|
|
+ contractNum += 1
|
|
|
|
+ classifyContracts[classify] = append(classifyContracts[classify], rd.ClassifyType)
|
|
|
|
+
|
|
|
|
+ if classifyContractsData[flag] == nil {
|
|
|
|
+ classifyContractsData[flag] = make([]*tradeAnalysisModel.ContractTopRankData, 0)
|
|
|
|
+ }
|
|
|
|
+ classifyContractsData[flag] = append(classifyContractsData[flag], rd)
|
|
|
|
+
|
|
|
|
+ // 以合约排序
|
|
|
|
+ if tableConfig.ContractType != tradeAnalysisModel.ContractQueryTypeTotal {
|
|
|
|
+ sortRules = append(sortRules, rd.ClassifyType)
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ // 以品种排序
|
|
|
|
+ if tableConfig.ContractType == tradeAnalysisModel.ContractQueryTypeTotal {
|
|
|
|
+ sortRules = append(sortRules, classify)
|
|
}
|
|
}
|
|
- exchangeContracts[rd.Exchange] = append(exchangeContracts[rd.Exchange], rd)
|
|
|
|
- sortContract = append(sortContract, rd.ClassifyType)
|
|
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
- // 查询对应品种与合约一周前后的多空单,填充预估值
|
|
|
|
mussyRows := make([]*tradeAnalysisModel.TableRowData, 0)
|
|
mussyRows := make([]*tradeAnalysisModel.TableRowData, 0)
|
|
- for exchange, contracts := range exchangeContracts {
|
|
|
|
- var classifyNames, classifyTypes []string
|
|
|
|
- for _, v := range contracts {
|
|
|
|
- if !utils.InArrayByStr(classifyNames, v.ClassifyName) {
|
|
|
|
- classifyNames = append(classifyNames, v.ClassifyName)
|
|
|
|
- }
|
|
|
|
- if !utils.InArrayByStr(classifyTypes, v.ClassifyType) {
|
|
|
|
- classifyTypes = append(classifyTypes, v.ClassifyType)
|
|
|
|
- }
|
|
|
|
|
|
+ for k, contracts := range classifyContractsData {
|
|
|
|
+ var exchange, classifyName string
|
|
|
|
+ keyArr := strings.Split(k, "-")
|
|
|
|
+ if len(keyArr) != 2 {
|
|
|
|
+ continue
|
|
|
|
+ }
|
|
|
|
+ exchange = keyArr[0]
|
|
|
|
+ classifyName = keyArr[1]
|
|
|
|
+ classifyTypes := classifyContracts[classifyName]
|
|
|
|
+ if len(classifyTypes) == 0 {
|
|
|
|
+ continue
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ // 合约加总时,contracts也需要加总,且ClassifyType为品种名
|
|
|
|
+ if tableConfig.ContractType == tradeAnalysisModel.ContractQueryTypeTotal {
|
|
|
|
+ contracts = MergeClassifyTypeTopRankData(contracts)
|
|
}
|
|
}
|
|
|
|
|
|
- contractRowData, e := GetTableTradeData(exchange, classifyNames, classifyTypes, []string{tableConfig.CompanyName}, tableConfig.PredictRatio, baseDate.AddDate(0, 0, -5), baseDate.AddDate(0, 0, 5), tableConfig.ContractType)
|
|
|
|
|
|
+ contractRowData, e := GetTableTradeData(exchange, classifyName, classifyTypes, tableConfig.CompanyName, tableConfig.PredictRatio, baseDate.AddDate(0, 0, -5), baseDate.AddDate(0, 0, 5), tableConfig.ContractType)
|
|
if e != nil {
|
|
if e != nil {
|
|
err = fmt.Errorf("获取公司合约多空单数据失败, %v", e)
|
|
err = fmt.Errorf("获取公司合约多空单数据失败, %v", e)
|
|
return
|
|
return
|
|
@@ -231,7 +255,7 @@ func GetTableRowsDataByConfig(tableConfig tradeAnalysisModel.TableExtraConfig) (
|
|
for _, v := range mussyRows {
|
|
for _, v := range mussyRows {
|
|
contractRow[v.ClassifyType] = v
|
|
contractRow[v.ClassifyType] = v
|
|
}
|
|
}
|
|
- for _, v := range sortContract {
|
|
|
|
|
|
+ for _, v := range sortRules {
|
|
t := contractRow[v]
|
|
t := contractRow[v]
|
|
if t != nil {
|
|
if t != nil {
|
|
tableRows = append(tableRows, t)
|
|
tableRows = append(tableRows, t)
|
|
@@ -239,3 +263,25 @@ func GetTableRowsDataByConfig(tableConfig tradeAnalysisModel.TableExtraConfig) (
|
|
}
|
|
}
|
|
return
|
|
return
|
|
}
|
|
}
|
|
|
|
+
|
|
|
|
+// MergeClassifyTypeTopRankData 类型为合约加总时-合并当日的TOP数据
|
|
|
|
+func MergeClassifyTypeTopRankData(classifyTypesData []*tradeAnalysisModel.ContractTopRankData) (mergedData []*tradeAnalysisModel.ContractTopRankData) {
|
|
|
|
+ mergedData = make([]*tradeAnalysisModel.ContractTopRankData, 0)
|
|
|
|
+
|
|
|
|
+ mergeData := new(tradeAnalysisModel.ContractTopRankData)
|
|
|
|
+ for _, v := range classifyTypesData {
|
|
|
|
+ mergeData.Exchange = v.Exchange
|
|
|
|
+ mergeData.DealValue += v.DealValue
|
|
|
|
+ mergeData.BuyValue += v.BuyValue
|
|
|
|
+ mergeData.BuyChange += v.BuyChange
|
|
|
|
+ mergeData.SoldValue += v.SoldValue
|
|
|
|
+ mergeData.SoldChange += v.SoldChange
|
|
|
|
+ mergeData.PureBuyValue += v.PureBuyValue
|
|
|
|
+ mergeData.PureBuyChange += v.PureBuyChange
|
|
|
|
+ mergeData.ClassifyName = v.ClassifyName
|
|
|
|
+ mergeData.ClassifyType = v.ClassifyName // 合约合并后为品种名
|
|
|
|
+ mergeData.DataTime = v.DataTime
|
|
|
|
+ }
|
|
|
|
+ mergedData = append(mergedData, mergeData)
|
|
|
|
+ return
|
|
|
|
+}
|