core: pull out RecoverTrade method

This commit is contained in:
c9s 2023-07-22 17:57:02 +08:00
parent fad8642a59
commit 2abd84aec9
No known key found for this signature in database
GPG Key ID: 7385E7E464CB0A54
2 changed files with 16 additions and 6 deletions

View File

@ -101,11 +101,8 @@ func (c *TradeCollector) Recover(ctx context.Context, ex types.ExchangeTradeHist
cnt := 0
for _, td := range trades {
logrus.Debugf("checking trade: %s", td.String())
if c.processTrade(td) {
logrus.Infof("recovered trade: %s", td.String())
if c.RecoverTrade(td) {
cnt++
c.EmitRecover(td)
}
}
@ -113,6 +110,19 @@ func (c *TradeCollector) Recover(ctx context.Context, ex types.ExchangeTradeHist
return nil
}
func (c *TradeCollector) RecoverTrade(td types.Trade) bool {
logrus.Debugf("checking trade: %s", td.String())
if c.processTrade(td) {
logrus.Infof("recovered trade: %s", td.String())
c.EmitRecover(td)
return true
}
// add to the trade store, and then we can recover it when an order is matched
c.tradeStore.Add(td)
return false
}
// Process filters the received trades and see if there are orders matching the trades
// if we have the order in the order store, then the trade will be considered for the position.
// profit will also be calculated.

View File

@ -16,7 +16,7 @@ func TestTradeCollector_ShouldNotCountDuplicatedTrade(t *testing.T) {
collector := NewTradeCollector(symbol, position, orderStore)
assert.NotNil(t, collector)
matched := collector.ProcessTrade(types.Trade{
matched := collector.RecoverTrade(types.Trade{
ID: 1,
OrderID: 399,
Exchange: types.ExchangeBinance,
@ -28,7 +28,7 @@ func TestTradeCollector_ShouldNotCountDuplicatedTrade(t *testing.T) {
IsBuyer: true,
})
assert.False(t, matched, "should be added to the trade store")
assert.Equal(t, 1, len(collector.tradeStore.Trades()), "should have one trade in the trade store")
assert.Equal(t, 1, len(collector.tradeStore.Trades()), "should have 1 trade in the trade store")
orderStore.Add(types.Order{
SubmitOrder: types.SubmitOrder{