Fix bug from earlier fix

This commit is contained in:
Luke I. Wilson 2023-05-16 08:26:41 -05:00
parent cf119ea682
commit fae70079dd
2 changed files with 10 additions and 6 deletions

View File

@ -67,8 +67,15 @@ func (b *TestBroker) Advance() {
} }
func (b *TestBroker) Candles(symbol string, frequency string, count int) (*DataFrame, error) { func (b *TestBroker) Candles(symbol string, frequency string, count int) (*DataFrame, error) {
start := Max(Max(b.candleCount, 1)-count, 0)
end := b.candleCount - 1
if b.Data != nil && b.candleCount >= b.Data.Len() { // We have data and we are at the end of it. if b.Data != nil && b.candleCount >= b.Data.Len() { // We have data and we are at the end of it.
if count >= b.Data.Len() { // We are asking for more data than we have.
return b.Data.Copy(0, -1).(*DataFrame), ErrEOF return b.Data.Copy(0, -1).(*DataFrame), ErrEOF
} else {
return b.Data.Copy(start, -1).(*DataFrame), ErrEOF
}
} else if b.DataBroker != nil && b.Data == nil { // We have a data broker but no data. } else if b.DataBroker != nil && b.Data == nil { // We have a data broker but no data.
// Fetch a lot of candles from the broker so we don't keep asking. // Fetch a lot of candles from the broker so we don't keep asking.
candles, err := b.DataBroker.Candles(symbol, frequency, Max(count, 1000)) candles, err := b.DataBroker.Candles(symbol, frequency, Max(count, 1000))
@ -82,9 +89,6 @@ func (b *TestBroker) Candles(symbol string, frequency string, count int) (*DataF
// TODO: check if count > our rows if we are using a data broker and then fetch more data if so. // TODO: check if count > our rows if we are using a data broker and then fetch more data if so.
end := b.candleCount - 1
start := Max(Max(b.candleCount, 1)-count, 0)
return b.Data.Copy(start, end).(*DataFrame), nil return b.Data.Copy(start, end).(*DataFrame), nil
} }

View File

@ -65,8 +65,8 @@ func TestBacktestingBrokerCandles(t *testing.T) {
for i := 0; i < 7; i++ { // 6 because we want to call broker.Candles 9 times total for i := 0; i < 7; i++ { // 6 because we want to call broker.Candles 9 times total
broker.Advance() broker.Advance()
candles, err = broker.Candles("EUR_USD", "D", 5) candles, err = broker.Candles("EUR_USD", "D", 5)
if err != nil { if err != nil && err != ErrEOF && i != 6 { // Allow ErrEOF on last iteration.
t.Fatalf("Got an error on iteration %d: %v (called Candles %d times)", i, err, 2+i+1) t.Fatalf("Got an error on iteration %d: %v (called Advance() %d times)", i, err, broker.CandleIndex()+1)
} }
if candles == nil { if candles == nil {
t.Errorf("Candles is nil on iteration %d", i+1) t.Errorf("Candles is nil on iteration %d", i+1)