quote.go 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  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. // CycleTypeWeek 周期类型 - 周线
  29. CycleTypeWeek CycleType = 12
  30. // CycleTypeMonth 周期类型 - 月线
  31. CycleTypeMonth CycleType = 13
  32. // CycleTypeYear 周期类型 - 年线
  33. CycleTypeYear CycleType = 14
  34. // CycleTypeTik 周期类型 - Tik
  35. CycleTypeTik CycleType = 10
  36. )
  37. // CycleData MongoDB中历史数据模型
  38. type CycleData struct {
  39. ID bson.ObjectId `json:"-" bson:"_id"` // id
  40. GC string `bson:"GC"` // 商品代码
  41. ST int `json:"-" bson:"ST"` // 时间戳
  42. SST string `bson:"SST"` // 时间文本
  43. Open int `bson:"Open"` // 开盘价
  44. High int `bson:"High"` // 最高价
  45. Low int `bson:"Low"` // 最低价
  46. Close int `bson:"Close"` // 收盘价
  47. TV int `bson:"TV"` // 总量
  48. TT int `bson:"TT"` // 总金额
  49. HV int `bson:"HV"` // 持仓量
  50. SP int `bson:"SP"` // 结算价,日线周期(包括)以上才有
  51. FI bool `json:"FI"` // 是否补充数据
  52. }
  53. // TikData MongoDB中Tik数据模型
  54. type TikData struct {
  55. ID bson.ObjectId `json:"-" bson:"_id"` // id
  56. GC string `bson:"GC"` // 商品代码
  57. TD int `bson:"TD"` // 交易日时间戳
  58. AT int `bson:"AT"` // 行情时间戳
  59. SAT string `bson:"SAT"` // 行情时间文本
  60. PE int `bson:"PE"` // 现价
  61. Vol int `bson:"Vol"` // 现量
  62. TT int `bson:"TT"` // 现金额
  63. Bid int `bson:"Bid"` // 买价
  64. BV int `bson:"BV"` // 买量
  65. Ask int `bson:"Ask"` // 卖价
  66. AV int `bson:"AV"` // 卖量
  67. HV int `bson:"HV"` // 持仓量
  68. HI int `bson:"HI"` // 单笔持仓
  69. TDR int `bson:"TDR"` // 交易方向
  70. TK int `bson:"TK"` // 交易类型
  71. OId int `bson:"OId"` // 行情序号
  72. }
  73. // Quoteday 行情盘面
  74. type Quoteday struct {
  75. Id int64 `xorm:"pk autoincr BIGINT(20)"`
  76. Exchangedate int64 `xorm:"not null BIGINT(20)"` // 交易日
  77. Goodscode string `xorm:"not null unique CHAR(10)"` // 商品代码
  78. Exchangecode int `xorm:"INT(11)"` // 交易所代码
  79. Preclose int64 `xorm:"default 0 BIGINT(20)"` // 昨收
  80. Opentime int64 `xorm:"BIGINT(20)"` // 开盘时间
  81. Opened int64 `xorm:"not null default 0 BIGINT(20)"` // 开盘价
  82. Highest int64 `xorm:"not null default 0 BIGINT(20)"` // 最高价
  83. Lowest int64 `xorm:"not null default 0 BIGINT(20)"` // 最低价
  84. Lasttime string `xorm:"VARCHAR(20)"` // 行情时间(只有现价变化行情时间才变化)
  85. Utclasttime int64 `xorm:"not null BIGINT(20)"` // utc的行情时间
  86. Last int64 `xorm:"not null BIGINT(20)"` // 最新价
  87. Lastvolume int64 `xorm:"default 0 BIGINT(20)"` // 最新成交量
  88. Lastturnover int64 `xorm:"default 0 BIGINT(20)"` // 最新成交金额
  89. Totalbidvolume int64 `xorm:"default 0 BIGINT(20)"` // 外盘
  90. Totalaskvolume int64 `xorm:"default 0 BIGINT(20)"` // 内盘
  91. Totalvolume int64 `xorm:"default 0 BIGINT(20)"` // 总量
  92. Totalturnover int64 `xorm:"default 0 BIGINT(20)"` // 总金额
  93. Bid int64 `xorm:"default 0 BIGINT(20)"` // 买1
  94. Bid2 int64 `xorm:"default 0 BIGINT(20)"` // 买2
  95. Bid3 int64 `xorm:"default 0 BIGINT(20)"` // 买3
  96. Bid4 int64 `xorm:"default 0 BIGINT(20)"` // 买4
  97. Bid5 int64 `xorm:"default 0 BIGINT(20)"` // 买5
  98. Bidvolume int64 `xorm:"default 0 BIGINT(20)"` // 买量1
  99. Bidvolume2 int64 `xorm:"default 0 BIGINT(20)"` // 买量2
  100. Bidvolume3 int64 `xorm:"default 0 BIGINT(20)"` // 买量3
  101. Bidvolume4 int64 `xorm:"default 0 BIGINT(20)"` // 买量4
  102. Bidvolume5 int64 `xorm:"default 0 BIGINT(20)"` // 买量5
  103. Ask int64 `xorm:"default 0 BIGINT(20)"` // 卖1
  104. Ask2 int64 `xorm:"default 0 BIGINT(20)"` // 卖2
  105. Ask3 int64 `xorm:"default 0 BIGINT(20)"` // 卖3
  106. Ask4 int64 `xorm:"default 0 BIGINT(20)"` // 卖4
  107. Ask5 int64 `xorm:"default 0 BIGINT(20)"` // 卖5
  108. Askvolume int64 `xorm:"default 0 BIGINT(20)"` // 卖量1
  109. Askvolume2 int64 `xorm:"default 0 BIGINT(20)"` // 卖量2
  110. Askvolume3 int64 `xorm:"default 0 BIGINT(20)"` // 卖量3
  111. Askvolume4 int64 `xorm:"default 0 BIGINT(20)"` // 卖量4
  112. Askvolume5 int64 `xorm:"default 0 BIGINT(20)"` // 卖量5
  113. Presettle int64 `xorm:"default 0 BIGINT(20)"` // 昨结价
  114. Settle int64 `xorm:"default 0 BIGINT(20)"` // 结算价
  115. Preholdvolume int64 `xorm:"default 0 BIGINT(20)"` // 昨持仓
  116. Holdvolume int64 `xorm:"default 0 BIGINT(20)"` // 持仓
  117. Averageprice int64 `xorm:"default 0 BIGINT(20)"` // 均价
  118. Orderid int64 `xorm:"default 0 BIGINT(20)"` // 序号
  119. Limitup int64 `xorm:"default 0 BIGINT(20)"` // 涨停价
  120. Limitdown int64 `xorm:"default 0 BIGINT(20)"` // 跌停价
  121. Inventory int64 `xorm:"default 0 BIGINT(20)"` // 库存
  122. Holdincrement int64 `xorm:"default 0 BIGINT(20)"` // 单笔持仓
  123. Iscleared int `xorm:"INT(11)"` // 是否清盘标志
  124. Issettled int `xorm:"INT(11)"` // 是否结算标志
  125. Bidqueueinfo string `xorm:"VARCHAR(2000)"` // 大利市买港股用
  126. Askqueueinfo string `xorm:"VARCHAR(2000)"` // 大利市卖港股用
  127. Bidorderid int64 `xorm:"BIGINT(20)"` // 买单号1
  128. Bidorderid2 int64 `xorm:"BIGINT(20)"` // 买单号2
  129. Bidorderid3 int64 `xorm:"BIGINT(20)"` // 买单号3
  130. Bidorderid4 int64 `xorm:"BIGINT(20)"` // 买单号4
  131. Bidorderid5 int64 `xorm:"BIGINT(20)"` // 买单号5
  132. Askorderid int64 `xorm:"BIGINT(20)"` // 卖单号1
  133. Askorderid2 int64 `xorm:"BIGINT(20)"` // 卖单号2
  134. Askorderid3 int64 `xorm:"BIGINT(20)"` // 卖单号3
  135. Askorderid4 int64 `xorm:"BIGINT(20)"` // 卖单号4
  136. Askorderid5 int64 `xorm:"BIGINT(20)"` // 卖单号5
  137. Lastlot int64 `xorm:"BIGINT(20)"` // 最新成交手数
  138. Totallot int64 `xorm:"BIGINT(20)"` // 总手数
  139. Strikeprice int64 `xorm:"BIGINT(20)"` // 发行价
  140. Cleartime int64 `xorm:"BIGINT(20)"` // 清盘时间
  141. Calloptionpremiums int64 `xorm:"default 0 BIGINT(20)"` // 认购期权1
  142. Calloptionpremiums2 int64 `xorm:"default 0 BIGINT(20)"` // 认购期权2
  143. Calloptionpremiums3 int64 `xorm:"default 0 BIGINT(20)"` // 认购期权3
  144. Calloptionpremiums4 int64 `xorm:"default 0 BIGINT(20)"` // 认购期权4
  145. Calloptionpremiums5 int64 `xorm:"default 0 BIGINT(20)"` // 认购期权5
  146. Putoptionpremiums int64 `xorm:"default 0 BIGINT(20)"` // 认沽期权1
  147. Putoptionpremiums2 int64 `xorm:"default 0 BIGINT(20)"` // 认沽期权2
  148. Putoptionpremiums3 int64 `xorm:"default 0 BIGINT(20)"` // 认沽期权3
  149. Putoptionpremiums4 int64 `xorm:"default 0 BIGINT(20)"` // 认沽期权4
  150. Putoptionpremiums5 int64 `xorm:"default 0 BIGINT(20)"` // 认沽期权5
  151. Nontotalvolume int64 `xorm:"default 0 BIGINT(20)"` // 非交易总量
  152. Nontotalholdervolume int64 `xorm:"default 0 BIGINT(20)"` // 非交易持仓量
  153. Nontotalturnover int64 `xorm:"default 0 BIGINT(20)"` // 非交易总金额
  154. Nontotallot int64 `xorm:"default 0 BIGINT(20)"` // 非交易总手数
  155. Publictradetype string `xorm:"VARCHAR(2)"` // 公共交易标志类型 港股专用
  156. Iep int64 `xorm:"default 0 BIGINT(20)"` // 平衡价 港股专用
  157. Iev int64 `xorm:"default 0 BIGINT(20)"` // 平衡量 港股专用
  158. Grepmarketprice int64 `xorm:"default 0 BIGINT(20)"` // 暗盘价 港股专用
  159. Bid6 int64 `xorm:"default 0 BIGINT(20)"` // 买6
  160. Bid7 int64 `xorm:"default 0 BIGINT(20)"` // 买7
  161. Bid8 int64 `xorm:"default 0 BIGINT(20)"` // 买8
  162. Bid9 int64 `xorm:"default 0 BIGINT(20)"` // 买9
  163. Bid10 int64 `xorm:"default 0 BIGINT(20)"` // 买10
  164. Bidvolume6 int64 `xorm:"default 0 BIGINT(20)"` // 买量6
  165. Bidvolume7 int64 `xorm:"default 0 BIGINT(20)"` // 买量7
  166. Bidvolume8 int64 `xorm:"default 0 BIGINT(20)"` // 买量8
  167. Bidvolume9 int64 `xorm:"default 0 BIGINT(20)"` // 买量9
  168. Bidvolume10 int64 `xorm:"default 0 BIGINT(20)"` // 买量10
  169. Ask6 int64 `xorm:"default 0 BIGINT(20)"` // 卖6
  170. Ask7 int64 `xorm:"default 0 BIGINT(20)"` // 卖7
  171. Ask8 int64 `xorm:"default 0 BIGINT(20)"` // 卖8
  172. Ask9 int64 `xorm:"default 0 BIGINT(20)"` // 卖9
  173. Ask10 int64 `xorm:"default 0 BIGINT(20)"` // 卖10
  174. Askvolume6 int64 `xorm:"default 0 BIGINT(20)"` // 卖量6
  175. Askvolume7 int64 `xorm:"default 0 BIGINT(20)"` // 卖量7
  176. Askvolume8 int64 `xorm:"default 0 BIGINT(20)"` // 卖量8
  177. Askvolume9 int64 `xorm:"default 0 BIGINT(20)"` // 卖量9
  178. Askvolume10 int64 `xorm:"default 0 BIGINT(20)"` // 卖量10
  179. Bidordervolume int64 `xorm:"default 0 BIGINT(20)"` // 买单量1
  180. Bidordervolume2 int64 `xorm:"default 0 BIGINT(20)"` // 买单量2
  181. Bidordervolume3 int64 `xorm:"default 0 BIGINT(20)"` // 买单量3
  182. Bidordervolume4 int64 `xorm:"default 0 BIGINT(20)"` // 买单量4
  183. Bidordervolume5 int64 `xorm:"default 0 BIGINT(20)"` // 买单量5
  184. Bidordervolume6 int64 `xorm:"default 0 BIGINT(20)"` // 买单量6
  185. Bidordervolume7 int64 `xorm:"default 0 BIGINT(20)"` // 买单量7
  186. Bidordervolume8 int64 `xorm:"default 0 BIGINT(20)"` // 买单量8
  187. Bidordervolume9 int64 `xorm:"default 0 BIGINT(20)"` // 买单量9
  188. Bidordervolume10 int64 `xorm:"default 0 BIGINT(20)"` // 买单量10
  189. Askordervolume int64 `xorm:"default 0 BIGINT(20)"` // 卖单量1
  190. Askordervolume2 int64 `xorm:"default 0 BIGINT(20)"` // 卖单量2
  191. Askordervolume3 int64 `xorm:"default 0 BIGINT(20)"` // 卖单量3
  192. Askordervolume4 int64 `xorm:"default 0 BIGINT(20)"` // 卖单量4
  193. Askordervolume5 int64 `xorm:"default 0 BIGINT(20)"` // 卖单量5
  194. Askordervolume6 int64 `xorm:"default 0 BIGINT(20)"` // 卖单量6
  195. Askordervolume7 int64 `xorm:"default 0 BIGINT(20)"` // 卖单量7
  196. Askordervolume8 int64 `xorm:"default 0 BIGINT(20)"` // 卖单量8
  197. Askordervolume9 int64 `xorm:"default 0 BIGINT(20)"` // 卖单量9
  198. Askordervolume10 int64 `xorm:"default 0 BIGINT(20)"` // 卖单量10
  199. }
  200. // TableName is Quoteday
  201. func (*Quoteday) TableName() string {
  202. return "quoteday"
  203. }
  204. // GetHistoryCycleDatas 获取历史数据
  205. // 参数 cycleType CycleType 周期类型
  206. // 参数 goodsCode string 商品代码
  207. // 参数 startTime time.Time 开始时间(闭区间)
  208. // 参数 endTime time.Time 结束时间(闭区间)
  209. // 参数 count int 条数
  210. // 参数 isAscForST bool 是否按时间顺序排序,默认为时间倒序排序
  211. // 返回值 []CycleData 历史数据
  212. // 返回值 error 错误
  213. func GetHistoryCycleDatas(cycleType CycleType, goodsCode string, startTime, endTime *time.Time, count int, isAscForST bool) ([]CycleData, error) {
  214. db := db.GetMongoDB()
  215. // 获取目标Collection
  216. collection := "mincycle"
  217. switch cycleType {
  218. case CycleTypeTik:
  219. collection = "quotetik"
  220. case CycleTypeMinutes1:
  221. collection = "mincycle"
  222. case CycleTypeMinutes5:
  223. collection = "min5cycle"
  224. case CycleTypeMinutes30:
  225. collection = "min30cycle"
  226. case CycleTypeMinutes60:
  227. collection = "min60cycle"
  228. case CycleTypeMinutes240:
  229. collection = "min240cycle"
  230. case CycleTypeMinutesDay:
  231. collection = "daycycle"
  232. case CycleTypeWeek:
  233. collection = "weekcycle"
  234. case CycleTypeMonth:
  235. collection = "monthcycle"
  236. case CycleTypeYear:
  237. collection = "yearcycle"
  238. default:
  239. return nil, errors.New("不支持的周期类型")
  240. }
  241. c := db.C(collection)
  242. // 按时间排序
  243. sort := "-ST"
  244. if isAscForST {
  245. sort = "ST"
  246. }
  247. // 查询数据
  248. var cycleDatas []CycleData
  249. m := bson.M{"GC": goodsCode}
  250. if startTime != nil && endTime == nil {
  251. m["ST"] = bson.M{"$gte": startTime.Unix()}
  252. } else if startTime == nil && endTime != nil {
  253. m["ST"] = bson.M{"$lte": endTime.Unix()}
  254. } else if startTime != nil && endTime != nil {
  255. m["ST"] = bson.M{"$gte": startTime.Unix(), "$lte": endTime.Unix()}
  256. }
  257. query := c.Find(m)
  258. if count > 0 {
  259. query = query.Limit(count)
  260. }
  261. if err := query.Sort(sort).All(&cycleDatas); err != nil {
  262. return nil, err
  263. }
  264. return cycleDatas, nil
  265. }
  266. // GetHistoryTikDatas 获取历史数据
  267. // 参数 goodsCode string 商品代码
  268. // 参数 startTime time.Time 开始时间(闭区间)
  269. // 参数 endTime time.Time 结束时间(闭区间)
  270. // 参数 count int 条数
  271. // 参数 isAscForST bool 是否按时间顺序排序,默认为时间倒序排序
  272. // 返回值 []TikData Tik数据
  273. // 返回值 error 错误
  274. func GetHistoryTikDatas(goodsCode string, startTime, endTime *time.Time, count int, isAscForST bool) ([]TikData, error) {
  275. db := db.GetMongoDB()
  276. // 获取目标Collection
  277. collection := "quotetik"
  278. c := db.C(collection)
  279. // 按时间排序
  280. sort := "-AT"
  281. if isAscForST {
  282. sort = "AT"
  283. }
  284. // 查询数据
  285. var tikDatas []TikData
  286. m := bson.M{"GC": goodsCode}
  287. if startTime != nil && endTime == nil {
  288. m["AT"] = bson.M{"$gte": startTime.Unix()}
  289. } else if startTime == nil && endTime != nil {
  290. m["AT"] = bson.M{"$lte": endTime.Unix()}
  291. } else if startTime != nil && endTime != nil {
  292. m["AT"] = bson.M{"$gte": startTime.Unix(), "$lte": endTime.Unix()}
  293. }
  294. query := c.Find(m)
  295. if count > 0 {
  296. query = query.Limit(count)
  297. }
  298. if err := query.Sort(sort).All(&tikDatas); err != nil {
  299. return nil, err
  300. }
  301. return tikDatas, nil
  302. }
  303. // GetQuoteDays 获取目标商品的盘面数据
  304. // 参数 goodsCodes string 商品代码字串,以“,”分隔
  305. // 返回 []Quoteday 盘面数据
  306. // 返回 error error
  307. func GetQuoteDays(goodsCodes string) ([]Quoteday, error) {
  308. engine := db.GetMySQLEngine()
  309. datas := make([]Quoteday, 0)
  310. if err := engine.Where(fmt.Sprintf("goodscode in (%s)", goodsCodes)).Find(&datas); err != nil {
  311. return nil, err
  312. }
  313. return datas, nil
  314. }