Skip to content

Commit

Permalink
Implements entering immediately.
Browse files Browse the repository at this point in the history
  • Loading branch information
marianogappa committed Aug 2, 2021
1 parent 7284b8b commit 75850de
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 5 deletions.
2 changes: 1 addition & 1 deletion signalchecker/signalchecker.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ func (s *checkSignalState) applyTick(tick common.Tick, err error) (bool, error)
if s.hasInvalidAt && (tickTime.After(s.invalidAt) || tickTime.Equal(s.invalidAt)) {
return s.applyEvent(common.INVALIDATED, tick), nil
}
if !s.entered && tick.Price >= s.input.EnterRangeLow && tick.Price <= s.input.EnterRangeHigh {
if !s.entered && ((s.input.EnterRangeLow == -1 && s.input.EnterRangeHigh == -1) || (tick.Price >= s.input.EnterRangeLow && tick.Price <= s.input.EnterRangeHigh)) {
s.entered = true
return s.applyEvent(common.ENTERED, tick), nil
}
Expand Down
36 changes: 36 additions & 0 deletions signalchecker/signalchecker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,42 @@ func TestSignalChecker(t *testing.T) {
IsError: false,
},
},
{
name: "Enters immediately",
input: common.SignalCheckInput{
EnterRangeLow: f(-1.0),
EnterRangeHigh: f(-1.0),
StopLoss: f(0.1),
InitialISO8601: startISO8601,
InvalidateISO8601: "",
InvalidateAfterSeconds: 10,
TakeProfits: []common.JsonFloat64{5.0, 6.0, 7.0},
TakeProfitRatios: []common.JsonFloat64{0.5, 0.25, 0.25},
IfTP1StopAtEntry: false,
IfTP2StopAtTP1: false,
IfTP3StopAtTP2: false,
IfTP4StopAtTP3: false,
DontCalculateMaxEnterUSD: true,
},
candlesticks: []common.Candlestick{
{Timestamp: startTs + 0, LowestPrice: f(0.2), HighestPrice: f(0.2), Volume: f(1.0)},
{Timestamp: startTs + 1, LowestPrice: f(1.0), HighestPrice: f(1.0), Volume: f(1.0)},
{Timestamp: startTs + 2, LowestPrice: f(0.3), HighestPrice: f(0.3), Volume: f(1.0)},
},
expected: common.SignalCheckOutput{
Events: []common.SignalCheckOutputEvent{
{EventType: common.ENTERED, At: startISO8601, Price: f(0.2)},
{EventType: common.FINISHED_DATASET, At: tick3, Price: f(0.3)},
},
Entered: true,
FirstCandleOpenPrice: f(0.2),
FirstCandleAt: startISO8601,
HighestTakeProfit: 0,
ReachedStopLoss: false,
ProfitRatio: f(0.0),
IsError: false,
},
},
{
name: "Stops losses",
input: common.SignalCheckInput{
Expand Down
8 changes: 4 additions & 4 deletions signalchecker/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,16 +37,16 @@ func validateInput(input common.SignalCheckInput) (common.SignalCheckOutput, err
if input.EnterRangeHigh < input.EnterRangeLow {
return invalidateWith(common.ErrEnterRangeHighIsLessThanEnterRangeLow, input)
}
if !input.IsShort && input.StopLoss != -1 && input.StopLoss >= input.EnterRangeLow {
if !input.IsShort && input.StopLoss != -1 && input.EnterRangeLow != -1 && input.StopLoss >= input.EnterRangeLow {
return invalidateWith(common.ErrStopLossIsGreaterThanOrEqualToEnterRangeLow, input)
}
if input.IsShort && input.StopLoss != -1 && input.StopLoss <= input.EnterRangeHigh {
if input.IsShort && input.StopLoss != -1 && input.EnterRangeHigh != -1 && input.StopLoss <= input.EnterRangeHigh {
return invalidateWith(common.ErrStopLossIsLessThanOrEqualToEnterRangeHigh, input)
}
if !input.IsShort && len(input.TakeProfits) > 0 && input.TakeProfits[0] <= input.EnterRangeHigh {
if !input.IsShort && input.EnterRangeHigh != -1 && len(input.TakeProfits) > 0 && input.TakeProfits[0] <= input.EnterRangeHigh {
return invalidateWith(common.ErrFirstTPIsLessThanOrEqualToEnterRangeHigh, input)
}
if input.IsShort && len(input.TakeProfits) > 0 && input.TakeProfits[0] >= input.EnterRangeLow {
if input.IsShort && input.EnterRangeLow != -1 && len(input.TakeProfits) > 0 && input.TakeProfits[0] >= input.EnterRangeLow {
return invalidateWith(common.ErrFirstTPIsGreaterThanOrEqualToEnterRangeLow, input)
}
if input.Exchange == "" {
Expand Down

0 comments on commit 75850de

Please sign in to comment.