Fixed ATR stop loss

Questions about MultiCharts and user contributed studies.
Tibouss
Posts: 31
Joined: 26 May 2022
Has thanked: 7 times
Been thanked: 1 time

Fixed ATR stop loss

Postby Tibouss » 07 Jun 2022

Hello

I use an ATR for my stop loss. The problem is that the stop loss is recalculated after every bar.

I would like to have a fixed ATR stop loss based on the ATR value when the trade is initiated and which will remain fixed until the trade end.

The current code is like this (simplified code) :

Code: Select all

inputs: ATRLengthS (5), NumATRsS (5); variables: var1( 0 ); condition3 = marketposition=0 and .... ; if condition3 then Buy ( "BBStochLE" ) next bar at market ; condition4 = marketposition=0 and ..... ; if condition4 then sellshort ( "BBStochDLX" ) next bar at market ; condition5 = marketposition=1 and ..... ; if condition5 then sell( "BBStochSE" ) next bar at market ; condition6 = marketposition=-1 and ..... ; if condition6 then buytocover ( "BBStochSX" ) next bar at market ; SetStopLoss( AvgTrueRange( ATRLengthS * NumATRsS )) ;


Can someone helps me to code it?

Tibouss
Posts: 31
Joined: 26 May 2022
Has thanked: 7 times
Been thanked: 1 time

Re: Fixed ATR stop loss

Postby Tibouss » 08 Jun 2022

I get the answer.

I post it here if someone might be interested :

Code: Select all

inputs: ATRLengthS (5), NumATRsS (5); variables: var1( 0 ); ATRStopLossValue( 0 ); condition3 = marketposition=0 and .... ; if condition3 then Buy ( "BBStochLE" ) next bar at market ; condition4 = marketposition=0 and ..... ; if condition4 then sellshort ( "BBStochDLX" ) next bar at market ; condition5 = marketposition=1 and ..... ; if condition5 then sell( "BBStochSE" ) next bar at market ; condition6 = marketposition=-1 and ..... ; if condition6 then buytocover ( "BBStochSX" ) next bar at market ; if condition3 or condition4 or condition5 or condition6 then ATRStopLossValue = AvgTrueRange( ATRLengthS ) * NumATRsS ; SetStopLoss( ATRStopLossValue ) ; Or you could do condition3 = marketposition=0 and .... ; if condition3 then begin Buy ( "BBStochLE" ) next bar at market ; ATRStopLossValue = AvgTrueRange( ATRLengthS ) * NumATRsS ; end; condition4 = marketposition=0 and ..... ; if condition4 then begin sellshort ( "BBStochDLX" ) next bar at market ; ATRStopLossValue = AvgTrueRange( ATRLengthS ) * NumATRsS ; end; condition5 = marketposition=1 and ..... ; if condition5 then begin sell( "BBStochSE" ) next bar at market ; ATRStopLossValue = AvgTrueRange( ATRLengthS ) * NumATRsS ; end; condition6 = marketposition=-1 and ..... ; if condition6 then begin buytocover ( "BBStochSX" ) next bar at market ; ATRStopLossValue = AvgTrueRange( ATRLengthS ) * NumATRsS ; end; SetStopLoss( ATRStopLossValue ) ;


Return to “MultiCharts”