Kill Switch

wayum999

Active member
Joined
May 26, 2020
Posts
26
Likes
5
Hello

I am trying to have a strategy stop or deactivate when it hits a stop loss order. I am wondering what the best way to go about doing this would be. I would like to not have any more trades initiated if the stop order is filled. I would then have to reactivate the strategy for any more trades to be made. This prevents a bunch of unwanted trades during a crash with dips and dives. Any help is greatly appreciated.
 

Spin

Well-known member
Joined
May 22, 2019
Posts
477
Likes
191
initiate a value "canOpenTrades" and set it to 1

Set this Value to 0 whenever a StopLoss is hit.

check before opening a trade that the value is 1 or 0
 

wayum999

Active member
Joined
May 26, 2020
Posts
26
Likes
5
yeah that was my thought exact...so i can be sure i would do something like:

var killSwitch = 0

and put this in the onActivate

then in trailFilled, but var killSwitch = 1

then in onSignal do something like

if (killSwitch = 1) {
return;
}
else {
the trading code
}

am I missing anything? I tried this initially without success but perhaps im missing a step..

that so much for your help!
 

Spin

Well-known member
Joined
May 22, 2019
Posts
477
Likes
191
That seems about right. Make sure your 'Killswitch' is defined class-wide, so that all parts of your code can 'see' it.

For debugging I would add these lines:

in trailFilled:
debug("trailFilled, so Killswitch to 1 " + killSwitch);

a line above the if (killSwitch =1) statement:
debug("check Killswitch before new trade " + killSwitch);

Good luck! (y)
 

sas7085

Active member
Joined
Jul 7, 2020
Posts
33
Likes
7
You can have like this

if (order.getType().equals(OrderType.STOP)) // Then stop may use setState(Enums.StrategyState.INACTIVE); or return based on the requirement.

else // proceed with your logic.
 
Top