trade_position_analysis.go 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728
  1. package data
  2. import (
  3. "context"
  4. "fmt"
  5. "hongze/hongze_task/models/data_manage"
  6. "hongze/hongze_task/services/alarm_msg"
  7. "hongze/hongze_task/utils"
  8. "sort"
  9. "strconv"
  10. "time"
  11. )
  12. // InitPositionTask 统计今日交易所的持仓分析数据
  13. func InitPositionTask(cont context.Context) (err error) {
  14. exchanges := []string{"zhengzhou", "dalian", "shanghai", "cffex", "ine"} //郑商所,大商所,上期所,中金所,上期能源
  15. for i := 1; i >= 0; i-- {
  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("InitPositionTask: 启动:" + exchange)
  22. utils.FileLog.Info("InitPositionTask: 启动:" + exchange)
  23. fmt.Println("开始" + startDate + "结束" + endDate)
  24. utils.FileLog.Info(fmt.Sprintf("InitTradePosition:开始:%s; 结束:%s", startDate, endDate))
  25. tErr, errMsg := InitTradePosition(exchange, startDate, endDate)
  26. if tErr != nil {
  27. err = tErr
  28. fmt.Println("InitTradePosition: 操作失败:" + errMsg + tErr.Error())
  29. utils.FileLog.Info(fmt.Sprintf("InitTradePosition: 操作失败:%s:%s", errMsg, tErr.Error()))
  30. continue
  31. }
  32. fmt.Println("InitTradePosition:" + exchange + "已完成")
  33. utils.FileLog.Info("InitTradePosition:" + exchange + "已完成")
  34. }
  35. }
  36. // 处理交易所的分类
  37. {
  38. allBaseFromTradeClassify, tmpErr := data_manage.GetAllBaseFromTradeClassify()
  39. if tmpErr != nil {
  40. utils.FileLog.Info(fmt.Sprintf("获取所有交易所分类失败,;err:%s", tmpErr.Error()))
  41. return
  42. }
  43. tradeClassifyMap := make(map[string]*data_manage.BaseFromTradeClassify)
  44. for _, v := range allBaseFromTradeClassify {
  45. key := fmt.Sprintf("%s_%s_%s", v.Exchange, v.ClassifyName, v.ClassifyType)
  46. tradeClassifyMap[key] = v
  47. }
  48. baseFromTradeClassifyList := make([]*data_manage.BaseFromTradeClassify, 0)
  49. for _, v := range exchanges {
  50. tradeClassifyNameList, tmpErr := data_manage.GetExchangeClassify(v)
  51. if tmpErr != nil {
  52. utils.FileLog.Info(fmt.Sprintf("获取%s分类失败,;err:%s", v, tmpErr.Error()))
  53. continue
  54. }
  55. for _, classify := range tradeClassifyNameList {
  56. key := fmt.Sprintf("%s_%s_%s", v, classify.ClassifyName, classify.ClassifyType)
  57. if tradeClassify, ok := tradeClassifyMap[key]; !ok {
  58. baseFromTradeClassifyList = append(baseFromTradeClassifyList, &data_manage.BaseFromTradeClassify{
  59. Id: 0,
  60. ClassifyName: classify.ClassifyName,
  61. ClassifyType: classify.ClassifyType,
  62. Exchange: v,
  63. LatestDate: classify.DataTime,
  64. CreateTime: time.Now(),
  65. ModifyTime: classify.ModifyTime,
  66. })
  67. } else {
  68. if tradeClassify.LatestDate.Before(classify.DataTime) {
  69. tradeClassify.LatestDate = classify.DataTime
  70. tradeClassify.ModifyTime = classify.ModifyTime
  71. tradeClassify.Update([]string{"LatestDate", "ModifyTime"})
  72. }
  73. }
  74. }
  75. }
  76. lenAddList := len(baseFromTradeClassifyList)
  77. if lenAddList > 0 {
  78. baseAddNum := 500
  79. num := lenAddList / baseAddNum
  80. lastNum := lenAddList % baseAddNum
  81. for i := 0; i <= num; i++ {
  82. tmpNum := baseAddNum
  83. if i == num && lastNum > 0 {
  84. tmpNum = lastNum
  85. }
  86. data_manage.MultiAddBaseFromTradeClassify(baseFromTradeClassifyList[i*baseAddNum : (i*baseAddNum + tmpNum)])
  87. }
  88. }
  89. }
  90. return
  91. }
  92. func InitTradePosition(exchange, startDate, endDate string) (err error, errMsg string) {
  93. defer func() {
  94. if err != nil {
  95. tips := fmt.Sprintf("统计今日交易所的持仓分析数据失败, Exchange: %s, Err: %s, Msg: %s", exchange, err.Error(), errMsg)
  96. alarm_msg.SendAlarmMsg(tips, 3)
  97. }
  98. }()
  99. // 批量插入今日的初始值
  100. num, err := data_manage.GetTradePositionTopCountByExchangeDataTime(exchange, startDate, endDate)
  101. if err != nil {
  102. errMsg = "查询原始数据失败,GetTradePositionTopCountByExchangeDataTime() Err: "
  103. return
  104. }
  105. if num > 0 {
  106. //err = fmt.Errorf("数据已存在,无需处理")
  107. //数据存在不同步的情况,有些合约会提早更新,有些合约会延迟更新
  108. //判断合约数是否一致
  109. originNum, tmpErr := data_manage.GetTradePositionOriginClassifyCountByExchangeDataTime(exchange, startDate, endDate)
  110. if tmpErr != nil {
  111. err = tmpErr
  112. errMsg = "查询原始数据分类个数失败,GetTradePositionOriginClassifyCountByExchangeDataTime() Err: "
  113. return
  114. }
  115. topNum, tmpErr := data_manage.GetTradePositionTopClassifyCountByExchangeDataTime(exchange, startDate, endDate)
  116. if tmpErr != nil {
  117. err = tmpErr
  118. errMsg = "查询榜单数据分类个数失败,GetTradePositionTopClassifyCountByExchangeDataTime() Err: "
  119. return
  120. }
  121. if originNum == topNum {
  122. //err = fmt.Errorf("数据已存在,无需处理")
  123. return
  124. }
  125. //如果合约数不一致,则删除今日数据
  126. err = data_manage.DeleteTradePositionTopAllByExchangeDataTime(exchange, startDate, endDate)
  127. if err != nil {
  128. errMsg = "删除榜单数据失败,DeleteTradePositionTopAllByExchangeDataTime() Err: "
  129. return
  130. }
  131. }
  132. err = data_manage.MultiInsertTradeBaseDataToTop(exchange, startDate, endDate)
  133. if err != nil {
  134. errMsg = "新增原始数据失败,MultiInsertTradeBaseDataToTop() Err: "
  135. return
  136. }
  137. originList, err := data_manage.GetTradePositionTopByExchangeDataTime(exchange, startDate, endDate)
  138. if err != nil {
  139. errMsg = "查询原始数据失败, GetTradePositionTopByExchangeDataTime() Err: "
  140. return
  141. }
  142. if len(originList) <= 0 {
  143. // 忽略周末
  144. w := time.Now().Weekday().String()
  145. if w == "Saturday" || w == "Sunday" {
  146. return
  147. }
  148. // 每天最后一个小时执行依旧无数据时, 才进行邮件提示
  149. if time.Now().Hour() != 23 {
  150. return
  151. }
  152. err = fmt.Errorf("原始数据没有值")
  153. return
  154. }
  155. // 原始数据日期
  156. dates, e := data_manage.GetTradePositionTopOriginDataTimes(exchange)
  157. if e != nil {
  158. err = fmt.Errorf("GetTradePositionTopOriginDataTimes err: %s", e.Error())
  159. return
  160. }
  161. now := time.Now()
  162. dataTimeMap := make(map[string]*data_manage.TradePositionTop)
  163. onlyEmptyMap := make(map[string]bool)
  164. onlyEmptyNameMap := make(map[string]*data_manage.TradePositionTop)
  165. topLastMap := make(map[string]int)
  166. topLastRankMap := make(map[string]int)
  167. list := make([]*data_manage.TradePositionTop, 0)
  168. for _, v := range originList {
  169. tmp0, tmpErr := dealTradeOriginData(dataTimeMap, onlyEmptyMap, onlyEmptyNameMap, v, topLastMap, topLastRankMap, startDate, now, dates)
  170. if tmpErr != nil {
  171. err = tmpErr
  172. errMsg = "处理原始数据失败 dealTradeOriginData() Err: "
  173. return
  174. }
  175. if tmp0 != nil {
  176. list = append(list, tmp0)
  177. }
  178. if len(list) >= 1000 {
  179. err = data_manage.InsertMultiTradePositionTop(exchange, list)
  180. if err != nil {
  181. errMsg = "批量新增昨日数据失败,InsertMultiTradePositionTop() Err: "
  182. return
  183. }
  184. list = make([]*data_manage.TradePositionTop, 0)
  185. }
  186. }
  187. if len(list) > 0 {
  188. err = data_manage.InsertMultiTradePositionTop(exchange, list)
  189. if err != nil {
  190. errMsg = "批量新增昨日数据失败,InsertMultiTradePositionTop() Err: "
  191. return
  192. }
  193. list = make([]*data_manage.TradePositionTop, 0)
  194. }
  195. // 处理某个期货公司只有买单没有卖单,或者只有卖单没有买单的情况
  196. for k, v := range onlyEmptyNameMap {
  197. _, ok1 := onlyEmptyMap[k+"_1"]
  198. _, ok2 := onlyEmptyMap[k+"_2"]
  199. var dealType int
  200. if ok1 && !ok2 {
  201. dealType = 2 //只有买单没有卖单
  202. } else if !ok1 && ok2 {
  203. dealType = 1 //只有卖单没有买单的情况
  204. } else {
  205. continue
  206. }
  207. if dealType > 0 {
  208. str := v.ClassifyName + "_" + v.ClassifyType + "_" + v.DataTime + "_" + strconv.Itoa(dealType)
  209. dealValue := 0
  210. if lastVal, ok := topLastMap[str]; ok {
  211. dealValue = int(float64(lastVal)*0.7 + 0.5)
  212. }
  213. tmp := &data_manage.TradePositionTop{
  214. ClassifyName: v.ClassifyName,
  215. ClassifyType: v.ClassifyType,
  216. DealShortName: v.DealShortName,
  217. DataTime: v.DataTime,
  218. DealValue: dealValue,
  219. CreateTime: now,
  220. ModifyTime: now,
  221. DealType: dealType,
  222. SourceType: 2,
  223. }
  224. list = append(list, tmp)
  225. if len(list) >= 1000 {
  226. err = data_manage.InsertMultiTradePositionTop(exchange, list)
  227. if err != nil {
  228. errMsg = "批量新增前日数据失败,InsertMultiTradePositionTop() Err: "
  229. return
  230. }
  231. list = make([]*data_manage.TradePositionTop, 0)
  232. }
  233. }
  234. }
  235. if len(list) > 0 {
  236. err = data_manage.InsertMultiTradePositionTop(exchange, list)
  237. if err != nil {
  238. errMsg = "批量新增前日数据失败,InsertMultiTradePositionTop() Err: "
  239. return
  240. }
  241. }
  242. //生成净多单,净空单榜单
  243. err = createAnalysisCleanTop(exchange, startDate, endDate)
  244. if err != nil {
  245. errMsg = "创建净多单,净空单数据失败,createAnalysisCleanTop() Err: "
  246. return
  247. }
  248. err = DealYesterdayData(exchange, startDate)
  249. if err != nil {
  250. errMsg = "处理昨日数据失败,DealYesterdayData() Err: "
  251. return
  252. }
  253. return
  254. }
  255. func dealTradeOriginData(dataTimeMap map[string]*data_manage.TradePositionTop, onlyEmptyMap map[string]bool, onlyEmptyNameMap map[string]*data_manage.TradePositionTop, currentItem *data_manage.TradePositionTop, topLastMap map[string]int, topLastRankMap map[string]int, startDate string, now time.Time, dates []string) (tmp0 *data_manage.TradePositionTop, err error) {
  256. classifyName := currentItem.ClassifyName
  257. classifyType := currentItem.ClassifyType
  258. dealShortName := currentItem.DealShortName
  259. dealValue := currentItem.DealValue
  260. dealChange := currentItem.DealChange
  261. dataTime := currentItem.DataTime
  262. dealType := currentItem.DealType
  263. dealTypeStr := strconv.Itoa(dealType)
  264. dataTimeMap[classifyName+"_"+classifyType+"_"+dealTypeStr+"_"+dealShortName+"_"+dataTime] = currentItem
  265. onlyEmptyMap[classifyName+"_"+classifyType+"_"+dataTime+"_"+dealShortName+"_"+dealTypeStr] = true
  266. onlyEmptyNameMap[classifyName+"_"+classifyType+"_"+dataTime+"_"+dealShortName] = currentItem
  267. if currentItem.Rank > topLastRankMap[classifyName+"_"+classifyType+"_"+dataTime+"_"+dealTypeStr] {
  268. topLastMap[classifyName+"_"+classifyType+"_"+dataTime+"_"+dealTypeStr] = dealValue
  269. topLastRankMap[classifyName+"_"+classifyType+"_"+dataTime+"_"+dealTypeStr] = currentItem.Rank
  270. }
  271. if dataTime > startDate {
  272. //tmpTimeStr, tErr := getYesterdayDate(dataTime)
  273. //if tErr != nil {
  274. // err = tErr
  275. // return
  276. //}
  277. tmpTimeStr := getPrevTradeDataDate(dataTime, dates)
  278. if tmpTimeStr < startDate {
  279. return
  280. }
  281. // 判断T-1日是否有值, 如果T-1日为空,则根据T日的值计算出T-1的值
  282. if _, ok := dataTimeMap[classifyName+"_"+classifyType+"_"+dealTypeStr+"_"+dealShortName+"_"+tmpTimeStr]; !ok {
  283. yesterdayVal := dealValue - dealChange
  284. yesterdayChange := 0
  285. //beforeYesterday, _ := getYesterdayDate(tmpTimeStr)
  286. beforeYesterday := getPrevTradeDataDate(tmpTimeStr, dates)
  287. beforeYesterdayItem, ok1 := dataTimeMap[classifyName+"_"+classifyType+"_"+dealTypeStr+"_"+dealShortName+"_"+beforeYesterday]
  288. if ok1 {
  289. yesterdayChange = yesterdayVal - beforeYesterdayItem.DealValue
  290. }
  291. tmp0 = &data_manage.TradePositionTop{
  292. ClassifyName: classifyName,
  293. ClassifyType: classifyType,
  294. DealShortName: dealShortName,
  295. DealValue: yesterdayVal,
  296. DealChange: yesterdayChange,
  297. DataTime: tmpTimeStr,
  298. CreateTime: now,
  299. ModifyTime: now,
  300. DealType: dealType,
  301. SourceType: 1,
  302. }
  303. dataTimeMap[classifyName+"_"+classifyType+"_"+dealTypeStr+"_"+dealShortName+"_"+tmpTimeStr] = tmp0
  304. onlyEmptyMap[classifyName+"_"+classifyType+"_"+tmpTimeStr+"_"+dealShortName+"_"+dealTypeStr] = true
  305. onlyEmptyNameMap[classifyName+"_"+classifyType+"_"+tmpTimeStr+"_"+dealShortName] = tmp0
  306. }
  307. }
  308. return
  309. }
  310. // DealYesterdayData 更新昨日数据
  311. func DealYesterdayData(exchange, startDate string) (err error) {
  312. // 查询最早的日期
  313. firstItem, err := data_manage.GetFirstBaseFromTradeIndexByDate(exchange)
  314. if err != nil {
  315. return
  316. }
  317. if startDate == firstItem.DataTime { //如果当前是起始日,则无需统计修改前一天的数据
  318. return
  319. }
  320. // 前一个交易日, 前两个交易日
  321. dates, e := data_manage.GetTradePositionTopOriginDataTimes(exchange)
  322. if e != nil {
  323. err = fmt.Errorf("GetTradePositionTopOriginDataTimes err: %s", e.Error())
  324. return
  325. }
  326. yesterdayStr := getPrevTradeDataDate(startDate, dates)
  327. beforeYesterdayStr := getPrevTradeDataDate(yesterdayStr, dates)
  328. //yesterdayStr, err := getYesterdayDate(startDate)
  329. //if err != nil {
  330. // return
  331. //}
  332. ////查找前日的值,并更新对应的更改
  333. //beforeYesterdayStr, err := getYesterdayDate(yesterdayStr)
  334. //if err != nil {
  335. // return
  336. //}
  337. // 先查出T日最原始的数据
  338. originList, err := data_manage.GetTradePositionTopByExchangeDataTime(exchange, startDate, startDate)
  339. if err != nil {
  340. return
  341. }
  342. originBuyMap := make(map[string]*data_manage.TradePositionTop)
  343. originSoldMap := make(map[string]*data_manage.TradePositionTop)
  344. for _, v := range originList {
  345. if v.SourceType != 0 {
  346. continue
  347. }
  348. str := v.ClassifyName + "_" + v.ClassifyType + "_" + v.DealShortName
  349. if v.DealType == 1 {
  350. originBuyMap[str] = v
  351. } else if v.DealType == 2 {
  352. originSoldMap[str] = v
  353. }
  354. }
  355. // 然后查询T-1中数据来源类型是2的数据
  356. changeList, err := data_manage.GetTradePositionTopByExchangeSourceType(exchange, yesterdayStr, 2)
  357. if err != nil {
  358. return
  359. }
  360. if len(changeList) <= 0 {
  361. //err = fmt.Errorf("前天的数据无需修改")
  362. return
  363. }
  364. // 查询出前日的成交量
  365. beforeYesterdayList, err := data_manage.GetTradePositionTopByExchangeDataTime(exchange, beforeYesterdayStr, beforeYesterdayStr)
  366. if err != nil {
  367. return
  368. }
  369. beforeYesterdayMap1 := make(map[string]int)
  370. beforeYesterdayMap2 := make(map[string]int)
  371. if len(beforeYesterdayList) > 0 {
  372. for _, v := range beforeYesterdayList {
  373. if v.SourceType == 2 {
  374. continue
  375. }
  376. str := v.ClassifyName + "_" + v.ClassifyType + "_" + v.DealShortName
  377. if v.DealType == 1 {
  378. beforeYesterdayMap1[str] = v.DealValue
  379. } else if v.DealType == 2 {
  380. beforeYesterdayMap2[str] = v.DealValue
  381. }
  382. }
  383. }
  384. // 根据原始数据中的值推算出最新的值
  385. now := time.Now()
  386. // 批量更新到分析表中,
  387. var updateAnalysisData []data_manage.UpdateDealValueChange
  388. for _, v := range changeList {
  389. str := v.ClassifyName + "_" + v.ClassifyType + "_" + v.DealShortName
  390. dealValue := 0
  391. dealChange := 0
  392. if v.DealType == 1 {
  393. if n, ok := originBuyMap[str]; ok {
  394. dealValue = n.DealValue - n.DealChange
  395. if beforeVal, ok1 := beforeYesterdayMap1[str]; ok1 {
  396. dealChange = dealValue - beforeVal
  397. }
  398. tmp := data_manage.UpdateDealValueChange{
  399. Id: v.Id,
  400. DealValue: dealValue,
  401. DealChange: dealChange,
  402. SourceType: 1,
  403. ModifyTime: now,
  404. }
  405. updateAnalysisData = append(updateAnalysisData, tmp)
  406. }
  407. } else if v.DealType == 2 {
  408. if n, ok := originSoldMap[str]; ok {
  409. dealValue = n.DealValue - n.DealChange
  410. if beforeVal, ok1 := beforeYesterdayMap2[str]; ok1 {
  411. dealChange = dealValue - beforeVal
  412. }
  413. tmp := data_manage.UpdateDealValueChange{
  414. Id: v.Id,
  415. DealValue: dealValue,
  416. DealChange: dealChange,
  417. SourceType: 1,
  418. ModifyTime: now,
  419. }
  420. updateAnalysisData = append(updateAnalysisData, tmp)
  421. }
  422. }
  423. }
  424. if len(updateAnalysisData) > 0 {
  425. err = data_manage.MultiUpdatePositionTop(exchange, updateAnalysisData)
  426. if err != nil {
  427. return
  428. }
  429. //删除T-1日净多单和净空单的榜单
  430. err = data_manage.DeletePositionTopByDataTime(exchange, yesterdayStr, 3)
  431. if err != nil {
  432. return
  433. }
  434. err = data_manage.DeletePositionTopByDataTime(exchange, yesterdayStr, 4)
  435. if err != nil {
  436. return
  437. }
  438. //重新生成净多单和净空单的榜单
  439. err = createAnalysisCleanTop(exchange, yesterdayStr, yesterdayStr)
  440. if err != nil {
  441. return
  442. }
  443. //T-1日重新生成净多单和净空单的榜单后,需要更新T日净多单和净空单榜单里的变化量
  444. err = updateAnalysisCleanTopChangeVal(exchange, startDate, yesterdayStr)
  445. if err != nil {
  446. return
  447. }
  448. }
  449. return
  450. }
  451. // createAnalysisCleanTop 生成净多单,净空单榜单
  452. func createAnalysisCleanTop(exchange, startDate, endDate string) (err error) {
  453. defer func() {
  454. if err != nil {
  455. fmt.Println("createAnalysisCleanTop err: " + err.Error())
  456. }
  457. }()
  458. topList := make([]*data_manage.TradePositionTop, 0)
  459. now := time.Now()
  460. var subDataList data_manage.TradePositionSubList
  461. subChangeMap1 := make(map[string]int) //净多单map
  462. subChangeMap2 := make(map[string]int) //净空单map
  463. // 2023-05-10 此处取前一个交易日, 不一定是昨日
  464. dates, e := data_manage.GetTradePositionTopOriginDataTimes(exchange)
  465. if e != nil {
  466. err = fmt.Errorf("GetTradePositionTopOriginDataTimes err: %s", e.Error())
  467. return
  468. }
  469. yesterday := getPrevTradeDataDate(startDate, dates)
  470. //查询所有差值数据,
  471. //yesterday, err := getYesterdayDate(startDate)
  472. //if err != nil {
  473. // return
  474. //}
  475. // 上一个交易日的净多单
  476. yesterdayTopList1, tErr := data_manage.GetTradePositionTopByExchangeDataTimeType(exchange, yesterday, 3)
  477. if tErr != nil {
  478. err = tErr
  479. return
  480. }
  481. if len(yesterdayTopList1) > 0 {
  482. for _, v := range yesterdayTopList1 {
  483. nameStr := v.ClassifyName + "_" + v.ClassifyType + "_" + v.DataTime + "_" + v.DealShortName
  484. subChangeMap1[nameStr] = v.DealValue
  485. }
  486. }
  487. // 上一个交易日的净空单
  488. yesterdayTopList2, tErr := data_manage.GetTradePositionTopByExchangeDataTimeType(exchange, yesterday, 4)
  489. if tErr != nil {
  490. err = tErr
  491. return
  492. }
  493. if len(yesterdayTopList2) > 0 {
  494. for _, v := range yesterdayTopList2 {
  495. nameStr := v.ClassifyName + "_" + v.ClassifyType + "_" + v.DataTime + "_" + v.DealShortName
  496. subChangeMap2[nameStr] = v.DealValue
  497. }
  498. }
  499. // 根据当日多单/空单数据, 生成净多单/净空单数据
  500. originDataList, err := data_manage.GetTradePositionTopByExchangeDataTime(exchange, startDate, endDate)
  501. if err != nil {
  502. return
  503. }
  504. buyDataMap := make(map[string]int)
  505. for _, v := range originDataList {
  506. str := v.ClassifyName + "_" + v.ClassifyType + "_" + v.DataTime + "_" + v.DealShortName
  507. if v.DealType == 1 {
  508. buyDataMap[str] = v.DealValue
  509. } else if v.DealType == 2 {
  510. subValue := 0
  511. dealType := 0
  512. if buy, ok := buyDataMap[str]; ok {
  513. subValue = buy - v.DealValue
  514. if subValue >= 0 {
  515. dealType = 3
  516. } else {
  517. subValue = -subValue
  518. dealType = 4
  519. }
  520. }
  521. tmp := &data_manage.TradePositionSub{
  522. ClassifyName: v.ClassifyName,
  523. ClassifyType: v.ClassifyType,
  524. DataTime: v.DataTime,
  525. DealShortName: v.DealShortName,
  526. SubValue: subValue,
  527. DealType: dealType,
  528. }
  529. subDataList = append(subDataList, tmp)
  530. }
  531. }
  532. if len(subDataList) > 0 {
  533. sort.Sort(subDataList)
  534. }
  535. // 根据净多单/净空单数据, 比对上一个交易日的日期计算成交变化量, 并写入
  536. var dealType int
  537. rankMap := make(map[string]int)
  538. for _, v := range subDataList {
  539. subValue := v.SubValue
  540. nameStr := v.ClassifyName + "_" + v.ClassifyType + "_" + v.DataTime + "_" + v.DealShortName
  541. if v.DealType == 3 {
  542. subChangeMap1[nameStr] = subValue
  543. dealType = 3
  544. if _, ok := rankMap[v.ClassifyName+"_"+v.ClassifyType+"_"+v.DataTime+"_3"]; !ok {
  545. rankMap[v.ClassifyName+"_"+v.ClassifyType+"_"+v.DataTime+"_3"] = 1
  546. } else {
  547. rankMap[v.ClassifyName+"_"+v.ClassifyType+"_"+v.DataTime+"_3"]++
  548. }
  549. } else if v.DealType == 4 {
  550. subChangeMap2[nameStr] = subValue
  551. dealType = 4
  552. if _, ok := rankMap[v.ClassifyName+"_"+v.ClassifyType+"_"+v.DataTime+"_4"]; !ok {
  553. rankMap[v.ClassifyName+"_"+v.ClassifyType+"_"+v.DataTime+"_4"] = 1
  554. } else {
  555. rankMap[v.ClassifyName+"_"+v.ClassifyType+"_"+v.DataTime+"_4"]++
  556. }
  557. }
  558. // 2023-05-10 目前看该方法的引用startDate和endDate其实是同一天, 所以前一个交易日直接用上面的yesterday
  559. tmpTimeStr := yesterday
  560. //和T-1日比较差值
  561. //var tmpTimeStr string
  562. //tmpTimeStr, err = getYesterdayDate(v.DataTime)
  563. //if err != nil {
  564. // return
  565. //}
  566. yesterdayStr := v.ClassifyName + "_" + v.ClassifyType + "_" + tmpTimeStr + "_" + v.DealShortName
  567. dealChange := 0
  568. if dealType == 3 {
  569. if c, ok := subChangeMap1[yesterdayStr]; ok {
  570. dealChange = subValue - c
  571. }
  572. } else if dealType == 4 {
  573. if c, ok := subChangeMap2[yesterdayStr]; ok {
  574. dealChange = subValue - c
  575. }
  576. }
  577. tmp := &data_manage.TradePositionTop{
  578. ClassifyName: v.ClassifyName,
  579. ClassifyType: v.ClassifyType,
  580. DataTime: v.DataTime,
  581. CreateTime: now,
  582. ModifyTime: now,
  583. DealShortName: v.DealShortName,
  584. DealValue: subValue,
  585. DealChange: dealChange,
  586. DealType: dealType,
  587. Rank: rankMap[v.ClassifyName+"_"+v.ClassifyType+"_"+v.DataTime+"_"+strconv.Itoa(dealType)],
  588. }
  589. topList = append(topList, tmp)
  590. if len(topList) >= 1000 {
  591. err = data_manage.InsertMultiTradePositionTop(exchange, topList)
  592. if err != nil {
  593. return
  594. }
  595. topList = make([]*data_manage.TradePositionTop, 0)
  596. }
  597. }
  598. if len(topList) >= 0 {
  599. err = data_manage.InsertMultiTradePositionTop(exchange, topList)
  600. if err != nil {
  601. return
  602. }
  603. }
  604. return
  605. }
  606. // getPrevTradeDataDate 获取指定日期上一个交易日日期
  607. func getPrevTradeDataDate(date string, dates []string) string {
  608. pre := -1
  609. for k, v := range dates {
  610. n := k - 1
  611. if v == date && n >= 0 {
  612. pre = n
  613. break
  614. }
  615. }
  616. // 找不到就随便给个不存在日期
  617. if pre == -1 {
  618. return "1980-01-01"
  619. }
  620. return dates[pre]
  621. }
  622. // updateAnalysisCleanTopChangeVal T-1日重新生成净多单和净空单的榜单后,需要更新T日净多单和净空单榜单里的变化量
  623. func updateAnalysisCleanTopChangeVal(exchange, startDate, yesterday string) (err error) {
  624. defer func() {
  625. if err != nil {
  626. fmt.Println("updateAnalysisCleanTopChangeVal err: " + err.Error())
  627. }
  628. }()
  629. //查询T日的净多单和净空单榜单列表
  630. //查询T-1日的净多单和净空单列表
  631. //组装数据,计算T日与T-1日的变更值
  632. //更新变更值
  633. topList := make([]*data_manage.TradePositionTop, 0) //T日和T+1日列表
  634. todayTopList := make([]*data_manage.TradePositionTop, 0) //T日列表
  635. yesterdayTopListMap := make(map[string]int) //净多单净空单持仓量map
  636. // 查询T日和T-1日的净多单和净空单列表
  637. topList, err = data_manage.GetTradePositionTopCleanByExchangeDataTime(exchange, yesterday, startDate)
  638. if err != nil {
  639. return
  640. }
  641. if len(topList) == 0 {
  642. return
  643. }
  644. for _, v := range topList {
  645. if v.DataTime == startDate {
  646. todayTopList = append(todayTopList, v)
  647. } else if v.DataTime == yesterday {
  648. nameStr := v.ClassifyName + "_" + v.ClassifyType + "_" + v.DataTime + "_" + v.DealShortName + "_" + strconv.Itoa(v.DealType)
  649. yesterdayTopListMap[nameStr] = v.DealValue
  650. }
  651. }
  652. if len(todayTopList) == 0 {
  653. return
  654. }
  655. // 根据净多单/净空单数据, 比对上一个交易日的日期计算成交变化量, 并写入
  656. now := time.Now()
  657. updateList := make([]data_manage.UpdateChangeVal, 0)
  658. for _, v := range todayTopList {
  659. //T日值-T-1日值
  660. yesterdayStr := v.ClassifyName + "_" + v.ClassifyType + "_" + yesterday + "_" + v.DealShortName + "_" + strconv.Itoa(v.DealType)
  661. dealChange := 0
  662. if c, ok := yesterdayTopListMap[yesterdayStr]; ok {
  663. dealChange = v.DealValue - c
  664. }
  665. if dealChange != v.DealChange {
  666. tmp := data_manage.UpdateChangeVal{
  667. Id: v.Id,
  668. ModifyTime: now,
  669. DealChange: dealChange,
  670. }
  671. updateList = append(updateList, tmp)
  672. }
  673. }
  674. if len(updateList) > 0 {
  675. err = data_manage.MultiUpdatePositionTopChangeVal(exchange, updateList)
  676. if err != nil {
  677. return
  678. }
  679. }
  680. return
  681. }