Hi
In the below code, is the ATR related stop loss level re-calculated after close of every bar when trade is on market? I want to use the ATR of the bar just before the trade was executed. Any ideas how this can be coded please?
Thanks
Sachin
// Definition of code parameters
DEFPARAM CumulateOrders = False // Cumulating positions deactivated
x = AroonUp[10]
y = AroonDown[10]
// Conditions to enter long positions
IF (x > y) THEN
BUY 1 PERPOINT AT MARKET
ENDIF
// Conditions to exit long positions
IF (y > x) THEN
SELL AT MARKET
ENDIF
// Conditions to enter short positions
//
IF (y > x) THEN
SELLSHORT 1 PERPOINT AT MARKET
ENDIF
// Conditions to exit short positions
IF (x > y) THEN
EXITSHORT AT MARKET
ENDIF
SET STOP LOSS AverageTrueRange[14](close)
// Definition of code parameters
DEFPARAM CumulateOrders = False // Cumulating positions deactivated
IF OnMarket THEN
SL = 0
ELSE
SL = AverageTrueRange[14](close)
ENDIF
x = AroonUp[10]
y = AroonDown[10]
// Conditions to enter long positions
IF (x > y) THEN
BUY 1 PERPOINT AT MARKET
ENDIF
// Conditions to exit long positions
IF (y > x) THEN
SELL AT MARKET
ENDIF
// Conditions to enter short positions
//
IF (y > x) THEN
SELLSHORT 1 PERPOINT AT MARKET
ENDIF
// Conditions to exit short positions
IF (x > y) THEN
EXITSHORT AT MARKET
ENDIF
IF SL > 0 THEN
SET STOP LOSS SL
ENDIF
you need to calculate and set SL only when not Onmarket, once a trade is opened it must no be set anymore.
Try this (not tested). The ATR stop is fixed at the ATR value at the close of the decision bar which is the one before the trade is actually executed. Not sure if you mean this bar or the one before the decision bar so just change the [0] to [1] in AverageTrueRange[14](close[0]) if it is the the ATR of the bar before the decision bar that you mean.
// Definition of code parameters
DEFPARAM CumulateOrders = False // Cumulating positions deactivated
x = AroonUp[10]
y = AroonDown[10]
// Conditions to enter long positions
IF not longonmarket and(x > y) THEN
BUY 1 PERPOINT AT MARKET
SET STOP LOSS AverageTrueRange[14](close[0])
ENDIF
// Conditions to exit long positions
IF (y > x) THEN
SELL AT MARKET
ENDIF
// Conditions to enter short positions
//
IF not shortonmarket and (y > x) THEN
SELLSHORT 1 PERPOINT AT MARKET
SET STOP LOSS AverageTrueRange[14](close[0])
ENDIF
// Conditions to exit short positions
IF (x > y) THEN
EXITSHORT AT MARKET
ENDIF
Roberto – you beat me by three minutes but your code will not set the stop loss until the trade has been open for one bar.
Thanks Vonasi
Yes, ATR at close of the decision bar. Your code will execute both rows 10 and 11? No need for AND or anything else after MARKET on row 10 and before SET on row 11?
No. Any time SET is read in the code it sends an order to your broker. That order stays on the market till it is either filled or it is cancelled or changed by another SET instruction being read in the code.
Thank you Vonasi. Learnt something very useful today.
FYI – I’ve done a quick test and it seems to work