mirror of
https://github.com/c9s/bbgo.git
synced 2024-11-10 09:11:55 +00:00
make session trades map thread safe
This commit is contained in:
parent
1c80d30ce2
commit
1892d03326
|
@ -216,9 +216,9 @@ func (environ *Environment) Init(ctx context.Context) (err error) {
|
|||
log.Infof("symbol %s: %d trades loaded", symbol, len(trades))
|
||||
}
|
||||
|
||||
session.Trades[symbol] = trades
|
||||
session.Trades[symbol] = &types.TradeSlice{Trades: trades}
|
||||
session.Stream.OnTradeUpdate(func(trade types.Trade) {
|
||||
session.Trades[trade.Symbol] = append(session.Trades[trade.Symbol], trade)
|
||||
session.Trades[symbol].Append(trade)
|
||||
})
|
||||
|
||||
session.lastPrices[symbol] = 0.0
|
||||
|
@ -271,7 +271,6 @@ func (environ *Environment) Init(ctx context.Context) (err error) {
|
|||
session.lastPrices[kline.Symbol] = kline.Close
|
||||
})
|
||||
|
||||
|
||||
// feed klines into the market data store
|
||||
if environ.startTime == emptyTime {
|
||||
environ.startTime = time.Now()
|
||||
|
|
|
@ -65,4 +65,3 @@ func TestMemoryService(t *testing.T) {
|
|||
assert.Equal(t, i, j)
|
||||
})
|
||||
}
|
||||
|
||||
|
|
|
@ -74,7 +74,7 @@ func (reporter *AverageCostPnLReporter) Run() {
|
|||
}
|
||||
|
||||
for _, symbol := range reporter.Symbols {
|
||||
report := calculator.Calculate(symbol, session.Trades[symbol], session.lastPrices[symbol])
|
||||
report := calculator.Calculate(symbol, session.Trades[symbol].Copy(), session.lastPrices[symbol])
|
||||
report.Print()
|
||||
}
|
||||
}
|
||||
|
|
|
@ -116,7 +116,7 @@ type ExchangeSession struct {
|
|||
|
||||
// Trades collects the executed trades from the exchange
|
||||
// map: symbol -> []trade
|
||||
Trades map[string][]types.Trade
|
||||
Trades map[string]*types.TradeSlice
|
||||
|
||||
// marketDataStores contains the market data store of each market
|
||||
marketDataStores map[string]*MarketDataStore
|
||||
|
@ -150,7 +150,7 @@ func NewExchangeSession(name string, exchange types.Exchange) *ExchangeSession {
|
|||
Stream: exchange.NewStream(),
|
||||
Subscriptions: make(map[types.Subscription]types.Subscription),
|
||||
Account: &types.Account{},
|
||||
Trades: make(map[string][]types.Trade),
|
||||
Trades: make(map[string]*types.TradeSlice),
|
||||
|
||||
markets: make(map[string]types.Market),
|
||||
startPrices: make(map[string]float64),
|
||||
|
|
|
@ -255,7 +255,7 @@ var BacktestCmd = &cobra.Command{
|
|||
return fmt.Errorf("last price not found: %s", symbol)
|
||||
}
|
||||
|
||||
report := calculator.Calculate(symbol, trades, lastPrice)
|
||||
report := calculator.Calculate(symbol, trades.Trades, lastPrice)
|
||||
report.Print()
|
||||
|
||||
initBalances := userConfig.Backtest.Account.Balances.BalanceMap()
|
||||
|
|
|
@ -18,13 +18,13 @@ func init() {
|
|||
|
||||
type TradeSlice struct {
|
||||
mu sync.Mutex
|
||||
Items []Trade
|
||||
Trades []Trade
|
||||
}
|
||||
|
||||
func (s *TradeSlice) Slice() []Trade {
|
||||
func (s *TradeSlice) Copy() []Trade {
|
||||
s.mu.Lock()
|
||||
slice := make([]Trade, len(s.Items), len(s.Items))
|
||||
copy(slice, s.Items)
|
||||
slice := make([]Trade, len(s.Trades), len(s.Trades))
|
||||
copy(slice, s.Trades)
|
||||
s.mu.Unlock()
|
||||
|
||||
return slice
|
||||
|
@ -32,7 +32,7 @@ func (s *TradeSlice) Slice() []Trade {
|
|||
|
||||
func (s *TradeSlice) Append(t Trade) {
|
||||
s.mu.Lock()
|
||||
s.Items = append(s.Items, t)
|
||||
s.Trades = append(s.Trades, t)
|
||||
s.mu.Unlock()
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue
Block a user