|
|
@@ -45,7 +45,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: 日线"
|
|
|
+// @Param cycleType query int true "周期类型, 0-秒 1: 1分钟 2: 5分钟 3: 30分钟 4: 60分钟 120: 2小时 240: 4小时 11: 日线 12:周线 13:月线 14:年线"
|
|
|
// @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"
|
|
|
@@ -120,6 +120,113 @@ func QueryHistoryDatas(c *gin.Context) {
|
|
|
rst = append(rst, historyData)
|
|
|
}
|
|
|
|
|
|
+ // 包括日线以上的周期,如果没有带当前周期则通过盘面数据生成当前周期数据
|
|
|
+ if req.CycleType >= 11 {
|
|
|
+ // 获取商品信息
|
|
|
+ goods, err := models.GetGoodsByGoodsCode(req.GoodsCode)
|
|
|
+ if goods == nil {
|
|
|
+ logger.GetLogger().Errorf("QueryHistoryDatas failed: %s", err.Error())
|
|
|
+ appG.Response(http.StatusBadRequest, e.ERROR_GET_GOODS_FAILED, nil)
|
|
|
+ return
|
|
|
+ }
|
|
|
+ // 获取市场
|
|
|
+ market, err := models.GetMarketByGoodsCode(req.GoodsCode)
|
|
|
+ if err != nil {
|
|
|
+ logger.GetLogger().Errorf("QueryHistoryDatas failed: %s", err.Error())
|
|
|
+ appG.Response(http.StatusBadRequest, e.ERROR_QUERY_FAIL, nil)
|
|
|
+ return
|
|
|
+ }
|
|
|
+ if market == nil {
|
|
|
+ logger.GetLogger().Errorf("QueryHistoryDatas failed: %s", err.Error())
|
|
|
+ appG.Response(http.StatusBadRequest, e.ERROR_GET_MARKET_FAILED, nil)
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ // 获取目标品种交易日
|
|
|
+ // FIXME: - 由于mtp2.0目前未同步外部交易所品种的当前交易日,
|
|
|
+ // 故通道交易的品种目前只能在交易系统的外部市场中获
|
|
|
+ // 取统一的交易日,后期应要求服务端同步外部数据
|
|
|
+ // 2021.06.30 业务更新:15、97和99市场的行情交易日要从商品盘面中获取
|
|
|
+ quoteTradeDate := ""
|
|
|
+ if market.Trademode == 15 || market.Trademode == 97 || market.Trademode == 99 {
|
|
|
+ if quoteDays, err := models.GetQuoteDays("'" + req.GoodsCode + "'"); err == nil {
|
|
|
+ if len(quoteDays) > 0 {
|
|
|
+ quoteTradeDate = time.Unix(quoteDays[0].Exchangedate, 0).Format("20060102")
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ if marketRun, err := models.GetMarketRun(int(market.Marketid)); err == nil {
|
|
|
+ quoteTradeDate = marketRun.Tradedate2
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if quoteTradeDate != "" {
|
|
|
+ if tradeDate, err := time.ParseInLocation("20060102", quoteTradeDate, time.Local); err == nil {
|
|
|
+ var fisrtDate time.Time
|
|
|
+ switch req.CycleType {
|
|
|
+ case 12: // 周
|
|
|
+ fisrtDate = getFirstDateOfWeek(tradeDate)
|
|
|
+ case 13: // 月
|
|
|
+ fisrtDate = getFirstDateOfMonth(tradeDate)
|
|
|
+ case 14: // 年
|
|
|
+ fisrtDate = getFirstDateOfYear(tradeDate)
|
|
|
+ default: // 日
|
|
|
+ fisrtDate = tradeDate
|
|
|
+ }
|
|
|
+
|
|
|
+ // 获取盘面数据
|
|
|
+ if quoteDays, err := models.GetQuoteDays(req.GoodsCode); err == nil && len(quoteDays) > 0 {
|
|
|
+ v := quoteDays[0]
|
|
|
+ if len(rst) == 0 {
|
|
|
+ // 历史数据当前没数据则直接加当前盘面数据
|
|
|
+ historyData := HistoryData{
|
|
|
+ Opened: utils.IntToFloat64(int(v.Last), dcplace),
|
|
|
+ Highest: utils.IntToFloat64(int(v.Last), dcplace),
|
|
|
+ Lowest: utils.IntToFloat64(int(v.Last), dcplace),
|
|
|
+ Closed: utils.IntToFloat64(int(v.Last), dcplace),
|
|
|
+ TotleVolume: int(v.Totalvolume),
|
|
|
+ TotleTurnover: float64(v.Totalturnover),
|
|
|
+ HoldVolume: int(v.Holdvolume),
|
|
|
+ Settle: utils.IntToFloat64(int(v.Settle), dcplace),
|
|
|
+ TimeStamp: fisrtDate,
|
|
|
+ }
|
|
|
+
|
|
|
+ if req.IsAsc {
|
|
|
+ rst = append(rst, historyData)
|
|
|
+ } else {
|
|
|
+ // 插入第一条
|
|
|
+ rear := append([]HistoryData{}, rst[0:]...)
|
|
|
+ rst = append(append(rst[:0], historyData), rear...)
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ // 判断最后周期是否已经存在盘面周期
|
|
|
+ hd := rst[len(rst)-1]
|
|
|
+ if hd.TimeStamp.Before(fisrtDate) {
|
|
|
+ historyData := HistoryData{
|
|
|
+ Opened: utils.IntToFloat64(int(v.Last), dcplace),
|
|
|
+ Highest: utils.IntToFloat64(int(v.Last), dcplace),
|
|
|
+ Lowest: utils.IntToFloat64(int(v.Last), dcplace),
|
|
|
+ Closed: utils.IntToFloat64(int(v.Last), dcplace),
|
|
|
+ TotleVolume: int(v.Totalvolume),
|
|
|
+ TotleTurnover: float64(v.Totalturnover),
|
|
|
+ HoldVolume: int(v.Holdvolume),
|
|
|
+ Settle: utils.IntToFloat64(int(v.Settle), dcplace),
|
|
|
+ TimeStamp: fisrtDate,
|
|
|
+ }
|
|
|
+
|
|
|
+ if req.IsAsc {
|
|
|
+ rst = append(rst, historyData)
|
|
|
+ } else {
|
|
|
+ // 插入第一条
|
|
|
+ rear := append([]HistoryData{}, rst[0:]...)
|
|
|
+ rst = append(append(rst[:0], historyData), rear...)
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
// 查询成功
|
|
|
logger.GetLogger().Debugln("QueryHistoryDatas successed, rows:%v", len(rst))
|
|
|
appG.Response(http.StatusOK, e.SUCCESS, rst)
|
|
|
@@ -718,3 +825,38 @@ func getTradeDay(tradeDay, weekDay int) int {
|
|
|
|
|
|
return betWeekDay
|
|
|
}
|
|
|
+
|
|
|
+/**
|
|
|
+获取某日所在周周一的日期
|
|
|
+*/
|
|
|
+func getFirstDateOfWeek(d time.Time) time.Time {
|
|
|
+ offset := int(time.Monday - d.Weekday())
|
|
|
+ if offset > 0 {
|
|
|
+ offset = -6
|
|
|
+ }
|
|
|
+
|
|
|
+ weekStartDate := time.Date(d.Year(), d.Month(), d.Day(), 0, 0, 0, 0, time.Local).AddDate(0, 0, offset)
|
|
|
+ return weekStartDate
|
|
|
+}
|
|
|
+
|
|
|
+//获取传入的时间所在月份的第一天,即某月第一天的0点。如传入time.Now(), 返回当前月份的第一天0点时间。
|
|
|
+func getFirstDateOfMonth(d time.Time) time.Time {
|
|
|
+ d = d.AddDate(0, 0, -d.Day()+1)
|
|
|
+ return getZeroTime(d)
|
|
|
+}
|
|
|
+
|
|
|
+//获取传入的时间所在年的第一天,即某年第一天的0点。如传入time.Now(), 返回当前年的第一天0点时间。
|
|
|
+func getFirstDateOfYear(d time.Time) time.Time {
|
|
|
+ d = d.AddDate(0, -d.Year()+1, 0)
|
|
|
+ return getZeroTime(d)
|
|
|
+}
|
|
|
+
|
|
|
+//获取传入的时间所在月份的最后一天,即某月最后一天的0点。如传入time.Now(), 返回当前月份的最后一天0点时间。
|
|
|
+func getLastDateOfMonth(d time.Time) time.Time {
|
|
|
+ return getFirstDateOfMonth(d).AddDate(0, 1, -1)
|
|
|
+}
|
|
|
+
|
|
|
+//获取某一天的0点时间
|
|
|
+func getZeroTime(d time.Time) time.Time {
|
|
|
+ return time.Date(d.Year(), d.Month(), d.Day(), 0, 0, 0, 0, d.Location())
|
|
|
+}
|