quote.go 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  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)"` // utc的行情时间
  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)"` // 买1
  68. Bid2 int64 `xorm:"default 0 BIGINT(20)"` // 买2
  69. Bid3 int64 `xorm:"default 0 BIGINT(20)"` // 买3
  70. Bid4 int64 `xorm:"default 0 BIGINT(20)"` // 买4
  71. Bid5 int64 `xorm:"default 0 BIGINT(20)"` // 买5
  72. Bidvolume int64 `xorm:"default 0 BIGINT(20)"` // 买量1
  73. Bidvolume2 int64 `xorm:"default 0 BIGINT(20)"` // 买量2
  74. Bidvolume3 int64 `xorm:"default 0 BIGINT(20)"` // 买量3
  75. Bidvolume4 int64 `xorm:"default 0 BIGINT(20)"` // 买量4
  76. Bidvolume5 int64 `xorm:"default 0 BIGINT(20)"` // 买量5
  77. Ask int64 `xorm:"default 0 BIGINT(20)"` // 卖1
  78. Ask2 int64 `xorm:"default 0 BIGINT(20)"` // 卖2
  79. Ask3 int64 `xorm:"default 0 BIGINT(20)"` // 卖3
  80. Ask4 int64 `xorm:"default 0 BIGINT(20)"` // 卖4
  81. Ask5 int64 `xorm:"default 0 BIGINT(20)"` // 卖5
  82. Askvolume int64 `xorm:"default 0 BIGINT(20)"` // 卖量1
  83. Askvolume2 int64 `xorm:"default 0 BIGINT(20)"` // 卖量2
  84. Askvolume3 int64 `xorm:"default 0 BIGINT(20)"` // 卖量3
  85. Askvolume4 int64 `xorm:"default 0 BIGINT(20)"` // 卖量4
  86. Askvolume5 int64 `xorm:"default 0 BIGINT(20)"` // 卖量5
  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. Bidqueueinfo string `xorm:"VARCHAR(2000)"` // 大利市买港股用
  100. Askqueueinfo string `xorm:"VARCHAR(2000)"` // 大利市卖港股用
  101. Bidorderid int64 `xorm:"BIGINT(20)"` // 买单号1
  102. Bidorderid2 int64 `xorm:"BIGINT(20)"` // 买单号2
  103. Bidorderid3 int64 `xorm:"BIGINT(20)"` // 买单号3
  104. Bidorderid4 int64 `xorm:"BIGINT(20)"` // 买单号4
  105. Bidorderid5 int64 `xorm:"BIGINT(20)"` // 买单号5
  106. Askorderid int64 `xorm:"BIGINT(20)"` // 卖单号1
  107. Askorderid2 int64 `xorm:"BIGINT(20)"` // 卖单号2
  108. Askorderid3 int64 `xorm:"BIGINT(20)"` // 卖单号3
  109. Askorderid4 int64 `xorm:"BIGINT(20)"` // 卖单号4
  110. Askorderid5 int64 `xorm:"BIGINT(20)"` // 卖单号5
  111. Lastlot int64 `xorm:"BIGINT(20)"` // 最新成交手数
  112. Totallot int64 `xorm:"BIGINT(20)"` // 总手数
  113. Strikeprice int64 `xorm:"BIGINT(20)"` // 发行价
  114. Cleartime int64 `xorm:"BIGINT(20)"` // 清盘时间
  115. Calloptionpremiums int64 `xorm:"default 0 BIGINT(20)"` // 认购期权1
  116. Calloptionpremiums2 int64 `xorm:"default 0 BIGINT(20)"` // 认购期权2
  117. Calloptionpremiums3 int64 `xorm:"default 0 BIGINT(20)"` // 认购期权3
  118. Calloptionpremiums4 int64 `xorm:"default 0 BIGINT(20)"` // 认购期权4
  119. Calloptionpremiums5 int64 `xorm:"default 0 BIGINT(20)"` // 认购期权5
  120. Putoptionpremiums int64 `xorm:"default 0 BIGINT(20)"` // 认沽期权1
  121. Putoptionpremiums2 int64 `xorm:"default 0 BIGINT(20)"` // 认沽期权2
  122. Putoptionpremiums3 int64 `xorm:"default 0 BIGINT(20)"` // 认沽期权3
  123. Putoptionpremiums4 int64 `xorm:"default 0 BIGINT(20)"` // 认沽期权4
  124. Putoptionpremiums5 int64 `xorm:"default 0 BIGINT(20)"` // 认沽期权5
  125. Nontotalvolume int64 `xorm:"default 0 BIGINT(20)"` // 非交易总量
  126. Nontotalholdervolume int64 `xorm:"default 0 BIGINT(20)"` // 非交易持仓量
  127. Nontotalturnover int64 `xorm:"default 0 BIGINT(20)"` // 非交易总金额
  128. Nontotallot int64 `xorm:"default 0 BIGINT(20)"` // 非交易总手数
  129. Publictradetype string `xorm:"VARCHAR(2)"` // 公共交易标志类型 港股专用
  130. Iep int64 `xorm:"default 0 BIGINT(20)"` // 平衡价 港股专用
  131. Iev int64 `xorm:"default 0 BIGINT(20)"` // 平衡量 港股专用
  132. Grepmarketprice int64 `xorm:"default 0 BIGINT(20)"` // 暗盘价 港股专用
  133. Bid6 int64 `xorm:"default 0 BIGINT(20)"` // 买6
  134. Bid7 int64 `xorm:"default 0 BIGINT(20)"` // 买7
  135. Bid8 int64 `xorm:"default 0 BIGINT(20)"` // 买8
  136. Bid9 int64 `xorm:"default 0 BIGINT(20)"` // 买9
  137. Bid10 int64 `xorm:"default 0 BIGINT(20)"` // 买10
  138. Bidvolume6 int64 `xorm:"default 0 BIGINT(20)"` // 买量6
  139. Bidvolume7 int64 `xorm:"default 0 BIGINT(20)"` // 买量7
  140. Bidvolume8 int64 `xorm:"default 0 BIGINT(20)"` // 买量8
  141. Bidvolume9 int64 `xorm:"default 0 BIGINT(20)"` // 买量9
  142. Bidvolume10 int64 `xorm:"default 0 BIGINT(20)"` // 买量10
  143. Ask6 int64 `xorm:"default 0 BIGINT(20)"` // 卖6
  144. Ask7 int64 `xorm:"default 0 BIGINT(20)"` // 卖7
  145. Ask8 int64 `xorm:"default 0 BIGINT(20)"` // 卖8
  146. Ask9 int64 `xorm:"default 0 BIGINT(20)"` // 卖9
  147. Ask10 int64 `xorm:"default 0 BIGINT(20)"` // 卖10
  148. Askvolume6 int64 `xorm:"default 0 BIGINT(20)"` // 卖量6
  149. Askvolume7 int64 `xorm:"default 0 BIGINT(20)"` // 卖量7
  150. Askvolume8 int64 `xorm:"default 0 BIGINT(20)"` // 卖量8
  151. Askvolume9 int64 `xorm:"default 0 BIGINT(20)"` // 卖量9
  152. Askvolume10 int64 `xorm:"default 0 BIGINT(20)"` // 卖量10
  153. Bidordervolume int64 `xorm:"default 0 BIGINT(20)"` // 买单量1
  154. Bidordervolume2 int64 `xorm:"default 0 BIGINT(20)"` // 买单量2
  155. Bidordervolume3 int64 `xorm:"default 0 BIGINT(20)"` // 买单量3
  156. Bidordervolume4 int64 `xorm:"default 0 BIGINT(20)"` // 买单量4
  157. Bidordervolume5 int64 `xorm:"default 0 BIGINT(20)"` // 买单量5
  158. Bidordervolume6 int64 `xorm:"default 0 BIGINT(20)"` // 买单量6
  159. Bidordervolume7 int64 `xorm:"default 0 BIGINT(20)"` // 买单量7
  160. Bidordervolume8 int64 `xorm:"default 0 BIGINT(20)"` // 买单量8
  161. Bidordervolume9 int64 `xorm:"default 0 BIGINT(20)"` // 买单量9
  162. Bidordervolume10 int64 `xorm:"default 0 BIGINT(20)"` // 买单量10
  163. Askordervolume int64 `xorm:"default 0 BIGINT(20)"` // 卖单量1
  164. Askordervolume2 int64 `xorm:"default 0 BIGINT(20)"` // 卖单量2
  165. Askordervolume3 int64 `xorm:"default 0 BIGINT(20)"` // 卖单量3
  166. Askordervolume4 int64 `xorm:"default 0 BIGINT(20)"` // 卖单量4
  167. Askordervolume5 int64 `xorm:"default 0 BIGINT(20)"` // 卖单量5
  168. Askordervolume6 int64 `xorm:"default 0 BIGINT(20)"` // 卖单量6
  169. Askordervolume7 int64 `xorm:"default 0 BIGINT(20)"` // 卖单量7
  170. Askordervolume8 int64 `xorm:"default 0 BIGINT(20)"` // 卖单量8
  171. Askordervolume9 int64 `xorm:"default 0 BIGINT(20)"` // 卖单量9
  172. Askordervolume10 int64 `xorm:"default 0 BIGINT(20)"` // 卖单量10
  173. }
  174. // TableName is Quoteday
  175. func (*Quoteday) TableName() string {
  176. return "quoteday"
  177. }
  178. // GetHistoryCycleDatas 获取历史数据
  179. // 参数 cycleType CycleType 周期类型
  180. // 参数 goodsCode string 商品代码
  181. // 参数 startTime time.Time 开始时间(闭区间)
  182. // 参数 endTime time.Time 结束时间(闭区间)
  183. // 参数 count int 条数
  184. // 参数 isAscForST bool 是否按时间顺序排序,默认为时间倒序排序
  185. // 返回值 []CycleData 历史数据
  186. // 返回值 error 错误
  187. func GetHistoryCycleDatas(cycleType CycleType, goodsCode string, startTime, endTime *time.Time, count int, isAscForST bool) ([]CycleData, error) {
  188. db := db.GetMongoDB()
  189. // 获取目标Collection
  190. collection := "mincycle"
  191. switch cycleType {
  192. case CycleTypeSecond:
  193. collection = "quotetik"
  194. case CycleTypeMinutes1:
  195. collection = "mincycle"
  196. case CycleTypeMinutes5:
  197. collection = "min5cycle"
  198. case CycleTypeMinutes30:
  199. collection = "min30cycle"
  200. case CycleTypeMinutesDay:
  201. collection = "daycycle"
  202. default:
  203. return nil, errors.New("不支持的周期类型")
  204. }
  205. c := db.C(collection)
  206. // 按时间排序
  207. sort := "-ST"
  208. if isAscForST {
  209. sort = "ST"
  210. }
  211. // 查询数据
  212. var cycleDatas []CycleData
  213. m := bson.M{"GC": goodsCode}
  214. if startTime != nil && endTime == nil {
  215. m["ST"] = bson.M{"$gte": startTime.Unix()}
  216. } else if startTime == nil && endTime != nil {
  217. m["ST"] = bson.M{"$lte": endTime.Unix()}
  218. } else if startTime != nil && endTime != nil {
  219. m["ST"] = bson.M{"$gte": startTime.Unix(), "$lte": endTime.Unix()}
  220. }
  221. query := c.Find(m)
  222. if count > 0 {
  223. query = query.Limit(count)
  224. }
  225. if err := query.Sort(sort).All(&cycleDatas); err != nil {
  226. return nil, err
  227. }
  228. return cycleDatas, nil
  229. }
  230. // GetQuoteDays 获取目标商品的盘面数据
  231. // 参数 goodsCodes string 商品代码字串,以“,”分隔
  232. // 返回 []Quoteday 盘面数据
  233. // 返回 error error
  234. func GetQuoteDays(goodsCodes string) ([]Quoteday, error) {
  235. engine := db.GetMySQLEngine()
  236. datas := make([]Quoteday, 0)
  237. if err := engine.Where(fmt.Sprintf("goodscode in (%s)", goodsCodes)).Find(&datas); err != nil {
  238. return nil, err
  239. }
  240. return datas, nil
  241. }