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 [This can be configured in the strategy settings UI] . 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.
Not sure when to trigger the Buy / Sell as it reached the max point. IN my back testing it used to trigger once the bar is closed. Any Help to fire the SL as and when the SL is reached ?
Here is the code need to add in the Strategy class to trigger the stop loss.
Java:
@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);
}
}