trade_position_analysis.go 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  1. package data_manage
  2. import (
  3. "eta_gn/eta_task/global"
  4. "eta_gn/eta_task/utils"
  5. "fmt"
  6. "time"
  7. "unsafe"
  8. )
  9. type TradePositionTop struct {
  10. Id uint64 `gorm:"column:id;primaryKey"` //`orm:"column(id);pk"`
  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. type TradePositionGuangzhouTop struct {
  39. TradePositionTop
  40. }
  41. func InsertMultiTradePositionTop(exchange string, items []*TradePositionTop) (err error) {
  42. if exchange == "dalian" {
  43. list := make([]*TradePositionDalianTop, 0)
  44. for _, v := range items {
  45. tmp := (*TradePositionDalianTop)(unsafe.Pointer(v))
  46. list = append(list, tmp)
  47. }
  48. err = global.DEFAULT_DmSQL.CreateInBatches(list, utils.MultiAddNum).Error
  49. return
  50. } else if exchange == "zhengzhou" {
  51. list := make([]*TradePositionZhengzhouTop, 0)
  52. for _, v := range items {
  53. tmp := (*TradePositionZhengzhouTop)(unsafe.Pointer(v))
  54. list = append(list, tmp)
  55. }
  56. err = global.DEFAULT_DmSQL.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 = global.DEFAULT_DmSQL.CreateInBatches(list, utils.MultiAddNum).Error
  65. return
  66. } else if exchange == "shanghai" {
  67. list := make([]*TradePositionShanghaiTop, 0)
  68. for _, v := range items {
  69. tmp := (*TradePositionShanghaiTop)(unsafe.Pointer(v))
  70. list = append(list, tmp)
  71. }
  72. err = global.DEFAULT_DmSQL.CreateInBatches(list, utils.MultiAddNum).Error
  73. return
  74. } else if exchange == "ine" {
  75. list := make([]*TradePositionIneTop, 0)
  76. for _, v := range items {
  77. tmp := (*TradePositionIneTop)(unsafe.Pointer(v))
  78. list = append(list, tmp)
  79. }
  80. err = global.DEFAULT_DmSQL.CreateInBatches(list, utils.MultiAddNum).Error
  81. return
  82. } else if exchange == "guangzhou" {
  83. list := make([]*TradePositionGuangzhouTop, 0)
  84. for _, v := range items {
  85. tmp := (*TradePositionGuangzhouTop)(unsafe.Pointer(v))
  86. list = append(list, tmp)
  87. }
  88. err = global.DEFAULT_DmSQL.CreateInBatches(list, utils.MultiAddNum).Error
  89. return
  90. }
  91. return
  92. }
  93. func GetTradePositionTopByExchangeDataTime(exchange string, startDate, endDate string) (list []*TradePositionTop, err error) {
  94. 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"
  95. err = global.DEFAULT_DmSQL.Raw(sql, startDate, endDate).Find(&list).Error
  96. return
  97. }
  98. func GetTradePositionTopByExchangeDataTimeByClassify(exchange string, startDate, endDate string, classifyNames, classifyTypes []string) (list []*TradePositionTop, err error) {
  99. 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`
  100. err = global.DEFAULT_DmSQL.Raw(sql, startDate, endDate, classifyNames, classifyTypes).Find(&list).Error
  101. return
  102. }
  103. func GetTradePositionTopCountByExchangeDataTime(exchange string, startDate, endDate string) (count int64, err error) {
  104. 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"
  105. err = global.DEFAULT_DmSQL.Raw(sql, startDate, endDate).Scan(&count).Error
  106. return
  107. }
  108. func GetTradePositionTopByExchangeSourceType(exchange string, dataTime string, sourceType int) (list []*TradePositionTop, err error) {
  109. sql := "SELECT * FROM trade_position_" + exchange + "_top where data_time= ? and source_type = ? ORDER BY classify_name, classify_type, deal_type, deal_value desc"
  110. err = global.DEFAULT_DmSQL.Raw(sql, dataTime, sourceType).Find(&list).Error
  111. return
  112. }
  113. func GetTradePositionTopByExchangeSourceTypeClassify(exchange string, dataTime string, sourceType int, classifyNames, classifyTypes []string) (list []*TradePositionTop, err error) {
  114. 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`
  115. err = global.DEFAULT_DmSQL.Raw(sql, dataTime, sourceType, classifyNames, classifyTypes).Find(&list).Error
  116. return
  117. }
  118. type TradeTopClassify struct {
  119. ClassifyName string //分类名称
  120. ClassifyType string //分类名称下的类型
  121. }
  122. type TradePositionSub struct {
  123. ClassifyName string //分类名称
  124. ClassifyType string //分类名称下的类型
  125. DataTime string //数据日期
  126. DealShortName string //成交量公司简称
  127. SubValue int //差值
  128. DealType int
  129. }
  130. type TradePositionSubList []*TradePositionSub
  131. func (v TradePositionSubList) Len() int {
  132. return len(v)
  133. }
  134. func (v TradePositionSubList) Swap(i, j int) {
  135. v[i], v[j] = v[j], v[i]
  136. }
  137. func (v TradePositionSubList) Less(i, j int) bool {
  138. return v[i].SubValue > v[j].SubValue
  139. }
  140. type UpdateDealValueChange struct {
  141. Id uint64
  142. DealValue int //成交量
  143. DealChange int
  144. SourceType int
  145. ModifyTime time.Time //修改时间
  146. }
  147. type UpdateChangeVal struct {
  148. Id uint64
  149. DealChange int
  150. ModifyTime time.Time //修改时间
  151. }
  152. func MultiUpdatePositionTop(exchange string, updates []UpdateDealValueChange) (err error) {
  153. sql := `"UPDATE trade_position_" + exchange + "_top SET deal_value=?, deal_change=?, source_type=?, modify_time=? WHERE id = ?"`
  154. for _, v := range updates {
  155. err = global.DEFAULT_DmSQL.Exec(sql, v.DealValue, v.DealChange, v.SourceType, v.ModifyTime, v.Id).Error
  156. if err != nil {
  157. return
  158. }
  159. }
  160. return
  161. }
  162. func DeletePositionTopByDataTime(exchange string, dataTime string, dealType int) (err error) {
  163. sql := "delete from trade_position_" + exchange + "_top WHERE data_time=? and deal_type=?"
  164. err = global.DEFAULT_DmSQL.Exec(sql, dataTime, dealType).Error
  165. return
  166. }
  167. func DeletePositionTopByDataTimeClassify(exchange string, dataTime string, dealType int, classifyNames, classifyTypes []string) (err error) {
  168. 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)) + `)`
  169. err = global.DEFAULT_DmSQL.Exec(sql, dataTime, dealType, classifyNames, classifyTypes).Error
  170. return
  171. }
  172. func GetTradePositionTopByExchangeDataTimeType(exchange string, dataTime string, dealType int) (list []TradePositionTop, err error) {
  173. sql := "select * from trade_position_" + exchange + "_top WHERE data_time=? and deal_type=?"
  174. err = global.DEFAULT_DmSQL.Raw(sql, dataTime, dealType).Find(&list).Error
  175. return
  176. }
  177. func GetTradePositionTopByExchangeDataTimeTypeClassify(exchange string, dataTime string, dealType int, classifyNames, classifyTypes []string) (list []TradePositionTop, err error) {
  178. 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)) + `)`
  179. err = global.DEFAULT_DmSQL.Raw(sql, dataTime, dealType, classifyNames, classifyTypes).Find(&list).Error
  180. return
  181. }
  182. func MultiInsertTradeBaseDataToTop(exchange string, startDate, endDate string) (err error) {
  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 buy_short_name !=' ' and data_time between ? and ?"
  186. err = global.DEFAULT_DmSQL.Exec(sql1, now, now, startDate, endDate).Error
  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 sold_short_name !=' ' and data_time between ? and ?"
  192. err = global.DEFAULT_DmSQL.Exec(sql2, now, now, startDate, endDate).Error
  193. return
  194. }
  195. func MultiInsertTradeBaseDataToTopByClassify(exchange string, startDate, endDate string, classifyNames, classifyTypes []string) (err error) {
  196. now := time.Now().Format(utils.FormatDateTime)
  197. 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)" +
  198. "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)) + ")"
  199. err = global.DEFAULT_DmSQL.Exec(sql1, now, now, startDate, endDate, classifyNames, classifyTypes).Error
  200. if err != nil {
  201. return
  202. }
  203. 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)" +
  204. "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)) + ")"
  205. err = global.DEFAULT_DmSQL.Exec(sql2, now, now, startDate, endDate, classifyNames, classifyTypes).Error
  206. return
  207. }
  208. func GetTradePositionTopOriginDataTimes(exchange string) (dates []string, err error) {
  209. sql := `SELECT DISTINCT data_time FROM base_from_trade_%s_index ORDER BY data_time ASC`
  210. sql = fmt.Sprintf(sql, exchange)
  211. err = global.DEFAULT_DmSQL.Raw(sql).Find(&dates).Error
  212. return
  213. }
  214. type BaseFromTradeClassify struct {
  215. Id uint64 `gorm:"column:id;primaryKey"` // `orm:"column(id);pk"`
  216. ClassifyName string //分类名称
  217. ClassifyType string //分类名称下的类型
  218. Exchange string //交易所
  219. LatestDate time.Time //数据最近的日期
  220. CreateTime time.Time //插入时间
  221. ModifyTime time.Time //修改时间
  222. }
  223. func GetAllBaseFromTradeClassify() (list []*BaseFromTradeClassify, err error) {
  224. sql := `SELECT * FROM base_from_trade_classify `
  225. err = global.DEFAULT_DmSQL.Raw(sql).Find(&list).Error
  226. return
  227. }
  228. func (m *BaseFromTradeClassify) Update(cols []string) (err error) {
  229. err = global.DEFAULT_DmSQL.Select(cols).Updates(m).Error
  230. return
  231. }
  232. func MultiAddBaseFromTradeClassify(items []*BaseFromTradeClassify) (err error) {
  233. if len(items) == 0 {
  234. return
  235. }
  236. err = global.DEFAULT_DmSQL.CreateInBatches(items, utils.MultiAddNum).Error
  237. return
  238. }
  239. type TradeClassifyName struct {
  240. ClassifyName string //分类名称
  241. ClassifyType string //分类名称下的类型
  242. DataTime time.Time //数据最近的日期
  243. ModifyTime time.Time //数据最近的日期
  244. }
  245. func GetExchangeClassify(exchange string) (list []TradeClassifyName, err error) {
  246. tableName := "trade_position_" + exchange + "_top"
  247. orderStr := "classify_name DESC, classify_type asc"
  248. if exchange == "zhengzhou" {
  249. orderStr = "classify_name asc"
  250. }
  251. 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 ,data_time , modify_time `
  252. sql += ` ORDER BY ` + orderStr
  253. err = global.DEFAULT_DmSQL.Raw(sql).Find(&list).Error
  254. return
  255. }
  256. func GetTradePositionTopCleanByExchangeDataTime(exchange string, startDate, endDate string) (list []*TradePositionTop, err error) {
  257. 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"
  258. err = global.DEFAULT_DmSQL.Raw(sql, startDate, endDate).Find(&list).Error
  259. return
  260. }
  261. func GetTradePositionTopCleanByExchangeDataTimeClassify(exchange string, startDate, endDate string, classifyNames, classifyTypes []string) (list []*TradePositionTop, err error) {
  262. 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`
  263. err = global.DEFAULT_DmSQL.Raw(sql, startDate, endDate, classifyNames, classifyTypes).Find(&list).Error
  264. return
  265. }
  266. func MultiUpdatePositionTopChangeVal(exchange string, updates []UpdateChangeVal) (err error) {
  267. sql := `"UPDATE trade_position_" + exchange + "_top SET deal_change=?, modify_time=? WHERE id = ?"`
  268. for _, v := range updates {
  269. err = global.DEFAULT_DmSQL.Exec(sql, v.DealChange, v.ModifyTime, v.Id).Error
  270. if err != nil {
  271. return
  272. }
  273. }
  274. return
  275. }
  276. func GetTradePositionOriginClassifyCountByExchangeDataTime(exchange string, startDate, endDate string) (count int64, err error) {
  277. 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 <= ?"
  278. err = global.DEFAULT_DmSQL.Raw(sql, startDate, endDate).Scan(&count).Error
  279. return
  280. }
  281. func GetTradePositionTopClassifyCountByExchangeDataTime(exchange string, startDate, endDate string) (count int64, err error) {
  282. 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) "
  283. err = global.DEFAULT_DmSQL.Raw(sql, startDate, endDate).Scan(&count).Error
  284. return
  285. }
  286. type TradePositionClassifyInfo struct {
  287. ClassifyName string //分类名称
  288. ClassifyType string //分类名称下的类型
  289. }
  290. func GetTradePositionOriginClassifyByExchangeDataTime(exchange string, startDate, endDate string) (list []TradePositionClassifyInfo, err error) {
  291. 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 <= ?"
  292. err = global.DEFAULT_DmSQL.Raw(sql, startDate, endDate).Find(&list).Error
  293. return
  294. }
  295. func GetTradePositionTopClassifyByExchangeDataTime(exchange string, startDate, endDate string) (list []TradePositionClassifyInfo, err error) {
  296. sql := "SELECT DISTINCT classify_name, classify_type FROM trade_position_" + exchange + "_top where data_time >= ? and data_time <= ? and deal_type in (1,2) "
  297. err = global.DEFAULT_DmSQL.Raw(sql, startDate, endDate).Find(&list).Error
  298. return
  299. }
  300. func DeleteTradePositionTopAllByExchangeDataTime(exchange string, startDate, endDate string) (err error) {
  301. sql := "DELETE FROM trade_position_" + exchange + "_top where data_time >= ? and data_time <= ? "
  302. err = global.DEFAULT_DmSQL.Exec(sql, startDate, endDate).Error
  303. return
  304. }
  305. type GetFirstBaseFromTradeIndeDate struct {
  306. DataTime string
  307. }
  308. func GetFirstBaseFromTradeIndexByDate(exchange string) (item *GetFirstBaseFromTradeIndeDate, err error) {
  309. sql := "SELECT * FROM base_from_trade_" + exchange + "_index where `rank` < 50 order by data_time asc"
  310. err = global.DEFAULT_DmSQL.Raw(sql).Find(&item).Error
  311. return
  312. }