bbgo: make position object optional for trade collector

This commit is contained in:
c9s 2022-08-04 21:13:13 +08:00
parent cae8bc2882
commit f191d3c091
No known key found for this signature in database
GPG Key ID: 7385E7E464CB0A54

View File

@ -115,21 +115,26 @@ func (c *TradeCollector) Process() bool {
if c.orderStore.Exists(trade.OrderID) { if c.orderStore.Exists(trade.OrderID) {
c.doneTrades[key] = struct{}{} c.doneTrades[key] = struct{}{}
profit, netProfit, madeProfit := c.position.AddTrade(trade) if c.position != nil {
if madeProfit { profit, netProfit, madeProfit := c.position.AddTrade(trade)
p := c.position.NewProfit(trade, profit, netProfit) if madeProfit {
c.EmitTrade(trade, profit, netProfit) p := c.position.NewProfit(trade, profit, netProfit)
c.EmitProfit(trade, &p) c.EmitTrade(trade, profit, netProfit)
c.EmitProfit(trade, &p)
} else {
c.EmitTrade(trade, fixedpoint.Zero, fixedpoint.Zero)
c.EmitProfit(trade, nil)
}
positionChanged = true
} else { } else {
c.EmitTrade(trade, fixedpoint.Zero, fixedpoint.Zero) c.EmitTrade(trade, fixedpoint.Zero, fixedpoint.Zero)
c.EmitProfit(trade, nil)
} }
positionChanged = true
return true return true
} }
return false return false
}) })
if positionChanged {
if positionChanged && c.position != nil {
c.EmitPositionUpdate(c.position) c.EmitPositionUpdate(c.position)
} }
@ -149,16 +154,21 @@ func (c *TradeCollector) processTrade(trade types.Trade) bool {
return false return false
} }
profit, netProfit, madeProfit := c.position.AddTrade(trade) if c.position != nil {
if madeProfit { profit, netProfit, madeProfit := c.position.AddTrade(trade)
p := c.position.NewProfit(trade, profit, netProfit) if madeProfit {
c.EmitTrade(trade, profit, netProfit) p := c.position.NewProfit(trade, profit, netProfit)
c.EmitProfit(trade, &p) c.EmitTrade(trade, profit, netProfit)
c.EmitProfit(trade, &p)
} else {
c.EmitTrade(trade, fixedpoint.Zero, fixedpoint.Zero)
c.EmitProfit(trade, nil)
}
c.EmitPositionUpdate(c.position)
} else { } else {
c.EmitTrade(trade, fixedpoint.Zero, fixedpoint.Zero) c.EmitTrade(trade, fixedpoint.Zero, fixedpoint.Zero)
c.EmitProfit(trade, nil)
} }
c.EmitPositionUpdate(c.position)
c.doneTrades[key] = struct{}{} c.doneTrades[key] = struct{}{}
return true return true
} }