|
@@ -59,144 +59,18 @@ func FormatCompanyTradeData2EdbData(companyTradeData *tradeAnalysisModel.Contrac
|
|
|
return
|
|
|
}
|
|
|
|
|
|
-func GetOriginTradeData(exchange, classifyName string, contracts, companies []string, predictRatio float64) (companyTradeData []*tradeAnalysisModel.ContractCompanyTradeData, err error) {
|
|
|
- // 各原始数据表期货公司名称不一致
|
|
|
- companyMap := make(map[string]string)
|
|
|
- {
|
|
|
- ob := new(tradeAnalysisModel.TradeFuturesCompany)
|
|
|
- list, e := ob.GetItemsByCondition(``, make([]interface{}, 0), []string{}, "")
|
|
|
- if e != nil {
|
|
|
- err = fmt.Errorf("获取期货公司名称失败: %v", e)
|
|
|
- return
|
|
|
- }
|
|
|
- switch exchange {
|
|
|
- case "zhengzhou":
|
|
|
- for _, v := range list {
|
|
|
- companyMap[v.CompanyName] = v.ZhengzhouName
|
|
|
- }
|
|
|
- case "dalian":
|
|
|
- for _, v := range list {
|
|
|
- companyMap[v.CompanyName] = v.DalianName
|
|
|
- }
|
|
|
- case "shanghai":
|
|
|
- for _, v := range list {
|
|
|
- companyMap[v.CompanyName] = v.ShanghaiName
|
|
|
- }
|
|
|
- case "cffex":
|
|
|
- for _, v := range list {
|
|
|
- companyMap[v.CompanyName] = v.CffexName
|
|
|
- }
|
|
|
- case "ine":
|
|
|
- for _, v := range list {
|
|
|
- companyMap[v.CompanyName] = v.IneName
|
|
|
- }
|
|
|
- case "guangzhou":
|
|
|
- for _, v := range list {
|
|
|
- companyMap[v.CompanyName] = v.GuangzhouName
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
- var queryCompanies []string
|
|
|
- for _, v := range companies {
|
|
|
- if v == tradeAnalysisModel.TradeFuturesCompanyTop20 {
|
|
|
- queryCompanies = append(queryCompanies, tradeAnalysisModel.TradeFuturesCompanyTop20)
|
|
|
- continue
|
|
|
- }
|
|
|
- companyName, ok := companyMap[v]
|
|
|
- if !ok {
|
|
|
- utils.FileLog.Info(fmt.Sprintf("交易所%s公司名称映射不存在: %s", exchange, v))
|
|
|
- continue
|
|
|
- }
|
|
|
- queryCompanies = append(queryCompanies, companyName)
|
|
|
- }
|
|
|
-
|
|
|
- // 郑商所/广期所查询方式不一样
|
|
|
- var tradeAnalysis TradeAnalysisInterface
|
|
|
- switch exchange {
|
|
|
- case tradeAnalysisModel.TradeExchangeZhengzhou:
|
|
|
- tradeAnalysis = &ZhengzhouTradeAnalysis{}
|
|
|
- case tradeAnalysisModel.TradeExchangeGuangzhou:
|
|
|
- tradeAnalysis = &GuangzhouTradeAnalysis{}
|
|
|
- default:
|
|
|
- tradeAnalysis = &BaseTradeAnalysis{}
|
|
|
- }
|
|
|
-
|
|
|
- // 获取多单/空单原始数据
|
|
|
- originList, e := tradeAnalysis.GetTradeDataByClassifyAndCompany(exchange, classifyName, contracts, queryCompanies)
|
|
|
+func GetWarehouseTradeData(exchange, classifyName string, contracts, companies []string, predictRatio float64) (companyTradeData []*tradeAnalysisModel.ContractCompanyTradeData, err error) {
|
|
|
+ // 获取合约持仓数据
|
|
|
+ contractTradeData, lastBuyVal, lastSoldVal, e := GetContractCompanyTradeData(exchange, []string{classifyName}, contracts, companies, time.Time{}, time.Time{})
|
|
|
if e != nil {
|
|
|
- err = fmt.Errorf("获取多空单原始数据失败, %v", e)
|
|
|
+ err = fmt.Errorf("获取合约-持仓数据失败, %v", e)
|
|
|
return
|
|
|
}
|
|
|
|
|
|
- keyItems := make(map[string]*tradeAnalysisModel.ContractCompanyTradeData)
|
|
|
- keyDateData := make(map[string]*tradeAnalysisModel.ContractCompanyTradeDataList)
|
|
|
- keyDateDataExist := make(map[string]bool)
|
|
|
- for _, v := range originList {
|
|
|
- // Rank999和0对应的是TOP20
|
|
|
- companyName := v.CompanyName
|
|
|
- if v.Rank == 999 || v.Rank == 0 {
|
|
|
- companyName = tradeAnalysisModel.TradeFuturesCompanyTop20
|
|
|
- }
|
|
|
-
|
|
|
- k := fmt.Sprintf("%s-%s", v.ClassifyType, companyName)
|
|
|
- if keyItems[k] == nil {
|
|
|
- keyItems[k] = new(tradeAnalysisModel.ContractCompanyTradeData)
|
|
|
- keyItems[k].CompanyName = companyName
|
|
|
- keyItems[k].ClassifyType = v.ClassifyType
|
|
|
- keyItems[k].DataList = make([]*tradeAnalysisModel.ContractCompanyTradeDataList, 0)
|
|
|
- }
|
|
|
-
|
|
|
- kd := fmt.Sprintf("%s-%s", k, v.DataTime.Format(utils.FormatDate))
|
|
|
- if keyDateData[kd] == nil {
|
|
|
- keyDateData[kd] = new(tradeAnalysisModel.ContractCompanyTradeDataList)
|
|
|
- keyDateData[kd].Date = v.DataTime
|
|
|
- }
|
|
|
- if v.ValType == 1 {
|
|
|
- keyDateData[kd].BuyVal = v.Val
|
|
|
- keyDateData[kd].BuyValType = tradeAnalysisModel.TradeDataTypeOrigin
|
|
|
- keyDateData[kd].BuyChange = v.ValChange
|
|
|
- keyDateData[kd].BuyChangeType = tradeAnalysisModel.TradeDataTypeOrigin
|
|
|
- }
|
|
|
- if v.ValType == 2 {
|
|
|
- keyDateData[kd].SoldVal = v.Val
|
|
|
- keyDateData[kd].SoldValType = tradeAnalysisModel.TradeDataTypeOrigin
|
|
|
- keyDateData[kd].SoldChange = v.ValChange
|
|
|
- keyDateData[kd].SoldChangeType = tradeAnalysisModel.TradeDataTypeOrigin
|
|
|
- }
|
|
|
- if !keyDateDataExist[kd] {
|
|
|
- keyItems[k].DataList = append(keyItems[k].DataList, keyDateData[kd])
|
|
|
- keyDateDataExist[kd] = true
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- // 获取[合约]每日的末位多空单
|
|
|
- contractLastBuyDateVal := make(map[string]map[time.Time]int)
|
|
|
- contractLastSoldDateVal := make(map[string]map[time.Time]int)
|
|
|
- {
|
|
|
- lastOriginList, e := tradeAnalysis.GetLastTradeDataByClassify(exchange, classifyName, contracts)
|
|
|
- if e != nil {
|
|
|
- err = fmt.Errorf("获取末位多空单原始数据失败, %v", e)
|
|
|
- return
|
|
|
- }
|
|
|
- for _, v := range lastOriginList {
|
|
|
- if v.ValType == 1 {
|
|
|
- if contractLastBuyDateVal[v.ClassifyType] == nil {
|
|
|
- contractLastBuyDateVal[v.ClassifyType] = make(map[time.Time]int)
|
|
|
- }
|
|
|
- contractLastBuyDateVal[v.ClassifyType][v.DataTime] = v.Val
|
|
|
- continue
|
|
|
- }
|
|
|
- if contractLastSoldDateVal[v.ClassifyType] == nil {
|
|
|
- contractLastSoldDateVal[v.ClassifyType] = make(map[time.Time]int)
|
|
|
- }
|
|
|
- contractLastSoldDateVal[v.ClassifyType][v.DataTime] = v.Val
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
// 填充[合约-公司]预估数据, 并根据[公司-多合约]分组, [公司]算作一个指标, 指标值为[多个合约]的计算加总
|
|
|
companyContracts := make(map[string][]*tradeAnalysisModel.ContractCompanyTradeData)
|
|
|
- for _, v := range keyItems {
|
|
|
- td, fd, ed, e := PredictingTradeData(v.DataList, contractLastBuyDateVal[v.ClassifyType], contractLastSoldDateVal[v.ClassifyType], predictRatio)
|
|
|
+ for _, v := range contractTradeData {
|
|
|
+ td, fd, ed, e := PredictingTradeData(v.DataList, lastBuyVal[v.ClassifyType], lastSoldVal[v.ClassifyType], predictRatio)
|
|
|
if e != nil {
|
|
|
err = fmt.Errorf("数据补全失败, %v", e)
|
|
|
return
|
|
@@ -302,6 +176,87 @@ func GetOriginTradeData(exchange, classifyName string, contracts, companies []st
|
|
|
|
|
|
// PredictingTradeData 根据数据库中的多空数据填充预估数据
|
|
|
func PredictingTradeData(originData []*tradeAnalysisModel.ContractCompanyTradeDataList, lastBuyDateVal, lastSoldDateVal map[time.Time]int, predictRatio float64) (newData []*tradeAnalysisModel.ContractCompanyTradeDataList, firstDate, endDate time.Time, err error) {
|
|
|
+ // 测试用的验证数据
|
|
|
+ //lastBuyDateVal, lastSoldDateVal = make(map[time.Time]int), make(map[time.Time]int)
|
|
|
+ //lastBuyDateVal[time.Date(2024, 7, 16, 0, 0, 0, 0, time.Local)] = 4602
|
|
|
+ //lastBuyDateVal[time.Date(2024, 7, 17, 0, 0, 0, 0, time.Local)] = 5116
|
|
|
+ //lastBuyDateVal[time.Date(2024, 7, 18, 0, 0, 0, 0, time.Local)] = 5130
|
|
|
+ //lastBuyDateVal[time.Date(2024, 7, 19, 0, 0, 0, 0, time.Local)] = 5354
|
|
|
+ //lastBuyDateVal[time.Date(2024, 7, 22, 0, 0, 0, 0, time.Local)] = 5916
|
|
|
+ //lastBuyDateVal[time.Date(2024, 7, 23, 0, 0, 0, 0, time.Local)] = 6524
|
|
|
+ //lastBuyDateVal[time.Date(2024, 7, 26, 0, 0, 0, 0, time.Local)] = 6575
|
|
|
+ //lastBuyDateVal[time.Date(2024, 7, 29, 0, 0, 0, 0, time.Local)] = 7461
|
|
|
+ //lastBuyDateVal[time.Date(2024, 7, 30, 0, 0, 0, 0, time.Local)] = 8488
|
|
|
+ //
|
|
|
+ //lastSoldDateVal[time.Date(2024, 7, 11, 0, 0, 0, 0, time.Local)] = 5467
|
|
|
+ //lastSoldDateVal[time.Date(2024, 7, 12, 0, 0, 0, 0, time.Local)] = 5248
|
|
|
+ //lastSoldDateVal[time.Date(2024, 7, 15, 0, 0, 0, 0, time.Local)] = 5102
|
|
|
+ //lastSoldDateVal[time.Date(2024, 7, 16, 0, 0, 0, 0, time.Local)] = 4771
|
|
|
+ //lastSoldDateVal[time.Date(2024, 7, 23, 0, 0, 0, 0, time.Local)] = 5989
|
|
|
+ //lastSoldDateVal[time.Date(2024, 7, 26, 0, 0, 0, 0, time.Local)] = 6745
|
|
|
+ //lastSoldDateVal[time.Date(2024, 7, 30, 0, 0, 0, 0, time.Local)] = 7272
|
|
|
+ //
|
|
|
+ //originData = make([]*tradeAnalysisModel.ContractCompanyTradeDataList, 0)
|
|
|
+ //originData = append(originData, &tradeAnalysisModel.ContractCompanyTradeDataList{
|
|
|
+ // Date: time.Date(2024, 7, 10, 0, 0, 0, 0, time.Local),
|
|
|
+ // BuyVal: 14324,
|
|
|
+ // BuyValType: tradeAnalysisModel.TradeDataTypeOrigin,
|
|
|
+ // BuyChange: -1107,
|
|
|
+ // BuyChangeType: tradeAnalysisModel.TradeDataTypeOrigin,
|
|
|
+ // SoldVal: 0,
|
|
|
+ // SoldValType: tradeAnalysisModel.TradeDataTypeNull,
|
|
|
+ // SoldChange: 0,
|
|
|
+ // SoldChangeType: tradeAnalysisModel.TradeDataTypeNull,
|
|
|
+ //}, &tradeAnalysisModel.ContractCompanyTradeDataList{
|
|
|
+ // Date: time.Date(2024, 7, 11, 0, 0, 0, 0, time.Local),
|
|
|
+ // BuyVal: 14280,
|
|
|
+ // BuyValType: tradeAnalysisModel.TradeDataTypeOrigin,
|
|
|
+ // BuyChange: -44,
|
|
|
+ // BuyChangeType: tradeAnalysisModel.TradeDataTypeOrigin,
|
|
|
+ //}, &tradeAnalysisModel.ContractCompanyTradeDataList{
|
|
|
+ // Date: time.Date(2024, 7, 12, 0, 0, 0, 0, time.Local),
|
|
|
+ // BuyVal: 14214,
|
|
|
+ // BuyValType: tradeAnalysisModel.TradeDataTypeOrigin,
|
|
|
+ // BuyChange: -66,
|
|
|
+ // BuyChangeType: tradeAnalysisModel.TradeDataTypeOrigin,
|
|
|
+ //}, &tradeAnalysisModel.ContractCompanyTradeDataList{
|
|
|
+ // Date: time.Date(2024, 7, 15, 0, 0, 0, 0, time.Local),
|
|
|
+ // BuyVal: 14269,
|
|
|
+ // BuyValType: tradeAnalysisModel.TradeDataTypeOrigin,
|
|
|
+ // BuyChange: 55,
|
|
|
+ // BuyChangeType: tradeAnalysisModel.TradeDataTypeOrigin,
|
|
|
+ //}, &tradeAnalysisModel.ContractCompanyTradeDataList{
|
|
|
+ // Date: time.Date(2024, 7, 17, 0, 0, 0, 0, time.Local),
|
|
|
+ // SoldVal: 5254,
|
|
|
+ // SoldValType: tradeAnalysisModel.TradeDataTypeOrigin,
|
|
|
+ // SoldChange: 708,
|
|
|
+ // SoldChangeType: tradeAnalysisModel.TradeDataTypeOrigin,
|
|
|
+ //}, &tradeAnalysisModel.ContractCompanyTradeDataList{
|
|
|
+ // Date: time.Date(2024, 7, 18, 0, 0, 0, 0, time.Local),
|
|
|
+ // SoldVal: 6595,
|
|
|
+ // SoldValType: tradeAnalysisModel.TradeDataTypeOrigin,
|
|
|
+ // SoldChange: 1341,
|
|
|
+ // SoldChangeType: tradeAnalysisModel.TradeDataTypeOrigin,
|
|
|
+ //}, &tradeAnalysisModel.ContractCompanyTradeDataList{
|
|
|
+ // Date: time.Date(2024, 7, 19, 0, 0, 0, 0, time.Local),
|
|
|
+ // SoldVal: 5938,
|
|
|
+ // SoldValType: tradeAnalysisModel.TradeDataTypeOrigin,
|
|
|
+ // SoldChange: -657,
|
|
|
+ // SoldChangeType: tradeAnalysisModel.TradeDataTypeOrigin,
|
|
|
+ //}, &tradeAnalysisModel.ContractCompanyTradeDataList{
|
|
|
+ // Date: time.Date(2024, 7, 22, 0, 0, 0, 0, time.Local),
|
|
|
+ // SoldVal: 6131,
|
|
|
+ // SoldValType: tradeAnalysisModel.TradeDataTypeOrigin,
|
|
|
+ // SoldChange: 193,
|
|
|
+ // SoldChangeType: tradeAnalysisModel.TradeDataTypeOrigin,
|
|
|
+ //}, &tradeAnalysisModel.ContractCompanyTradeDataList{
|
|
|
+ // Date: time.Date(2024, 7, 29, 0, 0, 0, 0, time.Local),
|
|
|
+ // SoldVal: 6679,
|
|
|
+ // SoldValType: tradeAnalysisModel.TradeDataTypeOrigin,
|
|
|
+ // SoldChange: 312,
|
|
|
+ // SoldChangeType: tradeAnalysisModel.TradeDataTypeOrigin,
|
|
|
+ //})
|
|
|
+
|
|
|
if len(originData) == 0 {
|
|
|
return
|
|
|
}
|
|
@@ -478,3 +433,502 @@ func PredictingTradeData(originData []*tradeAnalysisModel.ContractCompanyTradeDa
|
|
|
}
|
|
|
return
|
|
|
}
|
|
|
+
|
|
|
+// GetTopContractRank 获取TOP20根据成交量的合约排名
|
|
|
+func GetTopContractRank(exchange string, classifyNames []string, dataDate time.Time) (items []*tradeAnalysisModel.ContractTopRankData, err error) {
|
|
|
+ // 郑商所/广期所查询方式不一样
|
|
|
+ var tradeAnalysis TradeAnalysisInterface
|
|
|
+ switch exchange {
|
|
|
+ case tradeAnalysisModel.TradeExchangeZhengzhou:
|
|
|
+ tradeAnalysis = &ZhengzhouTradeAnalysis{}
|
|
|
+ case tradeAnalysisModel.TradeExchangeGuangzhou:
|
|
|
+ tradeAnalysis = &GuangzhouTradeAnalysis{}
|
|
|
+ default:
|
|
|
+ tradeAnalysis = &BaseTradeAnalysis{}
|
|
|
+ }
|
|
|
+
|
|
|
+ // 郑商所-需要把所选品种转为实际合约进行后续的查询
|
|
|
+ if exchange == tradeAnalysisModel.TradeExchangeZhengzhou {
|
|
|
+ classifies, e := GetZhengzhouContractsByClassifyNames(classifyNames)
|
|
|
+ if e != nil {
|
|
|
+ err = fmt.Errorf("获取郑商所实际合约失败, %v", e)
|
|
|
+ return
|
|
|
+ }
|
|
|
+ classifyNames = classifies
|
|
|
+ }
|
|
|
+
|
|
|
+ // 获取多单/空单原始数据
|
|
|
+ rankData, e := tradeAnalysis.GetContractTopRankData(exchange, classifyNames, dataDate)
|
|
|
+ if e != nil {
|
|
|
+ err = fmt.Errorf("获取多空单原始数据失败, %v", e)
|
|
|
+ return
|
|
|
+ }
|
|
|
+ items = make([]*tradeAnalysisModel.ContractTopRankData, 0)
|
|
|
+ for _, v := range rankData {
|
|
|
+ v.Exchange = exchange
|
|
|
+ // 郑商所-这里注意把查出来的品种和合约赋值,不然后续是乱的
|
|
|
+ if v.Exchange == tradeAnalysisModel.TradeExchangeZhengzhou {
|
|
|
+ v.ClassifyType = v.ClassifyName
|
|
|
+ v.ClassifyName = GetZhengzhouClassifyName(v.ClassifyName)
|
|
|
+ }
|
|
|
+ items = append(items, v)
|
|
|
+ }
|
|
|
+ return
|
|
|
+}
|
|
|
+
|
|
|
+// GetTableTradeData 获取多空分析表格持仓数据
|
|
|
+func GetTableTradeData(exchange string, classifyName string, contracts []string, companyName string, predictRatio float64, startDate, endDate time.Time, contractType int) (companyTradeData []*tradeAnalysisModel.ContractCompanyTradeData, err error) {
|
|
|
+ companyTradeData = make([]*tradeAnalysisModel.ContractCompanyTradeData, 0)
|
|
|
+
|
|
|
+ // 获取合约持仓数据
|
|
|
+ contractTradeData, lastBuyVal, lastSoldVal, e := GetContractCompanyTradeData(exchange, []string{classifyName}, contracts, []string{companyName}, startDate, endDate)
|
|
|
+ if e != nil {
|
|
|
+ err = fmt.Errorf("获取合约-持仓数据失败, %v", e)
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ // 填充[合约-公司]预估数据, 并根据[公司-多合约]分组, [公司]算作一个指标, 指标值为[多个合约]的计算加总
|
|
|
+ companyContracts := make(map[string][]*tradeAnalysisModel.ContractCompanyTradeData)
|
|
|
+ for _, v := range contractTradeData {
|
|
|
+ td, fd, ed, e := PredictingTradeData(v.DataList, lastBuyVal[v.ClassifyType], lastSoldVal[v.ClassifyType], predictRatio)
|
|
|
+ if e != nil {
|
|
|
+ err = fmt.Errorf("数据补全失败, %v", e)
|
|
|
+ return
|
|
|
+ }
|
|
|
+ v.DataList = td
|
|
|
+ v.StartDate = fd
|
|
|
+ v.EndDate = ed
|
|
|
+
|
|
|
+ // 合约类型参数不为合约加总时, 每个合约算一行数据
|
|
|
+ if contractType != tradeAnalysisModel.ContractQueryTypeTotal {
|
|
|
+ companyTradeData = append(companyTradeData, v)
|
|
|
+ continue
|
|
|
+ }
|
|
|
+
|
|
|
+ // 往下计算合约加总
|
|
|
+ if companyContracts[v.CompanyName] == nil {
|
|
|
+ companyContracts[v.CompanyName] = make([]*tradeAnalysisModel.ContractCompanyTradeData, 0)
|
|
|
+ }
|
|
|
+ companyContracts[v.CompanyName] = append(companyContracts[v.CompanyName], v)
|
|
|
+ }
|
|
|
+
|
|
|
+ // 类型为合约加总才往下合并
|
|
|
+ if contractType != tradeAnalysisModel.ContractQueryTypeTotal {
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ // 以[公司]为组, 计算合约加总
|
|
|
+ for k, v := range companyContracts {
|
|
|
+ companyData := new(tradeAnalysisModel.ContractCompanyTradeData)
|
|
|
+ companyData.CompanyName = k
|
|
|
+ companyData.DataList = make([]*tradeAnalysisModel.ContractCompanyTradeDataList, 0)
|
|
|
+ contractArr := make([]string, 0)
|
|
|
+
|
|
|
+ // 合约加总
|
|
|
+ sumDateData := make(map[time.Time]*tradeAnalysisModel.ContractCompanyTradeDataList)
|
|
|
+ for _, vv := range v {
|
|
|
+ contractArr = append(contractArr, vv.ClassifyType)
|
|
|
+ for _, dv := range vv.DataList {
|
|
|
+ if sumDateData[dv.Date] == nil {
|
|
|
+ sumDateData[dv.Date] = new(tradeAnalysisModel.ContractCompanyTradeDataList)
|
|
|
+ sumDateData[dv.Date].Date = dv.Date
|
|
|
+ }
|
|
|
+ // 数据类型以第一个非零值为准, 只处理多空和净多, 变化就不管了
|
|
|
+ if sumDateData[dv.Date].BuyValType == tradeAnalysisModel.TradeDataTypeNull && dv.BuyValType > tradeAnalysisModel.TradeDataTypeNull {
|
|
|
+ sumDateData[dv.Date].BuyValType = dv.BuyValType
|
|
|
+ }
|
|
|
+ if sumDateData[dv.Date].BuyValType == tradeAnalysisModel.TradeDataTypeOrigin && dv.BuyValType == tradeAnalysisModel.TradeDataTypeCalculate {
|
|
|
+ sumDateData[dv.Date].BuyValType = dv.BuyValType
|
|
|
+ }
|
|
|
+ if dv.BuyValType > tradeAnalysisModel.TradeDataTypeNull {
|
|
|
+ sumDateData[dv.Date].BuyVal += dv.BuyVal
|
|
|
+ }
|
|
|
+ // 空单
|
|
|
+ if sumDateData[dv.Date].SoldValType == tradeAnalysisModel.TradeDataTypeNull && dv.SoldValType > tradeAnalysisModel.TradeDataTypeNull {
|
|
|
+ sumDateData[dv.Date].SoldValType = dv.SoldValType
|
|
|
+ }
|
|
|
+ if sumDateData[dv.Date].SoldValType == tradeAnalysisModel.TradeDataTypeOrigin && dv.SoldValType == tradeAnalysisModel.TradeDataTypeCalculate {
|
|
|
+ sumDateData[dv.Date].SoldValType = dv.SoldValType
|
|
|
+ }
|
|
|
+ if dv.SoldValType > tradeAnalysisModel.TradeDataTypeNull {
|
|
|
+ sumDateData[dv.Date].SoldVal += dv.SoldVal
|
|
|
+ }
|
|
|
+ // 净多单
|
|
|
+ if sumDateData[dv.Date].PureBuyValType == tradeAnalysisModel.TradeDataTypeNull && dv.PureBuyValType > tradeAnalysisModel.TradeDataTypeNull {
|
|
|
+ sumDateData[dv.Date].PureBuyValType = dv.PureBuyValType
|
|
|
+ }
|
|
|
+ if sumDateData[dv.Date].PureBuyValType == tradeAnalysisModel.TradeDataTypeOrigin && dv.PureBuyValType == tradeAnalysisModel.TradeDataTypeCalculate {
|
|
|
+ sumDateData[dv.Date].PureBuyValType = dv.PureBuyValType
|
|
|
+ }
|
|
|
+ if dv.PureBuyValType > tradeAnalysisModel.TradeDataTypeNull {
|
|
|
+ sumDateData[dv.Date].PureBuyVal += dv.PureBuyVal
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // 多个合约比对开始结束时间
|
|
|
+ if companyData.StartDate.IsZero() {
|
|
|
+ companyData.StartDate = vv.StartDate
|
|
|
+ }
|
|
|
+ if vv.StartDate.Before(companyData.StartDate) {
|
|
|
+ companyData.StartDate = vv.StartDate
|
|
|
+ }
|
|
|
+ if companyData.EndDate.IsZero() {
|
|
|
+ companyData.EndDate = vv.EndDate
|
|
|
+ }
|
|
|
+ if vv.EndDate.Before(companyData.EndDate) {
|
|
|
+ companyData.EndDate = vv.EndDate
|
|
|
+ }
|
|
|
+ }
|
|
|
+ for _, sv := range sumDateData {
|
|
|
+ companyData.DataList = append(companyData.DataList, sv)
|
|
|
+ }
|
|
|
+ sort.Slice(companyData.DataList, func(i, j int) bool {
|
|
|
+ return companyData.DataList[i].Date.Before(companyData.DataList[j].Date)
|
|
|
+ })
|
|
|
+ //companyData.ClassifyType = strings.Join(contractArr, ",")
|
|
|
+ companyData.Exchange = exchange
|
|
|
+ companyData.CompanyName = k
|
|
|
+ companyData.ClassifyType = classifyName
|
|
|
+ companyTradeData = append(companyTradeData, companyData)
|
|
|
+ }
|
|
|
+ return
|
|
|
+}
|
|
|
+
|
|
|
+// GetCorrelationTableTradeData 获取相关性表格持仓数据
|
|
|
+func GetCorrelationTableTradeData(exchange string, classifyName string, contracts, companies []string, predictRatio float64, contractType int) (companyTradeData []*tradeAnalysisModel.ContractCompanyTradeData, err error) {
|
|
|
+ companyTradeData = make([]*tradeAnalysisModel.ContractCompanyTradeData, 0)
|
|
|
+
|
|
|
+ // 获取合约持仓数据
|
|
|
+ contractTradeData, lastBuyVal, lastSoldVal, e := GetContractCompanyTradeData(exchange, []string{classifyName}, contracts, companies, time.Time{}, time.Time{})
|
|
|
+ if e != nil {
|
|
|
+ err = fmt.Errorf("获取合约-持仓数据失败, %v", e)
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ // 填充[合约-公司]预估数据, 并根据[公司-多合约]分组, [公司]算作一个指标, 指标值为[多个合约]的计算加总
|
|
|
+ companyContracts := make(map[string][]*tradeAnalysisModel.ContractCompanyTradeData)
|
|
|
+ for _, v := range contractTradeData {
|
|
|
+ td, fd, ed, e := PredictingTradeData(v.DataList, lastBuyVal[v.ClassifyType], lastSoldVal[v.ClassifyType], predictRatio)
|
|
|
+ if e != nil {
|
|
|
+ err = fmt.Errorf("数据补全失败, %v", e)
|
|
|
+ return
|
|
|
+ }
|
|
|
+ v.DataList = td
|
|
|
+ v.StartDate = fd
|
|
|
+ v.EndDate = ed
|
|
|
+
|
|
|
+ // 合约类型参数不为合约加总时, 每个合约算一行数据
|
|
|
+ if contractType != tradeAnalysisModel.ContractQueryTypeTotal {
|
|
|
+ companyTradeData = append(companyTradeData, v)
|
|
|
+ continue
|
|
|
+ }
|
|
|
+
|
|
|
+ // 往下计算合约加总
|
|
|
+ if companyContracts[v.CompanyName] == nil {
|
|
|
+ companyContracts[v.CompanyName] = make([]*tradeAnalysisModel.ContractCompanyTradeData, 0)
|
|
|
+ }
|
|
|
+ companyContracts[v.CompanyName] = append(companyContracts[v.CompanyName], v)
|
|
|
+ }
|
|
|
+
|
|
|
+ // 类型为合约加总才往下合并
|
|
|
+ if contractType != tradeAnalysisModel.ContractQueryTypeTotal {
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ // 以[公司]为组, 计算合约加总
|
|
|
+ for k, v := range companyContracts {
|
|
|
+ companyData := new(tradeAnalysisModel.ContractCompanyTradeData)
|
|
|
+ companyData.CompanyName = k
|
|
|
+ companyData.DataList = make([]*tradeAnalysisModel.ContractCompanyTradeDataList, 0)
|
|
|
+ contractArr := make([]string, 0)
|
|
|
+
|
|
|
+ // 合约加总
|
|
|
+ sumDateData := make(map[time.Time]*tradeAnalysisModel.ContractCompanyTradeDataList)
|
|
|
+ for _, vv := range v {
|
|
|
+ contractArr = append(contractArr, vv.ClassifyType)
|
|
|
+ for _, dv := range vv.DataList {
|
|
|
+ if sumDateData[dv.Date] == nil {
|
|
|
+ sumDateData[dv.Date] = new(tradeAnalysisModel.ContractCompanyTradeDataList)
|
|
|
+ sumDateData[dv.Date].Date = dv.Date
|
|
|
+ }
|
|
|
+ // 数据类型以第一个非零值为准, 只处理多空和净多, 变化就不管了
|
|
|
+ if sumDateData[dv.Date].BuyValType == tradeAnalysisModel.TradeDataTypeNull && dv.BuyValType > tradeAnalysisModel.TradeDataTypeNull {
|
|
|
+ sumDateData[dv.Date].BuyValType = dv.BuyValType
|
|
|
+ }
|
|
|
+ if sumDateData[dv.Date].BuyValType == tradeAnalysisModel.TradeDataTypeOrigin && dv.BuyValType == tradeAnalysisModel.TradeDataTypeCalculate {
|
|
|
+ sumDateData[dv.Date].BuyValType = dv.BuyValType
|
|
|
+ }
|
|
|
+ if dv.BuyValType > tradeAnalysisModel.TradeDataTypeNull {
|
|
|
+ sumDateData[dv.Date].BuyVal += dv.BuyVal
|
|
|
+ }
|
|
|
+ // 空单
|
|
|
+ if sumDateData[dv.Date].SoldValType == tradeAnalysisModel.TradeDataTypeNull && dv.SoldValType > tradeAnalysisModel.TradeDataTypeNull {
|
|
|
+ sumDateData[dv.Date].SoldValType = dv.SoldValType
|
|
|
+ }
|
|
|
+ if sumDateData[dv.Date].SoldValType == tradeAnalysisModel.TradeDataTypeOrigin && dv.SoldValType == tradeAnalysisModel.TradeDataTypeCalculate {
|
|
|
+ sumDateData[dv.Date].SoldValType = dv.SoldValType
|
|
|
+ }
|
|
|
+ if dv.SoldValType > tradeAnalysisModel.TradeDataTypeNull {
|
|
|
+ sumDateData[dv.Date].SoldVal += dv.SoldVal
|
|
|
+ }
|
|
|
+ // 净多单
|
|
|
+ if sumDateData[dv.Date].PureBuyValType == tradeAnalysisModel.TradeDataTypeNull && dv.PureBuyValType > tradeAnalysisModel.TradeDataTypeNull {
|
|
|
+ sumDateData[dv.Date].PureBuyValType = dv.PureBuyValType
|
|
|
+ }
|
|
|
+ if sumDateData[dv.Date].PureBuyValType == tradeAnalysisModel.TradeDataTypeOrigin && dv.PureBuyValType == tradeAnalysisModel.TradeDataTypeCalculate {
|
|
|
+ sumDateData[dv.Date].PureBuyValType = dv.PureBuyValType
|
|
|
+ }
|
|
|
+ if dv.PureBuyValType > tradeAnalysisModel.TradeDataTypeNull {
|
|
|
+ sumDateData[dv.Date].PureBuyVal += dv.PureBuyVal
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // 多个合约比对开始结束时间
|
|
|
+ if companyData.StartDate.IsZero() {
|
|
|
+ companyData.StartDate = vv.StartDate
|
|
|
+ }
|
|
|
+ if vv.StartDate.Before(companyData.StartDate) {
|
|
|
+ companyData.StartDate = vv.StartDate
|
|
|
+ }
|
|
|
+ if companyData.EndDate.IsZero() {
|
|
|
+ companyData.EndDate = vv.EndDate
|
|
|
+ }
|
|
|
+ if vv.EndDate.Before(companyData.EndDate) {
|
|
|
+ companyData.EndDate = vv.EndDate
|
|
|
+ }
|
|
|
+ }
|
|
|
+ for _, sv := range sumDateData {
|
|
|
+ companyData.DataList = append(companyData.DataList, sv)
|
|
|
+ }
|
|
|
+ sort.Slice(companyData.DataList, func(i, j int) bool {
|
|
|
+ return companyData.DataList[i].Date.Before(companyData.DataList[j].Date)
|
|
|
+ })
|
|
|
+ //companyData.ClassifyType = strings.Join(contractArr, ",")
|
|
|
+ companyData.IsTotal = true
|
|
|
+ companyData.Exchange = exchange
|
|
|
+ companyData.CompanyName = k
|
|
|
+ companyData.ClassifyName = classifyName
|
|
|
+ companyData.ClassifyType = classifyName
|
|
|
+ companyTradeData = append(companyTradeData, companyData)
|
|
|
+ }
|
|
|
+ return
|
|
|
+}
|
|
|
+
|
|
|
+// TransTradeData2EdbData 持仓数据转为指标数据
|
|
|
+func TransTradeData2EdbData(tradeData []*tradeAnalysisModel.ContractCompanyTradeData, contractPosition int) (edbData []*tradeAnalysisModel.ContractCompanyTradeEdb, edbDataMap []map[time.Time]int, err error) {
|
|
|
+ if len(tradeData) == 0 {
|
|
|
+ return
|
|
|
+ }
|
|
|
+ edbData = make([]*tradeAnalysisModel.ContractCompanyTradeEdb, 0)
|
|
|
+ edbDataMap = make([]map[time.Time]int, 0)
|
|
|
+
|
|
|
+ for _, v := range tradeData {
|
|
|
+ newEdb := new(tradeAnalysisModel.ContractCompanyTradeEdb)
|
|
|
+ newEdb.Exchange = v.Exchange
|
|
|
+ newEdb.ClassifyName = v.ClassifyName
|
|
|
+ newEdb.ClassifyType = v.ClassifyType
|
|
|
+ newEdb.CompanyName = v.CompanyName
|
|
|
+ newEdb.IsTotal = v.IsTotal
|
|
|
+ newEdb.ContractPosition = contractPosition
|
|
|
+ newEdb.StartDate = v.StartDate
|
|
|
+ newEdb.EndDate = v.EndDate
|
|
|
+ newEdb.DataList = make([]*tradeAnalysisModel.ContractCompanyTradeEdbData, 0)
|
|
|
+ dataMap := make(map[time.Time]int)
|
|
|
+ for _, d := range v.DataList {
|
|
|
+ var vd int
|
|
|
+ switch contractPosition {
|
|
|
+ case tradeAnalysisModel.ContractPositionBuy:
|
|
|
+ if d.BuyValType == tradeAnalysisModel.TradeDataTypeNull {
|
|
|
+ continue
|
|
|
+ }
|
|
|
+ vd = d.BuyVal
|
|
|
+ case tradeAnalysisModel.ContractPositionSold:
|
|
|
+ if d.SoldValType == tradeAnalysisModel.TradeDataTypeNull {
|
|
|
+ continue
|
|
|
+ }
|
|
|
+ vd = d.SoldVal
|
|
|
+ case tradeAnalysisModel.ContractPositionPureBuy:
|
|
|
+ if d.PureBuyValType == tradeAnalysisModel.TradeDataTypeNull {
|
|
|
+ continue
|
|
|
+ }
|
|
|
+ vd = d.PureBuyVal
|
|
|
+ default:
|
|
|
+ continue
|
|
|
+ }
|
|
|
+ newEdb.DataList = append(newEdb.DataList, &tradeAnalysisModel.ContractCompanyTradeEdbData{
|
|
|
+ DataTime: d.Date,
|
|
|
+ Val: vd,
|
|
|
+ })
|
|
|
+ dataMap[d.Date] = vd
|
|
|
+ }
|
|
|
+ edbData = append(edbData, newEdb)
|
|
|
+ edbDataMap = append(edbDataMap, dataMap)
|
|
|
+ }
|
|
|
+ return
|
|
|
+}
|
|
|
+
|
|
|
+// GetContractCompanyTradeData 获取合约持仓数据
|
|
|
+func GetContractCompanyTradeData(exchange string, classifyNames, contracts, companies []string, startDate, endDate time.Time) (contractTradeData map[string]*tradeAnalysisModel.ContractCompanyTradeData, lastBuyVal, lastSoldVal map[string]map[time.Time]int, err error) {
|
|
|
+ // 各原始数据表期货公司名称不一致
|
|
|
+ companyMap := make(map[string]string)
|
|
|
+ {
|
|
|
+ ob := new(tradeAnalysisModel.TradeFuturesCompany)
|
|
|
+ list, e := ob.GetItemsByCondition(``, make([]interface{}, 0), []string{}, "")
|
|
|
+ if e != nil {
|
|
|
+ err = fmt.Errorf("获取期货公司名称失败: %v", e)
|
|
|
+ return
|
|
|
+ }
|
|
|
+ switch exchange {
|
|
|
+ case "zhengzhou":
|
|
|
+ for _, v := range list {
|
|
|
+ companyMap[v.CompanyName] = v.ZhengzhouName
|
|
|
+ }
|
|
|
+ case "dalian":
|
|
|
+ for _, v := range list {
|
|
|
+ companyMap[v.CompanyName] = v.DalianName
|
|
|
+ }
|
|
|
+ case "shanghai":
|
|
|
+ for _, v := range list {
|
|
|
+ companyMap[v.CompanyName] = v.ShanghaiName
|
|
|
+ }
|
|
|
+ case "cffex":
|
|
|
+ for _, v := range list {
|
|
|
+ companyMap[v.CompanyName] = v.CffexName
|
|
|
+ }
|
|
|
+ case "ine":
|
|
|
+ for _, v := range list {
|
|
|
+ companyMap[v.CompanyName] = v.IneName
|
|
|
+ }
|
|
|
+ case "guangzhou":
|
|
|
+ for _, v := range list {
|
|
|
+ companyMap[v.CompanyName] = v.GuangzhouName
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ var queryCompanies []string
|
|
|
+ for _, v := range companies {
|
|
|
+ if v == tradeAnalysisModel.TradeFuturesCompanyTop20 {
|
|
|
+ queryCompanies = append(queryCompanies, tradeAnalysisModel.TradeFuturesCompanyTop20)
|
|
|
+ continue
|
|
|
+ }
|
|
|
+ companyName, ok := companyMap[v]
|
|
|
+ if !ok {
|
|
|
+ utils.FileLog.Info(fmt.Sprintf("交易所%s公司名称映射不存在: %s", exchange, v))
|
|
|
+ continue
|
|
|
+ }
|
|
|
+ queryCompanies = append(queryCompanies, companyName)
|
|
|
+ }
|
|
|
+
|
|
|
+ // 郑商所/广期所查询方式不一样
|
|
|
+ var tradeAnalysis TradeAnalysisInterface
|
|
|
+ switch exchange {
|
|
|
+ case tradeAnalysisModel.TradeExchangeZhengzhou:
|
|
|
+ tradeAnalysis = &ZhengzhouTradeAnalysis{}
|
|
|
+ case tradeAnalysisModel.TradeExchangeGuangzhou:
|
|
|
+ tradeAnalysis = &GuangzhouTradeAnalysis{}
|
|
|
+ default:
|
|
|
+ tradeAnalysis = &BaseTradeAnalysis{}
|
|
|
+ }
|
|
|
+
|
|
|
+ // 获取多单/空单原始数据
|
|
|
+ originList, _, lastOriginList, e := tradeAnalysis.GetTradeDataByContracts(exchange, classifyNames, contracts, queryCompanies, startDate, endDate)
|
|
|
+ if e != nil {
|
|
|
+ err = fmt.Errorf("获取多空单原始数据失败, %v", e)
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ // [合约-期货公司]数据分组
|
|
|
+ contractTradeData = make(map[string]*tradeAnalysisModel.ContractCompanyTradeData)
|
|
|
+ {
|
|
|
+ keyDateData := make(map[string]*tradeAnalysisModel.ContractCompanyTradeDataList)
|
|
|
+ keyDateDataExist := make(map[string]bool)
|
|
|
+ for _, v := range originList {
|
|
|
+ companyName := v.CompanyName
|
|
|
+
|
|
|
+ k := fmt.Sprintf("%s-%s", v.ClassifyType, companyName)
|
|
|
+ if contractTradeData[k] == nil {
|
|
|
+ contractTradeData[k] = new(tradeAnalysisModel.ContractCompanyTradeData)
|
|
|
+ contractTradeData[k].Exchange = exchange
|
|
|
+ contractTradeData[k].CompanyName = companyName
|
|
|
+ contractTradeData[k].ClassifyName = v.ClassifyName
|
|
|
+ contractTradeData[k].ClassifyType = v.ClassifyType
|
|
|
+ contractTradeData[k].DataList = make([]*tradeAnalysisModel.ContractCompanyTradeDataList, 0)
|
|
|
+ }
|
|
|
+
|
|
|
+ kd := fmt.Sprintf("%s-%s", k, v.DataTime.Format(utils.FormatDate))
|
|
|
+ if keyDateData[kd] == nil {
|
|
|
+ keyDateData[kd] = new(tradeAnalysisModel.ContractCompanyTradeDataList)
|
|
|
+ keyDateData[kd].Date = v.DataTime
|
|
|
+ }
|
|
|
+ if v.ValType == 1 {
|
|
|
+ keyDateData[kd].BuyVal = v.Val
|
|
|
+ keyDateData[kd].BuyValType = tradeAnalysisModel.TradeDataTypeOrigin
|
|
|
+ keyDateData[kd].BuyChange = v.ValChange
|
|
|
+ keyDateData[kd].BuyChangeType = tradeAnalysisModel.TradeDataTypeOrigin
|
|
|
+ }
|
|
|
+ if v.ValType == 2 {
|
|
|
+ keyDateData[kd].SoldVal = v.Val
|
|
|
+ keyDateData[kd].SoldValType = tradeAnalysisModel.TradeDataTypeOrigin
|
|
|
+ keyDateData[kd].SoldChange = v.ValChange
|
|
|
+ keyDateData[kd].SoldChangeType = tradeAnalysisModel.TradeDataTypeOrigin
|
|
|
+ }
|
|
|
+ if !keyDateDataExist[kd] {
|
|
|
+ contractTradeData[k].DataList = append(contractTradeData[k].DataList, keyDateData[kd])
|
|
|
+ keyDateDataExist[kd] = true
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // 合约的[日期-末位值]
|
|
|
+ lastBuyVal = make(map[string]map[time.Time]int)
|
|
|
+ lastSoldVal = make(map[string]map[time.Time]int)
|
|
|
+ {
|
|
|
+ for _, v := range lastOriginList {
|
|
|
+ if v.ValType == 1 {
|
|
|
+ if lastBuyVal[v.ClassifyType] == nil {
|
|
|
+ lastBuyVal[v.ClassifyType] = make(map[time.Time]int)
|
|
|
+ }
|
|
|
+ lastBuyVal[v.ClassifyType][v.DataTime] = v.Val
|
|
|
+ continue
|
|
|
+ }
|
|
|
+ if lastSoldVal[v.ClassifyType] == nil {
|
|
|
+ lastSoldVal[v.ClassifyType] = make(map[time.Time]int)
|
|
|
+ }
|
|
|
+ lastSoldVal[v.ClassifyType][v.DataTime] = v.Val
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return
|
|
|
+}
|
|
|
+
|
|
|
+// GetTradeClassifyNewestDataTime 获取数据最新日期
|
|
|
+func GetTradeClassifyNewestDataTime(exchange string, classifyNames []string) (dataTime time.Time, err error) {
|
|
|
+ var tradeAnalysis TradeAnalysisInterface
|
|
|
+ switch exchange {
|
|
|
+ case tradeAnalysisModel.TradeExchangeZhengzhou:
|
|
|
+ tradeAnalysis = &ZhengzhouTradeAnalysis{}
|
|
|
+ case tradeAnalysisModel.TradeExchangeGuangzhou:
|
|
|
+ tradeAnalysis = &GuangzhouTradeAnalysis{}
|
|
|
+ default:
|
|
|
+ tradeAnalysis = &BaseTradeAnalysis{}
|
|
|
+ }
|
|
|
+ if exchange == tradeAnalysisModel.TradeExchangeZhengzhou {
|
|
|
+ classifies, e := GetZhengzhouContractsByClassifyNames(classifyNames)
|
|
|
+ if e != nil {
|
|
|
+ err = fmt.Errorf("获取郑商所实际合约失败, %v", e)
|
|
|
+ return
|
|
|
+ }
|
|
|
+ classifyNames = classifies
|
|
|
+ }
|
|
|
+
|
|
|
+ d, e := tradeAnalysis.GetClassifyNewestDataTime(exchange, classifyNames)
|
|
|
+ if e != nil && e.Error() != utils.ErrNoRow() {
|
|
|
+ err = fmt.Errorf("获取品种最新数据日期失败, %v", e)
|
|
|
+ return
|
|
|
+ }
|
|
|
+ if !d.IsZero() {
|
|
|
+ dataTime = d
|
|
|
+ } else {
|
|
|
+ dataTime = time.Now().Local()
|
|
|
+ }
|
|
|
+ return
|
|
|
+}
|