trade_position_analysis.go 27 KB

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