Quellcode durchsuchen

新增“查询行情Tik数据”接口

zhou.xiaoning vor 4 Jahren
Ursprung
Commit
e71fd2e8a3
6 geänderte Dateien mit 497 neuen und 4 gelöschten Zeilen
  1. 118 1
      controllers/quote/history.go
  2. 117 1
      docs/docs.go
  3. 117 1
      docs/swagger.json
  4. 80 1
      docs/swagger.yaml
  5. 63 0
      models/quote.go
  6. 2 0
      routers/router.go

+ 118 - 1
controllers/quote/history.go

@@ -43,7 +43,7 @@ type QueryHistoryDatasReq struct {
 // @Summary 查询行情历史数据
 // @Produce json
 // @Security ApiKeyAuth
-// @Param cycleType query int true "周期类型, 0-秒 1: 1分钟 2: 5分钟 3: 30分钟 4: 60分钟 120: 2小时 240: 4小时 11: 日线 10: Tik"
+// @Param cycleType query int true "周期类型, 0-秒 1: 1分钟 2: 5分钟 3: 30分钟 4: 60分钟 120: 2小时 240: 4小时 11: 日线"
 // @Param goodsCode query string true "商品代码"
 // @Param startTime query string false "开始时间,格式:yyyy-MM-dd HH:mm:ss"
 // @Param endTime query string false "结束时间,格式:yyyy-MM-dd HH:mm:ss"
@@ -126,6 +126,123 @@ func QueryHistoryDatas(c *gin.Context) {
 	appG.Response(http.StatusOK, e.SUCCESS, rst)
 }
 
+// HistoryTikData Tik数据
+type HistoryTikData struct {
+	TimeStamp time.Time `json:"TS"`  // 行情时间文本
+	PE        float64   `json:"PE"`  // 现价
+	Vol       int       `json:"Vol"` // 现量
+	TT        float64   `json:"TT"`  // 现金额
+	Bid       float64   `json:"Bid"` // 买价
+	BV        int       `json:"BV"`  // 买量
+	Ask       float64   `json:"Ask"` // 卖价
+	AV        int       `json:"AV"`  // 卖量
+	HV        int       `json:"HV"`  // 持仓量
+	HI        int       `json:"HI"`  // 单笔持仓
+	TDR       int       `json:"TDR"` // 交易方向,0:买 1:卖
+	TK        int       `json:"TK"`  // 交易类型
+}
+
+// QueryHistoryTikDatasReq 查询行情Tik数据请求参数
+type QueryHistoryTikDatasReq struct {
+	GoodsCode string `form:"goodsCode"  binding:"required"`
+	StartTime string `form:"startTime"`
+	EndTime   string `form:"endTime"`
+	Count     int    `form:"count"`
+	IsAsc     bool   `form:"isAsc"`
+}
+
+// QueryHistoryTikDatas 查询行情Tik数据
+// @Summary 查询行情Tik数据
+// @Produce json
+// @Security ApiKeyAuth
+// @Param goodsCode query string true "商品代码"
+// @Param startTime query string false "开始时间,格式:yyyy-MM-dd HH:mm:ss"
+// @Param endTime query string false "结束时间,格式:yyyy-MM-dd HH:mm:ss"
+// @Param count query int false "条数"
+// @Param isAsc query bool false "是否按时间顺序排序(默认为时间倒序排序)"
+// @Success 200 {object} HistoryTikData
+// @Failure 500 {object} app.Response
+// @Router /Quote/QueryHistoryTikDatas [get]
+// @Tags 行情服务
+func QueryHistoryTikDatas(c *gin.Context) {
+	appG := app.Gin{C: c}
+
+	// 获取请求参数
+	var req QueryHistoryTikDatasReq
+	if err := appG.C.ShouldBindQuery(&req); err != nil {
+		logger.GetLogger().Errorf("QueryHistoryTikDatas failed: %s", err.Error())
+		appG.Response(http.StatusBadRequest, e.INVALID_PARAMS, nil)
+		return
+	}
+
+	// 转换时间
+	timeFormat := "2006-01-02 15:04:05" // go中的时间格式化必须是这个时间
+	var startTime *time.Time
+	if len(req.StartTime) > 0 {
+		st, err := time.ParseInLocation(timeFormat, req.StartTime, time.Local)
+		if err != nil {
+			logger.GetLogger().Errorf("QueryHistoryTikDatas failed: %s", err.Error())
+			appG.Response(http.StatusBadRequest, e.ERROR_QUERY_TIME_FORMAT_FAIL, nil)
+			return
+		}
+		startTime = &st
+	}
+	var endTime *time.Time
+	if len(req.EndTime) > 0 {
+		et, err := time.ParseInLocation(timeFormat, req.EndTime, time.Local)
+		if err != nil {
+			logger.GetLogger().Errorf("QueryHistoryTikDatas failed: %s", err.Error())
+			appG.Response(http.StatusBadRequest, e.ERROR_QUERY_TIME_FORMAT_FAIL, nil)
+			return
+		}
+		endTime = &et
+	}
+
+	// 查询数据
+	tikDatas, err := models.GetHistoryTikDatas(req.GoodsCode, startTime, endTime, req.Count, req.IsAsc)
+	if err != nil {
+		logger.GetLogger().Errorf("QueryHistoryTikDatas failed: %s", err.Error())
+		appG.Response(http.StatusBadRequest, e.ERROR_QUERY_FAIL, nil)
+		return
+	}
+
+	// 获取目标商品信息
+	goods, err := models.GetGoodsByGoodsCode(req.GoodsCode)
+	if err != nil {
+		logger.GetLogger().Errorf("QueryHistoryTikDatas failed: %s", err.Error())
+		appG.Response(http.StatusBadRequest, e.ERROR_QUERY_FAIL, nil)
+		return
+	}
+
+	// 计算最终价格
+	rst := make([]HistoryTikData, 0)
+	for _, v := range tikDatas {
+		// 获取方向
+		buyOrSell := 0
+		if v.TDR == 83 { // (Ascii) B-66 S-83
+			buyOrSell = 1
+		}
+		rst = append(rst, HistoryTikData{
+			TimeStamp: time.Unix(int64(v.AT), 0),
+			PE:        utils.IntToFloat64(v.PE, int(goods.Decimalplace)),
+			Vol:       v.Vol,
+			TT:        float64(v.TT),
+			Bid:       utils.IntToFloat64(v.Bid, int(goods.Decimalplace)),
+			BV:        v.BV,
+			Ask:       utils.IntToFloat64(v.Ask, int(goods.Decimalplace)),
+			AV:        v.AV,
+			HV:        v.HV,
+			HI:        v.HI,
+			TDR:       buyOrSell,
+			TK:        v.TK,
+		})
+	}
+
+	// 查询成功
+	logger.GetLogger().Debugln("QueryHistoryTikDatas successed: %v", rst)
+	appG.Response(http.StatusOK, e.SUCCESS, rst)
+}
+
 // QueryTSDataReq 分时图数据查询请求参数
 type QueryTSDataReq struct {
 	GoodsCode string `form:"goodsCode" binding:"required"` // 商品代码

+ 117 - 1
docs/docs.go

@@ -4966,7 +4966,7 @@ var doc = `{
                 "parameters": [
                     {
                         "type": "integer",
-                        "description": "周期类型, 0-秒 1: 1分钟 2: 5分钟 3: 30分钟 4: 60分钟 120: 2小时 240: 4小时 11: 日线 10: Tik",
+                        "description": "周期类型, 0-秒 1: 1分钟 2: 5分钟 3: 30分钟 4: 60分钟 120: 2小时 240: 4小时 11: 日线",
                         "name": "cycleType",
                         "in": "query",
                         "required": true
@@ -5019,6 +5019,69 @@ var doc = `{
                 }
             }
         },
+        "/Quote/QueryHistoryTikDatas": {
+            "get": {
+                "security": [
+                    {
+                        "ApiKeyAuth": []
+                    }
+                ],
+                "produces": [
+                    "application/json"
+                ],
+                "tags": [
+                    "行情服务"
+                ],
+                "summary": "查询行情Tik数据",
+                "parameters": [
+                    {
+                        "type": "string",
+                        "description": "商品代码",
+                        "name": "goodsCode",
+                        "in": "query",
+                        "required": true
+                    },
+                    {
+                        "type": "string",
+                        "description": "开始时间,格式:yyyy-MM-dd HH:mm:ss",
+                        "name": "startTime",
+                        "in": "query"
+                    },
+                    {
+                        "type": "string",
+                        "description": "结束时间,格式:yyyy-MM-dd HH:mm:ss",
+                        "name": "endTime",
+                        "in": "query"
+                    },
+                    {
+                        "type": "integer",
+                        "description": "条数",
+                        "name": "count",
+                        "in": "query"
+                    },
+                    {
+                        "type": "boolean",
+                        "description": "是否按时间顺序排序(默认为时间倒序排序)",
+                        "name": "isAsc",
+                        "in": "query"
+                    }
+                ],
+                "responses": {
+                    "200": {
+                        "description": "OK",
+                        "schema": {
+                            "$ref": "#/definitions/quote.HistoryTikData"
+                        }
+                    },
+                    "500": {
+                        "description": "Internal Server Error",
+                        "schema": {
+                            "$ref": "#/definitions/app.Response"
+                        }
+                    }
+                }
+            }
+        },
         "/Quote/QueryQuoteDay": {
             "get": {
                 "security": [
@@ -17131,6 +17194,59 @@ var doc = `{
                 }
             }
         },
+        "quote.HistoryTikData": {
+            "type": "object",
+            "properties": {
+                "AV": {
+                    "description": "卖量",
+                    "type": "integer"
+                },
+                "Ask": {
+                    "description": "卖价",
+                    "type": "number"
+                },
+                "BV": {
+                    "description": "买量",
+                    "type": "integer"
+                },
+                "Bid": {
+                    "description": "买价",
+                    "type": "number"
+                },
+                "HI": {
+                    "description": "单笔持仓",
+                    "type": "integer"
+                },
+                "HV": {
+                    "description": "持仓量",
+                    "type": "integer"
+                },
+                "PE": {
+                    "description": "现价",
+                    "type": "number"
+                },
+                "TDR": {
+                    "description": "交易方向,0:买 1:卖",
+                    "type": "integer"
+                },
+                "TK": {
+                    "description": "交易类型",
+                    "type": "integer"
+                },
+                "TS": {
+                    "description": "行情时间文本",
+                    "type": "string"
+                },
+                "TT": {
+                    "description": "现金额",
+                    "type": "number"
+                },
+                "Vol": {
+                    "description": "现量",
+                    "type": "integer"
+                }
+            }
+        },
         "quote.QueryQuoteDayRsp": {
             "type": "object",
             "properties": {

+ 117 - 1
docs/swagger.json

@@ -4950,7 +4950,7 @@
                 "parameters": [
                     {
                         "type": "integer",
-                        "description": "周期类型, 0-秒 1: 1分钟 2: 5分钟 3: 30分钟 4: 60分钟 120: 2小时 240: 4小时 11: 日线 10: Tik",
+                        "description": "周期类型, 0-秒 1: 1分钟 2: 5分钟 3: 30分钟 4: 60分钟 120: 2小时 240: 4小时 11: 日线",
                         "name": "cycleType",
                         "in": "query",
                         "required": true
@@ -5003,6 +5003,69 @@
                 }
             }
         },
+        "/Quote/QueryHistoryTikDatas": {
+            "get": {
+                "security": [
+                    {
+                        "ApiKeyAuth": []
+                    }
+                ],
+                "produces": [
+                    "application/json"
+                ],
+                "tags": [
+                    "行情服务"
+                ],
+                "summary": "查询行情Tik数据",
+                "parameters": [
+                    {
+                        "type": "string",
+                        "description": "商品代码",
+                        "name": "goodsCode",
+                        "in": "query",
+                        "required": true
+                    },
+                    {
+                        "type": "string",
+                        "description": "开始时间,格式:yyyy-MM-dd HH:mm:ss",
+                        "name": "startTime",
+                        "in": "query"
+                    },
+                    {
+                        "type": "string",
+                        "description": "结束时间,格式:yyyy-MM-dd HH:mm:ss",
+                        "name": "endTime",
+                        "in": "query"
+                    },
+                    {
+                        "type": "integer",
+                        "description": "条数",
+                        "name": "count",
+                        "in": "query"
+                    },
+                    {
+                        "type": "boolean",
+                        "description": "是否按时间顺序排序(默认为时间倒序排序)",
+                        "name": "isAsc",
+                        "in": "query"
+                    }
+                ],
+                "responses": {
+                    "200": {
+                        "description": "OK",
+                        "schema": {
+                            "$ref": "#/definitions/quote.HistoryTikData"
+                        }
+                    },
+                    "500": {
+                        "description": "Internal Server Error",
+                        "schema": {
+                            "$ref": "#/definitions/app.Response"
+                        }
+                    }
+                }
+            }
+        },
         "/Quote/QueryQuoteDay": {
             "get": {
                 "security": [
@@ -17115,6 +17178,59 @@
                 }
             }
         },
+        "quote.HistoryTikData": {
+            "type": "object",
+            "properties": {
+                "AV": {
+                    "description": "卖量",
+                    "type": "integer"
+                },
+                "Ask": {
+                    "description": "卖价",
+                    "type": "number"
+                },
+                "BV": {
+                    "description": "买量",
+                    "type": "integer"
+                },
+                "Bid": {
+                    "description": "买价",
+                    "type": "number"
+                },
+                "HI": {
+                    "description": "单笔持仓",
+                    "type": "integer"
+                },
+                "HV": {
+                    "description": "持仓量",
+                    "type": "integer"
+                },
+                "PE": {
+                    "description": "现价",
+                    "type": "number"
+                },
+                "TDR": {
+                    "description": "交易方向,0:买 1:卖",
+                    "type": "integer"
+                },
+                "TK": {
+                    "description": "交易类型",
+                    "type": "integer"
+                },
+                "TS": {
+                    "description": "行情时间文本",
+                    "type": "string"
+                },
+                "TT": {
+                    "description": "现金额",
+                    "type": "number"
+                },
+                "Vol": {
+                    "description": "现量",
+                    "type": "integer"
+                }
+            }
+        },
         "quote.QueryQuoteDayRsp": {
             "type": "object",
             "properties": {

+ 80 - 1
docs/swagger.yaml

@@ -8168,6 +8168,45 @@ definitions:
         description: 总量
         type: integer
     type: object
+  quote.HistoryTikData:
+    properties:
+      AV:
+        description: 卖量
+        type: integer
+      Ask:
+        description: 卖价
+        type: number
+      BV:
+        description: 买量
+        type: integer
+      Bid:
+        description: 买价
+        type: number
+      HI:
+        description: 单笔持仓
+        type: integer
+      HV:
+        description: 持仓量
+        type: integer
+      PE:
+        description: 现价
+        type: number
+      TDR:
+        description: 交易方向,0:买 1:卖
+        type: integer
+      TK:
+        description: 交易类型
+        type: integer
+      TS:
+        description: 行情时间文本
+        type: string
+      TT:
+        description: 现金额
+        type: number
+      Vol:
+        description: 现量
+        type: integer
+    type: object
   quote.QueryQuoteDayRsp:
     properties:
       ask:
@@ -12220,7 +12259,7 @@ paths:
     get:
       parameters:
       - description: '周期类型, 0-秒 1: 1分钟 2: 5分钟 3: 30分钟 4: 60分钟 120: 2小时 240: 4小时 11:
-          日线 10: Tik'
+          日线'
         in: query
         name: cycleType
         required: true
@@ -12262,6 +12301,46 @@ paths:
       summary: 查询行情历史数据
       tags:
       - 行情服务
+  /Quote/QueryHistoryTikDatas:
+    get:
+      parameters:
+      - description: 商品代码
+        in: query
+        name: goodsCode
+        required: true
+        type: string
+      - description: 开始时间,格式:yyyy-MM-dd HH:mm:ss
+        in: query
+        name: startTime
+        type: string
+      - description: 结束时间,格式:yyyy-MM-dd HH:mm:ss
+        in: query
+        name: endTime
+        type: string
+      - description: 条数
+        in: query
+        name: count
+        type: integer
+      - description: 是否按时间顺序排序(默认为时间倒序排序)
+        in: query
+        name: isAsc
+        type: boolean
+      produces:
+      - application/json
+      responses:
+        "200":
+          description: OK
+          schema:
+            $ref: '#/definitions/quote.HistoryTikData'
+        "500":
+          description: Internal Server Error
+          schema:
+            $ref: '#/definitions/app.Response'
+      security:
+      - ApiKeyAuth: []
+      summary: 查询行情Tik数据
+      tags:
+      - 行情服务
   /Quote/QueryQuoteDay:
     get:
       parameters:

+ 63 - 0
models/quote.go

@@ -54,6 +54,27 @@ type CycleData struct {
 	FI    bool          `json:"FI"`           // 是否补充数据
 }
 
+// TikData MongoDB中Tik数据模型
+type TikData struct {
+	ID  bson.ObjectId `json:"-" bson:"_id"` // id
+	GC  string        `bson:"GC"`           // 商品代码
+	TD  int           `bson:"TD"`           // 交易日时间戳
+	AT  int           `bson:"AT"`           // 行情时间戳
+	SAT string        `bson:"SAT"`          // 行情时间文本
+	PE  int           `bson:"PE"`           // 现价
+	Vol int           `bson:"Vol"`          // 现量
+	TT  int           `bson:"TT"`           // 现金额
+	Bid int           `bson:"Bid"`          // 买价
+	BV  int           `bson:"BV"`           // 买量
+	Ask int           `bson:"Ask"`          // 卖价
+	AV  int           `bson:"AV"`           // 卖量
+	HV  int           `bson:"HV"`           // 持仓量
+	HI  int           `bson:"HI"`           // 单笔持仓
+	TDR int           `bson:"TDR"`          // 交易方向
+	TK  int           `bson:"TK"`           // 交易类型
+	OId int           `bson:"OId"`          // 行情序号
+}
+
 // Quoteday 行情盘面
 type Quoteday struct {
 	Id                   int64  `xorm:"pk autoincr BIGINT(20)"`
@@ -252,6 +273,48 @@ func GetHistoryCycleDatas(cycleType CycleType, goodsCode string, startTime, endT
 	return cycleDatas, nil
 }
 
+// GetHistoryTikDatas 获取历史数据
+// 参数 goodsCode string 商品代码
+// 参数 startTime time.Time 开始时间(闭区间)
+// 参数 endTime time.Time 结束时间(闭区间)
+// 参数 count int 条数
+// 参数 isAscForST bool 是否按时间顺序排序,默认为时间倒序排序
+// 返回值 []TikData Tik数据
+// 返回值 error 错误
+func GetHistoryTikDatas(goodsCode string, startTime, endTime *time.Time, count int, isAscForST bool) ([]TikData, error) {
+	db := db.GetMongoDB()
+
+	// 获取目标Collection
+	collection := "quotetik"
+	c := db.C(collection)
+
+	// 按时间排序
+	sort := "-AT"
+	if isAscForST {
+		sort = "AT"
+	}
+
+	// 查询数据
+	var tikDatas []TikData
+	m := bson.M{"GC": goodsCode}
+	if startTime != nil && endTime == nil {
+		m["AT"] = bson.M{"$gte": startTime.Unix()}
+	} else if startTime == nil && endTime != nil {
+		m["AT"] = bson.M{"$lte": endTime.Unix()}
+	} else if startTime != nil && endTime != nil {
+		m["AT"] = bson.M{"$gte": startTime.Unix(), "$lte": endTime.Unix()}
+	}
+	query := c.Find(m)
+	if count > 0 {
+		query = query.Limit(count)
+	}
+	if err := query.Sort(sort).All(&tikDatas); err != nil {
+		return nil, err
+	}
+
+	return tikDatas, nil
+}
+
 // GetQuoteDays 获取目标商品的盘面数据
 // 参数 goodsCodes string 商品代码字串,以“,”分隔
 // 返回 []Quoteday 盘面数据

+ 2 - 0
routers/router.go

@@ -157,6 +157,8 @@ func InitRouter() *gin.Engine {
 	{
 		// 查询行情历史数据
 		quoteR.GET("/QueryHistoryDatas", quote.QueryHistoryDatas)
+		// 查询行情Tik数据
+		quoteR.GET("/QueryHistoryTikDatas", quote.QueryHistoryTikDatas)
 		// 查询行情历史数据
 		quoteR.GET("/QueryTSData", quote.QueryTSData)
 		// 获取商品盘面信息