qryOrder.go 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  1. package ermcp
  2. import (
  3. "mtp2_if/global/app"
  4. "mtp2_if/global/e"
  5. "mtp2_if/logger"
  6. "mtp2_if/models"
  7. "mtp2_if/utils"
  8. "net/http"
  9. "github.com/gin-gonic/gin"
  10. )
  11. // QueryErmcpTradePositionReq 获取企业风管期货持仓头寸信息请求参数
  12. type QueryErmcpTradePositionReq struct {
  13. AccountID int `form:"accountID" binding:"required"`
  14. MarketID int `form:"marketID"`
  15. }
  16. // QueryErmcpTradePositionRsp 获取企业风管期货持仓头寸信息返回模型
  17. type QueryErmcpTradePositionRsp struct {
  18. Goodsname string `json:"goodsname"` // 商品名称
  19. BuyOrSell int64 `json:"buyorsell"` // 方向 - 0:买 1:卖
  20. EnableQTY int64 `json:"enableqty"` // 可用量(总仓可用)
  21. CurPositionQTY int64 `json:"curpositionqty"` // 当前持仓总数量(总仓数量)
  22. Last float64 `json:"last"` // 现价
  23. CurHolderAmount float64 `json:"curholderamount" ` // 当前持仓总金额[商品币种](成本???)
  24. PositionPL float64 `json:"positionpl"` // 持仓盈亏
  25. PositionPLRate float64 `json:"positionplrate"` // 持仓盈亏率
  26. CurTDPosition int64 `json:"curtdposition"` // 期末今日头寸(今仓数量)
  27. CurTDPositionEnabled int64 `json:"curtdpositionenabled"` // 今仓可用
  28. AveragePrice float64 `json:"averageprice"` // 持仓均价
  29. UsedMargin float64 `json:"usedmargin"` // 占用保证金[商品币种]
  30. ExExehangeName string `json:"exexchangename"` // 外部交易所名称(简称)
  31. Goodsid int64 `json:"goodsid"` // 商品ID(自增ID SEQ_GOODS)
  32. Goodscode string `json:"goodscode"` // 商品代码(内部)
  33. Outgoodscode string `json:"outgoodscode"` // 商品代码(外部)
  34. Agreeunit float64 `json:"agreeunit"` // 合约单位
  35. Decimalplace int64 `json:"decimalplace"` // 报价小数位
  36. }
  37. // QueryErmcpTradePosition 获取企业风管期货持仓头寸信息
  38. // @Summary 获取企业风管期货持仓头寸信息
  39. // @Produce json
  40. // @Security ApiKeyAuth
  41. // @Param accountID query int true "资金账户ID"
  42. // @Param marketID query int false "所属市场ID"
  43. // @Success 200 {object} QueryErmcpTradePositionRsp
  44. // @Failure 500 {object} app.Response
  45. // @Router /Ermcp/QueryErmcpTradePosition [get]
  46. // @Tags 企业风险管理(app)
  47. func QueryErmcpTradePosition(c *gin.Context) {
  48. appG := app.Gin{C: c}
  49. // 获取请求参数
  50. var req QueryErmcpTradePositionReq
  51. if err := appG.C.ShouldBindQuery(&req); err != nil {
  52. logger.GetLogger().Errorf("QueryErmcpTradePosition failed: %s", err.Error())
  53. appG.Response(http.StatusBadRequest, e.INVALID_PARAMS, nil)
  54. return
  55. }
  56. rsp := make([]QueryErmcpTradePositionRsp, 0)
  57. // 获取资金账户信息
  58. taAccount, err := models.GetTaAccountByID(req.AccountID)
  59. if err != nil {
  60. logger.GetLogger().Errorf("QueryErmcpTradePosition failed: %s", err.Error())
  61. appG.Response(http.StatusBadRequest, e.ERROR_QUERY_FAIL, nil)
  62. return
  63. }
  64. if taAccount == nil {
  65. logger.GetLogger().Errorf("QueryErmcpTradePosition failed")
  66. appG.Response(http.StatusBadRequest, e.ERROR_QUERY_FAIL, nil)
  67. return
  68. }
  69. // 母账户要从HEDGE_OUTTRADEPOSITION表查询持仓头寸(未生成内部头寸);子账户从TRADEPOSITION表获持仓头寸
  70. if taAccount.Ismain == 1 {
  71. // 母账户
  72. hedgeOutTradePositions, err := models.GetHedgeOutTradePositions(req.AccountID, req.MarketID)
  73. if err != nil {
  74. // 查询失败
  75. logger.GetLogger().Errorf("QueryErmcpTradePosition failed: %s", err.Error())
  76. appG.Response(http.StatusBadRequest, e.ERROR_QUERY_FAIL, nil)
  77. return
  78. }
  79. // 分解买卖方向的头寸
  80. for _, v := range hedgeOutTradePositions {
  81. // 分解买卖方向的头寸
  82. if v.Curbuyposition > 0 {
  83. // 买方向
  84. item := QueryErmcpTradePositionRsp{
  85. BuyOrSell: 0,
  86. EnableQTY: v.Curbuyposition - v.Fretdbuyposition - v.Freydbuyposition, // 买可用 = 期末买头寸 - 冻结今日买头寸 - 冻结上日买头寸
  87. CurPositionQTY: v.Curbuyposition,
  88. CurHolderAmount: 0, // FIXME: - 缺少数据?
  89. CurTDPosition: v.Curtdbuyposition,
  90. CurTDPositionEnabled: v.Curtdbuyposition - v.Fretdbuyposition, // 今仓买可用 = 期末今日买头寸 - 冻结今日买头寸
  91. UsedMargin: 0, // FIXME: - 缺少数据?
  92. Goodsid: v.Hedgegoodsid,
  93. }
  94. // 获取对应商品信息
  95. goods, err := models.GetGoods(int(v.Hedgegoodsid))
  96. if err != nil {
  97. // 查询失败
  98. logger.GetLogger().Errorf("QueryErmcpTradePosition failed: %s", err.Error())
  99. appG.Response(http.StatusBadRequest, e.ERROR_GET_GOODS_FAILED, nil)
  100. return
  101. }
  102. if goods == nil {
  103. logger.GetLogger().Errorf("QueryErmcpTradePosition failed")
  104. appG.Response(http.StatusBadRequest, e.ERROR_GET_GOODS_FAILED, nil)
  105. return
  106. }
  107. item.Goodscode = goods.Goodscode
  108. item.Outgoodscode = goods.Outgoodscode
  109. item.Goodsname = goods.Goodsname
  110. item.Agreeunit = goods.Agreeunit
  111. // 获取对应外部交易所名称
  112. exchange, err := models.GetExternalexchangeByGoodsGroupID(int(goods.Goodsgroupid))
  113. if err != nil {
  114. // 查询失败
  115. logger.GetLogger().Errorf("QueryErmcpTradePosition failed: %s", err.Error())
  116. appG.Response(http.StatusBadRequest, e.ERROR_GET_MARKET_FAILED, nil)
  117. return
  118. }
  119. if exchange == nil {
  120. logger.GetLogger().Errorf("QueryErmcpTradePosition failed")
  121. appG.Response(http.StatusBadRequest, e.ERROR_GET_MARKET_FAILED, nil)
  122. return
  123. }
  124. item.ExExehangeName = exchange.Exchangefullname
  125. // 获取对应商品盘面信息
  126. quoteDays, err := models.GetQuoteDays("'" + goods.Outgoodscode + "'")
  127. if err != nil {
  128. logger.GetLogger().Errorf("QueryErmcpTradePosition failed: %s", err.Error())
  129. appG.Response(http.StatusBadRequest, e.ERROR_GET_GOODS_FAILED, nil)
  130. return
  131. }
  132. if len(quoteDays) > 0 {
  133. if quoteDays[0].Last != 0 {
  134. item.Last = utils.IntToFloat64(int(quoteDays[0].Last), int(goods.Decimalplace))
  135. }
  136. // 没有现价则尝试用昨结
  137. if item.Last == 0 && quoteDays[0].Presettle != 0 {
  138. item.Last = utils.IntToFloat64(int(quoteDays[0].Presettle), int(goods.Decimalplace))
  139. }
  140. }
  141. // FIXME: - 目前外部持仓头寸表里没有相关金额的字段,等相关服务处理
  142. rsp = append(rsp, item)
  143. }
  144. if v.Cursellposition > 0 {
  145. // 卖方向
  146. item := QueryErmcpTradePositionRsp{
  147. BuyOrSell: 1,
  148. EnableQTY: v.Cursellposition - v.Fretdsellposition - v.Freydsellposition,
  149. CurPositionQTY: v.Cursellposition,
  150. CurHolderAmount: 0, // FIXME: - 缺少数据?
  151. CurTDPosition: v.Curtdsellposition,
  152. CurTDPositionEnabled: v.Curtdsellposition - v.Fretdsellposition,
  153. UsedMargin: 0, // FIXME: - 缺少数据?
  154. Goodsid: v.Hedgegoodsid,
  155. }
  156. // 获取对应商品信息
  157. goods, err := models.GetGoods(int(v.Hedgegoodsid))
  158. if err != nil {
  159. // 查询失败
  160. logger.GetLogger().Errorf("QueryErmcpTradePosition failed: %s", err.Error())
  161. appG.Response(http.StatusBadRequest, e.ERROR_GET_GOODS_FAILED, nil)
  162. return
  163. }
  164. if goods == nil {
  165. logger.GetLogger().Errorf("QueryErmcpTradePosition failed")
  166. appG.Response(http.StatusBadRequest, e.ERROR_GET_GOODS_FAILED, nil)
  167. return
  168. }
  169. item.Goodscode = goods.Goodscode
  170. item.Outgoodscode = goods.Outgoodscode
  171. item.Goodsname = goods.Goodsname
  172. item.Agreeunit = goods.Agreeunit
  173. // 获取对应外部交易所名称
  174. exchange, err := models.GetExternalexchangeByGoodsGroupID(int(goods.Goodsgroupid))
  175. if err != nil {
  176. // 查询失败
  177. logger.GetLogger().Errorf("QueryErmcpTradePosition failed: %s", err.Error())
  178. appG.Response(http.StatusBadRequest, e.ERROR_GET_MARKET_FAILED, nil)
  179. return
  180. }
  181. if exchange == nil {
  182. logger.GetLogger().Errorf("QueryErmcpTradePosition failed")
  183. appG.Response(http.StatusBadRequest, e.ERROR_GET_MARKET_FAILED, nil)
  184. return
  185. }
  186. item.ExExehangeName = exchange.Exchangefullname
  187. // 获取对应商品盘面信息
  188. quoteDays, err := models.GetQuoteDays("'" + goods.Outgoodscode + "'")
  189. if err != nil {
  190. logger.GetLogger().Errorf("QueryErmcpTradePosition failed: %s", err.Error())
  191. appG.Response(http.StatusBadRequest, e.ERROR_GET_GOODS_FAILED, nil)
  192. return
  193. }
  194. if len(quoteDays) > 0 {
  195. if quoteDays[0].Last != 0 {
  196. item.Last = utils.IntToFloat64(int(quoteDays[0].Last), int(goods.Decimalplace))
  197. }
  198. // 没有现价则尝试用昨结
  199. if item.Last == 0 && quoteDays[0].Presettle != 0 {
  200. item.Last = utils.IntToFloat64(int(quoteDays[0].Presettle), int(goods.Decimalplace))
  201. }
  202. }
  203. // FIXME: - 目前外部持仓头寸表里没有相关金额的字段,等相关服务处理
  204. rsp = append(rsp, item)
  205. }
  206. }
  207. } else {
  208. // 子账户
  209. tradePositions, err := models.GetTradePositions(req.AccountID, req.MarketID)
  210. if err != nil {
  211. // 查询失败
  212. logger.GetLogger().Errorf("QueryErmcpTradePosition failed: %s", err.Error())
  213. appG.Response(http.StatusBadRequest, e.ERROR_QUERY_FAIL, nil)
  214. return
  215. }
  216. // 分解买卖方向的头寸
  217. for _, v := range tradePositions {
  218. if v.Buycurpositionqty > 0 {
  219. // 买方向
  220. item := QueryErmcpTradePositionRsp{
  221. BuyOrSell: 0,
  222. EnableQTY: v.Buycurpositionqty - v.Buyfrozenqty, // 买可用 = 买当前持仓总数量 - 买持仓冻结数量
  223. CurPositionQTY: v.Buycurpositionqty,
  224. CurHolderAmount: v.Buycurholderamount,
  225. CurTDPosition: v.Buycurtdposition,
  226. CurTDPositionEnabled: v.Buycurtdposition - v.Buyfretdposition, // 今仓买可用 = 买期末今日头寸 - 买冻结今日头寸
  227. UsedMargin: v.Usedmargin,
  228. Goodsid: int64(v.Goodsid),
  229. }
  230. // 获取对应商品信息
  231. goods, err := models.GetGoods(int(v.Goodsid))
  232. if err != nil {
  233. // 查询失败
  234. logger.GetLogger().Errorf("QueryErmcpTradePosition failed: %s", err.Error())
  235. appG.Response(http.StatusBadRequest, e.ERROR_GET_GOODS_FAILED, nil)
  236. return
  237. }
  238. if goods == nil {
  239. logger.GetLogger().Errorf("QueryErmcpTradePosition failed")
  240. appG.Response(http.StatusBadRequest, e.ERROR_GET_GOODS_FAILED, nil)
  241. return
  242. }
  243. item.Goodscode = goods.Goodscode
  244. item.Outgoodscode = goods.Outgoodscode
  245. item.Goodsname = goods.Goodsname
  246. item.Agreeunit = goods.Agreeunit
  247. // 获取对应外部交易所名称
  248. exchange, err := models.GetExternalexchangeByGoodsGroupID(int(goods.Goodsgroupid))
  249. if err != nil {
  250. // 查询失败
  251. logger.GetLogger().Errorf("QueryErmcpTradePosition failed: %s", err.Error())
  252. appG.Response(http.StatusBadRequest, e.ERROR_GET_MARKET_FAILED, nil)
  253. return
  254. }
  255. if exchange == nil {
  256. logger.GetLogger().Errorf("QueryErmcpTradePosition failed")
  257. appG.Response(http.StatusBadRequest, e.ERROR_GET_MARKET_FAILED, nil)
  258. return
  259. }
  260. item.ExExehangeName = exchange.Exchangefullname
  261. // 获取对应商品盘面信息
  262. quoteDays, err := models.GetQuoteDays("'" + goods.Outgoodscode + "'")
  263. if err != nil {
  264. logger.GetLogger().Errorf("QueryErmcpTradePosition failed: %s", err.Error())
  265. appG.Response(http.StatusBadRequest, e.ERROR_GET_GOODS_FAILED, nil)
  266. return
  267. }
  268. if len(quoteDays) > 0 {
  269. if quoteDays[0].Last != 0 {
  270. item.Last = utils.IntToFloat64(int(quoteDays[0].Last), int(goods.Decimalplace))
  271. }
  272. // 没有现价则尝试用昨结
  273. if item.Last == 0 && quoteDays[0].Presettle != 0 {
  274. item.Last = utils.IntToFloat64(int(quoteDays[0].Presettle), int(goods.Decimalplace))
  275. }
  276. }
  277. // 计算持仓均价 = 买当前持仓总金额 / 买当前持仓总数量 / 合约单位
  278. item.AveragePrice = v.Buycurholderamount / float64(v.Buycurpositionqty) / goods.Agreeunit
  279. // 计算持仓盈亏:买方向持仓盈亏 = (最新价 - 持仓价) * 数量 * 合约单位
  280. // 知识点:交易服务在结算后,会按结算价更新持仓单金额,所以持仓均价就是持仓价(结算价)
  281. // 知识点:持仓盈亏是今日浮动盈亏,针对结算价进行计算;平仓盈亏是针对建仓价进行计算
  282. // if item.Last == 0 {
  283. // item.PositionPL = 0
  284. // } else {
  285. // item.PositionPL = (item.Last - item.AveragePrice) * float64(v.Buycurpositionqty) * goods.Agreeunit
  286. // }
  287. // FIXME: - 与老邓及王旭讨论过后,确认后期头寸相关价格会由交易服务落地,目前暂不进行计算
  288. }
  289. if v.Sellcurpositionqty > 0 {
  290. // 卖方向
  291. item := QueryErmcpTradePositionRsp{
  292. BuyOrSell: 0,
  293. EnableQTY: v.Sellcurpositionqty - v.Sellfrozenqty,
  294. CurPositionQTY: v.Sellcurpositionqty,
  295. CurHolderAmount: v.Sellcurholderamount,
  296. CurTDPosition: v.Sellcurtdposition,
  297. CurTDPositionEnabled: v.Sellcurtdposition - v.Sellfretdposition,
  298. UsedMargin: v.Usedmargin,
  299. Goodsid: int64(v.Goodsid),
  300. }
  301. // 获取对应商品信息
  302. goods, err := models.GetGoods(int(v.Goodsid))
  303. if err != nil {
  304. // 查询失败
  305. logger.GetLogger().Errorf("QueryErmcpTradePosition failed: %s", err.Error())
  306. appG.Response(http.StatusBadRequest, e.ERROR_GET_GOODS_FAILED, nil)
  307. return
  308. }
  309. if goods == nil {
  310. logger.GetLogger().Errorf("QueryErmcpTradePosition failed")
  311. appG.Response(http.StatusBadRequest, e.ERROR_GET_GOODS_FAILED, nil)
  312. return
  313. }
  314. item.Goodscode = goods.Goodscode
  315. item.Outgoodscode = goods.Outgoodscode
  316. item.Goodsname = goods.Goodsname
  317. item.Agreeunit = goods.Agreeunit
  318. // 获取对应外部交易所名称
  319. exchange, err := models.GetExternalexchangeByGoodsGroupID(int(goods.Goodsgroupid))
  320. if err != nil {
  321. // 查询失败
  322. logger.GetLogger().Errorf("QueryErmcpTradePosition failed: %s", err.Error())
  323. appG.Response(http.StatusBadRequest, e.ERROR_GET_MARKET_FAILED, nil)
  324. return
  325. }
  326. if exchange == nil {
  327. logger.GetLogger().Errorf("QueryErmcpTradePosition failed")
  328. appG.Response(http.StatusBadRequest, e.ERROR_GET_MARKET_FAILED, nil)
  329. return
  330. }
  331. item.ExExehangeName = exchange.Exchangefullname
  332. // 获取对应商品盘面信息
  333. quoteDays, err := models.GetQuoteDays("'" + goods.Outgoodscode + "'")
  334. if err != nil {
  335. logger.GetLogger().Errorf("QueryErmcpTradePosition failed: %s", err.Error())
  336. appG.Response(http.StatusBadRequest, e.ERROR_GET_GOODS_FAILED, nil)
  337. return
  338. }
  339. if len(quoteDays) > 0 {
  340. if quoteDays[0].Last != 0 {
  341. item.Last = utils.IntToFloat64(int(quoteDays[0].Last), int(goods.Decimalplace))
  342. }
  343. // 没有现价则尝试用昨结
  344. if item.Last == 0 && quoteDays[0].Presettle != 0 {
  345. item.Last = utils.IntToFloat64(int(quoteDays[0].Presettle), int(goods.Decimalplace))
  346. }
  347. }
  348. // 计算持仓均价 = 买当前持仓总金额 / 买当前持仓总数量 / 合约单位
  349. item.AveragePrice = v.Buycurholderamount / float64(v.Buycurpositionqty) / goods.Agreeunit
  350. // 计算持仓盈亏:卖方向持仓盈亏 = (持仓价 - 最新价) * 数量 * 合约单位
  351. // 知识点:交易服务在结算后,会按结算价更新持仓单金额,所以持仓均价就是持仓价(结算价)
  352. // 知识点:持仓盈亏是今日浮动盈亏,针对结算价进行计算;平仓盈亏是针对建仓价进行计算
  353. }
  354. }
  355. }
  356. // 查询成功返回
  357. logger.GetLogger().Debugln("QueryErmcpTradePosition successed: %v", rsp)
  358. appG.Response(http.StatusOK, e.SUCCESS, rsp)
  359. }