trade_position_analysis.go 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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. //o := orm.NewOrmUsingDB("data")
  90. //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"
  91. //_, err = o.Raw(sql, startDate, endDate).QueryRows(&list)
  92. 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"
  93. err = global.DmSQL["data"].Raw(sql, startDate, endDate).Find(&list).Error
  94. return
  95. }
  96. func GetTradePositionTopCountByExchangeDataTime(exchange string, startDate, endDate string) (count int64, err error) {
  97. //o := orm.NewOrmUsingDB("data")
  98. //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"
  99. //err = o.Raw(sql, startDate, endDate).QueryRow(&count)
  100. 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"
  101. err = global.DmSQL["data"].Raw(sql, startDate, endDate).Scan(&count).Error
  102. return
  103. }
  104. func GetTradePositionTopByExchangeSourceType(exchange string, dataTime string, sourceType int) (list []*TradePositionTop, err error) {
  105. //o := orm.NewOrmUsingDB("data")
  106. //sql := "SELECT * FROM trade_position_" + exchange + "_top where data_time= ? and source_type = ? ORDER BY classify_name, classify_type, deal_type, deal_value desc"
  107. //_, err = o.Raw(sql, dataTime, sourceType).QueryRows(&list)
  108. sql := "SELECT * FROM trade_position_" + exchange + "_top where data_time= ? and source_type = ? ORDER BY classify_name, classify_type, deal_type, deal_value desc"
  109. err = global.DmSQL["data"].Raw(sql, dataTime, sourceType).Find(&list).Error
  110. return
  111. }
  112. type TradeTopClassify struct {
  113. ClassifyName string //分类名称
  114. ClassifyType string //分类名称下的类型
  115. }
  116. type TradePositionSub struct {
  117. ClassifyName string //分类名称
  118. ClassifyType string //分类名称下的类型
  119. DataTime string //数据日期
  120. DealShortName string //成交量公司简称
  121. SubValue int //差值
  122. DealType int
  123. }
  124. type TradePositionSubList []*TradePositionSub
  125. func (v TradePositionSubList) Len() int {
  126. return len(v)
  127. }
  128. func (v TradePositionSubList) Swap(i, j int) {
  129. v[i], v[j] = v[j], v[i]
  130. }
  131. func (v TradePositionSubList) Less(i, j int) bool {
  132. return v[i].SubValue > v[j].SubValue
  133. }
  134. type UpdateDealValueChange struct {
  135. Id uint64
  136. DealValue int //成交量
  137. DealChange int
  138. SourceType int
  139. ModifyTime time.Time //修改时间
  140. }
  141. func MultiUpdatePositionTop(exchange string, updates []UpdateDealValueChange) (err error) {
  142. //o := orm.NewOrmUsingDB("data")
  143. //p, err := o.Raw("UPDATE trade_position_" + exchange + "_top SET deal_value=?, deal_change=?, source_type=?, modify_time=? WHERE id = ?").Prepare()
  144. //if err != nil {
  145. // return
  146. //}
  147. //defer func() {
  148. // _ = p.Close() // 别忘记关闭 statement
  149. //}()
  150. //for _, v := range updates {
  151. // _, err = p.Exec(v.DealValue, v.DealChange, v.SourceType, v.ModifyTime, v.Id)
  152. // if err != nil {
  153. // return
  154. // }
  155. //}
  156. sql := ` "UPDATE trade_position_" + exchange + "_top SET deal_value=?, deal_change=?, source_type=?, modify_time=? WHERE id = ?" `
  157. for _, v := range updates {
  158. err = global.DmSQL["data"].Exec(sql, v.DealValue, v.DealChange, v.SourceType, v.ModifyTime, v.Id).Error
  159. if err != nil {
  160. return
  161. }
  162. }
  163. return
  164. }
  165. func DeletePositionTopByDataTime(exchange string, dataTime string, dealType int) (err error) {
  166. //o := orm.NewOrmUsingDB("data")
  167. //sql := "delete from trade_position_" + exchange + "_Top WHERE data_time=? and deal_type=?"
  168. //_, err = o.Raw(sql, dataTime, dealType).Exec()
  169. sql := "delete from trade_position_" + exchange + "_Top WHERE data_time=? and deal_type=?"
  170. err = global.DmSQL["data"].Exec(sql, dataTime, dealType).Error
  171. return
  172. }
  173. func GetTradePositionTopByExchangeDataTimeType(exchange string, dataTime string, dealType int) (list []TradePositionTop, err error) {
  174. //o := orm.NewOrmUsingDB("data")
  175. //sql := "select * from trade_position_" + exchange + "_Top WHERE data_time=? and deal_type=?"
  176. //_, err = o.Raw(sql, dataTime, dealType).QueryRows(&list)
  177. sql := "select * from trade_position_" + exchange + "_Top WHERE data_time=? and deal_type=?"
  178. err = global.DmSQL["data"].Raw(sql, dataTime, dealType).Find(&list).Error
  179. return
  180. }
  181. func MultiInsertTradeBaseDataToTop(exchange string, startDate, endDate string) (err error) {
  182. // o := orm.NewOrmUsingDB("data")
  183. // now := time.Now().Format(utils.FormatDateTime)
  184. // 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)
  185. // 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 ?`
  186. // _, err = o.Raw(sql1, now, now, startDate, endDate).Exec()
  187. // if err != nil {
  188. // return
  189. // }
  190. // 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)
  191. //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 ?`
  192. // _, err = o.Raw(sql2, now, now, startDate, endDate).Exec()
  193. now := time.Now().Format(utils.FormatDateTime)
  194. 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)
  195. 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 ?`
  196. err = global.DmSQL["data"].Exec(sql1, now, now, startDate, endDate).Error
  197. if err != nil {
  198. return
  199. }
  200. 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)
  201. 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 ?`
  202. err = global.DmSQL["data"].Raw(sql2, now, now, startDate, endDate).Error
  203. return
  204. }