ATR at entry price  [SOLVED]

Questions about MultiCharts and user contributed studies.
User avatar
meras
Posts: 11
Joined: 22 Feb 2022
Has thanked: 2 times
Been thanked: 1 time

ATR at entry price

Postby meras » 27 Jun 2022

Hi,
I'm trying to write a strategy where the TP/SL is defined by the ATR at entry price. But how can I specify that I need the ATR at that specific time and that it shouldnt change?

Code: Select all

variables: numbShares(200); //Take Profit if marketposition = 1 and (entryprice + averagetruerange(14)*1) < close then begin sell ("Take Profit") numbShares/2 contracts this bar at close; end; //Stop Loss if marketposition = 1 and (entryprice - averagetruerange(14)*1.5) > close then begin sell ("Stop Loss") numbShares contracts this bar at close; end;

User avatar
TJ
Posts: 7740
Joined: 29 Aug 2006
Location: Global Citizen
Has thanked: 1033 times
Been thanked: 2221 times

Re: ATR at entry price

Postby TJ » 27 Jun 2022

First, you create a variable to store the ATR value.
At the time of entry, you assign the ATR value to the said variable.

User avatar
meras
Posts: 11
Joined: 22 Feb 2022
Has thanked: 2 times
Been thanked: 1 time

Re: ATR at entry price

Postby meras » 28 Jun 2022

First, you create a variable to store the ATR value.
At the time of entry, you assign the ATR value to the said variable.
That was my thought as well. But what should i do to make sure the ATR is assigned at that specific time?

User avatar
TJ
Posts: 7740
Joined: 29 Aug 2006
Location: Global Citizen
Has thanked: 1033 times
Been thanked: 2221 times

Re: ATR at entry price

Postby TJ » 28 Jun 2022

First, you create a variable to store the ATR value.
At the time of entry, you assign the ATR value to the said variable.
That was my thought as well. But what should i do to make sure the ATR is assigned at that specific time?
That depends on how you code your entry.
If it is BUY or SELLSHORT at market, you can assign the value at the same time.

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

Re: ATR at entry price

Postby Tibouss » 28 Jun 2022

Thank you very much!!

You will find the entire code below :

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 ) ;

User avatar
meras
Posts: 11
Joined: 22 Feb 2022
Has thanked: 2 times
Been thanked: 1 time

Re: ATR at entry price  [SOLVED]

Postby meras » 29 Jun 2022

Thank you very much!!

You will find the entire code below :

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 ) ;
Thanks buddy! I'll take inspiration from that ;)

// EDIT//

I did not know that you could assign values inside the begin->end sequence. That helps a lot for my other ideas. Thanks a lot!


Return to “MultiCharts”