quote.go 16 KB

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