trade_position_analysis.go 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528
  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. // 持仓榜单表
  10. type TradePositionTop struct {
  11. Id uint64 `gorm:"column:id;primaryKey"` //`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. err = global.DEFAULT_DmSQL.CreateInBatches(list, utils.MultiAddNum).Error
  52. return
  53. } else if exchange == "zhengzhou" {
  54. list := make([]*TradePositionZhengzhouTop, 0)
  55. for _, v := range items {
  56. tmp := (*TradePositionZhengzhouTop)(unsafe.Pointer(v))
  57. list = append(list, tmp)
  58. }
  59. //_, err = o.InsertMulti(len(list), list)
  60. err = global.DEFAULT_DmSQL.CreateInBatches(list, utils.MultiAddNum).Error
  61. return
  62. } else if exchange == "cffex" {
  63. list := make([]*TradePositionCffexTop, 0)
  64. for _, v := range items {
  65. tmp := (*TradePositionCffexTop)(unsafe.Pointer(v))
  66. list = append(list, tmp)
  67. }
  68. //_, err = o.InsertMulti(len(list), list)
  69. err = global.DEFAULT_DmSQL.CreateInBatches(list, utils.MultiAddNum).Error
  70. return
  71. } else if exchange == "shanghai" {
  72. list := make([]*TradePositionShanghaiTop, 0)
  73. for _, v := range items {
  74. tmp := (*TradePositionShanghaiTop)(unsafe.Pointer(v))
  75. list = append(list, tmp)
  76. }
  77. //_, err = o.InsertMulti(len(list), list)
  78. err = global.DEFAULT_DmSQL.CreateInBatches(list, utils.MultiAddNum).Error
  79. return
  80. } else if exchange == "ine" {
  81. list := make([]*TradePositionIneTop, 0)
  82. for _, v := range items {
  83. tmp := (*TradePositionIneTop)(unsafe.Pointer(v))
  84. list = append(list, tmp)
  85. }
  86. //_, err = o.InsertMulti(len(list), list)
  87. err = global.DEFAULT_DmSQL.CreateInBatches(list, utils.MultiAddNum).Error
  88. return
  89. } else if exchange == "guangzhou" {
  90. list := make([]*TradePositionGuangzhouTop, 0)
  91. for _, v := range items {
  92. tmp := (*TradePositionGuangzhouTop)(unsafe.Pointer(v))
  93. list = append(list, tmp)
  94. }
  95. //_, err = o.InsertMulti(len(list), list)
  96. err = global.DEFAULT_DmSQL.CreateInBatches(list, utils.MultiAddNum).Error
  97. return
  98. }
  99. return
  100. }
  101. func GetTradePositionTopByExchangeDataTime(exchange string, startDate, endDate 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) ORDER BY classify_name, classify_type, deal_type, data_time, deal_value desc"
  104. //_, err = o.Raw(sql, startDate, endDate).QueryRows(&list)
  105. 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"
  106. err = global.DEFAULT_DmSQL.Raw(sql, startDate, endDate).Find(&list).Error
  107. return
  108. }
  109. func GetTradePositionTopByExchangeDataTimeByClassify(exchange string, startDate, endDate string, classifyNames, classifyTypes []string) (list []*TradePositionTop, err error) {
  110. //o := orm.NewOrm()
  111. //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`
  112. //_, err = o.Raw(sql, startDate, endDate, classifyNames, classifyTypes).QueryRows(&list)
  113. 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`
  114. err = global.DEFAULT_DmSQL.Raw(sql, startDate, endDate, classifyNames, classifyTypes).Find(&list).Error
  115. return
  116. }
  117. func GetTradePositionTopCountByExchangeDataTime(exchange string, startDate, endDate string) (count int64, err error) {
  118. //o := orm.NewOrm()
  119. //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"
  120. //err = o.Raw(sql, startDate, endDate).QueryRow(&count)
  121. 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"
  122. err = global.DEFAULT_DmSQL.Raw(sql, startDate, endDate).Scan(&count).Error
  123. return
  124. }
  125. func GetTradePositionTopByExchangeSourceType(exchange string, dataTime string, sourceType int) (list []*TradePositionTop, err error) {
  126. //o := orm.NewOrm()
  127. //sql := "SELECT * FROM trade_position_" + exchange + "_top where data_time= ? and source_type = ? ORDER BY classify_name, classify_type, deal_type, deal_value desc"
  128. //_, err = o.Raw(sql, dataTime, sourceType).QueryRows(&list)
  129. sql := "SELECT * FROM trade_position_" + exchange + "_top where data_time= ? and source_type = ? ORDER BY classify_name, classify_type, deal_type, deal_value desc"
  130. err = global.DEFAULT_DmSQL.Raw(sql, dataTime, sourceType).Find(&list).Error
  131. return
  132. }
  133. func GetTradePositionTopByExchangeSourceTypeClassify(exchange string, dataTime string, sourceType int, classifyNames, classifyTypes []string) (list []*TradePositionTop, err error) {
  134. //o := orm.NewOrm()
  135. //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`
  136. //_, err = o.Raw(sql, dataTime, sourceType, classifyNames, classifyTypes).QueryRows(&list)
  137. 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`
  138. err = global.DEFAULT_DmSQL.Raw(sql, dataTime, sourceType, classifyNames, classifyTypes).Find(&list).Error
  139. return
  140. }
  141. type TradeTopClassify struct {
  142. ClassifyName string //分类名称
  143. ClassifyType string //分类名称下的类型
  144. }
  145. type TradePositionSub struct {
  146. ClassifyName string //分类名称
  147. ClassifyType string //分类名称下的类型
  148. DataTime string //数据日期
  149. DealShortName string //成交量公司简称
  150. SubValue int //差值
  151. DealType int
  152. }
  153. type TradePositionSubList []*TradePositionSub
  154. func (v TradePositionSubList) Len() int {
  155. return len(v)
  156. }
  157. func (v TradePositionSubList) Swap(i, j int) {
  158. v[i], v[j] = v[j], v[i]
  159. }
  160. func (v TradePositionSubList) Less(i, j int) bool {
  161. return v[i].SubValue > v[j].SubValue
  162. }
  163. type UpdateDealValueChange struct {
  164. Id uint64
  165. DealValue int //成交量
  166. DealChange int
  167. SourceType int
  168. ModifyTime time.Time //修改时间
  169. }
  170. type UpdateChangeVal struct {
  171. Id uint64
  172. DealChange int
  173. ModifyTime time.Time //修改时间
  174. }
  175. func MultiUpdatePositionTop(exchange string, updates []UpdateDealValueChange) (err error) {
  176. //o := orm.NewOrm()
  177. //p, err := o.Raw("UPDATE trade_position_" + exchange + "_top SET deal_value=?, deal_change=?, source_type=?, modify_time=? WHERE id = ?").Prepare()
  178. //if err != nil {
  179. // return
  180. //}
  181. //defer func() {
  182. // _ = p.Close() // 别忘记关闭 statement
  183. //}()
  184. //for _, v := range updates {
  185. // _, err = p.Exec(v.DealValue, v.DealChange, v.SourceType, v.ModifyTime, v.Id)
  186. // if err != nil {
  187. // return
  188. // }
  189. //}
  190. sql := `"UPDATE trade_position_" + exchange + "_top SET deal_value=?, deal_change=?, source_type=?, modify_time=? WHERE id = ?"`
  191. for _, v := range updates {
  192. err = global.DEFAULT_DmSQL.Exec(sql, v.DealValue, v.DealChange, v.SourceType, v.ModifyTime, v.Id).Error
  193. if err != nil {
  194. return
  195. }
  196. }
  197. return
  198. }
  199. func DeletePositionTopByDataTime(exchange string, dataTime string, dealType int) (err error) {
  200. //o := orm.NewOrm()
  201. //sql := "delete from trade_position_" + exchange + "_top WHERE data_time=? and deal_type=?"
  202. //_, err = o.Raw(sql, dataTime, dealType).Exec()
  203. sql := "delete from trade_position_" + exchange + "_top WHERE data_time=? and deal_type=?"
  204. err = global.DEFAULT_DmSQL.Exec(sql, dataTime, dealType).Error
  205. return
  206. }
  207. func DeletePositionTopByDataTimeClassify(exchange string, dataTime string, dealType int, classifyNames, classifyTypes []string) (err error) {
  208. //o := orm.NewOrm()
  209. //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)) + `)`
  210. //_, err = o.Raw(sql, dataTime, dealType, classifyNames, classifyTypes).Exec()
  211. 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)) + `)`
  212. err = global.DEFAULT_DmSQL.Exec(sql, dataTime, dealType, classifyNames, classifyTypes).Error
  213. return
  214. }
  215. func GetTradePositionTopByExchangeDataTimeType(exchange string, dataTime string, dealType int) (list []TradePositionTop, err error) {
  216. //o := orm.NewOrm()
  217. //sql := "select * from trade_position_" + exchange + "_top WHERE data_time=? and deal_type=?"
  218. //_, err = o.Raw(sql, dataTime, dealType).QueryRows(&list)
  219. sql := "select * from trade_position_" + exchange + "_top WHERE data_time=? and deal_type=?"
  220. err = global.DEFAULT_DmSQL.Raw(sql, dataTime, dealType).Find(&list).Error
  221. return
  222. }
  223. func GetTradePositionTopByExchangeDataTimeTypeClassify(exchange string, dataTime string, dealType int, classifyNames, classifyTypes []string) (list []TradePositionTop, err error) {
  224. //o := orm.NewOrm()
  225. //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)) + `)`
  226. //_, err = o.Raw(sql, dataTime, dealType, classifyNames, classifyTypes).QueryRows(&list)
  227. 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)) + `)`
  228. err = global.DEFAULT_DmSQL.Raw(sql, dataTime, dealType, classifyNames, classifyTypes).Find(&list).Error
  229. return
  230. }
  231. func MultiInsertTradeBaseDataToTop(exchange string, startDate, endDate string) (err error) {
  232. //o := orm.NewOrm()
  233. //now := time.Now().Format(utils.FormatDateTime)
  234. //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) " +
  235. // "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 ?"
  236. //_, err = o.Raw(sql1, now, now, startDate, endDate).Exec()
  237. //if err != nil {
  238. // return
  239. //}
  240. //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) " +
  241. // "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 ?"
  242. //_, err = o.Raw(sql2, now, now, startDate, endDate).Exec()
  243. now := time.Now().Format(utils.FormatDateTime)
  244. 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) " +
  245. "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 ?"
  246. err = global.DEFAULT_DmSQL.Exec(sql1, now, now, startDate, endDate).Error
  247. if err != nil {
  248. return
  249. }
  250. 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) " +
  251. "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 ?"
  252. err = global.DEFAULT_DmSQL.Exec(sql2, now, now, startDate, endDate).Error
  253. return
  254. }
  255. func MultiInsertTradeBaseDataToTopByClassify(exchange string, startDate, endDate string, classifyNames, classifyTypes []string) (err error) {
  256. //o := orm.NewOrm()
  257. //now := time.Now().Format(utils.FormatDateTime)
  258. //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)" +
  259. // "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)) + ")"
  260. //_, err = o.Raw(sql1, now, now, startDate, endDate, classifyNames, classifyTypes).Exec()
  261. //if err != nil {
  262. // return
  263. //}
  264. //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)" +
  265. // "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)) + ")"
  266. //_, err = o.Raw(sql2, now, now, startDate, endDate, classifyNames, classifyTypes).Exec()
  267. now := time.Now().Format(utils.FormatDateTime)
  268. 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)" +
  269. "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)) + ")"
  270. err = global.DEFAULT_DmSQL.Exec(sql1, now, now, startDate, endDate, classifyNames, classifyTypes).Error
  271. if err != nil {
  272. return
  273. }
  274. 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)" +
  275. "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)) + ")"
  276. err = global.DEFAULT_DmSQL.Exec(sql2, now, now, startDate, endDate, classifyNames, classifyTypes).Error
  277. return
  278. }
  279. // GetTradePositionTopOriginDataTimes 获取榜单原始数据日期-正序
  280. func GetTradePositionTopOriginDataTimes(exchange string) (dates []string, err error) {
  281. //o := orm.NewOrm()
  282. //sql := `SELECT DISTINCT data_time FROM base_from_trade_%s_index ORDER BY data_time ASC`
  283. //sql = fmt.Sprintf(sql, exchange)
  284. //_, err = o.Raw(sql).QueryRows(&dates)
  285. sql := `SELECT DISTINCT data_time FROM base_from_trade_%s_index ORDER BY data_time ASC`
  286. sql = fmt.Sprintf(sql, exchange)
  287. err = global.DEFAULT_DmSQL.Raw(sql).Find(&dates).Error
  288. return
  289. }
  290. // BaseFromTradeClassify 交易所分类表
  291. type BaseFromTradeClassify struct {
  292. Id uint64 `gorm:"column:id;primaryKey"` // `orm:"column(id);pk"`
  293. ClassifyName string //分类名称
  294. ClassifyType string //分类名称下的类型
  295. Exchange string //交易所
  296. LatestDate time.Time //数据最近的日期
  297. CreateTime time.Time //插入时间
  298. ModifyTime time.Time //修改时间
  299. }
  300. // GetAllBaseFromTradeClassify 获取所有的交易所分类列表
  301. func GetAllBaseFromTradeClassify() (list []*BaseFromTradeClassify, err error) {
  302. //sql := `SELECT * FROM base_from_trade_classify `
  303. //
  304. //o := orm.NewOrm()
  305. //_, err = o.Raw(sql).QueryRows(&list)
  306. sql := `SELECT * FROM base_from_trade_classify `
  307. err = global.DEFAULT_DmSQL.Raw(sql).Find(&list).Error
  308. return
  309. }
  310. // Update 更新
  311. func (m *BaseFromTradeClassify) Update(cols []string) (err error) {
  312. //o := orm.NewOrm()
  313. //_, err = o.Update(m, cols...)
  314. err = global.DEFAULT_DmSQL.Select(cols).Updates(m).Error
  315. return
  316. }
  317. // MultiAddBaseFromTradeClassify 批量插入交易所分类
  318. func MultiAddBaseFromTradeClassify(items []*BaseFromTradeClassify) (err error) {
  319. if len(items) == 0 {
  320. return
  321. }
  322. //o := orm.NewOrm()
  323. //_, err = o.InsertMulti(len(items), items)
  324. err = global.DEFAULT_DmSQL.CreateInBatches(items, utils.MultiAddNum).Error
  325. return
  326. }
  327. type TradeClassifyName struct {
  328. ClassifyName string //分类名称
  329. ClassifyType string //分类名称下的类型
  330. DataTime time.Time //数据最近的日期
  331. ModifyTime time.Time //数据最近的日期
  332. }
  333. // GetExchangeClassify 获取交易所分类列表
  334. func GetExchangeClassify(exchange string) (list []TradeClassifyName, err error) {
  335. //tableName := "trade_position_" + exchange + "_top"
  336. //orderStr := "classify_name DESC, classify_type asc"
  337. //if exchange == "zhengzhou" {
  338. // orderStr = "classify_name asc"
  339. //}
  340. //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 `
  341. //sql += ` ORDER BY ` + orderStr
  342. //
  343. //o := orm.NewOrm()
  344. //_, err = o.Raw(sql).QueryRows(&list)
  345. tableName := "trade_position_" + exchange + "_top"
  346. orderStr := "classify_name DESC, classify_type asc"
  347. if exchange == "zhengzhou" {
  348. orderStr = "classify_name asc"
  349. }
  350. 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 `
  351. sql += ` ORDER BY ` + orderStr
  352. err = global.DEFAULT_DmSQL.Raw(sql).Find(&list).Error
  353. return
  354. }
  355. // GetTradePositionTopCleanByExchangeDataTime 根据时间查询净多单和净空单的值
  356. func GetTradePositionTopCleanByExchangeDataTime(exchange string, startDate, endDate string) (list []*TradePositionTop, err error) {
  357. //o := orm.NewOrm()
  358. //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"
  359. //_, err = o.Raw(sql, startDate, endDate).QueryRows(&list)
  360. 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"
  361. err = global.DEFAULT_DmSQL.Raw(sql, startDate, endDate).Find(&list).Error
  362. return
  363. }
  364. // GetTradePositionTopCleanByExchangeDataTimeClassify 根据时间查询净多单和净空单的值
  365. func GetTradePositionTopCleanByExchangeDataTimeClassify(exchange string, startDate, endDate string, classifyNames, classifyTypes []string) (list []*TradePositionTop, err error) {
  366. //o := orm.NewOrm()
  367. //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`
  368. //_, err = o.Raw(sql, startDate, endDate, classifyNames, classifyTypes).QueryRows(&list)
  369. 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`
  370. err = global.DEFAULT_DmSQL.Raw(sql, startDate, endDate, classifyNames, classifyTypes).Find(&list).Error
  371. return
  372. }
  373. // MultiUpdatePositionTopChangeVal 批量更新榜单里变化量的值
  374. func MultiUpdatePositionTopChangeVal(exchange string, updates []UpdateChangeVal) (err error) {
  375. //o := orm.NewOrm()
  376. //p, err := o.Raw("UPDATE trade_position_" + exchange + "_top SET deal_change=?, modify_time=? WHERE id = ?").Prepare()
  377. //if err != nil {
  378. // return
  379. //}
  380. //defer func() {
  381. // _ = p.Close() // 别忘记关闭 statement
  382. //}()
  383. //for _, v := range updates {
  384. // _, err = p.Exec(v.DealChange, v.ModifyTime, v.Id)
  385. // if err != nil {
  386. // return
  387. // }
  388. //}
  389. sql := `"UPDATE trade_position_" + exchange + "_top SET deal_change=?, modify_time=? WHERE id = ?"`
  390. for _, v := range updates {
  391. err = global.DEFAULT_DmSQL.Exec(sql, v.DealChange, v.ModifyTime, v.Id).Error
  392. if err != nil {
  393. return
  394. }
  395. }
  396. return
  397. }
  398. func GetTradePositionOriginClassifyCountByExchangeDataTime(exchange string, startDate, endDate string) (count int64, err error) {
  399. //o := orm.NewOrm()
  400. //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 <= ?"
  401. //err = o.Raw(sql, startDate, endDate).QueryRow(&count)
  402. 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 <= ?"
  403. err = global.DEFAULT_DmSQL.Raw(sql, startDate, endDate).Scan(&count).Error
  404. return
  405. }
  406. func GetTradePositionTopClassifyCountByExchangeDataTime(exchange string, startDate, endDate string) (count int64, err error) {
  407. //o := orm.NewOrm()
  408. //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) "
  409. //err = o.Raw(sql, startDate, endDate).QueryRow(&count)
  410. 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) "
  411. err = global.DEFAULT_DmSQL.Raw(sql, startDate, endDate).Scan(&count).Error
  412. return
  413. }
  414. type TradePositionClassifyInfo struct {
  415. ClassifyName string //分类名称
  416. ClassifyType string //分类名称下的类型
  417. }
  418. func GetTradePositionOriginClassifyByExchangeDataTime(exchange string, startDate, endDate string) (list []TradePositionClassifyInfo, err error) {
  419. //o := orm.NewOrm()
  420. //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 <= ?"
  421. //_, err = o.Raw(sql, startDate, endDate).QueryRows(&list)
  422. 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 <= ?"
  423. err = global.DEFAULT_DmSQL.Raw(sql, startDate, endDate).Find(&list).Error
  424. return
  425. }
  426. func GetTradePositionTopClassifyByExchangeDataTime(exchange string, startDate, endDate string) (list []TradePositionClassifyInfo, err error) {
  427. //o := orm.NewOrm()
  428. //sql := "SELECT DISTINCT classify_name, classify_type FROM trade_position_" + exchange + "_top where data_time >= ? and data_time <= ? and deal_type in (1,2) "
  429. //_, err = o.Raw(sql, startDate, endDate).QueryRows(&list)
  430. sql := "SELECT DISTINCT classify_name, classify_type FROM trade_position_" + exchange + "_top where data_time >= ? and data_time <= ? and deal_type in (1,2) "
  431. err = global.DEFAULT_DmSQL.Raw(sql, startDate, endDate).Find(&list).Error
  432. return
  433. }
  434. // DeleteTradePositionTopAllByExchangeDataTime 删除计算数据
  435. func DeleteTradePositionTopAllByExchangeDataTime(exchange string, startDate, endDate string) (err error) {
  436. //o := orm.NewOrm()
  437. //sql := "DELETE FROM trade_position_" + exchange + "_top where data_time >= ? and data_time <= ? "
  438. //_, err = o.Raw(sql, startDate, endDate).Exec()
  439. sql := "DELETE FROM trade_position_" + exchange + "_top where data_time >= ? and data_time <= ? "
  440. err = global.DEFAULT_DmSQL.Exec(sql, startDate, endDate).Error
  441. return
  442. }
  443. type GetFirstBaseFromTradeIndeDate struct {
  444. DataTime string
  445. }
  446. func GetFirstBaseFromTradeIndexByDate(exchange string) (item *GetFirstBaseFromTradeIndeDate, err error) {
  447. //o := orm.NewOrm()
  448. //sql := "SELECT * FROM base_from_trade_" + exchange + "_index where `rank` < 50 order by data_time asc"
  449. //err = o.Raw(sql).QueryRow(&item)
  450. sql := "SELECT * FROM base_from_trade_" + exchange + "_index where `rank` < 50 order by data_time asc"
  451. err = global.DEFAULT_DmSQL.Raw(sql).Find(&item).Error
  452. return
  453. }