trade_position_analysis.go 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522
  1. package data
  2. import (
  3. "fmt"
  4. "eta/eta_api/models/data_manage"
  5. "eta/eta_api/utils"
  6. "sort"
  7. "strconv"
  8. "time"
  9. )
  10. func InitPositionTask() (err error) {
  11. exchanges := []string{"zhengzhou", "dalian", "shanghai", "cffex", "ine"} //郑商所,大商所,上期所,中金所,上期能源
  12. startDate := "2023-02-14"
  13. endDate := time.Now().Format(utils.FormatDate)
  14. for _, v := range exchanges {
  15. endDateTmpStr := endDate
  16. exchange := v
  17. fmt.Println("InitPositionTask: 启动:" + exchange)
  18. utils.FileLog.Info("InitPositionTask: 启动:" + exchange)
  19. startDateTmpTime, _ := time.ParseInLocation(utils.FormatDate, startDate, time.Local)
  20. startDateStr := startDateTmpTime.Format(utils.FormatDate)
  21. for {
  22. if startDateStr > endDate {
  23. break
  24. }
  25. endDateTmpTime := startDateTmpTime.AddDate(0, 1, 0)
  26. endDateTmpStr = endDateTmpTime.Format(utils.FormatDate)
  27. if endDateTmpStr > endDate {
  28. endDateTmpStr = endDate
  29. }
  30. fmt.Println("开始" + startDateStr + "结束" + endDateTmpStr)
  31. utils.FileLog.Info(fmt.Sprintf("InitTradePosition:开始:%s; 结束:%s", startDateStr, endDateTmpStr))
  32. //err :=data.DealYesterdayData(exchange, startDateStr)
  33. tErr, errMsg := InitTradePosition(exchange, startDateStr, endDateTmpStr)
  34. if tErr != nil {
  35. err = tErr
  36. fmt.Println("InitTradePosition: 操作失败:" + errMsg + tErr.Error())
  37. utils.FileLog.Info(fmt.Sprintf("InitTradePosition: 操作失败:%s:%s", errMsg, tErr.Error()))
  38. return
  39. }
  40. startDateTmpTime = endDateTmpTime.AddDate(0, 0, 1)
  41. startDateStr = startDateTmpTime.Format(utils.FormatDate)
  42. }
  43. fmt.Println("InitTradePosition:" + exchange + "已完成")
  44. utils.FileLog.Info("InitTradePosition:" + exchange + "已完成")
  45. }
  46. return
  47. }
  48. func InitTradePosition(exchange, startDate, endDate string) (err error, errMsg string) {
  49. // 批量插入今日的初始值
  50. count, err := data_manage.GetTradePositionTopCountByExchangeDataTime(exchange, startDate, endDate)
  51. if err != nil {
  52. errMsg = "查询原始数据失败,GetTradePositionTopCountByExchangeDataTime() Err: "
  53. return
  54. }
  55. if count > 0 {
  56. err = fmt.Errorf("数据已存在,无需处理")
  57. return
  58. }
  59. err = data_manage.MultiInsertTradeBaseDataToTop(exchange, startDate, endDate)
  60. if err != nil {
  61. errMsg = "新增原始数据失败,MultiInsertTradeBaseDataToTop() Err: "
  62. return
  63. }
  64. originList, err := data_manage.GetTradePositionTopByExchangeDataTime(exchange, startDate, endDate)
  65. if err != nil {
  66. errMsg = "查询原始数据失败, GetTradePositionTopByExchangeDataTime() Err: "
  67. return
  68. }
  69. if len(originList) <= 0 {
  70. err = fmt.Errorf("原始数据没有值")
  71. return
  72. }
  73. now := time.Now()
  74. dataTimeMap := make(map[string]*data_manage.TradePositionTop)
  75. onlyEmptyMap := make(map[string]bool)
  76. onlyEmptyNameMap := make(map[string]*data_manage.TradePositionTop)
  77. topLastMap := make(map[string]int)
  78. topLastRankMap := make(map[string]int)
  79. list := make([]*data_manage.TradePositionTop, 0)
  80. for _, v := range originList {
  81. tmp0, tmpErr := dealTradeOriginData(dataTimeMap, onlyEmptyMap, onlyEmptyNameMap, v, topLastMap, topLastRankMap, startDate, now)
  82. if tmpErr != nil {
  83. err = tmpErr
  84. errMsg = "处理原始数据失败 dealTradeOriginData() Err: "
  85. return
  86. }
  87. if tmp0 != nil {
  88. list = append(list, tmp0)
  89. }
  90. if len(list) >= 1000 {
  91. err = data_manage.InsertMultiTradePositionTop(exchange, list)
  92. if err != nil {
  93. errMsg = "批量新增昨日数据失败,InsertMultiTradePositionTop() Err: "
  94. return
  95. }
  96. list = make([]*data_manage.TradePositionTop, 0)
  97. }
  98. }
  99. if len(list) > 0 {
  100. err = data_manage.InsertMultiTradePositionTop(exchange, list)
  101. if err != nil {
  102. errMsg = "批量新增昨日数据失败,InsertMultiTradePositionTop() Err: "
  103. return
  104. }
  105. list = make([]*data_manage.TradePositionTop, 0)
  106. }
  107. // 处理某个期货公司只有买单没有卖单,或者只有卖单没有买单的情况
  108. for k, v := range onlyEmptyNameMap {
  109. _, ok1 := onlyEmptyMap[k+"_1"]
  110. _, ok2 := onlyEmptyMap[k+"_2"]
  111. var dealType int
  112. if ok1 && !ok2 {
  113. dealType = 2 //只有买单没有卖单
  114. } else if !ok1 && ok2 {
  115. dealType = 1 //只有卖单没有买单的情况
  116. } else {
  117. continue
  118. }
  119. if dealType > 0 {
  120. str := v.ClassifyName + "_" + v.ClassifyType + "_" + v.DataTime + "_" + strconv.Itoa(dealType)
  121. dealValue := 0
  122. if lastVal, ok := topLastMap[str]; ok {
  123. dealValue = int(float64(lastVal)*0.7 + 0.5)
  124. }
  125. tmp := &data_manage.TradePositionTop{
  126. ClassifyName: v.ClassifyName,
  127. ClassifyType: v.ClassifyType,
  128. DealShortName: v.DealShortName,
  129. DataTime: v.DataTime,
  130. DealValue: dealValue,
  131. CreateTime: now,
  132. ModifyTime: now,
  133. DealType: dealType,
  134. SourceType: 2,
  135. }
  136. list = append(list, tmp)
  137. if len(list) >= 1000 {
  138. err = data_manage.InsertMultiTradePositionTop(exchange, list)
  139. if err != nil {
  140. errMsg = "批量新增前日数据失败,InsertMultiTradePositionTop() Err: "
  141. return
  142. }
  143. list = make([]*data_manage.TradePositionTop, 0)
  144. }
  145. }
  146. }
  147. if len(list) > 0 {
  148. err = data_manage.InsertMultiTradePositionTop(exchange, list)
  149. if err != nil {
  150. errMsg = "批量新增前日数据失败,InsertMultiTradePositionTop() Err: "
  151. return
  152. }
  153. }
  154. //生成净多单,净空单榜单
  155. err = createAnalysisCleanTop(exchange, startDate, endDate)
  156. if err != nil {
  157. errMsg = "创建净多单,净空单数据失败,createAnalysisCleanTop() Err: "
  158. return
  159. }
  160. // 特殊处理起始日期前一天的数据
  161. err = DealYesterdayData(exchange, startDate)
  162. if err != nil {
  163. errMsg = "处理昨日数据失败,DealYesterdayData() Err: "
  164. return
  165. }
  166. return
  167. }
  168. 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) (tmp0 *data_manage.TradePositionTop, err error) {
  169. classifyName := currentItem.ClassifyName
  170. classifyType := currentItem.ClassifyType
  171. dealShortName := currentItem.DealShortName
  172. dealValue := currentItem.DealValue
  173. dealChange := currentItem.DealChange
  174. dataTime := currentItem.DataTime
  175. dealType := currentItem.DealType
  176. dealTypeStr := strconv.Itoa(dealType)
  177. dataTimeMap[classifyName+"_"+classifyType+"_"+dealTypeStr+"_"+dealShortName+"_"+dataTime] = currentItem
  178. onlyEmptyMap[classifyName+"_"+classifyType+"_"+dataTime+"_"+dealShortName+"_"+dealTypeStr] = true
  179. onlyEmptyNameMap[classifyName+"_"+classifyType+"_"+dataTime+"_"+dealShortName] = currentItem
  180. if currentItem.Rank > topLastRankMap[classifyName+"_"+classifyType+"_"+dataTime+"_"+dealTypeStr] {
  181. topLastMap[classifyName+"_"+classifyType+"_"+dataTime+"_"+dealTypeStr] = dealValue
  182. topLastRankMap[classifyName+"_"+classifyType+"_"+dataTime+"_"+dealTypeStr] = currentItem.Rank
  183. }
  184. if dataTime > startDate {
  185. tmpTimeStr, tErr := getYesterdayDate(dataTime)
  186. if tErr != nil {
  187. err = tErr
  188. return
  189. }
  190. if tmpTimeStr < startDate {
  191. return
  192. }
  193. // 判断T-1日是否有值, 如果T-1日为空,则根据T日的值计算出T-1的值
  194. if _, ok := dataTimeMap[classifyName+"_"+classifyType+"_"+dealTypeStr+"_"+dealShortName+"_"+tmpTimeStr]; !ok {
  195. yesterdayVal := dealValue - dealChange
  196. yesterdayChange := 0
  197. beforeYesterday, _ := getYesterdayDate(tmpTimeStr)
  198. beforeYesterdayItem, ok1 := dataTimeMap[classifyName+"_"+classifyType+"_"+dealTypeStr+"_"+dealShortName+"_"+beforeYesterday]
  199. if ok1 {
  200. yesterdayChange = yesterdayVal - beforeYesterdayItem.DealValue
  201. }
  202. tmp0 = &data_manage.TradePositionTop{
  203. ClassifyName: classifyName,
  204. ClassifyType: classifyType,
  205. DealShortName: dealShortName,
  206. DealValue: yesterdayVal,
  207. DealChange: yesterdayChange,
  208. DataTime: tmpTimeStr,
  209. CreateTime: now,
  210. ModifyTime: now,
  211. DealType: dealType,
  212. SourceType: 1,
  213. }
  214. dataTimeMap[classifyName+"_"+classifyType+"_"+dealTypeStr+"_"+dealShortName+"_"+tmpTimeStr] = tmp0
  215. onlyEmptyMap[classifyName+"_"+classifyType+"_"+tmpTimeStr+"_"+dealShortName+"_"+dealTypeStr] = true
  216. onlyEmptyNameMap[classifyName+"_"+classifyType+"_"+tmpTimeStr+"_"+dealShortName] = tmp0
  217. }
  218. }
  219. return
  220. }
  221. // 更新昨日数据
  222. func DealYesterdayData(exchange, startDate string) (err error) {
  223. // 查询最早的日期
  224. firstItem, err := data_manage.GetFirstBaseFromTradeIndexByDate(exchange)
  225. if err != nil {
  226. return
  227. }
  228. if startDate == firstItem.DataTime { //如果当前是起始日,则无需统计修改前一天的数据
  229. return
  230. }
  231. yesterdayStr, err := getYesterdayDate(startDate)
  232. if err != nil {
  233. return
  234. }
  235. //查找前日的值,并更新对应的更改
  236. beforeYesterdayStr, err := getYesterdayDate(yesterdayStr)
  237. if err != nil {
  238. return
  239. }
  240. // 先查出T日最原始的数据
  241. originList, err := data_manage.GetTradePositionTopByExchangeDataTime(exchange, startDate, startDate)
  242. if err != nil {
  243. return
  244. }
  245. originBuyMap := make(map[string]*data_manage.TradePositionTop)
  246. originSoldMap := make(map[string]*data_manage.TradePositionTop)
  247. for _, v := range originList {
  248. if v.SourceType != 0 {
  249. continue
  250. }
  251. str := v.ClassifyName + "_" + v.ClassifyType + "_" + v.DealShortName
  252. if v.DealType == 1 {
  253. originBuyMap[str] = v
  254. } else if v.DealType == 2 {
  255. originSoldMap[str] = v
  256. }
  257. }
  258. // 然后查询T-1中数据来源类型是2的数据
  259. changeList, err := data_manage.GetTradePositionTopByExchangeSourceType(exchange, yesterdayStr, 2)
  260. if err != nil {
  261. return
  262. }
  263. if len(changeList) <= 0 {
  264. //err = fmt.Errorf("前天的数据无需修改")
  265. return
  266. }
  267. // 查询出前日的成交量
  268. beforeYesterdayList, err := data_manage.GetTradePositionTopByExchangeDataTime(exchange, beforeYesterdayStr, beforeYesterdayStr)
  269. if err != nil {
  270. return
  271. }
  272. beforeYesterdayMap1 := make(map[string]int)
  273. beforeYesterdayMap2 := make(map[string]int)
  274. if len(beforeYesterdayList) > 0 {
  275. for _, v := range beforeYesterdayList {
  276. if v.SourceType == 2 {
  277. continue
  278. }
  279. str := v.ClassifyName + "_" + v.ClassifyType + "_" + v.DealShortName
  280. if v.DealType == 1 {
  281. beforeYesterdayMap1[str] = v.DealValue
  282. } else if v.DealType == 2 {
  283. beforeYesterdayMap2[str] = v.DealValue
  284. }
  285. }
  286. }
  287. // 根据原始数据中的值推算出最新的值
  288. now := time.Now()
  289. // 批量更新到分析表中,
  290. var updateAnalysisData []data_manage.UpdateDealValueChange
  291. for _, v := range changeList {
  292. str := v.ClassifyName + "_" + v.ClassifyType + "_" + v.DealShortName
  293. dealValue := 0
  294. dealChange := 0
  295. if v.DealType == 1 {
  296. if n, ok := originBuyMap[str]; ok {
  297. dealValue = n.DealValue - n.DealChange
  298. if beforeVal, ok1 := beforeYesterdayMap1[str]; ok1 {
  299. dealChange = dealValue - beforeVal
  300. }
  301. tmp := data_manage.UpdateDealValueChange{
  302. Id: v.Id,
  303. DealValue: dealValue,
  304. DealChange: dealChange,
  305. SourceType: 1,
  306. ModifyTime: now,
  307. }
  308. updateAnalysisData = append(updateAnalysisData, tmp)
  309. }
  310. } else if v.DealType == 2 {
  311. if n, ok := originSoldMap[str]; ok {
  312. dealValue = n.DealValue - n.DealChange
  313. if beforeVal, ok1 := beforeYesterdayMap2[str]; ok1 {
  314. dealChange = dealValue - beforeVal
  315. }
  316. tmp := data_manage.UpdateDealValueChange{
  317. Id: v.Id,
  318. DealValue: dealValue,
  319. DealChange: dealChange,
  320. SourceType: 1,
  321. ModifyTime: now,
  322. }
  323. updateAnalysisData = append(updateAnalysisData, tmp)
  324. }
  325. }
  326. }
  327. if len(updateAnalysisData) > 0 {
  328. err = data_manage.MultiUpdatePositionTop(exchange, updateAnalysisData)
  329. if err != nil {
  330. return
  331. }
  332. //删除T-1日净多单和净空单的榜单
  333. err = data_manage.DeletePositionTopByDataTime(exchange, yesterdayStr, 3)
  334. if err != nil {
  335. return
  336. }
  337. err = data_manage.DeletePositionTopByDataTime(exchange, yesterdayStr, 4)
  338. if err != nil {
  339. return
  340. }
  341. //重新生成净多单和净空单的榜单
  342. err = createAnalysisCleanTop(exchange, yesterdayStr, yesterdayStr)
  343. if err != nil {
  344. return
  345. }
  346. }
  347. return
  348. }
  349. // createAnalysisCleanTop 生成净多单,净空单榜单
  350. func createAnalysisCleanTop(exchange, startDate, endDate string) (err error) {
  351. topList := make([]*data_manage.TradePositionTop, 0)
  352. now := time.Now()
  353. var subDataList data_manage.TradePositionSubList
  354. subChangeMap1 := make(map[string]int) //净多单map
  355. subChangeMap2 := make(map[string]int) //净空单map
  356. //查询所有差值数据,
  357. yesterday, err := getYesterdayDate(startDate)
  358. if err != nil {
  359. return
  360. }
  361. yesterdayTopList1, tErr := data_manage.GetTradePositionTopByExchangeDataTimeType(exchange, yesterday, 3)
  362. if tErr != nil {
  363. err = tErr
  364. return
  365. }
  366. if len(yesterdayTopList1) > 0 {
  367. for _, v := range yesterdayTopList1 {
  368. nameStr := v.ClassifyName + "_" + v.ClassifyType + "_" + v.DataTime + "_" + v.DealShortName
  369. subChangeMap1[nameStr] = v.DealValue
  370. }
  371. }
  372. yesterdayTopList2, tErr := data_manage.GetTradePositionTopByExchangeDataTimeType(exchange, yesterday, 4)
  373. if tErr != nil {
  374. err = tErr
  375. return
  376. }
  377. if len(yesterdayTopList2) > 0 {
  378. for _, v := range yesterdayTopList2 {
  379. nameStr := v.ClassifyName + "_" + v.ClassifyType + "_" + v.DataTime + "_" + v.DealShortName
  380. subChangeMap2[nameStr] = v.DealValue
  381. }
  382. }
  383. originDataList, err := data_manage.GetTradePositionTopByExchangeDataTime(exchange, startDate, endDate)
  384. if err != nil {
  385. return
  386. }
  387. buyDataMap := make(map[string]int)
  388. for _, v := range originDataList {
  389. str := v.ClassifyName + "_" + v.ClassifyType + "_" + v.DataTime + "_" + v.DealShortName
  390. if v.DealType == 1 {
  391. buyDataMap[str] = v.DealValue
  392. } else if v.DealType == 2 {
  393. subValue := 0
  394. dealType := 0
  395. if buy, ok := buyDataMap[str]; ok {
  396. subValue = buy - v.DealValue
  397. if subValue >= 0 {
  398. dealType = 3
  399. } else {
  400. subValue = -subValue
  401. dealType = 4
  402. }
  403. }
  404. tmp := &data_manage.TradePositionSub{
  405. ClassifyName: v.ClassifyName,
  406. ClassifyType: v.ClassifyType,
  407. DataTime: v.DataTime,
  408. DealShortName: v.DealShortName,
  409. SubValue: subValue,
  410. DealType: dealType,
  411. }
  412. subDataList = append(subDataList, tmp)
  413. }
  414. }
  415. if len(subDataList) > 0 {
  416. sort.Sort(subDataList)
  417. }
  418. var dealType int
  419. rankMap := make(map[string]int)
  420. for _, v := range subDataList {
  421. subValue := v.SubValue
  422. nameStr := v.ClassifyName + "_" + v.ClassifyType + "_" + v.DataTime + "_" + v.DealShortName
  423. if v.DealType == 3 {
  424. subChangeMap1[nameStr] = subValue
  425. dealType = 3
  426. if _, ok := rankMap[v.ClassifyName+"_"+v.ClassifyType+"_"+v.DataTime+"_3"]; !ok {
  427. rankMap[v.ClassifyName+"_"+v.ClassifyType+"_"+v.DataTime+"_3"] = 1
  428. } else {
  429. rankMap[v.ClassifyName+"_"+v.ClassifyType+"_"+v.DataTime+"_3"]++
  430. }
  431. } else if v.DealType == 4 {
  432. subChangeMap2[nameStr] = subValue
  433. dealType = 4
  434. if _, ok := rankMap[v.ClassifyName+"_"+v.ClassifyType+"_"+v.DataTime+"_4"]; !ok {
  435. rankMap[v.ClassifyName+"_"+v.ClassifyType+"_"+v.DataTime+"_4"] = 1
  436. } else {
  437. rankMap[v.ClassifyName+"_"+v.ClassifyType+"_"+v.DataTime+"_4"]++
  438. }
  439. }
  440. //和T-1日比较差值
  441. var tmpTimeStr string
  442. tmpTimeStr, err = getYesterdayDate(v.DataTime)
  443. if err != nil {
  444. return
  445. }
  446. yesterdayStr := v.ClassifyName + "_" + v.ClassifyType + "_" + tmpTimeStr + "_" + v.DealShortName
  447. dealChange := 0
  448. if dealType == 3 {
  449. if c, ok := subChangeMap1[yesterdayStr]; ok {
  450. dealChange = subValue - c
  451. }
  452. } else if dealType == 4 {
  453. if c, ok := subChangeMap2[yesterdayStr]; ok {
  454. dealChange = subValue - c
  455. }
  456. }
  457. tmp := &data_manage.TradePositionTop{
  458. ClassifyName: v.ClassifyName,
  459. ClassifyType: v.ClassifyType,
  460. DataTime: v.DataTime,
  461. CreateTime: now,
  462. ModifyTime: now,
  463. DealShortName: v.DealShortName,
  464. DealValue: subValue,
  465. DealChange: dealChange,
  466. DealType: dealType,
  467. Rank: rankMap[v.ClassifyName+"_"+v.ClassifyType+"_"+v.DataTime+"_"+strconv.Itoa(dealType)],
  468. }
  469. topList = append(topList, tmp)
  470. if len(topList) >= 1000 {
  471. err = data_manage.InsertMultiTradePositionTop(exchange, topList)
  472. if err != nil {
  473. return
  474. }
  475. topList = make([]*data_manage.TradePositionTop, 0)
  476. }
  477. }
  478. if len(topList) >= 0 {
  479. err = data_manage.InsertMultiTradePositionTop(exchange, topList)
  480. if err != nil {
  481. return
  482. }
  483. }
  484. return
  485. }
  486. func getYesterdayDate(today string) (yesterday string, err error) {
  487. i := 1
  488. tmpTime, err := time.ParseInLocation(utils.FormatDate, today, time.Local)
  489. if err != nil {
  490. return
  491. }
  492. tmpTimeDate := tmpTime.AddDate(0, 0, -i)
  493. weekStr := tmpTimeDate.Weekday().String()
  494. if weekStr == "Sunday" {
  495. i += 2
  496. } else if weekStr == "Saturday" {
  497. i += 1
  498. }
  499. tmpTimeDate = tmpTime.AddDate(0, 0, -i)
  500. yesterday = tmpTimeDate.Format(utils.FormatDate)
  501. return
  502. }