quote.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. package models
  2. import (
  3. "errors"
  4. "fmt"
  5. "mtp2_if/db"
  6. "time"
  7. "gopkg.in/mgo.v2/bson"
  8. )
  9. // CycleType 周期类型
  10. type CycleType int
  11. const (
  12. // CycleTypeSecond 周期类型 - 秒
  13. CycleTypeSecond CycleType = 0
  14. // CycleTypeMinutes1 周期类型 - 1分钟
  15. CycleTypeMinutes1 CycleType = 1
  16. // CycleTypeMinutes5 周期类型 - 5分钟
  17. CycleTypeMinutes5 CycleType = 2
  18. // CycleTypeMinutes30 周期类型 - 30分钟
  19. CycleTypeMinutes30 CycleType = 3
  20. // CycleTypeMinutes60 周期类型 - 60分钟
  21. CycleTypeMinutes60 CycleType = 4
  22. // CycleTypeMinutes120 周期类型 - 2小时
  23. CycleTypeMinutes120 CycleType = 120
  24. // CycleTypeMinutes240 周期类型 - 4小时
  25. CycleTypeMinutes240 CycleType = 240
  26. // CycleTypeMinutesDay 周期类型 - 日线
  27. CycleTypeMinutesDay CycleType = 11
  28. // CycleTypeTik 周期类型 - Tik
  29. CycleTypeTik CycleType = 10
  30. )
  31. // CycleData MongoDB中历史数据模型
  32. type CycleData struct {
  33. ID bson.ObjectId `bson:"_id"` // id
  34. GC string `bson:"GC"` // 商品代码
  35. ST int `bson:"ST"` // 时间戳
  36. SST string `bson:"SST"` // 时间文本
  37. Open int `bson:"Open"` // 开盘价
  38. High int `bson:"High"` // 最高价
  39. Low int `bson:"Low"` // 最低价
  40. Close int `bson:"Close"` // 收盘价
  41. TV int `bson:"TV"` // 总量
  42. TT int `bson:"TT"` // 总金额
  43. HV int `bson:"HV"` // 持仓量
  44. SP int `bson:"SP"` // 结算价,日线周期(包括)以上才有
  45. }
  46. // Quoteday 行情盘面
  47. type Quoteday struct {
  48. Id int64 `xorm:"pk autoincr BIGINT(20)"`
  49. Exchangedate int64 `xorm:"not null BIGINT(20)"`
  50. Goodscode string `xorm:"not null unique CHAR(10)"`
  51. Exchangecode int `xorm:"INT(11)"`
  52. Preclose int64 `xorm:"default 0 BIGINT(20)"`
  53. Opentime int64 `xorm:"BIGINT(20)"`
  54. Opened int64 `xorm:"not null default 0 BIGINT(20)"`
  55. Highest int64 `xorm:"not null default 0 BIGINT(20)"`
  56. Lowest int64 `xorm:"not null default 0 BIGINT(20)"`
  57. Lasttime string `xorm:"VARCHAR(20)"`
  58. Utclasttime int64 `xorm:"not null BIGINT(20)"`
  59. Last int64 `xorm:"not null BIGINT(20)"`
  60. Lastvolume int64 `xorm:"default 0 BIGINT(20)"`
  61. Lastturnover int64 `xorm:"default 0 BIGINT(20)"`
  62. Totalbidvolume int64 `xorm:"default 0 BIGINT(20)"`
  63. Totalaskvolume int64 `xorm:"default 0 BIGINT(20)"`
  64. Totalvolume int64 `xorm:"default 0 BIGINT(20)"`
  65. Totalturnover int64 `xorm:"default 0 BIGINT(20)"`
  66. Bid int64 `xorm:"default 0 BIGINT(20)"`
  67. Bid2 int64 `xorm:"default 0 BIGINT(20)"`
  68. Bid3 int64 `xorm:"default 0 BIGINT(20)"`
  69. Bid4 int64 `xorm:"default 0 BIGINT(20)"`
  70. Bid5 int64 `xorm:"default 0 BIGINT(20)"`
  71. Bidvolume int64 `xorm:"default 0 BIGINT(20)"`
  72. Bidvolume2 int64 `xorm:"default 0 BIGINT(20)"`
  73. Bidvolume3 int64 `xorm:"default 0 BIGINT(20)"`
  74. Bidvolume4 int64 `xorm:"default 0 BIGINT(20)"`
  75. Bidvolume5 int64 `xorm:"default 0 BIGINT(20)"`
  76. Ask int64 `xorm:"default 0 BIGINT(20)"`
  77. Ask2 int64 `xorm:"default 0 BIGINT(20)"`
  78. Ask3 int64 `xorm:"default 0 BIGINT(20)"`
  79. Ask4 int64 `xorm:"default 0 BIGINT(20)"`
  80. Ask5 int64 `xorm:"default 0 BIGINT(20)"`
  81. Askvolume int64 `xorm:"default 0 BIGINT(20)"`
  82. Askvolume2 int64 `xorm:"default 0 BIGINT(20)"`
  83. Askvolume3 int64 `xorm:"default 0 BIGINT(20)"`
  84. Askvolume4 int64 `xorm:"default 0 BIGINT(20)"`
  85. Askvolume5 int64 `xorm:"default 0 BIGINT(20)"`
  86. Presettle int64 `xorm:"default 0 BIGINT(20)"`
  87. Settle int64 `xorm:"default 0 BIGINT(20)"`
  88. Preholdvolume int64 `xorm:"default 0 BIGINT(20)"`
  89. Holdvolume int64 `xorm:"default 0 BIGINT(20)"`
  90. Averageprice int64 `xorm:"default 0 BIGINT(20)"`
  91. Orderid int64 `xorm:"default 0 BIGINT(20)"`
  92. Limitup int64 `xorm:"default 0 BIGINT(20)"`
  93. Limitdown int64 `xorm:"default 0 BIGINT(20)"`
  94. Inventory int64 `xorm:"default 0 BIGINT(20)"`
  95. Holdincrement int64 `xorm:"default 0 BIGINT(20)"`
  96. Iscleared int `xorm:"INT(11)"`
  97. Issettled int `xorm:"INT(11)"`
  98. Hightime string `xorm:"VARCHAR(30)"`
  99. Lowtime string `xorm:"VARCHAR(30)"`
  100. Bidqueueinfo string `xorm:"VARCHAR(2000)"`
  101. Askqueueinfo string `xorm:"VARCHAR(2000)"`
  102. Bidorderid int64 `xorm:"BIGINT(20)"`
  103. Bidorderid2 int64 `xorm:"BIGINT(20)"`
  104. Bidorderid3 int64 `xorm:"BIGINT(20)"`
  105. Bidorderid4 int64 `xorm:"BIGINT(20)"`
  106. Bidorderid5 int64 `xorm:"BIGINT(20)"`
  107. Askorderid int64 `xorm:"BIGINT(20)"`
  108. Askorderid2 int64 `xorm:"BIGINT(20)"`
  109. Askorderid3 int64 `xorm:"BIGINT(20)"`
  110. Askorderid4 int64 `xorm:"BIGINT(20)"`
  111. Askorderid5 int64 `xorm:"BIGINT(20)"`
  112. Originalturnover float64 `xorm:"default 0 DOUBLE"`
  113. Lastlot int64 `xorm:"BIGINT(20)"`
  114. Totallot int64 `xorm:"BIGINT(20)"`
  115. Strikeprice int64 `xorm:"BIGINT(20)"`
  116. Cleartime int64 `xorm:"BIGINT(20)"`
  117. Calloptionpremiums int64 `xorm:"default 0 BIGINT(20)"`
  118. Calloptionpremiums2 int64 `xorm:"default 0 BIGINT(20)"`
  119. Calloptionpremiums3 int64 `xorm:"default 0 BIGINT(20)"`
  120. Calloptionpremiums4 int64 `xorm:"default 0 BIGINT(20)"`
  121. Calloptionpremiums5 int64 `xorm:"default 0 BIGINT(20)"`
  122. Putoptionpremiums int64 `xorm:"default 0 BIGINT(20)"`
  123. Putoptionpremiums2 int64 `xorm:"default 0 BIGINT(20)"`
  124. Putoptionpremiums3 int64 `xorm:"default 0 BIGINT(20)"`
  125. Putoptionpremiums4 int64 `xorm:"default 0 BIGINT(20)"`
  126. Putoptionpremiums5 int64 `xorm:"default 0 BIGINT(20)"`
  127. Nontotalvolume int64 `xorm:"default 0 BIGINT(20)"`
  128. Nontotalholdervolume int64 `xorm:"default 0 BIGINT(20)"`
  129. Nontotalturnover int64 `xorm:"default 0 BIGINT(20)"`
  130. Nontotallot int64 `xorm:"default 0 BIGINT(20)"`
  131. Markprice int64 `xorm:"default 0 BIGINT(20)"`
  132. Fundsrate int64 `xorm:"default 0 BIGINT(20)"`
  133. Publictradetype string `xorm:"VARCHAR(2)"`
  134. Iep int64 `xorm:"default 0 BIGINT(20)"`
  135. Iev int64 `xorm:"default 0 BIGINT(20)"`
  136. Grepmarketprice int64 `xorm:"default 0 BIGINT(20)"`
  137. Bid6 int64 `xorm:"default 0 BIGINT(20)"`
  138. Bid7 int64 `xorm:"default 0 BIGINT(20)"`
  139. Bid8 int64 `xorm:"default 0 BIGINT(20)"`
  140. Bid9 int64 `xorm:"default 0 BIGINT(20)"`
  141. Bid10 int64 `xorm:"default 0 BIGINT(20)"`
  142. Bidvolume6 int64 `xorm:"default 0 BIGINT(20)"`
  143. Bidvolume7 int64 `xorm:"default 0 BIGINT(20)"`
  144. Bidvolume8 int64 `xorm:"default 0 BIGINT(20)"`
  145. Bidvolume9 int64 `xorm:"default 0 BIGINT(20)"`
  146. Bidvolume10 int64 `xorm:"default 0 BIGINT(20)"`
  147. Ask6 int64 `xorm:"default 0 BIGINT(20)"`
  148. Ask7 int64 `xorm:"default 0 BIGINT(20)"`
  149. Ask8 int64 `xorm:"default 0 BIGINT(20)"`
  150. Ask9 int64 `xorm:"default 0 BIGINT(20)"`
  151. Ask10 int64 `xorm:"default 0 BIGINT(20)"`
  152. Askvolume6 int64 `xorm:"default 0 BIGINT(20)"`
  153. Askvolume7 int64 `xorm:"default 0 BIGINT(20)"`
  154. Askvolume8 int64 `xorm:"default 0 BIGINT(20)"`
  155. Askvolume9 int64 `xorm:"default 0 BIGINT(20)"`
  156. Askvolume10 int64 `xorm:"default 0 BIGINT(20)"`
  157. Bidordervolume int64 `xorm:"default 0 BIGINT(20)"`
  158. Bidordervolume2 int64 `xorm:"default 0 BIGINT(20)"`
  159. Bidordervolume3 int64 `xorm:"default 0 BIGINT(20)"`
  160. Bidordervolume4 int64 `xorm:"default 0 BIGINT(20)"`
  161. Bidordervolume5 int64 `xorm:"default 0 BIGINT(20)"`
  162. Bidordervolume6 int64 `xorm:"default 0 BIGINT(20)"`
  163. Bidordervolume7 int64 `xorm:"default 0 BIGINT(20)"`
  164. Bidordervolume8 int64 `xorm:"default 0 BIGINT(20)"`
  165. Bidordervolume9 int64 `xorm:"default 0 BIGINT(20)"`
  166. Bidordervolume10 int64 `xorm:"default 0 BIGINT(20)"`
  167. Askordervolume int64 `xorm:"default 0 BIGINT(20)"`
  168. Askordervolume2 int64 `xorm:"default 0 BIGINT(20)"`
  169. Askordervolume3 int64 `xorm:"default 0 BIGINT(20)"`
  170. Askordervolume4 int64 `xorm:"default 0 BIGINT(20)"`
  171. Askordervolume5 int64 `xorm:"default 0 BIGINT(20)"`
  172. Askordervolume6 int64 `xorm:"default 0 BIGINT(20)"`
  173. Askordervolume7 int64 `xorm:"default 0 BIGINT(20)"`
  174. Askordervolume8 int64 `xorm:"default 0 BIGINT(20)"`
  175. Askordervolume9 int64 `xorm:"default 0 BIGINT(20)"`
  176. Askordervolume10 int64 `xorm:"default 0 BIGINT(20)"`
  177. }
  178. // GetHistoryCycleDatas 获取历史数据
  179. // 参数 cycleType CycleType 周期类型
  180. // 参数 goodsCode string 商品代码
  181. // 参数 startTime time.Time 开始时间(闭区间)
  182. // 参数 endTime time.Time 结束时间(闭区间)
  183. // 参数 count int 条数
  184. // 返回值 []CycleData 历史数据
  185. // 返回值 error 错误
  186. func GetHistoryCycleDatas(cycleType CycleType, goodsCode string, startTime, endTime time.Time, count int, isAscForST bool) ([]CycleData, error) {
  187. db := db.GetMongoDB()
  188. // 获取目标Collection
  189. collection := "mincycle"
  190. switch cycleType {
  191. case CycleTypeSecond:
  192. collection = "quotetik"
  193. case CycleTypeMinutes1:
  194. collection = "mincycle"
  195. case CycleTypeMinutes5:
  196. collection = "min5cycle"
  197. case CycleTypeMinutes30:
  198. collection = "min30cycle"
  199. default:
  200. return nil, errors.New("不支持的周期类型")
  201. }
  202. c := db.C(collection)
  203. // 按时间排序
  204. sort := "-ST"
  205. if isAscForST {
  206. sort = "ST"
  207. }
  208. // 查询数据
  209. var cycleDatas []CycleData
  210. if err := c.Find(bson.M{
  211. "GC": goodsCode,
  212. "ST": bson.M{"$gte": startTime.Unix(), "$lte": endTime.Unix()},
  213. }).Limit(count).Sort(sort).All(&cycleDatas); err != nil {
  214. return nil, err
  215. }
  216. return cycleDatas, nil
  217. }
  218. // GetQuoteDays 获取目标商品的盘面数据
  219. // 参数 goodsCodes string 商品代码字串,以“,”分隔
  220. // 返回 []Quoteday 盘面数据
  221. // 返回 error error
  222. func GetQuoteDays(goodsCodes string) ([]Quoteday, error) {
  223. engine := db.GetMySQLEngine()
  224. datas := make([]Quoteday, 0)
  225. if err := engine.Where(fmt.Sprintf("goodscode in ('%s')", goodsCodes)).Find(&datas); err != nil {
  226. return nil, err
  227. }
  228. return datas, nil
  229. }