trade_position_analysis.go 27 KB

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