trade_position_analysis.go 6.6 KB

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