Quellcode durchsuchen

1.科学计数法问题
2.新查持仓汇总结构

zou.yingbin vor 4 Jahren
Ursprung
Commit
52e635bc1c
3 geänderte Dateien mit 209 neuen und 2 gelöschten Zeilen
  1. 22 2
      models/ermcpCommon.go
  2. 175 0
      models/tradexx.go
  3. 12 0
      mtpcache/goodsquote.go

+ 22 - 2
models/ermcpCommon.go

@@ -13,6 +13,7 @@ import (
 	"github.com/shopspring/decimal"
 	"math"
 	"mtp2_if/utils"
+	"strconv"
 	"strings"
 )
 
@@ -131,8 +132,7 @@ type SFLOAT64 float64
 
 // MarshalJSON
 func (r *SFLOAT64) MarshalJSON() ([]byte, error) {
-	sData := fmt.Sprintf("%g", *r)
-	return []byte(sData), nil
+	return []byte(r.string()), nil
 }
 
 // Float64
@@ -140,11 +140,31 @@ func (r *SFLOAT64) Float64() float64 {
 	return float64(*r)
 }
 
+func (r *SFLOAT64) string() string {
+	return strconv.FormatFloat(r.Float64(), 'f', -1, 64)
+}
+
 // Set
 func (r *SFLOAT64) Set(v float64) {
 	*r = SFLOAT64(v)
 }
 
+// SetByInt64
+func (r *SFLOAT64) SetByInt64(v int64, place int) {
+	if place == 0 {
+		*r = SFLOAT64(v)
+	} else if place > 0 {
+		str := fmt.Sprintf("%v", *r)
+		if len(str) >= place {
+			pos := len(str) - place
+			str = str[0:pos] + "." + str[pos:]
+			if d, err := strconv.ParseFloat(str, 64); err == nil {
+				*r = SFLOAT64(d)
+			}
+		}
+	}
+}
+
 // Round
 func (r *SFLOAT64) Round(places int32) *SFLOAT64 {
 	d, ok := decimal.NewFromFloat(float64(*r)).Round(places).Float64()

+ 175 - 0
models/tradexx.go

@@ -0,0 +1,175 @@
+/**
+* @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.userid = 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
+}

+ 12 - 0
mtpcache/goodsquote.go

@@ -0,0 +1,12 @@
+/**
+* @Author: zou.yingbin
+* @Create  : 2021/8/13 12:46
+* @Modify  : 2021/8/13 12:46
+ */
+
+package mtpcache
+
+// GetQuotePrice 获取最新行情, 如若没有, 则获取昨结
+func GetQuotePrice(goodsCode string) (int64, bool) {
+	return 0, false
+}