| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175 |
- /**
- * @Author: zou.yingbin
- * @Create : 2021/8/13 10:04
- * @Modify : 2021/8/13 10:04
- */
- package models
- import (
- "mtp2_if/db"
- "mtp2_if/mtpcache"
- "mtp2_if/utils"
- )
- // TradeGoodsEx 交易商品相关字段
- type TradeGoodsEx struct {
- GOODSNAME string `json:"goodsname" xorm:"'GOODSNAME'"` // 商品名称
- GOODSCODE string `json:"goodscode" xorm:"'GOODSCODE'"` // 商品代码
- DECIMALPLACE int `json:"decimalplace" xorm:"'DECIMALPLACE'"` // 商品价格小数位
- QTYDECIMALPLACE int `json:"qtydecimalplace" xorm:"'QTYDECIMALPLACE'"` // 商品成交量小数位
- AGREEUNIT float64 `json:"agreeunit" xorm:"'AGREEUNIT'"` // 合约乘数
- MARKETID int32 `json:"marketid" xorm:"'MARKETID'"` // 市场id
- MARKETNAME string `json:"marketname" xorm:"'MARKETNAME'"` // 市场名称
- GOODUNITID int32 `json:"goodunitid" xorm:"'GOODUNITID'"` // 商品单位id
- CURRENCYID int32 `json:"currencyid" xorm:"'CURRENCYID'"` // 商品币种id
- CURRENCYNAME string `json:"currencyname" xorm:"'CURRENCYNAME'"` // 币种名称
- ENUMDICNAME string `json:"enumdicname" xorm:"'ENUMDICNAME'"` // 商品单位名称
- TRADEMODE int32 `json:"trademode" xorm:"'TRADEMODE'"` // 交易模式 - 10:做市 13:竞价 15:通道交易 16:挂牌点选 17:仓单贸易 18:期权 19:竞拍-降价式 20:竞拍-竞价式 21:竞拍-大宗式 22:受托竞价
- }
- // TradeAccountEx 交易账户相关字段
- type TradeAccountEx struct {
- TANAME string `json:"taname" xorm:"'TANAME'"` // 资金账号名称
- USERID int64 `json:"userid" xorm:"'USERID'"` // 用户id
- USERNAME string `json:"username" xorm:"'USERNAME'"` // 用户名称
- LOGINID string `json:"loginid" xorm:"'LOGINID'"` // 登录id
- LOGINCODE string `json:"logincode" xorm:"'LOGINCODE'"` // 登录代码
- LOGINNAME string `json:"loginname" xorm:"'LOGINNAME'"` // 登录名称
- }
- // TradePosition 持仓汇总
- type TradePosition struct {
- ACCOUNTID int64 `json:"accountid" xorm:"'ACCOUNTID'"` // 账户id
- GOODSID int32 `json:"goodsid" xorm:"'GOODSID'"` // 商品id
- HOLDERTYPE int32 `json:"holdertype" xorm:"'HOLDERTYPE'"` // 持仓类别 - 1:单边持仓 2:双边持仓
- TRADEPROPERTY int32 `json:"tradeproperty" xorm:"'TRADEPROPERTY'"` // 交易属性
- BUYORSELL int32 `json:"buyorsell" xorm:"'BUYORSELL'"` // 买卖方向 0-买 1-卖
- USEDMARGIN SFLOAT64 `json:"usedmargin" xorm:"'USEDMARGIN'"` // 占用保证金
- ORIQTY SFLOAT64 `json:"oriqty" xorm:"'ORIQTY'"` // 期初持仓数量
- QTY SFLOAT64 `json:"qty" xorm:"'QTY'"` // 持仓数量
- FROZENQTY SFLOAT64 `json:"frozenqty" xorm:"'FROZENQTY'"` // 冻结数量
- OTHERFROZENQTY SFLOAT64 `json:"otherfrozenqty" xorm:"'OTHERFROZENQTY'"` // 其它冻结数量
- CUROPENQTY SFLOAT64 `json:"curopenqty" xorm:"'CUROPENQTY'"` // 今日开仓数量
- CURCLOSEQTY SFLOAT64 `json:"curcloseqty" xorm:"'CURCLOSEQTY'"` // 今日平仓数量
- ORIAMOUNT SFLOAT64 `json:"oriamount" xorm:"'ORIAMOUNT'"` // 期初持仓金额
- AMOUNT SFLOAT64 `json:"amount" xorm:"'AMOUNT'"` // 持仓金额
- TradeGoodsEx `xorm:"extends"`
- TradeAccountEx `xorm:"extends"`
- HOLDERPRICE SFLOAT64 `json:"holderprice"` // 持仓均价 = 持仓金额 / 持仓数量 / 合约剩数 保留3位小数
- LASTPRICE SFLOAT64 `json:"lastprice"` // 最新价 从盘面上获取
- POSTIONPL SFLOAT64 `json:"postionpl"` // 持仓盈亏(浮动盈亏)
- }
- func (r *TradePosition) calc() {
- fNegativePower10 := func(n int, vl ...*SFLOAT64) {
- for i := range vl {
- vl[i].Power10(n * -1)
- }
- }
- // 数量 按成交量小数位缩小
- fNegativePower10(r.QTYDECIMALPLACE, &r.ORIQTY, &r.QTY, &r.FROZENQTY,
- &r.OTHERFROZENQTY, &r.CUROPENQTY, &r.CURCLOSEQTY)
- if r.AGREEUNIT > 1e-8 {
- r.HOLDERPRICE = r.AMOUNT / r.QTY / SFLOAT64(r.AGREEUNIT)
- r.HOLDERPRICE.Round(3)
- }
- if d, ok := mtpcache.GetQuotePrice(r.GOODSCODE); ok {
- r.LASTPRICE.SetByInt64(d, r.DECIMALPLACE)
- r.POSTIONPL = (r.LASTPRICE - r.HOLDERPRICE) * r.QTY * SFLOAT64(r.AGREEUNIT)
- r.POSTIONPL.Round(2)
- }
- }
- func (r *TradePosition) buildSql() string {
- var sqlId utils.SQLVal = "with gtmp as" +
- " (select g.goodsid," +
- " g.goodsname," +
- " g.goodscode," +
- " g.decimalplace," +
- " g.qtydecimalplace," +
- " g.agreeunit," +
- " g.marketid," +
- " g.goodunitid," +
- " g.currencyid," +
- " e.enumdicname," +
- " e2.enumdicname currencyname," +
- " m.marketname," +
- " m.trademode" +
- " from goods g" +
- " left join enumdicitem e" +
- " on g.goodunitid = e.enumitemname" +
- " and e.enumdiccode = 'goodsunit'" +
- " left join enumdicitem e2" +
- " on g.currencyid = e2.enumitemname" +
- " and e2.enumdiccode = 'currency'" +
- " left join market m" +
- " on g.marketid = m.marketid)," +
- "utmp as" +
- " (select ta.accountid," +
- " ta.accountname taname," +
- " u.userid," +
- " u.accountname username," +
- " a.loginid," +
- " b.logincode," +
- " b.accountname loginname" +
- " from taaccount ta" +
- " left join logintaaccount a" +
- " on ta.accountid = a.accountid" +
- " left join loginaccount b" +
- " on a.loginid = b.loginid" +
- " left join useraccount u" +
- " on ta.relateduserid = u.userid)" +
- "select k.*, t1.*, t2.*" +
- " from (select t.accountid," +
- " t.goodsid," +
- " t.holdertype," +
- " t.usedmargin," +
- " t.tradeproperty," +
- " 0 as buyorsell," +
- " t.buypositionqty as oriqty," +
- " t.buyholderamount as oriamount," +
- " t.buycurpositionqty as qty," +
- " t.buycurholderamount as amount," +
- " t.buyfrozenqty as frozenqty," +
- " t.buyotherfrozenqty as otherfrozenqty," +
- " t.buyopentotalqty curopenqty," +
- " t.buyclosetotalqty curcloseqty" +
- " from tradeposition t" +
- " where t.buycurpositionqty > 0" +
- " union all" +
- " select t.accountid," +
- " t.goodsid," +
- " t.holdertype," +
- " t.usedmargin," +
- " t.tradeproperty," +
- " 1 as buyorsell," +
- " t.sellpositionqty as oriqty," +
- " t.sellholderamount as oriamount," +
- " t.sellcurpositionqty as qty," +
- " t.sellcurholderamount as amount," +
- " t.sellfrozenqty as frozenqty," +
- " t.sellotherfrozenqty as otherfrozenqty," +
- " t.sellopentotalqty as curopenqty," +
- " t.sellclosetotalqty as curcloseqty" +
- " from tradeposition t" +
- " where t.sellcurpositionqty > 0) k" +
- " left join gtmp t1" +
- " on k.goodsid = t1.goodsid" +
- " left join utmp t2" +
- " on k.accountid = t2.accountid" +
- " where 1=1"
- return sqlId.String()
- }
- // GetDataEx 获取持仓汇总
- func (r *TradePosition) GetDataEx() (interface{}, error) {
- sData := make([]TradePosition, 0)
- err := db.GetEngine().SQL(r.buildSql()).Find(&sData)
- for i := range sData {
- sData[i].calc()
- }
- return sData, err
- }
|