bbgo: fix processTrade

This commit is contained in:
c9s 2022-01-07 16:52:45 +08:00
parent f4ebae17bb
commit e0b906a88b

View File

@ -93,13 +93,17 @@ func (c *TradeCollector) Process() bool {
return positionChanged
}
// processTrade takes a trade and see if there is a matched order
// if the order is found, then we add the trade to the position
// return true when the given trade is added
// return false when the given trade is not added
func (c *TradeCollector) processTrade(trade types.Trade) bool {
if c.orderStore.Exists(trade.OrderID) {
key := trade.Key()
// if it's already done, remove the trade from the trade store
if _, done := c.doneTrades[key]; done {
return true
return false
}
c.EmitTrade(trade)
@ -113,14 +117,23 @@ func (c *TradeCollector) processTrade(trade types.Trade) bool {
return false
}
// return true when the given trade is added
// return false when the given trade is not added
func (c *TradeCollector) ProcessTrade(trade types.Trade) bool {
if !c.processTrade(trade) {
c.tradeStore.Add(trade)
key := trade.Key()
// if it's already done, remove the trade from the trade store
if _, done := c.doneTrades[key]; done {
return false
}
if c.processTrade(trade) {
return true
}
c.tradeStore.Add(trade)
return false
}
// Run is a goroutine executed in the background
// Do not use this function if you need back-testing
func (c *TradeCollector) Run(ctx context.Context) {