trade_position_analysis.go 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. package data_manage
  2. import (
  3. "eta/eta_api/global"
  4. "eta/eta_api/utils"
  5. "time"
  6. "unsafe"
  7. )
  8. // 持仓榜单表
  9. type TradePositionTop struct {
  10. Id uint64 `orm:"column(id);pk" gorm:"primaryKey"`
  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 := global.DbMap[utils.DbNameIndex]
  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.CreateInBatches(list, utils.MultiAddNum).Error
  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.CreateInBatches(list, utils.MultiAddNum).Error
  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.CreateInBatches(list, utils.MultiAddNum).Error
  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.CreateInBatches(list, utils.MultiAddNum).Error
  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.CreateInBatches(list, utils.MultiAddNum).Error
  79. return
  80. }
  81. return
  82. }
  83. func GetTradePositionTopByExchangeDataTime(exchange string, startDate, endDate string) (list []*TradePositionTop, err error) {
  84. o := global.DbMap[utils.DbNameIndex]
  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).Find(&list).Error
  87. return
  88. }
  89. func GetTradePositionTopCountByExchangeDataTime(exchange string, startDate, endDate string) (count int64, err error) {
  90. o := global.DbMap[utils.DbNameIndex]
  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).Scan(&count).Error
  93. return
  94. }
  95. func GetTradePositionTopByExchangeSourceType(exchange string, dataTime string, sourceType int) (list []*TradePositionTop, err error) {
  96. o := global.DbMap[utils.DbNameIndex]
  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).Find(&list).Error
  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 := global.DbMap[utils.DbNameIndex]
  132. sql := "UPDATE trade_position_" + exchange + "_top SET deal_value=?, deal_change=?, source_type=?, modify_time=? WHERE id = ?"
  133. for _, v := range updates {
  134. err = o.Exec(sql, v.DealValue, v.DealChange, v.SourceType, v.ModifyTime, v.Id).Error
  135. if err != nil {
  136. return
  137. }
  138. }
  139. return
  140. }
  141. func DeletePositionTopByDataTime(exchange string, dataTime string, dealType int) (err error) {
  142. o := global.DbMap[utils.DbNameIndex]
  143. sql := "delete from trade_position_" + exchange + "_Top WHERE data_time=? and deal_type=?"
  144. err = o.Exec(sql, dataTime, dealType).Error
  145. return
  146. }
  147. func GetTradePositionTopByExchangeDataTimeType(exchange string, dataTime string, dealType int) (list []TradePositionTop, err error) {
  148. o := global.DbMap[utils.DbNameIndex]
  149. sql := "select * from trade_position_" + exchange + "_Top WHERE data_time=? and deal_type=?"
  150. err = o.Raw(sql, dataTime, dealType).Find(&list).Error
  151. return
  152. }
  153. func MultiInsertTradeBaseDataToTop(exchange string, startDate, endDate string) (err error) {
  154. o := global.DbMap[utils.DbNameIndex]
  155. now := time.Now().Format(utils.FormatDateTime)
  156. 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)
  157. 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 ?`
  158. err = o.Exec(sql1, now, now, startDate, endDate).Error
  159. if err != nil {
  160. return
  161. }
  162. 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)
  163. 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 ?`
  164. err = o.Exec(sql2, now, now, startDate, endDate).Error
  165. return
  166. }