quote.go 11 KB

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