trade_analysis_data.go 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647
  1. package trade_analysis
  2. import (
  3. "eta/eta_api/models/data_manage"
  4. tradeAnalysisModel "eta/eta_api/models/data_manage/trade_analysis"
  5. "eta/eta_api/services/data"
  6. "eta/eta_api/utils"
  7. "fmt"
  8. "sort"
  9. "strings"
  10. "time"
  11. )
  12. // FormatCompanyTradeData2EdbMappings [公司-合约加总]转为指标数据
  13. func FormatCompanyTradeData2EdbMappings(companyTradeData []*tradeAnalysisModel.ContractCompanyTradeData, tradeType, dateType, dateTypeNum int, startDate, endDate string, chartEdbList []*data_manage.ChartSaveItem) (edbMappings []*data_manage.ChartEdbInfoMapping, chartName string, err error) {
  14. edbMappings = make([]*data_manage.ChartEdbInfoMapping, 0)
  15. if dateType <= 0 {
  16. dateType = utils.DateTypeOneMonth
  17. }
  18. // 期货公司名称作为标识进行匹配
  19. edbMap := make(map[string]*data_manage.ChartSaveItem)
  20. if len(chartEdbList) > 0 {
  21. for _, v := range chartEdbList {
  22. edbMap[v.UniqueFlag] = v
  23. }
  24. }
  25. for k, v := range companyTradeData {
  26. mapping := new(data_manage.ChartEdbInfoMapping)
  27. mapping.EdbName = v.CompanyName
  28. mapping.EdbNameEn = v.CompanyName
  29. mapping.EdbAliasName = v.CompanyName
  30. mapping.EdbAliasNameEn = v.CompanyName
  31. mapping.Frequency = "日度"
  32. mapping.FrequencyEn = data.GetFrequencyEn(mapping.Frequency)
  33. mapping.SourceName = utils.SourceNameTradeAnalysis
  34. mapping.Source = utils.CHART_SOURCE_TRADE_ANALYSIS_PROCESS
  35. mapping.IsAxis = 1
  36. mapping.EdbInfoType = 1
  37. mapping.StartDate = v.StartDate.Format(utils.FormatDate)
  38. mapping.EndDate = v.EndDate.Format(utils.FormatDate)
  39. mapping.ConvertUnit = tradeAnalysisModel.WarehouseDefaultUnit // 固定单位
  40. mapping.UniqueFlag = v.CompanyName // 期货公司名称作为每条曲线的唯一标识
  41. // 有配置那么取配置中的图例名称和左右轴
  42. edbConf := edbMap[mapping.UniqueFlag]
  43. if edbConf != nil {
  44. mapping.EdbName = edbConf.EdbAliasName
  45. mapping.EdbNameEn = edbConf.EdbAliasName
  46. mapping.EdbAliasName = edbConf.EdbAliasName
  47. mapping.EdbAliasNameEn = edbConf.EdbAliasName
  48. mapping.IsAxis = edbConf.IsAxis
  49. }
  50. // 根据参数取日期范围
  51. var startTime, endTime time.Time
  52. if dateType > 0 {
  53. st, ed := utils.GetDateByDateTypeV2(dateType, startDate, endDate, dateTypeNum, 0)
  54. if st != "" {
  55. startTime, _ = time.ParseInLocation(utils.FormatDate, st, time.Local)
  56. }
  57. if startTime.IsZero() {
  58. startTime = v.StartDate
  59. }
  60. if ed != "" {
  61. endTime, _ = time.ParseInLocation(utils.FormatDate, ed, time.Local)
  62. }
  63. if endTime.IsZero() {
  64. endTime = v.EndDate
  65. }
  66. }
  67. // 指标数据和最值
  68. edbData := make([]*data_manage.EdbDataList, 0)
  69. var minData, maxData float64
  70. var setMinMax bool
  71. for _, dv := range v.DataList {
  72. if dv.Date.Before(startTime) || dv.Date.After(endTime) {
  73. continue
  74. }
  75. // 交易方向
  76. var (
  77. val float64
  78. hasVal bool
  79. )
  80. if tradeType == tradeAnalysisModel.WarehouseBuyChartType {
  81. if dv.BuyValType == tradeAnalysisModel.TradeDataTypeNull {
  82. continue
  83. }
  84. hasVal = true
  85. val = float64(dv.BuyVal)
  86. }
  87. if tradeType == tradeAnalysisModel.WarehouseSoldChartType {
  88. if dv.SoldValType == tradeAnalysisModel.TradeDataTypeNull {
  89. continue
  90. }
  91. hasVal = true
  92. val = float64(dv.SoldVal)
  93. }
  94. if tradeType == tradeAnalysisModel.WarehousePureBuyChartType {
  95. if dv.PureBuyValType == tradeAnalysisModel.TradeDataTypeNull {
  96. continue
  97. }
  98. hasVal = true
  99. val = float64(dv.PureBuyVal)
  100. }
  101. if !hasVal {
  102. continue
  103. }
  104. if !setMinMax {
  105. minData = val
  106. maxData = val
  107. setMinMax = true
  108. }
  109. if val < minData {
  110. minData = val
  111. }
  112. if val > maxData {
  113. maxData = val
  114. }
  115. edbData = append(edbData, &data_manage.EdbDataList{
  116. DataTime: dv.Date.Format(utils.FormatDate),
  117. DataTimestamp: dv.Date.UnixNano() / 1e6,
  118. Value: val,
  119. })
  120. }
  121. mapping.MinData = minData
  122. mapping.MaxData = maxData
  123. mapping.DataList = edbData
  124. edbMappings = append(edbMappings, mapping)
  125. // 图表默认名称
  126. if k == 0 {
  127. chartName += strings.ReplaceAll(v.ClassifyType, ",", "")
  128. }
  129. chartName += v.CompanyName
  130. }
  131. // 图表名称后缀
  132. chartName += tradeAnalysisModel.WarehouseTypeSuffixNames[tradeType]
  133. return
  134. }
  135. func GetOriginTradeData(exchange, classifyName string, contracts, companies []string, predictRatio float64) (companyTradeData []*tradeAnalysisModel.ContractCompanyTradeData, err error) {
  136. // 各原始数据表期货公司名称不一致
  137. companyMap := make(map[string]string)
  138. {
  139. ob := new(tradeAnalysisModel.TradeFuturesCompany)
  140. list, e := ob.GetItemsByCondition(``, make([]interface{}, 0), []string{}, "")
  141. if e != nil {
  142. err = fmt.Errorf("获取期货公司名称失败: %v", e)
  143. return
  144. }
  145. switch exchange {
  146. case "zhengzhou":
  147. for _, v := range list {
  148. companyMap[v.CompanyName] = v.ZhengzhouName
  149. }
  150. case "dalian":
  151. for _, v := range list {
  152. companyMap[v.CompanyName] = v.DalianName
  153. }
  154. case "shanghai":
  155. for _, v := range list {
  156. companyMap[v.CompanyName] = v.ShanghaiName
  157. }
  158. case "cffex":
  159. for _, v := range list {
  160. companyMap[v.CompanyName] = v.CffexName
  161. }
  162. case "ine":
  163. for _, v := range list {
  164. companyMap[v.CompanyName] = v.IneName
  165. }
  166. case "guangzhou":
  167. for _, v := range list {
  168. companyMap[v.CompanyName] = v.GuangzhouName
  169. }
  170. }
  171. }
  172. var queryCompanies []string
  173. for _, v := range companies {
  174. // TOP20用空名称去查询
  175. if v == tradeAnalysisModel.TradeFuturesCompanyTop20 {
  176. queryCompanies = append(queryCompanies, "")
  177. continue
  178. }
  179. companyName, ok := companyMap[v]
  180. if !ok {
  181. utils.FileLog.Info(fmt.Sprintf("交易所%s公司名称映射不存在: %s", exchange, v))
  182. continue
  183. }
  184. queryCompanies = append(queryCompanies, companyName)
  185. }
  186. // 郑商所/广期所查询方式不一样
  187. var tradeAnalysis TradeAnalysisInterface
  188. switch exchange {
  189. case tradeAnalysisModel.TradeExchangeZhengzhou:
  190. tradeAnalysis = &ZhengzhouTradeAnalysis{}
  191. case tradeAnalysisModel.TradeExchangeGuangzhou:
  192. tradeAnalysis = &GuangzhouTradeAnalysis{}
  193. default:
  194. tradeAnalysis = &BaseTradeAnalysis{}
  195. }
  196. // 获取多单/空单原始数据
  197. originList, e := tradeAnalysis.GetTradeDataByClassifyAndCompany(exchange, classifyName, contracts, queryCompanies)
  198. if e != nil {
  199. err = fmt.Errorf("获取多空单原始数据失败, %v", e)
  200. return
  201. }
  202. keyItems := make(map[string]*tradeAnalysisModel.ContractCompanyTradeData)
  203. keyDateData := make(map[string]*tradeAnalysisModel.ContractCompanyTradeDataList)
  204. keyDateDataExist := make(map[string]bool)
  205. for _, v := range originList {
  206. // TOP20对应数据库中的空名称
  207. companyName := v.CompanyName
  208. if companyName == "" {
  209. companyName = tradeAnalysisModel.TradeFuturesCompanyTop20
  210. }
  211. k := fmt.Sprintf("%s-%s", v.ClassifyType, companyName)
  212. if keyItems[k] == nil {
  213. keyItems[k] = new(tradeAnalysisModel.ContractCompanyTradeData)
  214. keyItems[k].CompanyName = companyName
  215. keyItems[k].ClassifyType = v.ClassifyType
  216. keyItems[k].DataList = make([]*tradeAnalysisModel.ContractCompanyTradeDataList, 0)
  217. }
  218. kd := fmt.Sprintf("%s-%s", k, v.DataTime.Format(utils.FormatDate))
  219. if keyDateData[kd] == nil {
  220. keyDateData[kd] = new(tradeAnalysisModel.ContractCompanyTradeDataList)
  221. keyDateData[kd].Date = v.DataTime
  222. }
  223. if v.ValType == 1 {
  224. keyDateData[kd].BuyVal = v.Val
  225. keyDateData[kd].BuyValType = tradeAnalysisModel.TradeDataTypeOrigin
  226. keyDateData[kd].BuyChange = v.ValChange
  227. keyDateData[kd].BuyChangeType = tradeAnalysisModel.TradeDataTypeOrigin
  228. }
  229. if v.ValType == 2 {
  230. keyDateData[kd].SoldVal = v.Val
  231. keyDateData[kd].SoldValType = tradeAnalysisModel.TradeDataTypeOrigin
  232. keyDateData[kd].SoldChange = v.ValChange
  233. keyDateData[kd].SoldChangeType = tradeAnalysisModel.TradeDataTypeOrigin
  234. }
  235. if !keyDateDataExist[kd] {
  236. keyItems[k].DataList = append(keyItems[k].DataList, keyDateData[kd])
  237. keyDateDataExist[kd] = true
  238. }
  239. }
  240. // 获取[合约]每日的末位多空单
  241. contractLastBuyDateVal := make(map[string]map[time.Time]int)
  242. contractLastSoldDateVal := make(map[string]map[time.Time]int)
  243. {
  244. lastOriginList, e := tradeAnalysis.GetLastTradeDataByClassify(exchange, classifyName, contracts)
  245. if e != nil {
  246. err = fmt.Errorf("获取末位多空单原始数据失败, %v", e)
  247. return
  248. }
  249. for _, v := range lastOriginList {
  250. if v.ValType == 1 {
  251. if contractLastBuyDateVal[v.ClassifyType] == nil {
  252. contractLastBuyDateVal[v.ClassifyType] = make(map[time.Time]int)
  253. }
  254. contractLastBuyDateVal[v.ClassifyType][v.DataTime] = v.Val
  255. continue
  256. }
  257. if contractLastSoldDateVal[v.ClassifyType] == nil {
  258. contractLastSoldDateVal[v.ClassifyType] = make(map[time.Time]int)
  259. }
  260. contractLastSoldDateVal[v.ClassifyType][v.DataTime] = v.Val
  261. }
  262. }
  263. // 填充[合约-公司]预估数据, 并根据[公司-多合约]分组, [公司]算作一个指标, 指标值为[多个合约]的计算加总
  264. companyContracts := make(map[string][]*tradeAnalysisModel.ContractCompanyTradeData)
  265. for _, v := range keyItems {
  266. td, fd, ed, e := PredictingTradeData(v.DataList, contractLastBuyDateVal[v.ClassifyType], contractLastSoldDateVal[v.ClassifyType], predictRatio)
  267. if e != nil {
  268. err = fmt.Errorf("数据补全失败, %v", e)
  269. return
  270. }
  271. v.DataList = td
  272. v.StartDate = fd
  273. v.EndDate = ed
  274. if companyContracts[v.CompanyName] == nil {
  275. companyContracts[v.CompanyName] = make([]*tradeAnalysisModel.ContractCompanyTradeData, 0)
  276. }
  277. companyContracts[v.CompanyName] = append(companyContracts[v.CompanyName], v)
  278. }
  279. // 以[公司]为组, 计算合约加总
  280. mussyTradeData := make(map[string]*tradeAnalysisModel.ContractCompanyTradeData)
  281. for k, v := range companyContracts {
  282. companyData := new(tradeAnalysisModel.ContractCompanyTradeData)
  283. companyData.CompanyName = k
  284. companyData.DataList = make([]*tradeAnalysisModel.ContractCompanyTradeDataList, 0)
  285. contractArr := make([]string, 0)
  286. // 合约加总
  287. sumDateData := make(map[time.Time]*tradeAnalysisModel.ContractCompanyTradeDataList)
  288. for _, vv := range v {
  289. contractArr = append(contractArr, vv.ClassifyType)
  290. for _, dv := range vv.DataList {
  291. if sumDateData[dv.Date] == nil {
  292. sumDateData[dv.Date] = new(tradeAnalysisModel.ContractCompanyTradeDataList)
  293. sumDateData[dv.Date].Date = dv.Date
  294. }
  295. // 数据类型以第一个非零值为准, 只处理多空和净多, 变化就不管了
  296. if sumDateData[dv.Date].BuyValType == tradeAnalysisModel.TradeDataTypeNull && dv.BuyValType > tradeAnalysisModel.TradeDataTypeNull {
  297. sumDateData[dv.Date].BuyValType = dv.BuyValType
  298. }
  299. if sumDateData[dv.Date].BuyValType == tradeAnalysisModel.TradeDataTypeOrigin && dv.BuyValType == tradeAnalysisModel.TradeDataTypeCalculate {
  300. sumDateData[dv.Date].BuyValType = dv.BuyValType
  301. }
  302. if dv.BuyValType > tradeAnalysisModel.TradeDataTypeNull {
  303. sumDateData[dv.Date].BuyVal += dv.BuyVal
  304. }
  305. // 空单
  306. if sumDateData[dv.Date].SoldValType == tradeAnalysisModel.TradeDataTypeNull && dv.SoldValType > tradeAnalysisModel.TradeDataTypeNull {
  307. sumDateData[dv.Date].SoldValType = dv.SoldValType
  308. }
  309. if sumDateData[dv.Date].SoldValType == tradeAnalysisModel.TradeDataTypeOrigin && dv.SoldValType == tradeAnalysisModel.TradeDataTypeCalculate {
  310. sumDateData[dv.Date].SoldValType = dv.SoldValType
  311. }
  312. if dv.SoldValType > tradeAnalysisModel.TradeDataTypeNull {
  313. sumDateData[dv.Date].SoldVal += dv.SoldVal
  314. }
  315. // 净多单
  316. if sumDateData[dv.Date].PureBuyValType == tradeAnalysisModel.TradeDataTypeNull && dv.PureBuyValType > tradeAnalysisModel.TradeDataTypeNull {
  317. sumDateData[dv.Date].PureBuyValType = dv.PureBuyValType
  318. }
  319. if sumDateData[dv.Date].PureBuyValType == tradeAnalysisModel.TradeDataTypeOrigin && dv.PureBuyValType == tradeAnalysisModel.TradeDataTypeCalculate {
  320. sumDateData[dv.Date].PureBuyValType = dv.PureBuyValType
  321. }
  322. if dv.PureBuyValType > tradeAnalysisModel.TradeDataTypeNull {
  323. sumDateData[dv.Date].PureBuyVal += dv.PureBuyVal
  324. }
  325. }
  326. // 多个合约比对开始结束时间
  327. if companyData.StartDate.IsZero() {
  328. companyData.StartDate = vv.StartDate
  329. }
  330. if vv.StartDate.Before(companyData.StartDate) {
  331. companyData.StartDate = vv.StartDate
  332. }
  333. if companyData.EndDate.IsZero() {
  334. companyData.EndDate = vv.EndDate
  335. }
  336. if vv.EndDate.Before(companyData.EndDate) {
  337. companyData.EndDate = vv.EndDate
  338. }
  339. }
  340. for _, sv := range sumDateData {
  341. companyData.DataList = append(companyData.DataList, sv)
  342. }
  343. sort.Slice(companyData.DataList, func(i, j int) bool {
  344. return companyData.DataList[i].Date.Before(companyData.DataList[j].Date)
  345. })
  346. companyData.ClassifyType = strings.Join(contractArr, ",")
  347. mussyTradeData[k] = companyData
  348. }
  349. // 数据根据公司排序, 不然会随机乱
  350. companyTradeData = make([]*tradeAnalysisModel.ContractCompanyTradeData, 0)
  351. for _, v := range companies {
  352. // 没数据也需要加进去, 不然edbList会少
  353. if mussyTradeData[v] == nil {
  354. companyData := new(tradeAnalysisModel.ContractCompanyTradeData)
  355. companyData.CompanyName = v
  356. companyData.DataList = make([]*tradeAnalysisModel.ContractCompanyTradeDataList, 0)
  357. companyTradeData = append(companyTradeData, companyData)
  358. continue
  359. }
  360. companyTradeData = append(companyTradeData, mussyTradeData[v])
  361. }
  362. return
  363. }
  364. // PredictingTradeData 根据数据库中的多空数据填充预估数据
  365. func PredictingTradeData(originData []*tradeAnalysisModel.ContractCompanyTradeDataList, lastBuyDateVal, lastSoldDateVal map[time.Time]int, predictRatio float64) (newData []*tradeAnalysisModel.ContractCompanyTradeDataList, firstDate, endDate time.Time, err error) {
  366. // 测试用的验证数据
  367. //lastBuyDateVal, lastSoldDateVal = make(map[time.Time]int), make(map[time.Time]int)
  368. //lastBuyDateVal[time.Date(2024, 7, 16, 0, 0, 0, 0, time.Local)] = 4602
  369. //lastBuyDateVal[time.Date(2024, 7, 17, 0, 0, 0, 0, time.Local)] = 5116
  370. //lastBuyDateVal[time.Date(2024, 7, 18, 0, 0, 0, 0, time.Local)] = 5130
  371. //lastBuyDateVal[time.Date(2024, 7, 19, 0, 0, 0, 0, time.Local)] = 5354
  372. //lastBuyDateVal[time.Date(2024, 7, 22, 0, 0, 0, 0, time.Local)] = 5916
  373. //lastBuyDateVal[time.Date(2024, 7, 23, 0, 0, 0, 0, time.Local)] = 6524
  374. //lastBuyDateVal[time.Date(2024, 7, 26, 0, 0, 0, 0, time.Local)] = 6575
  375. //lastBuyDateVal[time.Date(2024, 7, 29, 0, 0, 0, 0, time.Local)] = 7461
  376. //lastBuyDateVal[time.Date(2024, 7, 30, 0, 0, 0, 0, time.Local)] = 8488
  377. //
  378. //lastSoldDateVal[time.Date(2024, 7, 11, 0, 0, 0, 0, time.Local)] = 5467
  379. //lastSoldDateVal[time.Date(2024, 7, 12, 0, 0, 0, 0, time.Local)] = 5248
  380. //lastSoldDateVal[time.Date(2024, 7, 15, 0, 0, 0, 0, time.Local)] = 5102
  381. //lastSoldDateVal[time.Date(2024, 7, 16, 0, 0, 0, 0, time.Local)] = 4771
  382. //lastSoldDateVal[time.Date(2024, 7, 23, 0, 0, 0, 0, time.Local)] = 5989
  383. //lastSoldDateVal[time.Date(2024, 7, 26, 0, 0, 0, 0, time.Local)] = 6745
  384. //lastSoldDateVal[time.Date(2024, 7, 30, 0, 0, 0, 0, time.Local)] = 7272
  385. //
  386. //originData = make([]*tradeAnalysisModel.ContractCompanyTradeDataList, 0)
  387. //originData = append(originData, &tradeAnalysisModel.ContractCompanyTradeDataList{
  388. // Date: time.Date(2024, 7, 10, 0, 0, 0, 0, time.Local),
  389. // BuyVal: 14324,
  390. // BuyValType: tradeAnalysisModel.TradeDataTypeOrigin,
  391. // BuyChange: -1107,
  392. // BuyChangeType: tradeAnalysisModel.TradeDataTypeOrigin,
  393. // SoldVal: 0,
  394. // SoldValType: tradeAnalysisModel.TradeDataTypeNull,
  395. // SoldChange: 0,
  396. // SoldChangeType: tradeAnalysisModel.TradeDataTypeNull,
  397. //}, &tradeAnalysisModel.ContractCompanyTradeDataList{
  398. // Date: time.Date(2024, 7, 11, 0, 0, 0, 0, time.Local),
  399. // BuyVal: 14280,
  400. // BuyValType: tradeAnalysisModel.TradeDataTypeOrigin,
  401. // BuyChange: -44,
  402. // BuyChangeType: tradeAnalysisModel.TradeDataTypeOrigin,
  403. //}, &tradeAnalysisModel.ContractCompanyTradeDataList{
  404. // Date: time.Date(2024, 7, 12, 0, 0, 0, 0, time.Local),
  405. // BuyVal: 14214,
  406. // BuyValType: tradeAnalysisModel.TradeDataTypeOrigin,
  407. // BuyChange: -66,
  408. // BuyChangeType: tradeAnalysisModel.TradeDataTypeOrigin,
  409. //}, &tradeAnalysisModel.ContractCompanyTradeDataList{
  410. // Date: time.Date(2024, 7, 15, 0, 0, 0, 0, time.Local),
  411. // BuyVal: 14269,
  412. // BuyValType: tradeAnalysisModel.TradeDataTypeOrigin,
  413. // BuyChange: 55,
  414. // BuyChangeType: tradeAnalysisModel.TradeDataTypeOrigin,
  415. //}, &tradeAnalysisModel.ContractCompanyTradeDataList{
  416. // Date: time.Date(2024, 7, 17, 0, 0, 0, 0, time.Local),
  417. // SoldVal: 5254,
  418. // SoldValType: tradeAnalysisModel.TradeDataTypeOrigin,
  419. // SoldChange: 708,
  420. // SoldChangeType: tradeAnalysisModel.TradeDataTypeOrigin,
  421. //}, &tradeAnalysisModel.ContractCompanyTradeDataList{
  422. // Date: time.Date(2024, 7, 18, 0, 0, 0, 0, time.Local),
  423. // SoldVal: 6595,
  424. // SoldValType: tradeAnalysisModel.TradeDataTypeOrigin,
  425. // SoldChange: 1341,
  426. // SoldChangeType: tradeAnalysisModel.TradeDataTypeOrigin,
  427. //}, &tradeAnalysisModel.ContractCompanyTradeDataList{
  428. // Date: time.Date(2024, 7, 19, 0, 0, 0, 0, time.Local),
  429. // SoldVal: 5938,
  430. // SoldValType: tradeAnalysisModel.TradeDataTypeOrigin,
  431. // SoldChange: -657,
  432. // SoldChangeType: tradeAnalysisModel.TradeDataTypeOrigin,
  433. //}, &tradeAnalysisModel.ContractCompanyTradeDataList{
  434. // Date: time.Date(2024, 7, 22, 0, 0, 0, 0, time.Local),
  435. // SoldVal: 6131,
  436. // SoldValType: tradeAnalysisModel.TradeDataTypeOrigin,
  437. // SoldChange: 193,
  438. // SoldChangeType: tradeAnalysisModel.TradeDataTypeOrigin,
  439. //}, &tradeAnalysisModel.ContractCompanyTradeDataList{
  440. // Date: time.Date(2024, 7, 29, 0, 0, 0, 0, time.Local),
  441. // SoldVal: 6679,
  442. // SoldValType: tradeAnalysisModel.TradeDataTypeOrigin,
  443. // SoldChange: 312,
  444. // SoldChangeType: tradeAnalysisModel.TradeDataTypeOrigin,
  445. //})
  446. if len(originData) == 0 {
  447. return
  448. }
  449. if predictRatio < 0 || predictRatio > 1 {
  450. err = fmt.Errorf("估计参数不在0-1之间")
  451. return
  452. }
  453. sort.Slice(originData, func(i, j int) bool {
  454. return originData[i].Date.Before(originData[j].Date)
  455. })
  456. dateVal := make(map[time.Time]*tradeAnalysisModel.ContractCompanyTradeDataList)
  457. for _, v := range originData {
  458. dateVal[v.Date] = v
  459. }
  460. // 生成开始日期-1d(可能会往前面推算一天)至结束日期间的交易日, 以交易日为时间序列遍历
  461. tradeDays := utils.GetTradingDays(originData[0].Date.AddDate(0, 0, -1), originData[len(originData)-1].Date)
  462. for k, v := range tradeDays {
  463. // T日多空均无的情况
  464. //bothLast := false
  465. if dateVal[v] == nil {
  466. // T-1和T+1[原始数据]均无值, 那么T日无数据
  467. hasPrev, hasNext := false, false
  468. if k-1 >= 0 {
  469. hasPrev = true
  470. }
  471. if k+1 <= len(tradeDays)-1 {
  472. hasNext = true
  473. }
  474. if !hasPrev && !hasNext {
  475. continue
  476. }
  477. // T+1有值, 优先从T+1推, 然后继续走下面计算净多单的逻辑
  478. if hasNext {
  479. nextDay := tradeDays[k+1]
  480. if dateVal[nextDay] != nil {
  481. // T+1有多/空及多空变化, 且是原始数据, 那么推出数据并在map中新加一日数据
  482. if dateVal[nextDay].BuyValType == tradeAnalysisModel.TradeDataTypeOrigin && dateVal[nextDay].BuyChangeType == tradeAnalysisModel.TradeDataTypeOrigin {
  483. if _, ok := dateVal[v]; !ok {
  484. dateVal[v] = new(tradeAnalysisModel.ContractCompanyTradeDataList)
  485. dateVal[v].Date = v
  486. }
  487. dateVal[v].BuyVal = dateVal[nextDay].BuyVal - dateVal[nextDay].BuyChange
  488. dateVal[v].BuyValType = tradeAnalysisModel.TradeDataTypeOrigin
  489. }
  490. if dateVal[nextDay].SoldValType == tradeAnalysisModel.TradeDataTypeOrigin && dateVal[nextDay].SoldChangeType == tradeAnalysisModel.TradeDataTypeOrigin {
  491. if _, ok := dateVal[v]; !ok {
  492. dateVal[v] = new(tradeAnalysisModel.ContractCompanyTradeDataList)
  493. dateVal[v].Date = v
  494. }
  495. dateVal[v].SoldVal = dateVal[nextDay].SoldVal - dateVal[nextDay].SoldChange
  496. dateVal[v].SoldValType = tradeAnalysisModel.TradeDataTypeOrigin
  497. }
  498. }
  499. }
  500. // T+1没推出来而T-1有值, 那么T多空均取末位, 计算净多单
  501. _, has := dateVal[v]
  502. if hasPrev && !has {
  503. sv, sok := lastSoldDateVal[v]
  504. bv, bok := lastBuyDateVal[v]
  505. if !sok && !bok {
  506. continue
  507. }
  508. dateVal[v] = new(tradeAnalysisModel.ContractCompanyTradeDataList)
  509. dateVal[v].Date = v
  510. if sok {
  511. dateVal[v].SoldVal = int(predictRatio*float64(sv) + 0.5)
  512. dateVal[v].SoldValType = tradeAnalysisModel.TradeDataTypeCalculate
  513. }
  514. if bok {
  515. dateVal[v].BuyVal = int(predictRatio*float64(bv) + 0.5)
  516. dateVal[v].BuyValType = tradeAnalysisModel.TradeDataTypeCalculate
  517. }
  518. if dateVal[v].BuyValType > tradeAnalysisModel.TradeDataTypeNull && dateVal[v].SoldValType > tradeAnalysisModel.TradeDataTypeNull {
  519. dateVal[v].PureBuyVal = dateVal[v].BuyVal - dateVal[v].SoldVal
  520. dateVal[v].PureBuyValType = tradeAnalysisModel.TradeDataTypeCalculate
  521. }
  522. continue
  523. }
  524. }
  525. // 多空均有的情况下计算净多单
  526. if dateVal[v].BuyValType == tradeAnalysisModel.TradeDataTypeOrigin && dateVal[v].SoldValType == tradeAnalysisModel.TradeDataTypeOrigin {
  527. dateVal[v].PureBuyVal = dateVal[v].BuyVal - dateVal[v].SoldVal
  528. dateVal[v].PureBuyValType = tradeAnalysisModel.TradeDataTypeOrigin // 原始值算出来的也作原始值
  529. }
  530. // 仅有多单, 空单取末位, 计算净多单
  531. if dateVal[v].BuyValType == tradeAnalysisModel.TradeDataTypeOrigin && dateVal[v].SoldValType == tradeAnalysisModel.TradeDataTypeNull {
  532. if sv, ok := lastSoldDateVal[v]; ok {
  533. dateVal[v].SoldVal = int(predictRatio*float64(sv) + 0.5) // 估计参数*末位值, 向上取整
  534. dateVal[v].SoldValType = tradeAnalysisModel.TradeDataTypeCalculate
  535. dateVal[v].PureBuyVal = dateVal[v].BuyVal - dateVal[v].SoldVal
  536. dateVal[v].PureBuyValType = tradeAnalysisModel.TradeDataTypeCalculate
  537. }
  538. }
  539. // 仅有空单, 多单取末位, 计算净多单
  540. if dateVal[v].SoldValType == tradeAnalysisModel.TradeDataTypeOrigin && dateVal[v].BuyValType == tradeAnalysisModel.TradeDataTypeNull {
  541. if sv, ok := lastBuyDateVal[v]; ok {
  542. dateVal[v].BuyVal = int(predictRatio*float64(sv) + 0.5)
  543. dateVal[v].BuyValType = tradeAnalysisModel.TradeDataTypeCalculate
  544. dateVal[v].PureBuyVal = dateVal[v].BuyVal - dateVal[v].SoldVal
  545. dateVal[v].PureBuyValType = tradeAnalysisModel.TradeDataTypeCalculate
  546. }
  547. }
  548. }
  549. // 二次遍历, 计算与T-1的变化值
  550. for k, v := range tradeDays {
  551. // 无T/T-1数据, 忽略
  552. if dateVal[v] == nil {
  553. continue
  554. }
  555. if k-1 < 0 {
  556. continue
  557. }
  558. beforeDay := tradeDays[k-1]
  559. if dateVal[beforeDay] == nil {
  560. continue
  561. }
  562. // 多单变化
  563. if dateVal[v].BuyChangeType == tradeAnalysisModel.TradeDataTypeNull {
  564. if dateVal[v].BuyValType > tradeAnalysisModel.TradeDataTypeNull && dateVal[beforeDay].BuyValType > tradeAnalysisModel.TradeDataTypeNull {
  565. dateVal[v].BuyChange = dateVal[v].BuyVal - dateVal[beforeDay].BuyVal
  566. // 如果当日多单或者前日多单是估计值, 那么多单变化也为估计值
  567. if dateVal[v].BuyValType == tradeAnalysisModel.TradeDataTypeCalculate || dateVal[beforeDay].BuyValType == tradeAnalysisModel.TradeDataTypeCalculate {
  568. dateVal[v].BuyChangeType = tradeAnalysisModel.TradeDataTypeCalculate
  569. }
  570. }
  571. }
  572. // 空单变化
  573. if dateVal[v].SoldChangeType == tradeAnalysisModel.TradeDataTypeNull {
  574. if dateVal[v].SoldValType > tradeAnalysisModel.TradeDataTypeNull && dateVal[beforeDay].SoldValType > tradeAnalysisModel.TradeDataTypeNull {
  575. dateVal[v].SoldChange = dateVal[v].SoldVal - dateVal[beforeDay].SoldVal
  576. // 如果当日空单或者前日空单是估计值, 那么空单变化也为估计值
  577. if dateVal[v].SoldValType == tradeAnalysisModel.TradeDataTypeCalculate || dateVal[beforeDay].SoldValType == tradeAnalysisModel.TradeDataTypeCalculate {
  578. dateVal[v].SoldChangeType = tradeAnalysisModel.TradeDataTypeCalculate
  579. }
  580. }
  581. }
  582. // 净多变化
  583. if dateVal[v].PureBuyChangeType == tradeAnalysisModel.TradeDataTypeNull {
  584. if dateVal[v].PureBuyValType > tradeAnalysisModel.TradeDataTypeNull && dateVal[beforeDay].PureBuyValType > tradeAnalysisModel.TradeDataTypeNull {
  585. dateVal[v].PureBuyChange = dateVal[v].PureBuyVal - dateVal[beforeDay].PureBuyVal
  586. dateVal[v].PureBuyChangeType = tradeAnalysisModel.TradeDataTypeOrigin
  587. // 如果当日净多单或者前日净多单是估计值, 那么净多单变化也为估计值
  588. if dateVal[v].PureBuyValType == tradeAnalysisModel.TradeDataTypeCalculate || dateVal[beforeDay].PureBuyValType == tradeAnalysisModel.TradeDataTypeCalculate {
  589. dateVal[v].PureBuyChangeType = tradeAnalysisModel.TradeDataTypeCalculate
  590. }
  591. }
  592. }
  593. }
  594. // 重新遍历map, 生成数据序列并排序
  595. newData = make([]*tradeAnalysisModel.ContractCompanyTradeDataList, 0)
  596. for _, v := range dateVal {
  597. if v.BuyValType == tradeAnalysisModel.TradeDataTypeNull && v.SoldValType == tradeAnalysisModel.TradeDataTypeNull {
  598. continue
  599. }
  600. newData = append(newData, v)
  601. }
  602. sort.Slice(newData, func(i, j int) bool {
  603. return newData[i].Date.Before(newData[j].Date)
  604. })
  605. if len(newData) > 0 {
  606. firstDate = newData[0].Date
  607. endDate = newData[len(newData)-1].Date
  608. }
  609. return
  610. }