trade_analysis.go 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. package trade_analysis
  2. import (
  3. "hongze/hongze_yb/global"
  4. "time"
  5. )
  6. // 上期能源持仓榜单表
  7. type TradePositionTop struct {
  8. Id uint64 `gorm:"primaryKey;column:id" json:"id"`
  9. ClassifyName string `gorm:"column:classify_name" json:"classify_name"` //分类名称
  10. ClassifyType string `gorm:"column:classify_type" json:"classify_type"` //分类名称下的类型
  11. DealShortName string `gorm:"column:deal_short_name" json:"deal_short_name"` //成交量公司简称
  12. DealValue int `gorm:"column:deal_value" json:"deal_value"` //成交量
  13. DealChange int `gorm:"column:deal_change" json:"deal_change"` //成交变化量
  14. DataTime time.Time `gorm:"column:data_time" json:"data_time"` //数据日期
  15. CreateTime time.Time `gorm:"column:create_time" json:"create_time"` //插入时间
  16. ModifyTime time.Time `gorm:"column:modify_time" json:"modify_time"` //修改时间
  17. DealType int `gorm:"column:deal_type" json:"deal_type"` //交易类型:1多单,2空单,3净多单,4净空单
  18. SourceType int `gorm:"column:source_type" json:"source_type"` //数据来源,0是原始数据的值,1是由T+1日推算出的值,2是由T日的榜单数据推算出的值
  19. Rank int `gorm:"column:rank" json:"rank"` //排名
  20. }
  21. type TradeClassifyNameList struct {
  22. Exchange string `description:"交易所" json:"exchange"`
  23. Sort int `description:"排序字段" json:"sort"`
  24. Num int `description:"品种数量" json:"num"`
  25. DataTime string `description:"最新更新时间" json:"data_time"`
  26. CurrDate string `description:"当前日期" json:"curr_date"`
  27. Items []TradeClassifyNameListItem `description:"子类" json:"items"`
  28. }
  29. type TradeClassifyNameListSort []TradeClassifyNameList
  30. func (v TradeClassifyNameListSort) Len() int {
  31. return len(v)
  32. }
  33. func (v TradeClassifyNameListSort) Swap(i, j int) {
  34. v[i], v[j] = v[j], v[i]
  35. }
  36. func (v TradeClassifyNameListSort) Less(i, j int) bool {
  37. return v[i].Sort < v[j].Sort
  38. }
  39. type TradeClassifyNameListItemSort []TradeClassifyNameListItem
  40. func (v TradeClassifyNameListItemSort) Len() int {
  41. return len(v)
  42. }
  43. func (v TradeClassifyNameListItemSort) Swap(i, j int) {
  44. v[i], v[j] = v[j], v[i]
  45. }
  46. func (v TradeClassifyNameListItemSort) Less(i, j int) bool {
  47. return v[i].ClassifyName < v[j].ClassifyName
  48. }
  49. type TradeClassifyNameListItem struct {
  50. ClassifyName string `description:"交易分类" json:"classify_name"`
  51. Items []TradeClassifyNameListItemItem `description:"合约代码" json:"items"`
  52. }
  53. type TradeClassifyNameListItemItemSort []TradeClassifyNameListItemItem
  54. func (v TradeClassifyNameListItemItemSort) Len() int {
  55. return len(v)
  56. }
  57. func (v TradeClassifyNameListItemItemSort) Swap(i, j int) {
  58. v[i], v[j] = v[j], v[i]
  59. }
  60. func (v TradeClassifyNameListItemItemSort) Less(i, j int) bool {
  61. return v[i].ClassifyType < v[j].ClassifyType
  62. }
  63. type TradeClassifyNameListItemItem struct {
  64. ClassifyType string `json:"classify_type"` //分类名称下的类型
  65. }
  66. type TradeClassifyName struct {
  67. ClassifyName string //分类名称
  68. ClassifyType string //分类名称下的类型
  69. LatestDate string //分类下最晚日期
  70. }
  71. func GetExchangeClassify(exchange string) (list []TradeClassifyName, err error) {
  72. tableName := "base_from_trade_" + exchange + "_index"
  73. orderStr := "classify_name DESC, classify_type asc"
  74. if exchange == "zhengzhou" {
  75. orderStr = "classify_name asc"
  76. }
  77. err = global.MYSQL["data"].
  78. Table(tableName).
  79. Select("classify_name, classify_type").
  80. Group("classify_name, classify_type").
  81. Where("rank <=20 and rank > 0 ").
  82. Order(orderStr).Scan(&list).Error
  83. return
  84. }
  85. type LastTimeItem struct {
  86. CreateTime time.Time
  87. }
  88. func GetExchangeLastTime(exchange string) (item LastTimeItem, err error) {
  89. tableName := "base_from_trade_" + exchange + "_index"
  90. err = global.MYSQL["data"].
  91. Table(tableName).
  92. Select("create_time").
  93. Order("create_time desc").First(&item).Error
  94. return
  95. }
  96. type GetPositionTopReq struct {
  97. Exchange string `json:"exchange" form:"exchange"` //交易所
  98. ClassifyName string `json:"classify_name" form:"classify_name"` //分类名称
  99. ClassifyType string `json:"classify_type" form:"classify_type"` //具体合约名称
  100. DataTime string `json:"data_time" form:"data_time"` //请求日期,如果为空,则返回最新的榜单日期
  101. }
  102. type GetPositionTopResp struct {
  103. BuyList GetPositionTopList `json:"buy_list"` //多单列表
  104. SoldList GetPositionTopList `json:"sold_list"` //空单列表
  105. CleanBuyList GetPositionTopList `json:"clean_buy_list"` //净多单列表
  106. CleanSoldList GetPositionTopList `json:"clean_sold_list"` //净空单列表
  107. DataTime string `json:"data_time"` //最新日期或者请求日期
  108. LastDataTime string `json:"last_data_time" description:"最新日期"`
  109. }
  110. type GetPositionTopList struct {
  111. TotalDealValue int `json:"total_deal_value"` //总计成交量
  112. TotalDealChange int `json:"total_deal_change"` //校昨日变化
  113. List []GetPositionTopListItem `json:"list"` //榜单详情列表
  114. }
  115. type GetPositionTopListItem struct {
  116. DealShortName string `json:"deal_short_name"` //成交量公司简称
  117. DealValue int `json:"deal_value"` //成交量
  118. DealChange int `json:"deal_change"` //成交变化量
  119. Rank int `json:"rank"` //当前名次
  120. Rate string `json:"rate"` //当前占比
  121. BeforeAllRate string `json:"before_all_rate"` //排在前面的成交量总计占比(包含)
  122. BeforeAllValue int `json:"before_all_value"` //排在前面的成交量总计
  123. BeforeAllChange int `json:"before_all_change"` //排在前面的成交量增减总计
  124. }
  125. func GetTradePositionTop(exchange string, classifyName, classifyType, dataTime string) (list []TradePositionTop, err error) {
  126. tableName := "trade_position_" + exchange + "_top"
  127. err = global.MYSQL["data"].
  128. Table(tableName).
  129. Where("classify_name=? and classify_type=? and data_time=? and rank <=20 and rank > 0", classifyName, classifyType, dataTime).
  130. Order("deal_value desc").Scan(&list).Error
  131. return
  132. }
  133. // BaseFromTradeClassify 交易所分类表
  134. type BaseFromTradeClassify struct {
  135. Id uint64 `orm:"column(id);pk"`
  136. ClassifyName string //分类名称
  137. ClassifyType string //分类名称下的类型
  138. Exchange string //交易所
  139. LatestDate time.Time //数据最近的日期
  140. CreateTime time.Time //插入时间
  141. ModifyTime time.Time //修改时间
  142. }
  143. // GetAllBaseFromTradeClassify 获取所有的交易所分类列表
  144. func GetAllBaseFromTradeClassify() (list []*BaseFromTradeClassify, err error) {
  145. err = global.MYSQL["data"].Table("base_from_trade_classify").Find(&list).Error
  146. return
  147. }
  148. func GetTradeTopLastDataTime(exchange string, classifyName, classifyType string) (item *BaseFromTradeClassify, err error) {
  149. tableName := "base_from_trade_classify"
  150. if exchange == "zhengzhou" {
  151. err = global.MYSQL["data"].
  152. Table(tableName).
  153. Where("exchange = ? AND classify_name=? ", exchange, classifyType).
  154. Order("latest_date desc").First(&item).Error
  155. } else {
  156. err = global.MYSQL["data"].
  157. Table(tableName).
  158. Where("exchange = ? AND classify_name=? and classify_type=?", exchange, classifyName, classifyType).
  159. Order("latest_date desc").First(&item).Error
  160. }
  161. return
  162. }
  163. func GetZhengzhouClassifyTypeByClassifyName(classifyName string) (item *TradeClassifyName, err error) {
  164. tableName := "base_from_trade_zhengzhou_index"
  165. err = global.MYSQL["data"].
  166. Table(tableName).
  167. Select("classify_name, classify_type").
  168. Where("classify_name=?", classifyName).Limit(1).Find(&item).Error
  169. return
  170. }