trade_analysis.go 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. package trade_analysis
  2. import (
  3. "github.com/beego/beego/v2/client/orm"
  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:"交易所"`
  23. Sort int `description:"排序字段" `
  24. Num int `description:"品种数量"`
  25. DataTime string `description:"最新更新时间"`
  26. CurrDate string `description:"当前日期"`
  27. Items []TradeClassifyNameListItem `description:"子类"`
  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:"交易分类"`
  51. Items []TradeClassifyNameListItemItem `description:"合约代码"`
  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 `description:"分类名称下的类型"`
  65. }
  66. type TradeClassifyName struct {
  67. ClassifyName string //分类名称
  68. ClassifyType string //分类名称下的类型
  69. LatestDate string //分类下最晚日期
  70. }
  71. // GetExchangeClassify 获取交易所分类列表
  72. func GetExchangeClassify(exchange string) (list []TradeClassifyName, err error) {
  73. tableName := "base_from_trade_" + exchange + "_index"
  74. orderStr := "classify_name DESC, classify_type asc"
  75. if exchange == "zhengzhou" {
  76. orderStr = "classify_name asc"
  77. }
  78. sql := `SELECT classify_name, classify_type FROM ` + tableName + ` WHERE rank <=20 and rank > 0 GROUP BY classify_name, classify_type `
  79. sql += ` ORDER BY ` + orderStr
  80. o := orm.NewOrmUsingDB("data")
  81. _, err = o.Raw(sql).QueryRows(&list)
  82. return
  83. }
  84. type LastTimeItem struct {
  85. CreateTime time.Time
  86. }
  87. // GetExchangeLastTime 获取交易所数据最晚的时间
  88. func GetExchangeLastTime(exchange string) (item LastTimeItem, err error) {
  89. tableName := "base_from_trade_" + exchange + "_index"
  90. sql := `SELECT create_time FROM ` + tableName + ` ORDER BY create_time desc`
  91. o := orm.NewOrmUsingDB("data")
  92. err = o.Raw(sql).QueryRow(&item)
  93. return
  94. }
  95. type GetPositionTopReq struct {
  96. Exchange string `json:"exchange" form:"exchange"` //交易所
  97. ClassifyName string `json:"classify_name" form:"classify_name"` //分类名称
  98. ClassifyType string `json:"classify_type" form:"classify_type"` //具体合约名称
  99. DataTime string `json:"data_time" form:"data_time"` //请求日期,如果为空,则返回最新的榜单日期
  100. }
  101. type GetPositionTopResp struct {
  102. BuyList GetPositionTopList `description:"多单列表"`
  103. SoldList GetPositionTopList `description:"空单列表"`
  104. CleanBuyList GetPositionTopList `description:"净多单列表"`
  105. CleanSoldList GetPositionTopList `description:"净空单列表"`
  106. DataTime string `description:"最新日期或者请求日期"`
  107. LastDataTime string `description:"最新日期"`
  108. }
  109. type GetPositionTopList struct {
  110. TotalDealValue int `description:"总计成交量"`
  111. TotalDealChange int `description:"校昨日变化"`
  112. List []GetPositionTopListItem `description:"榜单详情列表"`
  113. }
  114. type GetPositionTopListItem struct {
  115. DealShortName string `description:"成交量公司简称"`
  116. DealValue int `description:"成交量"`
  117. DealChange int `description:"成交变化量"`
  118. Rank int `description:"当前名次"`
  119. Rate string `description:"当前占比"`
  120. BeforeAllRate string `description:"排在前面的成交量总计占比(包含)"`
  121. BeforeAllValue int `description:"排在前面的成交量总计"`
  122. BeforeAllChange int `description:"排在前面的成交量增减总计"`
  123. }
  124. func GetTradePositionTop(exchange string, classifyName, classifyType, dataTime string) (list []TradePositionTop, err error) {
  125. tableName := "trade_position_" + exchange + "_top"
  126. sql := `SELECT * FROM ` + tableName + ` WHERE classify_name=? and classify_type=? and data_time=? and rank <=20 and rank > 0 ORDER BY deal_value desc`
  127. o := orm.NewOrmUsingDB("data")
  128. _, err = o.Raw(sql, classifyName, classifyType, dataTime).QueryRows(&list)
  129. return
  130. }