Backtesting with Stop and Limit Orders

wayum999

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

I have made a custom strategy that deploys stop loss orders after a buy signaled by a study. When I go to backtest this strategy in the backtesting tool, it has trades that exceed my stop limit orders which means that the back test is not factoring these into the results. I have turned tick data on and all the other tools I can think of, but still the same result. Does the backtesting functionality not factor in limit and stop loss orders?

Thanks for any light you can shed on this situation.
 

wayum999

Active member
Joined
May 26, 2020
Posts
26
Likes
5
I didnt think the backtesting tool accounting for stop losses or limits, but when testing my strategy, it did seem to change when changing these parameters. That being said, I never fully trust the back testing tool. I find its always best to run the strategy in replay and let it play out to get the most accurate test.

Hope that helps at all!
 

bart

Member
Joined
Jul 27, 2020
Posts
21
Likes
4
I am looking for a stop loss code if you are will to share but also did you try it on replay mode to see if it does actually exit on stop loss?
 

wayum999

Active member
Joined
May 26, 2020
Posts
26
Likes
5
hey there, sorry for the late reply. To add a stop loss to your code, you will need to set inputs in your settings so you or the user can set them.
Then you will need to calculation the stop loss price when a buy or sell is triggered. Something like this:

float offset = (float)(getSettings().getDouble(MAX_LOSS) * ctx.getInstrument().getPointSize());
float basePrice = ctx.getAvgEntryPrice();
basePrice -= offset

The best place to understand and learn this, along with using ATR stop losses as trails and setting exist limit orders or break evens, you should look at the trade manager source code. i have put a link below it for your convenience. Everything is in there; it is a lot to get through if you are not familiar with Java, but it was a life saver in my learning how this worked. And it does work in back testing as well.


Hope that helps, cheers!
 

sas7085

Active member
Joined
Jul 7, 2020
Posts
33
Likes
7
Hi Wayum

Thanks for the reply . I will add the stop loss based on the ATR stop loss.
In the mean time i implemented the stop loss hard coded in the code.
 

wayum999

Active member
Joined
May 26, 2020
Posts
26
Likes
5
sounds good, be careful with the ATR stops, sometimes they can ruin your trade by taking you out too early..
 

sas7085

Active member
Joined
Jul 7, 2020
Posts
33
Likes
7
I am looking for a stop loss code if you are will to share but also did you try it on replay mode to see if it does actually exit on stop loss?

I have the hard coded piece of code , This is working fine. In the below code i have taken 25 points as my stop loss . You can modify that no to you suitable piece. Once the bar is closed the SL will be triggered. If using 30 mins candle and 30 min candle itself 100 pints then once that bar is completed this SL will be triggered.

Here is the code need to add in the Strategy class.

@Override
public void onBarUpdate(OrderContext ctx) {
Instrument instr = ctx.getInstrument();
float qty = (getSettings().getTradeLots() * instr.getDefaultQuantityAsFloat());

if (ctx.getAccountPosition() > 0 && instr.round(ctx.getAvgEntryPrice() - instr.getSellPrice()) >= 25) {
//System.out.println("SL triggered at price decresed by " + instr.round(ctx.getAvgEntryPrice() - instr.getSellPrice()) +"the account position " + ctx.getAccountPosition());
ctx.sell(qty);
}

if (ctx.getAccountPosition() < 0 && instr.round(instr.getBuyPrice() - ctx.getAvgEntryPrice()) >= 25) {
//System.out.println("SL triggered as price incresed by " + instr.round(instr.getBuyPrice() - ctx.getAvgEntryPrice()) +"the account position " + ctx.getAccountPosition());
ctx.buy(qty);

}
}
 

wayum999

Active member
Joined
May 26, 2020
Posts
26
Likes
5
that works, that trade manager java file taught me pretty much everything you need to know about the order management side of things, cheers!
 
Top