trade_position_analysis.go 17 KB

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