quote.go 11 KB

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