trade_position_analysis.go 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. package data_manage
  2. import (
  3. "eta/eta_api/utils"
  4. "github.com/beego/beego/v2/client/orm"
  5. "time"
  6. "unsafe"
  7. )
  8. // 持仓榜单表
  9. type TradePositionTop struct {
  10. Id uint64 `orm:"column(id);pk"`
  11. ClassifyName string //分类名称
  12. ClassifyType string //分类名称下的类型
  13. DataTime string //数据日期
  14. CreateTime time.Time //插入时间
  15. ModifyTime time.Time //修改时间
  16. DealShortName string //成交量公司简称
  17. DealValue int //成交量
  18. DealChange int //成交变化量
  19. DealType int //交易类型:1多单,2空单,3净多单,4净空单
  20. SourceType int //数据来源,0是原始数据的值,1是由T+1日推算出的值,2是由T日的榜单数据推算出的值
  21. Rank int //排名
  22. }
  23. type TradePositionDalianTop struct {
  24. TradePositionTop
  25. }
  26. type TradePositionZhengzhouTop struct {
  27. TradePositionTop
  28. }
  29. type TradePositionCffexTop struct {
  30. TradePositionTop
  31. }
  32. type TradePositionShanghaiTop struct {
  33. TradePositionTop
  34. }
  35. type TradePositionIneTop struct {
  36. TradePositionTop
  37. }
  38. func InsertMultiTradePositionTop(exchange string, items []*TradePositionTop) (err error) {
  39. o := orm.NewOrmUsingDB("data")
  40. if exchange == "dalian" {
  41. list := make([]*TradePositionDalianTop, 0)
  42. for _, v := range items {
  43. tmp := (*TradePositionDalianTop)(unsafe.Pointer(v))
  44. list = append(list, tmp)
  45. }
  46. _, err = o.InsertMulti(len(list), list)
  47. return
  48. } else if exchange == "zhengzhou" {
  49. list := make([]*TradePositionZhengzhouTop, 0)
  50. for _, v := range items {
  51. tmp := (*TradePositionZhengzhouTop)(unsafe.Pointer(v))
  52. list = append(list, tmp)
  53. }
  54. _, err = o.InsertMulti(len(list), list)
  55. return
  56. } else if exchange == "cffex" {
  57. list := make([]*TradePositionCffexTop, 0)
  58. for _, v := range items {
  59. tmp := (*TradePositionCffexTop)(unsafe.Pointer(v))
  60. list = append(list, tmp)
  61. }
  62. _, err = o.InsertMulti(len(list), list)
  63. return
  64. } else if exchange == "shanghai" {
  65. list := make([]*TradePositionShanghaiTop, 0)
  66. for _, v := range items {
  67. tmp := (*TradePositionShanghaiTop)(unsafe.Pointer(v))
  68. list = append(list, tmp)
  69. }
  70. _, err = o.InsertMulti(len(list), list)
  71. return
  72. } else if exchange == "ine" {
  73. list := make([]*TradePositionIneTop, 0)
  74. for _, v := range items {
  75. tmp := (*TradePositionIneTop)(unsafe.Pointer(v))
  76. list = append(list, tmp)
  77. }
  78. _, err = o.InsertMulti(len(list), list)
  79. return
  80. }
  81. return
  82. }
  83. func GetTradePositionTopByExchangeDataTime(exchange string, startDate, endDate string) (list []*TradePositionTop, err error) {
  84. o := orm.NewOrmUsingDB("data")
  85. sql := "SELECT * FROM trade_position_" + exchange + "_top where data_time >= ? and data_time <= ? and deal_type in (1,2) ORDER BY classify_name, classify_type, deal_type, data_time, deal_value desc"
  86. _, err = o.Raw(sql, startDate, endDate).QueryRows(&list)
  87. return
  88. }
  89. func GetTradePositionTopCountByExchangeDataTime(exchange string, startDate, endDate string) (count int64, err error) {
  90. o := orm.NewOrmUsingDB("data")
  91. sql := "SELECT count(*) FROM trade_position_" + exchange + "_top where data_time >= ? and data_time <= ? and deal_type in (1,2) ORDER BY classify_name, classify_type, deal_type, data_time, deal_value desc"
  92. err = o.Raw(sql, startDate, endDate).QueryRow(&count)
  93. return
  94. }
  95. func GetTradePositionTopByExchangeSourceType(exchange string, dataTime string, sourceType int) (list []*TradePositionTop, err error) {
  96. o := orm.NewOrmUsingDB("data")
  97. sql := "SELECT * FROM trade_position_" + exchange + "_top where data_time= ? and source_type = ? ORDER BY classify_name, classify_type, deal_type, deal_value desc"
  98. _, err = o.Raw(sql, dataTime, sourceType).QueryRows(&list)
  99. return
  100. }
  101. type TradeTopClassify struct {
  102. ClassifyName string //分类名称
  103. ClassifyType string //分类名称下的类型
  104. }
  105. type TradePositionSub struct {
  106. ClassifyName string //分类名称
  107. ClassifyType string //分类名称下的类型
  108. DataTime string //数据日期
  109. DealShortName string //成交量公司简称
  110. SubValue int //差值
  111. DealType int
  112. }
  113. type TradePositionSubList []*TradePositionSub
  114. func (v TradePositionSubList) Len() int {
  115. return len(v)
  116. }
  117. func (v TradePositionSubList) Swap(i, j int) {
  118. v[i], v[j] = v[j], v[i]
  119. }
  120. func (v TradePositionSubList) Less(i, j int) bool {
  121. return v[i].SubValue > v[j].SubValue
  122. }
  123. type UpdateDealValueChange struct {
  124. Id uint64
  125. DealValue int //成交量
  126. DealChange int
  127. SourceType int
  128. ModifyTime time.Time //修改时间
  129. }
  130. func MultiUpdatePositionTop(exchange string, updates []UpdateDealValueChange) (err error) {
  131. o := orm.NewOrmUsingDB("data")
  132. p, err := o.Raw("UPDATE trade_position_" + exchange + "_top SET deal_value=?, deal_change=?, source_type=?, modify_time=? WHERE id = ?").Prepare()
  133. if err != nil {
  134. return
  135. }
  136. defer func() {
  137. _ = p.Close() // 别忘记关闭 statement
  138. }()
  139. for _, v := range updates {
  140. _, err = p.Exec(v.DealValue, v.DealChange, v.SourceType, v.ModifyTime, v.Id)
  141. if err != nil {
  142. return
  143. }
  144. }
  145. return
  146. }
  147. func DeletePositionTopByDataTime(exchange string, dataTime string, dealType int) (err error) {
  148. o := orm.NewOrmUsingDB("data")
  149. sql := "delete from trade_position_" + exchange + "_Top WHERE data_time=? and deal_type=?"
  150. _, err = o.Raw(sql, dataTime, dealType).Exec()
  151. return
  152. }
  153. func GetTradePositionTopByExchangeDataTimeType(exchange string, dataTime string, dealType int) (list []TradePositionTop, err error) {
  154. o := orm.NewOrmUsingDB("data")
  155. sql := "select * from trade_position_" + exchange + "_Top WHERE data_time=? and deal_type=?"
  156. _, err = o.Raw(sql, dataTime, dealType).QueryRows(&list)
  157. return
  158. }
  159. func MultiInsertTradeBaseDataToTop(exchange string, startDate, endDate string) (err error) {
  160. o := orm.NewOrmUsingDB("data")
  161. now := time.Now().Format(utils.FormatDateTime)
  162. sql1 := `INSERT INTO trade_position_` + exchange + `_top(classify_name,classify_type,deal_short_name,deal_value,deal_change,data_time,deal_type,source_type,rank,create_time,modify_time)
  163. SELECT classify_name,classify_type,buy_short_name,buy_value,buy_change,data_time,1,0,rank,?,? FROM base_from_trade_` + exchange + `_index where rank <50 and buy_short_name !="" and data_time between ? and ?`
  164. _, err = o.Raw(sql1, now, now, startDate, endDate).Exec()
  165. if err != nil {
  166. return
  167. }
  168. sql2 := `INSERT INTO trade_position_` + exchange + `_top(classify_name,classify_type,deal_short_name,deal_value,deal_change,data_time,deal_type,source_type,rank,create_time,modify_time)
  169. SELECT classify_name,classify_type,sold_short_name,sold_value,sold_change,data_time,2,0,rank,?,? FROM base_from_trade_` + exchange + `_index where rank <50 and sold_short_name !="" and data_time between ? and ?`
  170. _, err = o.Raw(sql2, now, now, startDate, endDate).Exec()
  171. return
  172. }