trade_position_analysis.go 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  1. package data_manage
  2. import (
  3. "fmt"
  4. "github.com/beego/beego/v2/client/orm"
  5. "hongze/hongze_task/utils"
  6. "time"
  7. "unsafe"
  8. )
  9. // 持仓榜单表
  10. type TradePositionTop struct {
  11. Id uint64 `orm:"column(id);pk"`
  12. ClassifyName string //分类名称
  13. ClassifyType string //分类名称下的类型
  14. DataTime string //数据日期
  15. CreateTime time.Time //插入时间
  16. ModifyTime time.Time //修改时间
  17. DealShortName string //成交量公司简称
  18. DealValue int //成交量
  19. DealChange int //成交变化量
  20. DealType int //交易类型:1多单,2空单,3净多单,4净空单
  21. SourceType int //数据来源,0是原始数据的值,1是由T+1日推算出的值,2是由T日的榜单数据推算出的值
  22. Rank int //排名
  23. }
  24. type TradePositionDalianTop struct {
  25. TradePositionTop
  26. }
  27. type TradePositionZhengzhouTop struct {
  28. TradePositionTop
  29. }
  30. type TradePositionCffexTop struct {
  31. TradePositionTop
  32. }
  33. type TradePositionShanghaiTop struct {
  34. TradePositionTop
  35. }
  36. type TradePositionIneTop struct {
  37. TradePositionTop
  38. }
  39. func InsertMultiTradePositionTop(exchange string, items []*TradePositionTop) (err error) {
  40. o := orm.NewOrmUsingDB("data")
  41. if exchange == "dalian" {
  42. list := make([]*TradePositionDalianTop, 0)
  43. for _, v := range items {
  44. tmp := (*TradePositionDalianTop)(unsafe.Pointer(v))
  45. list = append(list, tmp)
  46. }
  47. _, err = o.InsertMulti(len(list), list)
  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. return
  57. } else if exchange == "cffex" {
  58. list := make([]*TradePositionCffexTop, 0)
  59. for _, v := range items {
  60. tmp := (*TradePositionCffexTop)(unsafe.Pointer(v))
  61. list = append(list, tmp)
  62. }
  63. _, err = o.InsertMulti(len(list), list)
  64. return
  65. } else if exchange == "shanghai" {
  66. list := make([]*TradePositionShanghaiTop, 0)
  67. for _, v := range items {
  68. tmp := (*TradePositionShanghaiTop)(unsafe.Pointer(v))
  69. list = append(list, tmp)
  70. }
  71. _, err = o.InsertMulti(len(list), list)
  72. return
  73. } else if exchange == "ine" {
  74. list := make([]*TradePositionIneTop, 0)
  75. for _, v := range items {
  76. tmp := (*TradePositionIneTop)(unsafe.Pointer(v))
  77. list = append(list, tmp)
  78. }
  79. _, err = o.InsertMulti(len(list), list)
  80. return
  81. }
  82. return
  83. }
  84. func GetTradePositionTopByExchangeDataTime(exchange string, startDate, endDate string) (list []*TradePositionTop, err error) {
  85. o := orm.NewOrmUsingDB("data")
  86. 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"
  87. _, err = o.Raw(sql, startDate, endDate).QueryRows(&list)
  88. return
  89. }
  90. func GetTradePositionTopByExchangeDataTimeByClassify(exchange string, startDate, endDate string, classifyNames, classifyTypes []string) (list []*TradePositionTop, err error) {
  91. o := orm.NewOrmUsingDB("data")
  92. sql := `SELECT * FROM trade_position_` + exchange + `_top where data_time >= ? and data_time <= ? and deal_type in (1,2) and classify_name in (` + utils.GetOrmInReplace(len(classifyNames)) + `) and classify_type in (` + utils.GetOrmInReplace(len(classifyTypes)) + `) ORDER BY classify_name, classify_type, deal_type, data_time, deal_value desc`
  93. _, err = o.Raw(sql, startDate, endDate, classifyNames, classifyTypes).QueryRows(&list)
  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. return
  101. }
  102. func GetTradePositionTopByExchangeSourceType(exchange string, dataTime string, sourceType int) (list []*TradePositionTop, err error) {
  103. o := orm.NewOrmUsingDB("data")
  104. sql := "SELECT * FROM trade_position_" + exchange + "_top where data_time= ? and source_type = ? ORDER BY classify_name, classify_type, deal_type, deal_value desc"
  105. _, err = o.Raw(sql, dataTime, sourceType).QueryRows(&list)
  106. return
  107. }
  108. func GetTradePositionTopByExchangeSourceTypeClassify(exchange string, dataTime string, sourceType int, classifyNames, classifyTypes []string) (list []*TradePositionTop, err error) {
  109. o := orm.NewOrmUsingDB("data")
  110. sql := `SELECT * FROM trade_position_` + exchange + `_top where data_time= ? and source_type = ? and classify_name in (` + utils.GetOrmInReplace(len(classifyNames)) + `) and classify_type in (` + utils.GetOrmInReplace(len(classifyTypes)) + `) ORDER BY classify_name, classify_type, deal_type, deal_value desc`
  111. _, err = o.Raw(sql, dataTime, sourceType, classifyNames, classifyTypes).QueryRows(&list)
  112. return
  113. }
  114. type TradeTopClassify struct {
  115. ClassifyName string //分类名称
  116. ClassifyType string //分类名称下的类型
  117. }
  118. type TradePositionSub struct {
  119. ClassifyName string //分类名称
  120. ClassifyType string //分类名称下的类型
  121. DataTime string //数据日期
  122. DealShortName string //成交量公司简称
  123. SubValue int //差值
  124. DealType int
  125. }
  126. type TradePositionSubList []*TradePositionSub
  127. func (v TradePositionSubList) Len() int {
  128. return len(v)
  129. }
  130. func (v TradePositionSubList) Swap(i, j int) {
  131. v[i], v[j] = v[j], v[i]
  132. }
  133. func (v TradePositionSubList) Less(i, j int) bool {
  134. return v[i].SubValue > v[j].SubValue
  135. }
  136. type UpdateDealValueChange struct {
  137. Id uint64
  138. DealValue int //成交量
  139. DealChange int
  140. SourceType int
  141. ModifyTime time.Time //修改时间
  142. }
  143. type UpdateChangeVal struct {
  144. Id uint64
  145. DealChange int
  146. ModifyTime time.Time //修改时间
  147. }
  148. func MultiUpdatePositionTop(exchange string, updates []UpdateDealValueChange) (err error) {
  149. o := orm.NewOrmUsingDB("data")
  150. p, err := o.Raw("UPDATE trade_position_" + exchange + "_top SET deal_value=?, deal_change=?, source_type=?, modify_time=? WHERE id = ?").Prepare()
  151. if err != nil {
  152. return
  153. }
  154. defer func() {
  155. _ = p.Close() // 别忘记关闭 statement
  156. }()
  157. for _, v := range updates {
  158. _, err = p.Exec(v.DealValue, v.DealChange, v.SourceType, v.ModifyTime, v.Id)
  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. return
  170. }
  171. func DeletePositionTopByDataTimeClassify(exchange string, dataTime string, dealType int, classifyNames, classifyTypes []string) (err error) {
  172. o := orm.NewOrmUsingDB("data")
  173. sql := `delete from trade_position_` + exchange + `_top WHERE data_time=? and deal_type=? and classify_name in (` + utils.GetOrmInReplace(len(classifyNames)) + `) and classify_type in (` + utils.GetOrmInReplace(len(classifyTypes)) + `)`
  174. _, err = o.Raw(sql, dataTime, dealType, classifyNames, classifyTypes).Exec()
  175. return
  176. }
  177. func GetTradePositionTopByExchangeDataTimeType(exchange string, dataTime string, dealType int) (list []TradePositionTop, err error) {
  178. o := orm.NewOrmUsingDB("data")
  179. sql := "select * from trade_position_" + exchange + "_top WHERE data_time=? and deal_type=?"
  180. _, err = o.Raw(sql, dataTime, dealType).QueryRows(&list)
  181. return
  182. }
  183. func GetTradePositionTopByExchangeDataTimeTypeClassify(exchange string, dataTime string, dealType int, classifyNames, classifyTypes []string) (list []TradePositionTop, err error) {
  184. o := orm.NewOrmUsingDB("data")
  185. sql := `select * from trade_position_` + exchange + `_top WHERE data_time=? and deal_type=? and classify_name in (` + utils.GetOrmInReplace(len(classifyNames)) + `) and classify_type in (` + utils.GetOrmInReplace(len(classifyTypes)) + `)`
  186. _, err = o.Raw(sql, dataTime, dealType, classifyNames, classifyTypes).QueryRows(&list)
  187. return
  188. }
  189. func MultiInsertTradeBaseDataToTop(exchange string, startDate, endDate string) (err error) {
  190. o := orm.NewOrmUsingDB("data")
  191. now := time.Now().Format(utils.FormatDateTime)
  192. 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)
  193. 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 ?`
  194. _, err = o.Raw(sql1, now, now, startDate, endDate).Exec()
  195. if err != nil {
  196. return
  197. }
  198. 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)
  199. 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 ?`
  200. _, err = o.Raw(sql2, now, now, startDate, endDate).Exec()
  201. return
  202. }
  203. func MultiInsertTradeBaseDataToTopByClassify(exchange string, startDate, endDate string, classifyNames, classifyTypes []string) (err error) {
  204. o := orm.NewOrmUsingDB("data")
  205. now := time.Now().Format(utils.FormatDateTime)
  206. 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)
  207. 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 ? and classify_name in (` + utils.GetOrmInReplace(len(classifyNames)) + `) and classify_type in (` + utils.GetOrmInReplace(len(classifyTypes)) + `)`
  208. _, err = o.Raw(sql1, now, now, startDate, endDate, classifyNames, classifyTypes).Exec()
  209. if err != nil {
  210. return
  211. }
  212. 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)
  213. 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 ? and classify_name in (` + utils.GetOrmInReplace(len(classifyNames)) + `) and classify_type in (` + utils.GetOrmInReplace(len(classifyTypes)) + `)`
  214. _, err = o.Raw(sql2, now, now, startDate, endDate, classifyNames, classifyTypes).Exec()
  215. return
  216. }
  217. // GetTradePositionTopOriginDataTimes 获取榜单原始数据日期-正序
  218. func GetTradePositionTopOriginDataTimes(exchange string) (dates []string, err error) {
  219. o := orm.NewOrmUsingDB("data")
  220. sql := `SELECT DISTINCT data_time FROM base_from_trade_%s_index ORDER BY data_time ASC`
  221. sql = fmt.Sprintf(sql, exchange)
  222. _, err = o.Raw(sql).QueryRows(&dates)
  223. return
  224. }
  225. // BaseFromTradeClassify 交易所分类表
  226. type BaseFromTradeClassify struct {
  227. Id uint64 `orm:"column(id);pk"`
  228. ClassifyName string //分类名称
  229. ClassifyType string //分类名称下的类型
  230. Exchange string //交易所
  231. LatestDate time.Time //数据最近的日期
  232. CreateTime time.Time //插入时间
  233. ModifyTime time.Time //修改时间
  234. }
  235. // GetAllBaseFromTradeClassify 获取所有的交易所分类列表
  236. func GetAllBaseFromTradeClassify() (list []*BaseFromTradeClassify, err error) {
  237. sql := `SELECT * FROM base_from_trade_classify `
  238. o := orm.NewOrmUsingDB("data")
  239. _, err = o.Raw(sql).QueryRows(&list)
  240. return
  241. }
  242. // Update 更新
  243. func (m *BaseFromTradeClassify) Update(cols []string) (err error) {
  244. o := orm.NewOrmUsingDB("data")
  245. _, err = o.Update(m, cols...)
  246. return
  247. }
  248. // MultiAddBaseFromTradeClassify 批量插入交易所分类
  249. func MultiAddBaseFromTradeClassify(items []*BaseFromTradeClassify) (err error) {
  250. if len(items) == 0 {
  251. return
  252. }
  253. o := orm.NewOrmUsingDB("data")
  254. _, err = o.InsertMulti(len(items), items)
  255. return
  256. }
  257. type TradeClassifyName struct {
  258. ClassifyName string //分类名称
  259. ClassifyType string //分类名称下的类型
  260. DataTime time.Time //数据最近的日期
  261. ModifyTime time.Time //数据最近的日期
  262. }
  263. // GetExchangeClassify 获取交易所分类列表
  264. func GetExchangeClassify(exchange string) (list []TradeClassifyName, err error) {
  265. tableName := "trade_position_" + exchange + "_top"
  266. orderStr := "classify_name DESC, classify_type asc"
  267. if exchange == "zhengzhou" {
  268. orderStr = "classify_name asc"
  269. }
  270. sql := `SELECT classify_name, classify_type,MAX(data_time) as data_time,MAX(modify_time) as modify_time FROM ` + tableName + ` GROUP BY classify_name, classify_type `
  271. sql += ` ORDER BY ` + orderStr
  272. o := orm.NewOrmUsingDB("data")
  273. _, err = o.Raw(sql).QueryRows(&list)
  274. return
  275. }
  276. // GetTradePositionTopCleanByExchangeDataTime 根据时间查询净多单和净空单的值
  277. func GetTradePositionTopCleanByExchangeDataTime(exchange string, startDate, endDate string) (list []*TradePositionTop, err error) {
  278. o := orm.NewOrmUsingDB("data")
  279. sql := "SELECT * FROM trade_position_" + exchange + "_top where data_time >= ? and data_time <= ? and deal_type in (3,4) ORDER BY classify_name, classify_type, deal_type, data_time, deal_value desc"
  280. _, err = o.Raw(sql, startDate, endDate).QueryRows(&list)
  281. return
  282. }
  283. // GetTradePositionTopCleanByExchangeDataTimeClassify 根据时间查询净多单和净空单的值
  284. func GetTradePositionTopCleanByExchangeDataTimeClassify(exchange string, startDate, endDate string, classifyNames, classifyTypes []string) (list []*TradePositionTop, err error) {
  285. o := orm.NewOrmUsingDB("data")
  286. sql := `SELECT * FROM trade_position_` + exchange + `_top where data_time >= ? and data_time <= ? and deal_type in (3,4) and classify_name in (` + utils.GetOrmInReplace(len(classifyNames)) + `) and classify_type in (` + utils.GetOrmInReplace(len(classifyTypes)) + `) ORDER BY classify_name, classify_type, deal_type, data_time, deal_value desc`
  287. _, err = o.Raw(sql, startDate, endDate, classifyNames, classifyTypes).QueryRows(&list)
  288. return
  289. }
  290. // MultiUpdatePositionTopChangeVal 批量更新榜单里变化量的值
  291. func MultiUpdatePositionTopChangeVal(exchange string, updates []UpdateChangeVal) (err error) {
  292. o := orm.NewOrmUsingDB("data")
  293. p, err := o.Raw("UPDATE trade_position_" + exchange + "_top SET deal_change=?, modify_time=? WHERE id = ?").Prepare()
  294. if err != nil {
  295. return
  296. }
  297. defer func() {
  298. _ = p.Close() // 别忘记关闭 statement
  299. }()
  300. for _, v := range updates {
  301. _, err = p.Exec(v.DealChange, v.ModifyTime, v.Id)
  302. if err != nil {
  303. return
  304. }
  305. }
  306. return
  307. }
  308. func GetTradePositionOriginClassifyCountByExchangeDataTime(exchange string, startDate, endDate string) (count int64, err error) {
  309. o := orm.NewOrmUsingDB("data")
  310. sql := `SELECT COUNT(DISTINCT classify_name, classify_type) FROM base_from_trade_` + exchange + `_index where rank <50 and (buy_short_name !="" || sold_short_name !="" ) and data_time >= ? and data_time <= ?`
  311. err = o.Raw(sql, startDate, endDate).QueryRow(&count)
  312. return
  313. }
  314. func GetTradePositionTopClassifyCountByExchangeDataTime(exchange string, startDate, endDate string) (count int64, err error) {
  315. o := orm.NewOrmUsingDB("data")
  316. sql := "SELECT COUNT(DISTINCT classify_name, classify_type) FROM trade_position_" + exchange + "_top where data_time >= ? and data_time <= ? and deal_type in (1,2) "
  317. err = o.Raw(sql, startDate, endDate).QueryRow(&count)
  318. return
  319. }
  320. type TradePositionClassifyInfo struct {
  321. ClassifyName string //分类名称
  322. ClassifyType string //分类名称下的类型
  323. }
  324. func GetTradePositionOriginClassifyByExchangeDataTime(exchange string, startDate, endDate string) (list []TradePositionClassifyInfo, err error) {
  325. o := orm.NewOrmUsingDB("data")
  326. sql := `SELECT DISTINCT classify_name, classify_type FROM base_from_trade_` + exchange + `_index where rank <50 and (buy_short_name !="" || sold_short_name !="" ) and data_time >= ? and data_time <= ?`
  327. _, err = o.Raw(sql, startDate, endDate).QueryRows(&list)
  328. return
  329. }
  330. func GetTradePositionTopClassifyByExchangeDataTime(exchange string, startDate, endDate string) (list []TradePositionClassifyInfo, err error) {
  331. o := orm.NewOrmUsingDB("data")
  332. sql := "SELECT DISTINCT classify_name, classify_type FROM trade_position_" + exchange + "_top where data_time >= ? and data_time <= ? and deal_type in (1,2) "
  333. _, err = o.Raw(sql, startDate, endDate).QueryRows(&list)
  334. return
  335. }
  336. // DeleteTradePositionTopAllByExchangeDataTime 删除计算数据
  337. func DeleteTradePositionTopAllByExchangeDataTime(exchange string, startDate, endDate string) (err error) {
  338. o := orm.NewOrmUsingDB("data")
  339. sql := "DELETE FROM trade_position_" + exchange + "_top where data_time >= ? and data_time <= ? "
  340. _, err = o.Raw(sql, startDate, endDate).Exec()
  341. return
  342. }