package trade_analysis import ( "eta/eta_index_lib/models" tradeAnalysisModel "eta/eta_index_lib/models/trade_analysis" "eta/eta_index_lib/utils" "fmt" "sort" "strings" "time" ) // FormatCompanyTradeData2EdbData [公司-合约加总]转为指标数据 func FormatCompanyTradeData2EdbData(companyTradeData *tradeAnalysisModel.ContractCompanyTradeData, tradeType int) (edbData []*models.EdbDataList, err error) { if companyTradeData == nil { err = fmt.Errorf("持仓数据异常") return } edbData = make([]*models.EdbDataList, 0) var minData, maxData float64 for dk, dv := range companyTradeData.DataList { // 交易方向 var val float64 if tradeType == tradeAnalysisModel.WarehouseBuyChartType { if dv.BuyValType == tradeAnalysisModel.TradeDataTypeNull { continue } val = float64(dv.BuyVal) } if tradeType == tradeAnalysisModel.WarehouseSoldChartType { if dv.SoldValType == tradeAnalysisModel.TradeDataTypeNull { continue } val = float64(dv.SoldVal) } if tradeType == tradeAnalysisModel.WarehousePureBuyChartType { if dv.PureBuyValType == tradeAnalysisModel.TradeDataTypeNull { continue } val = float64(dv.PureBuyVal) } if dk == 0 { minData = val maxData = val } if val < minData { minData = val } if val > maxData { maxData = val } edbData = append(edbData, &models.EdbDataList{ DataTime: dv.Date.Format(utils.FormatDate), DataTimestamp: dv.Date.UnixNano() / 1e6, Value: val, }) } return } // GetOriginTradeData 获取原始持仓数据 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 { // TOP20用空名称去查询 if v == tradeAnalysisModel.TradeFuturesCompanyTop20 { queryCompanies = append(queryCompanies, "") 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) if e != nil { 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 { // TOP20对应数据库中的空名称 companyName := v.CompanyName if companyName == "" { 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) if e != nil { err = fmt.Errorf("数据补全失败, %v", e) return } v.DataList = td v.StartDate = fd v.EndDate = ed if companyContracts[v.CompanyName] == nil { companyContracts[v.CompanyName] = make([]*tradeAnalysisModel.ContractCompanyTradeData, 0) } companyContracts[v.CompanyName] = append(companyContracts[v.CompanyName], v) } // 以[公司]为组, 计算合约加总 companyTradeData = make([]*tradeAnalysisModel.ContractCompanyTradeData, 0) 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, ",") companyTradeData = append(companyTradeData, companyData) } return } // PredictingTradeData 根据数据库中的多空数据填充预估数据 func PredictingTradeData(originData []*tradeAnalysisModel.ContractCompanyTradeDataList, lastBuyDateVal, lastSoldDateVal map[time.Time]int, predictRatio float64) (newData []*tradeAnalysisModel.ContractCompanyTradeDataList, firstDate, endDate time.Time, err error) { if len(originData) == 0 { return } if predictRatio < 0 || predictRatio > 1 { err = fmt.Errorf("估计参数不在0-1之间") return } sort.Slice(originData, func(i, j int) bool { return originData[i].Date.Before(originData[j].Date) }) dateVal := make(map[time.Time]*tradeAnalysisModel.ContractCompanyTradeDataList) for _, v := range originData { dateVal[v.Date] = v } // 生成开始日期-1d(可能会往前面推算一天)至结束日期间的交易日, 以交易日为时间序列遍历 tradeDays := utils.GetTradingDays(originData[0].Date.AddDate(0, 0, -1), originData[len(originData)-1].Date) for k, v := range tradeDays { // T日多空均无的情况 //bothLast := false if dateVal[v] == nil { // T-1和T+1[原始数据]均无值, 那么T日无数据 hasPrev, hasNext := false, false if k-1 >= 0 { hasPrev = true } if k+1 <= len(tradeDays)-1 { hasNext = true } if !hasPrev && !hasNext { continue } // T+1有值, 优先从T+1推, 然后继续走下面计算净多单的逻辑 if hasNext { nextDay := tradeDays[k+1] if dateVal[nextDay] != nil { // T+1有多/空及多空变化, 且是原始数据, 那么推出数据并在map中新加一日数据 if dateVal[nextDay].BuyValType == tradeAnalysisModel.TradeDataTypeOrigin && dateVal[nextDay].BuyChangeType == tradeAnalysisModel.TradeDataTypeOrigin { if _, ok := dateVal[v]; !ok { dateVal[v] = new(tradeAnalysisModel.ContractCompanyTradeDataList) dateVal[v].Date = v } dateVal[v].BuyVal = dateVal[nextDay].BuyVal - dateVal[nextDay].BuyChange dateVal[v].BuyValType = tradeAnalysisModel.TradeDataTypeOrigin } if dateVal[nextDay].SoldValType == tradeAnalysisModel.TradeDataTypeOrigin && dateVal[nextDay].SoldChangeType == tradeAnalysisModel.TradeDataTypeOrigin { if _, ok := dateVal[v]; !ok { dateVal[v] = new(tradeAnalysisModel.ContractCompanyTradeDataList) dateVal[v].Date = v } dateVal[v].SoldVal = dateVal[nextDay].SoldVal - dateVal[nextDay].SoldChange dateVal[v].SoldValType = tradeAnalysisModel.TradeDataTypeOrigin } } } // T+1没推出来而T-1有值, 那么T多空均取末位, 计算净多单 _, has := dateVal[v] if hasPrev && !has { sv, sok := lastSoldDateVal[v] bv, bok := lastBuyDateVal[v] if !sok && !bok { continue } dateVal[v] = new(tradeAnalysisModel.ContractCompanyTradeDataList) dateVal[v].Date = v if sok { dateVal[v].SoldVal = int(predictRatio*float64(sv) + 0.5) dateVal[v].SoldValType = tradeAnalysisModel.TradeDataTypeCalculate } if bok { dateVal[v].BuyVal = int(predictRatio*float64(bv) + 0.5) dateVal[v].BuyValType = tradeAnalysisModel.TradeDataTypeCalculate } if dateVal[v].BuyValType > tradeAnalysisModel.TradeDataTypeNull && dateVal[v].SoldValType > tradeAnalysisModel.TradeDataTypeNull { dateVal[v].PureBuyVal = dateVal[v].BuyVal - dateVal[v].SoldVal dateVal[v].PureBuyValType = tradeAnalysisModel.TradeDataTypeCalculate } continue } } // 多空均有的情况下计算净多单 if dateVal[v].BuyValType == tradeAnalysisModel.TradeDataTypeOrigin && dateVal[v].SoldValType == tradeAnalysisModel.TradeDataTypeOrigin { dateVal[v].PureBuyVal = dateVal[v].BuyVal - dateVal[v].SoldVal dateVal[v].PureBuyValType = tradeAnalysisModel.TradeDataTypeOrigin // 原始值算出来的也作原始值 } // 仅有多单, 空单取末位, 计算净多单 if dateVal[v].BuyValType == tradeAnalysisModel.TradeDataTypeOrigin && dateVal[v].SoldValType == tradeAnalysisModel.TradeDataTypeNull { if sv, ok := lastSoldDateVal[v]; ok { dateVal[v].SoldVal = int(predictRatio*float64(sv) + 0.5) // 估计参数*末位值, 向上取整 dateVal[v].SoldValType = tradeAnalysisModel.TradeDataTypeCalculate dateVal[v].PureBuyVal = dateVal[v].BuyVal - dateVal[v].SoldVal dateVal[v].PureBuyValType = tradeAnalysisModel.TradeDataTypeCalculate } } // 仅有空单, 多单取末位, 计算净多单 if dateVal[v].SoldValType == tradeAnalysisModel.TradeDataTypeOrigin && dateVal[v].BuyValType == tradeAnalysisModel.TradeDataTypeNull { if sv, ok := lastBuyDateVal[v]; ok { dateVal[v].BuyVal = int(predictRatio*float64(sv) + 0.5) dateVal[v].BuyValType = tradeAnalysisModel.TradeDataTypeCalculate dateVal[v].PureBuyVal = dateVal[v].BuyVal - dateVal[v].SoldVal dateVal[v].PureBuyValType = tradeAnalysisModel.TradeDataTypeCalculate } } } // 二次遍历, 计算与T-1的变化值 for k, v := range tradeDays { // 无T/T-1数据, 忽略 if dateVal[v] == nil { continue } if k-1 < 0 { continue } beforeDay := tradeDays[k-1] if dateVal[beforeDay] == nil { continue } // 多单变化 if dateVal[v].BuyChangeType == tradeAnalysisModel.TradeDataTypeNull { if dateVal[v].BuyValType > tradeAnalysisModel.TradeDataTypeNull && dateVal[beforeDay].BuyValType > tradeAnalysisModel.TradeDataTypeNull { dateVal[v].BuyChange = dateVal[v].BuyVal - dateVal[beforeDay].BuyVal // 如果当日多单或者前日多单是估计值, 那么多单变化也为估计值 if dateVal[v].BuyValType == tradeAnalysisModel.TradeDataTypeCalculate || dateVal[beforeDay].BuyValType == tradeAnalysisModel.TradeDataTypeCalculate { dateVal[v].BuyChangeType = tradeAnalysisModel.TradeDataTypeCalculate } } } // 空单变化 if dateVal[v].SoldChangeType == tradeAnalysisModel.TradeDataTypeNull { if dateVal[v].SoldValType > tradeAnalysisModel.TradeDataTypeNull && dateVal[beforeDay].SoldValType > tradeAnalysisModel.TradeDataTypeNull { dateVal[v].SoldChange = dateVal[v].SoldVal - dateVal[beforeDay].SoldVal // 如果当日空单或者前日空单是估计值, 那么空单变化也为估计值 if dateVal[v].SoldValType == tradeAnalysisModel.TradeDataTypeCalculate || dateVal[beforeDay].SoldValType == tradeAnalysisModel.TradeDataTypeCalculate { dateVal[v].SoldChangeType = tradeAnalysisModel.TradeDataTypeCalculate } } } // 净多变化 if dateVal[v].PureBuyChangeType == tradeAnalysisModel.TradeDataTypeNull { if dateVal[v].PureBuyValType > tradeAnalysisModel.TradeDataTypeNull && dateVal[beforeDay].PureBuyValType > tradeAnalysisModel.TradeDataTypeNull { dateVal[v].PureBuyChange = dateVal[v].PureBuyVal - dateVal[beforeDay].PureBuyVal dateVal[v].PureBuyChangeType = tradeAnalysisModel.TradeDataTypeOrigin // 如果当日净多单或者前日净多单是估计值, 那么净多单变化也为估计值 if dateVal[v].PureBuyValType == tradeAnalysisModel.TradeDataTypeCalculate || dateVal[beforeDay].PureBuyValType == tradeAnalysisModel.TradeDataTypeCalculate { dateVal[v].PureBuyChangeType = tradeAnalysisModel.TradeDataTypeCalculate } } } } // 重新遍历map, 生成数据序列并排序 newData = make([]*tradeAnalysisModel.ContractCompanyTradeDataList, 0) for _, v := range dateVal { if v.BuyValType == tradeAnalysisModel.TradeDataTypeNull && v.SoldValType == tradeAnalysisModel.TradeDataTypeNull { continue } newData = append(newData, v) } sort.Slice(newData, func(i, j int) bool { return newData[i].Date.Before(newData[j].Date) }) if len(newData) > 0 { firstDate = newData[0].Date endDate = newData[len(newData)-1].Date } return }