tradexx.go 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. /**
  2. * @Author: zou.yingbin
  3. * @Create : 2021/8/13 10:04
  4. * @Modify : 2021/8/13 10:04
  5. */
  6. package models
  7. import (
  8. "mtp2_if/db"
  9. "mtp2_if/mtpcache"
  10. "mtp2_if/utils"
  11. )
  12. // TradeGoodsEx 交易商品相关字段
  13. type TradeGoodsEx struct {
  14. GOODSNAME string `json:"goodsname" xorm:"'GOODSNAME'"` // 商品名称
  15. GOODSCODE string `json:"goodscode" xorm:"'GOODSCODE'"` // 商品代码
  16. DECIMALPLACE int `json:"decimalplace" xorm:"'DECIMALPLACE'"` // 商品价格小数位
  17. QTYDECIMALPLACE int `json:"qtydecimalplace" xorm:"'QTYDECIMALPLACE'"` // 商品成交量小数位
  18. AGREEUNIT float64 `json:"agreeunit" xorm:"'AGREEUNIT'"` // 合约乘数
  19. MARKETID int32 `json:"marketid" xorm:"'MARKETID'"` // 市场id
  20. MARKETNAME string `json:"marketname" xorm:"'MARKETNAME'"` // 市场名称
  21. GOODUNITID int32 `json:"goodunitid" xorm:"'GOODUNITID'"` // 商品单位id
  22. CURRENCYID int32 `json:"currencyid" xorm:"'CURRENCYID'"` // 商品币种id
  23. CURRENCYNAME string `json:"currencyname" xorm:"'CURRENCYNAME'"` // 币种名称
  24. ENUMDICNAME string `json:"enumdicname" xorm:"'ENUMDICNAME'"` // 商品单位名称
  25. TRADEMODE int32 `json:"trademode" xorm:"'TRADEMODE'"` // 交易模式 - 10:做市 13:竞价 15:通道交易 16:挂牌点选 17:仓单贸易 18:期权 19:竞拍-降价式 20:竞拍-竞价式 21:竞拍-大宗式 22:受托竞价
  26. }
  27. // TradeAccountEx 交易账户相关字段
  28. type TradeAccountEx struct {
  29. TANAME string `json:"taname" xorm:"'TANAME'"` // 资金账号名称
  30. USERID int64 `json:"userid" xorm:"'USERID'"` // 用户id
  31. USERNAME string `json:"username" xorm:"'USERNAME'"` // 用户名称
  32. LOGINID string `json:"loginid" xorm:"'LOGINID'"` // 登录id
  33. LOGINCODE string `json:"logincode" xorm:"'LOGINCODE'"` // 登录代码
  34. LOGINNAME string `json:"loginname" xorm:"'LOGINNAME'"` // 登录名称
  35. }
  36. // TradePosition 持仓汇总
  37. type TradePosition struct {
  38. ACCOUNTID int64 `json:"accountid" xorm:"'ACCOUNTID'"` // 账户id
  39. GOODSID int32 `json:"goodsid" xorm:"'GOODSID'"` // 商品id
  40. HOLDERTYPE int32 `json:"holdertype" xorm:"'HOLDERTYPE'"` // 持仓类别 - 1:单边持仓 2:双边持仓
  41. TRADEPROPERTY int32 `json:"tradeproperty" xorm:"'TRADEPROPERTY'"` // 交易属性
  42. BUYORSELL int32 `json:"buyorsell" xorm:"'BUYORSELL'"` // 买卖方向 0-买 1-卖
  43. USEDMARGIN SFLOAT64 `json:"usedmargin" xorm:"'USEDMARGIN'"` // 占用保证金
  44. ORIQTY SFLOAT64 `json:"oriqty" xorm:"'ORIQTY'"` // 期初持仓数量
  45. QTY SFLOAT64 `json:"qty" xorm:"'QTY'"` // 持仓数量
  46. FROZENQTY SFLOAT64 `json:"frozenqty" xorm:"'FROZENQTY'"` // 冻结数量
  47. OTHERFROZENQTY SFLOAT64 `json:"otherfrozenqty" xorm:"'OTHERFROZENQTY'"` // 其它冻结数量
  48. CUROPENQTY SFLOAT64 `json:"curopenqty" xorm:"'CUROPENQTY'"` // 今日开仓数量
  49. CURCLOSEQTY SFLOAT64 `json:"curcloseqty" xorm:"'CURCLOSEQTY'"` // 今日平仓数量
  50. ORIAMOUNT SFLOAT64 `json:"oriamount" xorm:"'ORIAMOUNT'"` // 期初持仓金额
  51. AMOUNT SFLOAT64 `json:"amount" xorm:"'AMOUNT'"` // 持仓金额
  52. TradeGoodsEx `xorm:"extends"`
  53. TradeAccountEx `xorm:"extends"`
  54. HOLDERPRICE SFLOAT64 `json:"holderprice"` // 持仓均价 = 持仓金额 / 持仓数量 / 合约剩数 保留3位小数
  55. LASTPRICE SFLOAT64 `json:"lastprice"` // 最新价 从盘面上获取
  56. POSTIONPL SFLOAT64 `json:"postionpl"` // 持仓盈亏(浮动盈亏)
  57. }
  58. func (r *TradePosition) calc() {
  59. fNegativePower10 := func(n int, vl ...*SFLOAT64) {
  60. for i := range vl {
  61. vl[i].Power10(n * -1)
  62. }
  63. }
  64. // 数量 按成交量小数位缩小
  65. fNegativePower10(r.QTYDECIMALPLACE, &r.ORIQTY, &r.QTY, &r.FROZENQTY,
  66. &r.OTHERFROZENQTY, &r.CUROPENQTY, &r.CURCLOSEQTY)
  67. if r.AGREEUNIT > 1e-8 {
  68. r.HOLDERPRICE = r.AMOUNT / r.QTY / SFLOAT64(r.AGREEUNIT)
  69. r.HOLDERPRICE.Round(3)
  70. }
  71. if d, ok := mtpcache.GetQuotePrice(r.GOODSCODE); ok {
  72. r.LASTPRICE.SetByInt64(d, r.DECIMALPLACE)
  73. r.POSTIONPL = (r.LASTPRICE - r.HOLDERPRICE) * r.QTY * SFLOAT64(r.AGREEUNIT)
  74. r.POSTIONPL.Round(2)
  75. }
  76. }
  77. func (r *TradePosition) buildSql() string {
  78. var sqlId utils.SQLVal = "with gtmp as" +
  79. " (select g.goodsid," +
  80. " g.goodsname," +
  81. " g.goodscode," +
  82. " g.decimalplace," +
  83. " g.qtydecimalplace," +
  84. " g.agreeunit," +
  85. " g.marketid," +
  86. " g.goodunitid," +
  87. " g.currencyid," +
  88. " e.enumdicname," +
  89. " e2.enumdicname currencyname," +
  90. " m.marketname," +
  91. " m.trademode" +
  92. " from goods g" +
  93. " left join enumdicitem e" +
  94. " on g.goodunitid = e.enumitemname" +
  95. " and e.enumdiccode = 'goodsunit'" +
  96. " left join enumdicitem e2" +
  97. " on g.currencyid = e2.enumitemname" +
  98. " and e2.enumdiccode = 'currency'" +
  99. " left join market m" +
  100. " on g.marketid = m.marketid)," +
  101. "utmp as" +
  102. " (select ta.accountid," +
  103. " ta.accountname taname," +
  104. " u.userid," +
  105. " u.accountname username," +
  106. " a.loginid," +
  107. " b.logincode," +
  108. " b.accountname loginname" +
  109. " from taaccount ta" +
  110. " left join logintaaccount a" +
  111. " on ta.accountid = a.accountid" +
  112. " left join loginaccount b" +
  113. " on a.loginid = b.loginid" +
  114. " left join useraccount u" +
  115. " on ta.relateduserid = u.userid)" +
  116. "select k.*, t1.*, t2.*" +
  117. " from (select t.accountid," +
  118. " t.goodsid," +
  119. " t.holdertype," +
  120. " t.usedmargin," +
  121. " t.tradeproperty," +
  122. " 0 as buyorsell," +
  123. " t.buypositionqty as oriqty," +
  124. " t.buyholderamount as oriamount," +
  125. " t.buycurpositionqty as qty," +
  126. " t.buycurholderamount as amount," +
  127. " t.buyfrozenqty as frozenqty," +
  128. " t.buyotherfrozenqty as otherfrozenqty," +
  129. " t.buyopentotalqty curopenqty," +
  130. " t.buyclosetotalqty curcloseqty" +
  131. " from tradeposition t" +
  132. " where t.buycurpositionqty > 0" +
  133. " union all" +
  134. " select t.accountid," +
  135. " t.goodsid," +
  136. " t.holdertype," +
  137. " t.usedmargin," +
  138. " t.tradeproperty," +
  139. " 1 as buyorsell," +
  140. " t.sellpositionqty as oriqty," +
  141. " t.sellholderamount as oriamount," +
  142. " t.sellcurpositionqty as qty," +
  143. " t.sellcurholderamount as amount," +
  144. " t.sellfrozenqty as frozenqty," +
  145. " t.sellotherfrozenqty as otherfrozenqty," +
  146. " t.sellopentotalqty as curopenqty," +
  147. " t.sellclosetotalqty as curcloseqty" +
  148. " from tradeposition t" +
  149. " where t.sellcurpositionqty > 0) k" +
  150. " left join gtmp t1" +
  151. " on k.goodsid = t1.goodsid" +
  152. " left join utmp t2" +
  153. " on k.accountid = t2.accountid" +
  154. " where 1=1"
  155. return sqlId.String()
  156. }
  157. // GetDataEx 获取持仓汇总
  158. func (r *TradePosition) GetDataEx() (interface{}, error) {
  159. sData := make([]TradePosition, 0)
  160. err := db.GetEngine().SQL(r.buildSql()).Find(&sData)
  161. for i := range sData {
  162. sData[i].calc()
  163. }
  164. return sData, err
  165. }