trade_analysis_interface.go 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. package trade_analysis
  2. import (
  3. tradeAnalysisModel "eta/eta_api/models/data_manage/trade_analysis"
  4. "eta/eta_api/utils"
  5. "fmt"
  6. "strconv"
  7. "strings"
  8. )
  9. // TradeAnalysisInterface 持仓分析查询接口
  10. type TradeAnalysisInterface interface {
  11. GetTradeDataByClassifyAndCompany(exchange, classifyName string, contracts, queryCompanies []string) (items []*tradeAnalysisModel.OriginTradeData, err error) // 根据品种和公司获取原始数据
  12. GetLastTradeDataByClassify(exchange, classifyName string, contracts []string) (items []*tradeAnalysisModel.OriginTradeData, err error) // 获取品种末位数据
  13. }
  14. // BaseTradeAnalysis 通用交易所
  15. type BaseTradeAnalysis struct{}
  16. func (b *BaseTradeAnalysis) GetTradeDataByClassifyAndCompany(exchange, classifyName string, contracts, queryCompanies []string) (items []*tradeAnalysisModel.OriginTradeData, err error) {
  17. return tradeAnalysisModel.GetTradeDataByClassifyAndCompany(exchange, classifyName, contracts, queryCompanies)
  18. }
  19. func (b *BaseTradeAnalysis) GetLastTradeDataByClassify(exchange, classifyName string, contracts []string) (items []*tradeAnalysisModel.OriginTradeData, err error) {
  20. return tradeAnalysisModel.GetLastTradeDataByClassify(exchange, classifyName, contracts)
  21. }
  22. // ZhengzhouTradeAnalysis 郑商所
  23. type ZhengzhouTradeAnalysis struct{}
  24. func (z *ZhengzhouTradeAnalysis) GetTradeDataByClassifyAndCompany(exchange, classifyName string, contracts, queryCompanies []string) (items []*tradeAnalysisModel.OriginTradeData, err error) {
  25. return tradeAnalysisModel.GetTradeZhengzhouDataByClassifyAndCompany(exchange, contracts, queryCompanies)
  26. }
  27. func (z *ZhengzhouTradeAnalysis) GetLastTradeDataByClassify(exchange, classifyName string, contracts []string) (items []*tradeAnalysisModel.OriginTradeData, err error) {
  28. return tradeAnalysisModel.GetLastTradeZhengzhouDataByClassify(exchange, contracts)
  29. }
  30. // GuangzhouTradeAnalysis 广期所
  31. type GuangzhouTradeAnalysis struct{}
  32. func (g *GuangzhouTradeAnalysis) GetTradeDataByClassifyAndCompany(exchange, classifyName string, contracts, queryCompanies []string) (items []*tradeAnalysisModel.OriginTradeData, err error) {
  33. classifyIdMap := map[string]int{"si": 7, "lc": 8}
  34. classifyId := classifyIdMap[classifyName]
  35. if classifyId == 0 {
  36. err = fmt.Errorf("品种有误")
  37. return
  38. }
  39. // TOP20
  40. seatNameArr := []string{tradeAnalysisModel.GuangZhouSeatNameBuy, tradeAnalysisModel.GuangZhouSeatNameSold}
  41. if utils.InArrayByStr(queryCompanies, tradeAnalysisModel.TradeFuturesCompanyTop20) {
  42. seatNameArr = append(seatNameArr, tradeAnalysisModel.GuangZhouTopSeatNameBuy, tradeAnalysisModel.GuangZhouTopSeatNameSold)
  43. }
  44. // 查询品种下所有指标
  45. indexes, e := tradeAnalysisModel.GetBaseFromTradeGuangzhouIndexByClassifyId(classifyId)
  46. if e != nil {
  47. err = fmt.Errorf("获取广期所指标失败, %v", e)
  48. return
  49. }
  50. var indexIds []int
  51. indexInfo := make(map[int]*tradeAnalysisModel.OriginTradeData)
  52. for _, v := range indexes {
  53. // eg.永安期货_si2401_持买单量
  54. nameArr := strings.Split(v.IndexName, "_")
  55. if len(nameArr) != 3 {
  56. continue
  57. }
  58. companyName := nameArr[0]
  59. if nameArr[0] == tradeAnalysisModel.GuangZhouTopCompanyAliasName {
  60. companyName = tradeAnalysisModel.TradeFuturesCompanyTop20
  61. }
  62. if !utils.InArrayByStr(seatNameArr, nameArr[2]) {
  63. continue
  64. }
  65. if !utils.InArrayByStr(queryCompanies, companyName) {
  66. continue
  67. }
  68. if !utils.InArrayByStr(contracts, nameArr[1]) {
  69. continue
  70. }
  71. indexIds = append(indexIds, v.BaseFromTradeGuangzhouIndexId)
  72. if indexInfo[v.BaseFromTradeGuangzhouIndexId] == nil {
  73. if tradeAnalysisModel.GuangzhouSeatNameValType[nameArr[2]] == 0 {
  74. continue
  75. }
  76. indexInfo[v.BaseFromTradeGuangzhouIndexId] = new(tradeAnalysisModel.OriginTradeData)
  77. indexInfo[v.BaseFromTradeGuangzhouIndexId].CompanyName = companyName
  78. indexInfo[v.BaseFromTradeGuangzhouIndexId].ClassifyName = classifyName
  79. indexInfo[v.BaseFromTradeGuangzhouIndexId].ClassifyType = nameArr[1]
  80. indexInfo[v.BaseFromTradeGuangzhouIndexId].ValType = tradeAnalysisModel.GuangzhouSeatNameValType[nameArr[2]]
  81. }
  82. }
  83. if len(indexIds) == 0 {
  84. return
  85. }
  86. // 查询指标数据
  87. indexesData, e := tradeAnalysisModel.GetBaseFromTradeGuangzhouDataByIndexIds(indexIds)
  88. if e != nil {
  89. err = fmt.Errorf("获取广期所指标数据失败, %v", e)
  90. return
  91. }
  92. items = make([]*tradeAnalysisModel.OriginTradeData, 0)
  93. for _, v := range indexesData {
  94. info, ok := indexInfo[v.BaseFromTradeGuangzhouIndexId]
  95. if !ok {
  96. continue
  97. }
  98. items = append(items, &tradeAnalysisModel.OriginTradeData{
  99. CompanyName: info.CompanyName,
  100. Val: int(v.Value),
  101. ValChange: int(v.QtySub),
  102. DataTime: v.DataTime,
  103. ClassifyName: info.ClassifyName,
  104. ClassifyType: info.ClassifyType,
  105. ValType: info.ValType,
  106. })
  107. }
  108. return
  109. }
  110. func (g *GuangzhouTradeAnalysis) GetLastTradeDataByClassify(exchange, classifyName string, contracts []string) (items []*tradeAnalysisModel.OriginTradeData, err error) {
  111. classifyIdMap := map[string]int{"si": 7, "lc": 8}
  112. classifyId := classifyIdMap[classifyName]
  113. if classifyId == 0 {
  114. err = fmt.Errorf("品种有误")
  115. return
  116. }
  117. seatNameArr := []string{tradeAnalysisModel.GuangZhouSeatNameBuy, tradeAnalysisModel.GuangZhouSeatNameSold}
  118. // 查询品种下所有指标
  119. indexes, e := tradeAnalysisModel.GetBaseFromTradeGuangzhouIndexByClassifyId(classifyId)
  120. if e != nil {
  121. err = fmt.Errorf("获取广期所指标失败, %v", e)
  122. return
  123. }
  124. // 获取各合约下的指标
  125. contractIndexIds := make(map[string][]int)
  126. for _, v := range indexes {
  127. // eg.永安期货_si2401_持买单量
  128. nameArr := strings.Split(v.IndexName, "_")
  129. if len(nameArr) != 3 {
  130. continue
  131. }
  132. if !utils.InArrayByStr(contracts, nameArr[1]) {
  133. continue
  134. }
  135. if !utils.InArrayByStr(seatNameArr, nameArr[2]) {
  136. continue
  137. }
  138. if tradeAnalysisModel.GuangzhouSeatNameValType[nameArr[2]] == 0 {
  139. continue
  140. }
  141. k := fmt.Sprintf("%s-%d", nameArr[1], tradeAnalysisModel.GuangzhouSeatNameValType[nameArr[2]])
  142. contractIndexIds[k] = append(contractIndexIds[k], v.BaseFromTradeGuangzhouIndexId)
  143. }
  144. // ps.如果后面如果有空可以优化一下这里, 把末位数据每天写进一张表里面
  145. for k, v := range contractIndexIds {
  146. keyArr := strings.Split(k, "-")
  147. contract := keyArr[0]
  148. valType, _ := strconv.Atoi(keyArr[1])
  149. lastVales, e := tradeAnalysisModel.GetBaseFromTradeGuangzhouMinDataByIndexIds(v)
  150. if e != nil {
  151. err = fmt.Errorf("获取合约末位数据失败, %v", e)
  152. return
  153. }
  154. for _, vv := range lastVales {
  155. items = append(items, &tradeAnalysisModel.OriginTradeData{
  156. Val: int(vv.Value),
  157. DataTime: vv.DataTime,
  158. ClassifyType: contract,
  159. ValType: valType,
  160. })
  161. }
  162. }
  163. return
  164. }