Skip to content

Commit

Permalink
Return last tick on error. Return last tick on finished_dataset.
Browse files Browse the repository at this point in the history
  • Loading branch information
marianogappa committed Aug 2, 2021
1 parent 3f3709a commit 7284b8b
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 14 deletions.
8 changes: 2 additions & 6 deletions signalchecker/signalchecker.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,12 +120,8 @@ func newChecker(input common.SignalCheckInput) *checkSignalState {
// N.B. appyEvent returns "isEnded" boolean, to decide whether to continue.
func (s *checkSignalState) applyEvent(eventType string, tick common.Tick) bool {
event := common.SignalCheckOutputEvent{EventType: eventType}
switch eventType {
case common.FINISHED_DATASET:
default:
event.At = common.ISO8601(time.Unix(int64(tick.Timestamp), 0).UTC().Format(time.RFC3339))
event.Price = tick.Price
}
event.At = common.ISO8601(time.Unix(int64(tick.Timestamp), 0).UTC().Format(time.RFC3339))
event.Price = tick.Price
s.events = append(s.events, event)
s.profitCalculator.ApplyEvent(event)
s.isEnded = eventType == common.FINISHED_DATASET || eventType == common.STOPPED_LOSS || s.profitCalculator.IsFinished()
Expand Down
12 changes: 7 additions & 5 deletions signalchecker/signalchecker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func TestSignalChecker(t *testing.T) {
{Timestamp: startTs + 2, LowestPrice: f(0.3), HighestPrice: f(0.3), Volume: f(1.0)},
},
expected: common.SignalCheckOutput{
Events: []common.SignalCheckOutputEvent{{EventType: common.FINISHED_DATASET}},
Events: []common.SignalCheckOutputEvent{{EventType: common.FINISHED_DATASET, At: tick3, Price: f(0.3)}},
Entered: false,
FirstCandleOpenPrice: f(0.2),
FirstCandleAt: startISO8601,
Expand Down Expand Up @@ -79,7 +79,7 @@ func TestSignalChecker(t *testing.T) {
expected: common.SignalCheckOutput{
Events: []common.SignalCheckOutputEvent{
{EventType: common.ENTERED, At: tick2, Price: f(1.0)},
{EventType: common.FINISHED_DATASET},
{EventType: common.FINISHED_DATASET, At: tick3, Price: f(0.3)},
},
Entered: true,
FirstCandleOpenPrice: f(0.2),
Expand Down Expand Up @@ -152,7 +152,7 @@ func TestSignalChecker(t *testing.T) {
Events: []common.SignalCheckOutputEvent{
{EventType: common.ENTERED, At: tick2, Price: f(1.0)},
{EventType: common.TAKEN_PROFIT_ + "1", At: tick3, Price: f(5.0)},
{EventType: common.FINISHED_DATASET},
{EventType: common.FINISHED_DATASET, At: tick3, Price: f(5.0)},
},
Entered: true,
FirstCandleOpenPrice: f(0.2),
Expand Down Expand Up @@ -204,7 +204,7 @@ func TestSignalChecker(t *testing.T) {
}
for _, ts := range tss {
t.Run(ts.name, func(t *testing.T) {
actual, err := doCheckSignal(ts.input, buildTickIterator(testCandlestickIterator(ts.candlesticks)))
actual, err := doCheckSignal(ts.input, common.NewCandlestickIterator(testCandlestickIterator(ts.candlesticks)))
if actual.IsError && err == nil {
t.Error("Output says there is an error but function did not return an error!")
t.FailNow()
Expand Down Expand Up @@ -244,11 +244,13 @@ func f(fl float64) common.JsonFloat64 {

func testCandlestickIterator(cs []common.Candlestick) func() (common.Candlestick, error) {
i := 0
last := common.Candlestick{}
return func() (common.Candlestick, error) {
if i >= len(cs) {
return common.Candlestick{}, common.ErrOutOfCandlesticks
return last, common.ErrOutOfCandlesticks
}
i++
last = cs[i-1]
return cs[i-1], nil
}
}
11 changes: 8 additions & 3 deletions signalchecker/tick_iterator.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,28 @@ func buildTickIterator(f func() (common.Candlestick, error)) func() (common.Tick
}

type tickIterator struct {
f func() (common.Candlestick, error)
ticks []common.Tick
f func() (common.Candlestick, error)
ticks []common.Tick
lastTick common.Tick
}

func newTickIterator(f func() (common.Candlestick, error)) *tickIterator {
return &tickIterator{f: f}
}

// N.B. When next() hits an error, it returns the previous (last) tick.
// This is because when ErrOutOfCandlesticks happens, we want the previous
// candlestick to calculate things for the finished_dataset event.
func (it *tickIterator) next() (common.Tick, error) {
if len(it.ticks) > 0 {
c := it.ticks[0]
it.ticks = it.ticks[1:]
it.lastTick = c
return c, nil
}
candlestick, err := it.f()
if err != nil {
return common.Tick{}, err
return it.lastTick, err
}
it.ticks = append(it.ticks, candlestick.ToTicks()...)
return it.next()
Expand Down

0 comments on commit 7284b8b

Please sign in to comment.