Stop-Loss in automated custom strategy - MW SDK

sas7085

Active member
Joined
Jul 7, 2020
Posts
33
Likes
7
Hi The existing strategy dint have any stop-loss feature. The Trade Manager - the manual one is having but it is required in all automated strategy.
Any help will be appreciated to add stop loss in custom strategy.
 

sas7085

Active member
Joined
Jul 7, 2020
Posts
33
Likes
7
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);

}
}
 
Last edited:
Top