quote.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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 `json:"-" bson:"_id"` // id
  34. GC string `bson:"GC"` // 商品代码
  35. ST int `json:"-" 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. // TableName is Quoteday
  179. func (*Quoteday) TableName() string {
  180. return "Quoteday"
  181. }
  182. // GetHistoryCycleDatas 获取历史数据
  183. // 参数 cycleType CycleType 周期类型
  184. // 参数 goodsCode string 商品代码
  185. // 参数 startTime time.Time 开始时间(闭区间)
  186. // 参数 endTime time.Time 结束时间(闭区间)
  187. // 参数 count int 条数
  188. // 参数 isAscForST bool 是否按时间顺序排序,默认为时间倒序排序
  189. // 返回值 []CycleData 历史数据
  190. // 返回值 error 错误
  191. func GetHistoryCycleDatas(cycleType CycleType, goodsCode string, startTime, endTime *time.Time, count int, isAscForST bool) ([]CycleData, error) {
  192. db := db.GetMongoDB()
  193. // 获取目标Collection
  194. collection := "mincycle"
  195. switch cycleType {
  196. case CycleTypeSecond:
  197. collection = "quotetik"
  198. case CycleTypeMinutes1:
  199. collection = "mincycle"
  200. case CycleTypeMinutes5:
  201. collection = "min5cycle"
  202. case CycleTypeMinutes30:
  203. collection = "min30cycle"
  204. case CycleTypeMinutesDay:
  205. collection = "daycycle"
  206. default:
  207. return nil, errors.New("不支持的周期类型")
  208. }
  209. c := db.C(collection)
  210. // 按时间排序
  211. sort := "-ST"
  212. if isAscForST {
  213. sort = "ST"
  214. }
  215. // 查询数据
  216. var cycleDatas []CycleData
  217. m := bson.M{"GC": goodsCode}
  218. if startTime != nil && endTime == nil {
  219. m["ST"] = bson.M{"$gte": startTime.Unix()}
  220. } else if startTime == nil && endTime != nil {
  221. m["ST"] = bson.M{"$lte": endTime.Unix()}
  222. } else if startTime != nil && endTime != nil {
  223. m["ST"] = bson.M{"$gte": startTime.Unix(), "$lte": endTime.Unix()}
  224. }
  225. query := c.Find(m)
  226. if count > 0 {
  227. query = query.Limit(count)
  228. }
  229. if err := query.Sort(sort).All(&cycleDatas); err != nil {
  230. return nil, err
  231. }
  232. return cycleDatas, nil
  233. }
  234. // GetQuoteDays 获取目标商品的盘面数据
  235. // 参数 goodsCodes string 商品代码字串,以“,”分隔
  236. // 返回 []Quoteday 盘面数据
  237. // 返回 error error
  238. func GetQuoteDays(goodsCodes string) ([]Quoteday, error) {
  239. engine := db.GetMySQLEngine()
  240. datas := make([]Quoteday, 0)
  241. if err := engine.Where(fmt.Sprintf("goodscode in (%s)", goodsCodes)).Find(&datas); err != nil {
  242. return nil, err
  243. }
  244. return datas, nil
  245. }