trade_position_analysis.go 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. package data_manage
  2. import (
  3. "eta_gn/eta_api/global"
  4. "eta_gn/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 := 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. err = global.DmSQL["data"].CreateInBatches(list, utils.MultiAddNum).Error
  48. return
  49. } else if exchange == "zhengzhou" {
  50. list := make([]*TradePositionZhengzhouTop, 0)
  51. for _, v := range items {
  52. tmp := (*TradePositionZhengzhouTop)(unsafe.Pointer(v))
  53. list = append(list, tmp)
  54. }
  55. //_, err = o.InsertMulti(len(list), list)
  56. err = global.DmSQL["data"].CreateInBatches(list, utils.MultiAddNum).Error
  57. return
  58. } else if exchange == "cffex" {
  59. list := make([]*TradePositionCffexTop, 0)
  60. for _, v := range items {
  61. tmp := (*TradePositionCffexTop)(unsafe.Pointer(v))
  62. list = append(list, tmp)
  63. }
  64. //_, err = o.InsertMulti(len(list), list)
  65. err = global.DmSQL["data"].CreateInBatches(list, utils.MultiAddNum).Error
  66. return
  67. } else if exchange == "shanghai" {
  68. list := make([]*TradePositionShanghaiTop, 0)
  69. for _, v := range items {
  70. tmp := (*TradePositionShanghaiTop)(unsafe.Pointer(v))
  71. list = append(list, tmp)
  72. }
  73. //_, err = o.InsertMulti(len(list), list)
  74. err = global.DmSQL["data"].CreateInBatches(list, utils.MultiAddNum).Error
  75. return
  76. } else if exchange == "ine" {
  77. list := make([]*TradePositionIneTop, 0)
  78. for _, v := range items {
  79. tmp := (*TradePositionIneTop)(unsafe.Pointer(v))
  80. list = append(list, tmp)
  81. }
  82. //_, err = o.InsertMulti(len(list), list)
  83. err = global.DmSQL["data"].CreateInBatches(list, utils.MultiAddNum).Error
  84. return
  85. }
  86. return
  87. }
  88. func GetTradePositionTopByExchangeDataTime(exchange string, startDate, endDate string) (list []*TradePositionTop, err error) {
  89. 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"
  90. err = global.DmSQL["data"].Raw(sql, startDate, endDate).Find(&list).Error
  91. return
  92. }
  93. func GetTradePositionTopCountByExchangeDataTime(exchange string, startDate, endDate string) (count int64, err error) {
  94. 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"
  95. err = global.DmSQL["data"].Raw(sql, startDate, endDate).Scan(&count).Error
  96. return
  97. }
  98. func GetTradePositionTopByExchangeSourceType(exchange string, dataTime string, sourceType int) (list []*TradePositionTop, err error) {
  99. sql := "SELECT * FROM trade_position_" + exchange + "_top where data_time= ? and source_type = ? ORDER BY classify_name, classify_type, deal_type, deal_value desc"
  100. err = global.DmSQL["data"].Raw(sql, dataTime, sourceType).Find(&list).Error
  101. return
  102. }
  103. type TradeTopClassify struct {
  104. ClassifyName string //分类名称
  105. ClassifyType string //分类名称下的类型
  106. }
  107. type TradePositionSub struct {
  108. ClassifyName string //分类名称
  109. ClassifyType string //分类名称下的类型
  110. DataTime string //数据日期
  111. DealShortName string //成交量公司简称
  112. SubValue int //差值
  113. DealType int
  114. }
  115. type TradePositionSubList []*TradePositionSub
  116. func (v TradePositionSubList) Len() int {
  117. return len(v)
  118. }
  119. func (v TradePositionSubList) Swap(i, j int) {
  120. v[i], v[j] = v[j], v[i]
  121. }
  122. func (v TradePositionSubList) Less(i, j int) bool {
  123. return v[i].SubValue > v[j].SubValue
  124. }
  125. type UpdateDealValueChange struct {
  126. Id uint64
  127. DealValue int //成交量
  128. DealChange int
  129. SourceType int
  130. ModifyTime time.Time //修改时间
  131. }
  132. func MultiUpdatePositionTop(exchange string, updates []UpdateDealValueChange) (err error) {
  133. sql := ` "UPDATE trade_position_" + exchange + "_top SET deal_value=?, deal_change=?, source_type=?, modify_time=? WHERE id = ?" `
  134. for _, v := range updates {
  135. err = global.DmSQL["data"].Exec(sql, v.DealValue, v.DealChange, v.SourceType, v.ModifyTime, v.Id).Error
  136. if err != nil {
  137. return
  138. }
  139. }
  140. return
  141. }
  142. func DeletePositionTopByDataTime(exchange string, dataTime string, dealType int) (err error) {
  143. sql := "delete from trade_position_" + exchange + "_Top WHERE data_time=? and deal_type=?"
  144. err = global.DmSQL["data"].Exec(sql, dataTime, dealType).Error
  145. return
  146. }
  147. func GetTradePositionTopByExchangeDataTimeType(exchange string, dataTime string, dealType int) (list []TradePositionTop, err error) {
  148. sql := "select * from trade_position_" + exchange + "_Top WHERE data_time=? and deal_type=?"
  149. err = global.DmSQL["data"].Raw(sql, dataTime, dealType).Find(&list).Error
  150. return
  151. }
  152. func MultiInsertTradeBaseDataToTop(exchange string, startDate, endDate string) (err error) {
  153. now := time.Now().Format(utils.FormatDateTime)
  154. 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)
  155. 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 ?`
  156. err = global.DmSQL["data"].Exec(sql1, now, now, startDate, endDate).Error
  157. if err != nil {
  158. return
  159. }
  160. 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)
  161. 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 ?`
  162. err = global.DmSQL["data"].Raw(sql2, now, now, startDate, endDate).Error
  163. return
  164. }