trade_position_analysis_classify.go 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592
  1. package data
  2. import (
  3. "fmt"
  4. "hongze/hongze_task/models/data_manage"
  5. "hongze/hongze_task/services/alarm_msg"
  6. "hongze/hongze_task/utils"
  7. "sort"
  8. "strconv"
  9. "time"
  10. )
  11. // FixPositionTask 补全缺失的合约, 注意和原先的定时任务区分开来
  12. func FixPositionTask() (err error) {
  13. exchanges := []string{"zhengzhou", "dalian", "shanghai", "cffex", "ine"} //郑商所,大商所,上期所,中金所,上期能源
  14. for i := 194; i > 1; i-- {
  15. // 定时任务避开昨日和今日的数据,以免和原先的定时任务InitPositionTask冲突
  16. startDate := time.Now().AddDate(0, 0, -i).Format(utils.FormatDate)
  17. endDate := startDate
  18. for _, v := range exchanges {
  19. exchange := v
  20. err = nil
  21. fmt.Println("FixPositionTask: 启动:" + exchange)
  22. utils.FileLog.Info("FixPositionTask: 启动:" + exchange)
  23. fmt.Println("开始" + startDate + "结束" + endDate)
  24. utils.FileLog.Info(fmt.Sprintf("FixPositionTask:开始:%s; 结束:%s", startDate, endDate))
  25. tErr, errMsg := InitTradePositionClassify(exchange, startDate, endDate)
  26. if tErr != nil {
  27. err = tErr
  28. fmt.Println("FixPositionTask: 操作失败:" + errMsg + tErr.Error())
  29. utils.FileLog.Info(fmt.Sprintf("InitTradePosition: 操作失败:%s:%s", errMsg, tErr.Error()))
  30. continue
  31. }
  32. fmt.Println("FixPositionTask:" + exchange + "已完成")
  33. utils.FileLog.Info("FixPositionTask:" + exchange + "已完成")
  34. }
  35. }
  36. return
  37. }
  38. // InitTradePositionClassify 持仓分析补全缺失的合约
  39. func InitTradePositionClassify(exchange, startDate, endDate string) (err error, errMsg string) {
  40. defer func() {
  41. if err != nil {
  42. tips := fmt.Sprintf("持仓分析-补全缺失的品种数据 操作失败, Exchange: %s, Err: %s, Msg: %s", exchange, err.Error(), errMsg)
  43. alarm_msg.SendAlarmMsg(tips, 3)
  44. }
  45. }()
  46. //判断合约数与数据源是否一致
  47. originClassifyList, tmpErr := data_manage.GetTradePositionOriginClassifyByExchangeDataTime(exchange, startDate, endDate)
  48. if tmpErr != nil {
  49. err = tmpErr
  50. errMsg = "查询原始数据分类个数失败,GetTradePositionOriginClassifyCountByExchangeDataTime() Err: "
  51. return
  52. }
  53. topClassifyList, tmpErr := data_manage.GetTradePositionTopClassifyByExchangeDataTime(exchange, startDate, endDate)
  54. if tmpErr != nil {
  55. err = tmpErr
  56. errMsg = "查询榜单数据分类个数失败,GetTradePositionTopClassifyCountByExchangeDataTime() Err: "
  57. return
  58. }
  59. if len(originClassifyList) == len(topClassifyList) {
  60. //合约数目一致,无需补全
  61. return
  62. }
  63. if len(originClassifyList) == 0 {
  64. return
  65. }
  66. topClassifyMap := make(map[string][]data_manage.TradePositionClassifyInfo)
  67. for _, v := range topClassifyList {
  68. str := fmt.Sprintf("%s_%s", v.ClassifyName, v.ClassifyType)
  69. topClassifyMap[str] = append(topClassifyMap[str], v)
  70. }
  71. classifyMap := make(map[string]struct{})
  72. classifyNames := make([]string, 0)
  73. classifyTypes := make([]string, 0)
  74. for _, v := range originClassifyList {
  75. str := fmt.Sprintf("%s_%s", v.ClassifyName, v.ClassifyType)
  76. if _, ok := topClassifyMap[str]; !ok {
  77. classifyTypes = append(classifyTypes, v.ClassifyType)
  78. if _, ok1 := classifyMap[v.ClassifyName]; !ok1 {
  79. classifyNames = append(classifyNames, v.ClassifyName)
  80. classifyMap[v.ClassifyName] = struct{}{}
  81. }
  82. }
  83. }
  84. if len(classifyTypes) == 0 {
  85. return
  86. }
  87. //批量导入缺失的合约
  88. err = data_manage.MultiInsertTradeBaseDataToTopByClassify(exchange, startDate, endDate, classifyNames, classifyTypes)
  89. if err != nil {
  90. errMsg = "新增原始数据失败,MultiInsertTradeBaseDataToTop() Err: "
  91. return
  92. }
  93. originList, err := data_manage.GetTradePositionTopByExchangeDataTimeByClassify(exchange, startDate, endDate, classifyNames, classifyTypes)
  94. if err != nil {
  95. errMsg = "查询原始数据失败, GetTradePositionTopByExchangeDataTime() Err: "
  96. return
  97. }
  98. if len(originList) <= 0 {
  99. // 忽略周末
  100. w := time.Now().Weekday().String()
  101. if w == "Saturday" || w == "Sunday" {
  102. return
  103. }
  104. // 每天最后一个小时执行依旧无数据时, 才进行邮件提示
  105. if time.Now().Hour() != 23 {
  106. return
  107. }
  108. err = fmt.Errorf("原始数据没有值")
  109. return
  110. }
  111. // 原始数据日期
  112. dates, e := data_manage.GetTradePositionTopOriginDataTimes(exchange)
  113. if e != nil {
  114. err = fmt.Errorf("GetTradePositionTopOriginDataTimes err: %s", e.Error())
  115. return
  116. }
  117. now := time.Now()
  118. dataTimeMap := make(map[string]*data_manage.TradePositionTop)
  119. onlyEmptyMap := make(map[string]bool)
  120. onlyEmptyNameMap := make(map[string]*data_manage.TradePositionTop)
  121. topLastMap := make(map[string]int)
  122. topLastRankMap := make(map[string]int)
  123. list := make([]*data_manage.TradePositionTop, 0)
  124. for _, v := range originList {
  125. tmp0, tmpErr := dealTradeOriginData(dataTimeMap, onlyEmptyMap, onlyEmptyNameMap, v, topLastMap, topLastRankMap, startDate, now, dates)
  126. if tmpErr != nil {
  127. err = tmpErr
  128. errMsg = "处理原始数据失败 dealTradeOriginData() Err: "
  129. return
  130. }
  131. if tmp0 != nil {
  132. list = append(list, tmp0)
  133. }
  134. if len(list) >= 1000 {
  135. err = data_manage.InsertMultiTradePositionTop(exchange, list)
  136. if err != nil {
  137. errMsg = "批量新增昨日数据失败,InsertMultiTradePositionTop() Err: "
  138. return
  139. }
  140. list = make([]*data_manage.TradePositionTop, 0)
  141. }
  142. }
  143. if len(list) > 0 {
  144. err = data_manage.InsertMultiTradePositionTop(exchange, list)
  145. if err != nil {
  146. errMsg = "批量新增昨日数据失败,InsertMultiTradePositionTop() Err: "
  147. return
  148. }
  149. list = make([]*data_manage.TradePositionTop, 0)
  150. }
  151. // 处理某个期货公司只有买单没有卖单,或者只有卖单没有买单的情况
  152. for k, v := range onlyEmptyNameMap {
  153. _, ok1 := onlyEmptyMap[k+"_1"]
  154. _, ok2 := onlyEmptyMap[k+"_2"]
  155. var dealType int
  156. if ok1 && !ok2 {
  157. dealType = 2 //只有买单没有卖单
  158. } else if !ok1 && ok2 {
  159. dealType = 1 //只有卖单没有买单的情况
  160. } else {
  161. continue
  162. }
  163. if dealType > 0 {
  164. str := v.ClassifyName + "_" + v.ClassifyType + "_" + v.DataTime + "_" + strconv.Itoa(dealType)
  165. dealValue := 0
  166. if lastVal, ok := topLastMap[str]; ok {
  167. dealValue = int(float64(lastVal)*0.7 + 0.5)
  168. }
  169. tmp := &data_manage.TradePositionTop{
  170. ClassifyName: v.ClassifyName,
  171. ClassifyType: v.ClassifyType,
  172. DealShortName: v.DealShortName,
  173. DataTime: v.DataTime,
  174. DealValue: dealValue,
  175. CreateTime: now,
  176. ModifyTime: now,
  177. DealType: dealType,
  178. SourceType: 2,
  179. }
  180. list = append(list, tmp)
  181. if len(list) >= 1000 {
  182. err = data_manage.InsertMultiTradePositionTop(exchange, list)
  183. if err != nil {
  184. errMsg = "批量新增前日数据失败,InsertMultiTradePositionTop() Err: "
  185. return
  186. }
  187. list = make([]*data_manage.TradePositionTop, 0)
  188. }
  189. }
  190. }
  191. if len(list) > 0 {
  192. err = data_manage.InsertMultiTradePositionTop(exchange, list)
  193. if err != nil {
  194. errMsg = "批量新增前日数据失败,InsertMultiTradePositionTop() Err: "
  195. return
  196. }
  197. }
  198. //生成净多单,净空单榜单
  199. err = createAnalysisCleanTopClassify(exchange, startDate, endDate, classifyNames, classifyTypes)
  200. if err != nil {
  201. errMsg = "创建净多单,净空单数据失败,createAnalysisCleanTop() Err: "
  202. return
  203. }
  204. err = DealYesterdayDataClassify(exchange, startDate, classifyNames, classifyTypes)
  205. if err != nil {
  206. errMsg = "处理昨日数据失败,DealYesterdayData() Err: "
  207. return
  208. }
  209. return
  210. }
  211. // DealYesterdayDataClassify 更新部分合约的昨日数据
  212. func DealYesterdayDataClassify(exchange, startDate string, classifyNames, classifyTypes []string) (err error) {
  213. // 查询最早的日期
  214. firstItem, err := data_manage.GetFirstBaseFromTradeIndexByDate(exchange)
  215. if err != nil {
  216. return
  217. }
  218. if startDate == firstItem.DataTime { //如果当前是起始日,则无需统计修改前一天的数据
  219. return
  220. }
  221. // 前一个交易日, 前两个交易日
  222. dates, e := data_manage.GetTradePositionTopOriginDataTimes(exchange)
  223. if e != nil {
  224. err = fmt.Errorf("GetTradePositionTopOriginDataTimes err: %s", e.Error())
  225. return
  226. }
  227. yesterdayStr := getPrevTradeDataDate(startDate, dates)
  228. beforeYesterdayStr := getPrevTradeDataDate(yesterdayStr, dates)
  229. // 先查出T日最原始的数据
  230. originList, err := data_manage.GetTradePositionTopByExchangeDataTimeByClassify(exchange, startDate, startDate, classifyNames, classifyTypes)
  231. if err != nil {
  232. return
  233. }
  234. originBuyMap := make(map[string]*data_manage.TradePositionTop)
  235. originSoldMap := make(map[string]*data_manage.TradePositionTop)
  236. for _, v := range originList {
  237. if v.SourceType != 0 {
  238. continue
  239. }
  240. str := v.ClassifyName + "_" + v.ClassifyType + "_" + v.DealShortName
  241. if v.DealType == 1 {
  242. originBuyMap[str] = v
  243. } else if v.DealType == 2 {
  244. originSoldMap[str] = v
  245. }
  246. }
  247. // 然后查询T-1中数据来源类型是2的数据
  248. changeList, err := data_manage.GetTradePositionTopByExchangeSourceTypeClassify(exchange, yesterdayStr, 2, classifyNames, classifyTypes)
  249. if err != nil {
  250. return
  251. }
  252. if len(changeList) <= 0 {
  253. //err = fmt.Errorf("前天的数据无需修改")
  254. return
  255. }
  256. // 查询出前日的成交量
  257. beforeYesterdayList, err := data_manage.GetTradePositionTopByExchangeDataTimeByClassify(exchange, beforeYesterdayStr, beforeYesterdayStr, classifyNames, classifyTypes)
  258. if err != nil {
  259. return
  260. }
  261. beforeYesterdayMap1 := make(map[string]int)
  262. beforeYesterdayMap2 := make(map[string]int)
  263. if len(beforeYesterdayList) > 0 {
  264. for _, v := range beforeYesterdayList {
  265. if v.SourceType == 2 {
  266. continue
  267. }
  268. str := v.ClassifyName + "_" + v.ClassifyType + "_" + v.DealShortName
  269. if v.DealType == 1 {
  270. beforeYesterdayMap1[str] = v.DealValue
  271. } else if v.DealType == 2 {
  272. beforeYesterdayMap2[str] = v.DealValue
  273. }
  274. }
  275. }
  276. // 根据原始数据中的值推算出最新的值
  277. now := time.Now()
  278. // 批量更新到分析表中,
  279. var updateAnalysisData []data_manage.UpdateDealValueChange
  280. for _, v := range changeList {
  281. str := v.ClassifyName + "_" + v.ClassifyType + "_" + v.DealShortName
  282. dealValue := 0
  283. dealChange := 0
  284. if v.DealType == 1 {
  285. if n, ok := originBuyMap[str]; ok {
  286. dealValue = n.DealValue - n.DealChange
  287. if beforeVal, ok1 := beforeYesterdayMap1[str]; ok1 {
  288. dealChange = dealValue - beforeVal
  289. }
  290. tmp := data_manage.UpdateDealValueChange{
  291. Id: v.Id,
  292. DealValue: dealValue,
  293. DealChange: dealChange,
  294. SourceType: 1,
  295. ModifyTime: now,
  296. }
  297. updateAnalysisData = append(updateAnalysisData, tmp)
  298. }
  299. } else if v.DealType == 2 {
  300. if n, ok := originSoldMap[str]; ok {
  301. dealValue = n.DealValue - n.DealChange
  302. if beforeVal, ok1 := beforeYesterdayMap2[str]; ok1 {
  303. dealChange = dealValue - beforeVal
  304. }
  305. tmp := data_manage.UpdateDealValueChange{
  306. Id: v.Id,
  307. DealValue: dealValue,
  308. DealChange: dealChange,
  309. SourceType: 1,
  310. ModifyTime: now,
  311. }
  312. updateAnalysisData = append(updateAnalysisData, tmp)
  313. }
  314. }
  315. }
  316. if len(updateAnalysisData) > 0 {
  317. err = data_manage.MultiUpdatePositionTop(exchange, updateAnalysisData)
  318. if err != nil {
  319. return
  320. }
  321. //删除T-1日净多单和净空单的榜单
  322. err = data_manage.DeletePositionTopByDataTimeClassify(exchange, yesterdayStr, 3, classifyNames, classifyTypes)
  323. if err != nil {
  324. return
  325. }
  326. err = data_manage.DeletePositionTopByDataTimeClassify(exchange, yesterdayStr, 4, classifyNames, classifyTypes)
  327. if err != nil {
  328. return
  329. }
  330. //重新生成净多单和净空单的榜单
  331. err = createAnalysisCleanTopClassify(exchange, yesterdayStr, yesterdayStr, classifyNames, classifyTypes)
  332. if err != nil {
  333. return
  334. }
  335. //T-1日重新生成净多单和净空单的榜单后,需要更新T日净多单和净空单榜单里的变化量
  336. err = updateAnalysisCleanTopChangeValClassify(exchange, startDate, yesterdayStr, classifyNames, classifyTypes)
  337. if err != nil {
  338. return
  339. }
  340. }
  341. return
  342. }
  343. // createAnalysisCleanTopClassify 生成部分合约的净多单,净空单榜单
  344. func createAnalysisCleanTopClassify(exchange, startDate, endDate string, classifyNames, classifyTypes []string) (err error) {
  345. defer func() {
  346. if err != nil {
  347. fmt.Println("createAnalysisCleanTop err: " + err.Error())
  348. }
  349. }()
  350. topList := make([]*data_manage.TradePositionTop, 0)
  351. now := time.Now()
  352. var subDataList data_manage.TradePositionSubList
  353. subChangeMap1 := make(map[string]int) //净多单map
  354. subChangeMap2 := make(map[string]int) //净空单map
  355. // 2023-05-10 此处取前一个交易日, 不一定是昨日
  356. dates, e := data_manage.GetTradePositionTopOriginDataTimes(exchange)
  357. if e != nil {
  358. err = fmt.Errorf("GetTradePositionTopOriginDataTimes err: %s", e.Error())
  359. return
  360. }
  361. yesterday := getPrevTradeDataDate(startDate, dates)
  362. // 上一个交易日的净多单
  363. yesterdayTopList1, tErr := data_manage.GetTradePositionTopByExchangeDataTimeTypeClassify(exchange, yesterday, 3, classifyNames, classifyTypes)
  364. if tErr != nil {
  365. err = tErr
  366. return
  367. }
  368. if len(yesterdayTopList1) > 0 {
  369. for _, v := range yesterdayTopList1 {
  370. nameStr := v.ClassifyName + "_" + v.ClassifyType + "_" + v.DataTime + "_" + v.DealShortName
  371. subChangeMap1[nameStr] = v.DealValue
  372. }
  373. }
  374. // 上一个交易日的净空单
  375. yesterdayTopList2, tErr := data_manage.GetTradePositionTopByExchangeDataTimeTypeClassify(exchange, yesterday, 4, classifyNames, classifyTypes)
  376. if tErr != nil {
  377. err = tErr
  378. return
  379. }
  380. if len(yesterdayTopList2) > 0 {
  381. for _, v := range yesterdayTopList2 {
  382. nameStr := v.ClassifyName + "_" + v.ClassifyType + "_" + v.DataTime + "_" + v.DealShortName
  383. subChangeMap2[nameStr] = v.DealValue
  384. }
  385. }
  386. // 根据当日多单/空单数据, 生成净多单/净空单数据
  387. originDataList, err := data_manage.GetTradePositionTopByExchangeDataTimeByClassify(exchange, startDate, endDate, classifyNames, classifyTypes)
  388. if err != nil {
  389. return
  390. }
  391. buyDataMap := make(map[string]int)
  392. for _, v := range originDataList {
  393. str := v.ClassifyName + "_" + v.ClassifyType + "_" + v.DataTime + "_" + v.DealShortName
  394. if v.DealType == 1 {
  395. buyDataMap[str] = v.DealValue
  396. } else if v.DealType == 2 {
  397. subValue := 0
  398. dealType := 0
  399. if buy, ok := buyDataMap[str]; ok {
  400. subValue = buy - v.DealValue
  401. if subValue >= 0 {
  402. dealType = 3
  403. } else {
  404. subValue = -subValue
  405. dealType = 4
  406. }
  407. }
  408. tmp := &data_manage.TradePositionSub{
  409. ClassifyName: v.ClassifyName,
  410. ClassifyType: v.ClassifyType,
  411. DataTime: v.DataTime,
  412. DealShortName: v.DealShortName,
  413. SubValue: subValue,
  414. DealType: dealType,
  415. }
  416. subDataList = append(subDataList, tmp)
  417. }
  418. }
  419. if len(subDataList) > 0 {
  420. sort.Sort(subDataList)
  421. }
  422. // 根据净多单/净空单数据, 比对上一个交易日的日期计算成交变化量, 并写入
  423. var dealType int
  424. rankMap := make(map[string]int)
  425. for _, v := range subDataList {
  426. subValue := v.SubValue
  427. nameStr := v.ClassifyName + "_" + v.ClassifyType + "_" + v.DataTime + "_" + v.DealShortName
  428. if v.DealType == 3 {
  429. subChangeMap1[nameStr] = subValue
  430. dealType = 3
  431. if _, ok := rankMap[v.ClassifyName+"_"+v.ClassifyType+"_"+v.DataTime+"_3"]; !ok {
  432. rankMap[v.ClassifyName+"_"+v.ClassifyType+"_"+v.DataTime+"_3"] = 1
  433. } else {
  434. rankMap[v.ClassifyName+"_"+v.ClassifyType+"_"+v.DataTime+"_3"]++
  435. }
  436. } else if v.DealType == 4 {
  437. subChangeMap2[nameStr] = subValue
  438. dealType = 4
  439. if _, ok := rankMap[v.ClassifyName+"_"+v.ClassifyType+"_"+v.DataTime+"_4"]; !ok {
  440. rankMap[v.ClassifyName+"_"+v.ClassifyType+"_"+v.DataTime+"_4"] = 1
  441. } else {
  442. rankMap[v.ClassifyName+"_"+v.ClassifyType+"_"+v.DataTime+"_4"]++
  443. }
  444. }
  445. // 2023-05-10 目前看该方法的引用startDate和endDate其实是同一天, 所以前一个交易日直接用上面的yesterday
  446. tmpTimeStr := yesterday
  447. //和T-1日比较差值
  448. //var tmpTimeStr string
  449. //tmpTimeStr, err = getYesterdayDate(v.DataTime)
  450. //if err != nil {
  451. // return
  452. //}
  453. yesterdayStr := v.ClassifyName + "_" + v.ClassifyType + "_" + tmpTimeStr + "_" + v.DealShortName
  454. dealChange := 0
  455. if dealType == 3 {
  456. if c, ok := subChangeMap1[yesterdayStr]; ok {
  457. dealChange = subValue - c
  458. }
  459. } else if dealType == 4 {
  460. if c, ok := subChangeMap2[yesterdayStr]; ok {
  461. dealChange = subValue - c
  462. }
  463. }
  464. tmp := &data_manage.TradePositionTop{
  465. ClassifyName: v.ClassifyName,
  466. ClassifyType: v.ClassifyType,
  467. DataTime: v.DataTime,
  468. CreateTime: now,
  469. ModifyTime: now,
  470. DealShortName: v.DealShortName,
  471. DealValue: subValue,
  472. DealChange: dealChange,
  473. DealType: dealType,
  474. Rank: rankMap[v.ClassifyName+"_"+v.ClassifyType+"_"+v.DataTime+"_"+strconv.Itoa(dealType)],
  475. }
  476. topList = append(topList, tmp)
  477. if len(topList) >= 1000 {
  478. err = data_manage.InsertMultiTradePositionTop(exchange, topList)
  479. if err != nil {
  480. return
  481. }
  482. topList = make([]*data_manage.TradePositionTop, 0)
  483. }
  484. }
  485. if len(topList) >= 0 {
  486. err = data_manage.InsertMultiTradePositionTop(exchange, topList)
  487. if err != nil {
  488. return
  489. }
  490. }
  491. return
  492. }
  493. // updateAnalysisCleanTopChangeValClassify T-1日重新生成净多单和净空单的榜单后,需要更新T日净多单和净空单榜单里的变化量
  494. func updateAnalysisCleanTopChangeValClassify(exchange, startDate, yesterday string, classifyNames, classifyTypes []string) (err error) {
  495. defer func() {
  496. if err != nil {
  497. fmt.Println("updateAnalysisCleanTopChangeVal err: " + err.Error())
  498. }
  499. }()
  500. //查询T日的净多单和净空单榜单列表
  501. //查询T-1日的净多单和净空单列表
  502. //组装数据,计算T日与T-1日的变更值
  503. //更新变更值
  504. topList := make([]*data_manage.TradePositionTop, 0) //T日和T+1日列表
  505. todayTopList := make([]*data_manage.TradePositionTop, 0) //T日列表
  506. yesterdayTopListMap := make(map[string]int) //净多单净空单持仓量map
  507. // 查询T日和T-1日的净多单和净空单列表
  508. topList, err = data_manage.GetTradePositionTopCleanByExchangeDataTimeClassify(exchange, yesterday, startDate, classifyNames, classifyTypes)
  509. if err != nil {
  510. return
  511. }
  512. if len(topList) == 0 {
  513. return
  514. }
  515. for _, v := range topList {
  516. if v.DataTime == startDate {
  517. todayTopList = append(todayTopList, v)
  518. } else if v.DataTime == yesterday {
  519. nameStr := v.ClassifyName + "_" + v.ClassifyType + "_" + v.DataTime + "_" + v.DealShortName + "_" + strconv.Itoa(v.DealType)
  520. yesterdayTopListMap[nameStr] = v.DealValue
  521. }
  522. }
  523. if len(todayTopList) == 0 {
  524. return
  525. }
  526. // 根据净多单/净空单数据, 比对上一个交易日的日期计算成交变化量, 并写入
  527. now := time.Now()
  528. updateList := make([]data_manage.UpdateChangeVal, 0)
  529. for _, v := range todayTopList {
  530. //T日值-T-1日值
  531. yesterdayStr := v.ClassifyName + "_" + v.ClassifyType + "_" + yesterday + "_" + v.DealShortName + "_" + strconv.Itoa(v.DealType)
  532. dealChange := 0
  533. if c, ok := yesterdayTopListMap[yesterdayStr]; ok {
  534. dealChange = v.DealValue - c
  535. }
  536. if dealChange != v.DealChange {
  537. tmp := data_manage.UpdateChangeVal{
  538. Id: v.Id,
  539. ModifyTime: now,
  540. DealChange: dealChange,
  541. }
  542. updateList = append(updateList, tmp)
  543. }
  544. }
  545. if len(updateList) > 0 {
  546. err = data_manage.MultiUpdatePositionTopChangeVal(exchange, updateList)
  547. if err != nil {
  548. return
  549. }
  550. }
  551. return
  552. }