Skip to content

Commit

Permalink
[hbdm] fix get position bug
Browse files Browse the repository at this point in the history
  • Loading branch information
nntaoli committed Nov 13, 2020
1 parent dad75f1 commit 32e00ff
Showing 1 changed file with 58 additions and 23 deletions.
81 changes: 58 additions & 23 deletions huobi/Hbdm.go
Original file line number Diff line number Diff line change
Expand Up @@ -199,37 +199,72 @@ func (dm *Hbdm) GetFuturePosition(currencyPair CurrencyPair, contractType string

// log.Println(data)

var positions []FuturePosition
var (
positions []FuturePosition
positionMap = make(map[string]FuturePosition, 1)
)

for _, d := range data {
if d.ContractType == "next_quarter" {
d.ContractType = BI_QUARTER_CONTRACT
}

if d.ContractType != contractType {
continue
}

pos := positionMap[d.ContractCode]
pos.ContractType = d.ContractType
pos.ContractId = int64(ToInt(d.ContractCode[3:]))
pos.Symbol = currencyPair

switch d.Direction {
case "buy":
positions = append(positions, FuturePosition{
ContractType: d.ContractType,
ContractId: int64(ToInt(d.ContractCode[3:])),
Symbol: currencyPair,
BuyAmount: d.Volume,
BuyAvailable: d.Available,
BuyPriceAvg: d.CostOpen,
BuyPriceCost: d.CostHold,
BuyProfitReal: d.ProfitRate,
BuyProfit: d.Profit,
LeverRate: d.LeverRate})
//positions = append(positions, FuturePosition{
// ContractType: d.ContractType,
// ContractId: int64(ToInt(d.ContractCode[3:])),
// Symbol: currencyPair,
// BuyAmount: d.Volume,
// BuyAvailable: d.Available,
// BuyPriceAvg: d.CostOpen,
// BuyPriceCost: d.CostHold,
// BuyProfitReal: d.ProfitRate,
// BuyProfit: d.Profit,
// LeverRate: d.LeverRate})
pos.BuyAmount = d.Volume
pos.BuyAvailable = d.Available
pos.BuyPriceAvg = d.CostOpen
pos.BuyPriceCost = d.CostHold
pos.BuyProfit = d.Profit
pos.BuyProfitReal = d.ProfitRate
pos.LeverRate = d.LeverRate
case "sell":
positions = append(positions, FuturePosition{
ContractType: d.ContractType,
ContractId: int64(ToInt(d.ContractCode[3:])),
Symbol: currencyPair,
SellAmount: d.Volume,
SellAvailable: d.Available,
SellPriceAvg: d.CostOpen,
SellPriceCost: d.CostHold,
SellProfitReal: d.ProfitRate,
SellProfit: d.Profit,
LeverRate: d.LeverRate})
// positions = append(positions, FuturePosition{
// ContractType: d.ContractType,
// ContractId: int64(ToInt(d.ContractCode[3:])),
// Symbol: currencyPair,
// SellAmount: d.Volume,
// SellAvailable: d.Available,
// SellPriceAvg: d.CostOpen,
// SellPriceCost: d.CostHold,
// SellProfitReal: d.ProfitRate,
// SellProfit: d.Profit,
// LeverRate: d.LeverRate})
pos.SellAmount = d.Volume
pos.SellAvailable = d.Available
pos.SellPriceAvg = d.CostOpen
pos.SellPriceCost = d.CostHold
pos.SellProfit = d.Profit
pos.SellProfitReal = d.ProfitRate
pos.LeverRate = d.LeverRate
}

positionMap[d.ContractCode] = pos
}

for _, pos := range positionMap {
if pos.BuyAmount > 0 || pos.SellAmount > 0 {
positions = append(positions, pos)
}
}

Expand Down

0 comments on commit 32e00ff

Please sign in to comment.