Initial stop-loss and profit target with ATR

I have mentioned this issue here before.  https://strategyquant.com/forum/topic/sqxs-new-method-slpt-based-on-fixed-pips-or-atr-value/


The initial stop loss and profit target in SQ X, with ATR , would not be accurate, even though the setting with fixed pips is right.


This is a minor, but very serious mistake. Because it is against logic and common sense. I am not sure about SQ X's own calculation rule. But in MC/TS code, I guess it's wrong, and in mt4/mt5 code also.


I do an informal test. This a strategy generated by SQ X on daily data.


There is a profitable trade start from 2018/06/29. So I check the SL and PT values with this n the MC's strategy code.

//check the sl moveif Date>=1180628 and Date<=1180806 then begin print("date: ",date,",mp=",marketposition,",entryprice=",entryprice,",atr=",SQ_ATR(20)[1],",sl=",LongSL);end



And the result is ,
date: 1180628.00,mp=   0.00,entryprice=   0.00,atr=  70.37,sl=3500.00
date: 1180629.00,mp=   1.00,entryprice=3749.00,atr=  71.70,sl=3649.00
date: 1180702.00,mp=   1.00,entryprice=3749.00,atr=  71.71,sl=3649.00
date: 1180703.00,mp=   1.00,entryprice=3749.00,atr=  71.68,sl=3649.00
date: 1180704.00,mp=   1.00,entryprice=3749.00,atr=  70.79,sl=3650.00
date: 1180705.00,mp=   1.00,entryprice=3749.00,atr=  70.40,sl=3650.00
date: 1180706.00,mp=   1.00,entryprice=3749.00,atr=  70.43,sl=3650.00
date: 1180709.00,mp=   1.00,entryprice=3749.00,atr=  70.56,sl=3650.00
date: 1180710.00,mp=   1.00,entryprice=3749.00,atr=  71.23,sl=3649.00
date: 1180711.00,mp=   1.00,entryprice=3749.00,atr=  70.22,sl=3651.00
date: 1180712.00,mp=   1.00,entryprice=3749.00,atr=  70.06,sl=3651.00
date: 1180713.00,mp=   1.00,entryprice=3749.00,atr=  72.06,sl=3648.00
date: 1180716.00,mp=   1.00,entryprice=3749.00,atr=  70.70,sl=3650.00
date: 1180717.00,mp=   1.00,entryprice=3749.00,atr=  69.32,sl=3652.00
date: 1180718.00,mp=   1.00,entryprice=3749.00,atr=  69.15,sl=3652.00
date: 1180719.00,mp=   1.00,entryprice=3749.00,atr=  67.60,sl=3654.00
date: 1180720.00,mp=   1.00,entryprice=3749.00,atr=  67.92,sl=3654.00
date: 1180723.00,mp=   1.00,entryprice=3749.00,atr=  67.07,sl=3655.00
date: 1180724.00,mp=   1.00,entryprice=3749.00,atr=  66.32,sl=3656.00
date: 1180725.00,mp=   1.00,entryprice=3749.00,atr=  65.90,sl=3657.00
date: 1180726.00,mp=   1.00,entryprice=3749.00,atr=  65.16,sl=3658.00
date: 1180727.00,mp=   1.00,entryprice=3749.00,atr=  63.50,sl=3660.00
date: 1180730.00,mp=   1.00,entryprice=3749.00,atr=  66.22,sl=3656.00
date: 1180731.00,mp=   1.00,entryprice=3749.00,atr=  65.01,sl=3658.00
date: 1180801.00,mp=   1.00,entryprice=3749.00,atr=  65.01,sl=3658.00
date: 1180802.00,mp=   1.00,entryprice=3749.00,atr=  67.26,sl=3655.00
date: 1180803.00,mp=   1.00,entryprice=3749.00,atr=  67.00,sl=3655.00
date: 1180806.00,mp=   1.00,entryprice=3749.00,atr=  67.05,sl=3655.00


The Sl and PT value would be changed bar by bar, because the ATR value is not the fixed one. 


In the picture, I have plot the SL and PT line.


 Although the changes were modest. But the PT/SL ratio could be changed.  Also, let's assume  the volatility is small when entering the market, then the ATR value will increase after  rapid increase, and then fall quickly. The initial stop loss will be greater than the value set at the beginning.

Attachments
slptline.png
(103.91 KiB)
  • Votes +2
  • Project StrategyQuant X
  • Type Bug
  • Status Fixed
  • Priority Normal

History

e
#1

eastpeace

14.07.2019 09:26

Task created

MF
#2

Mark Fric

18.07.2019 13:11

Status changed from New to Refused

I understand your point, but it is like this on purpose and it is because of money management.


See, if you have MM to risk for example 5% of your account balance, it is computed from Stop Loss. So that you'll lose only 5% when your SL is hit.

This is why we need SL to be computed at the time when trade is placed, and it cannot be dynamically recomputed every bar.



e
#3

eastpeace

19.07.2019 08:40

Hello, Mark. I agree with you in the first half.


And in the TS/MC code now , the SL level and PT target is truly changed every bar when it hold position. You can print the SL/PT value in MC.  Please checked it again.                     


With Stop entry order, we know the entry price firstly.   A little trouble when using Market order. In bar by bar mode of MC, it can't compute the SL/PT with ATR at the same time of entry.  


We can assume that the entry price is the close of   previous signal bar.  



MF
#4

Mark Fric

19.07.2019 14:07

Status changed from Refused to Fixed

ok, you are right. I modified EasyLanguage code to match how I described it. 


It now sets SL/PT only once, when order was opened - as same as in SQ 3.

e
#5

eastpeace

18.09.2019 10:56

Attachment Strategy 28305 0918.sqx added

Attachment atr stop.png added

I find that the bug have not been fixed yet.


date: 1180921.00,mp=   1.00,entryprice=3385.60,atr=   9.73,sl=3373.00
date: 1180921.00,mp=   1.00,entryprice=3385.60,atr=   9.86,sl=3372.80
date: 1180921.00,mp=   1.00,entryprice=3385.60,atr=   9.87,sl=3372.80
date: 1180921.00,mp=   1.00,entryprice=3385.60,atr=  10.10,sl=3372.40
date: 1180921.00,mp=   1.00,entryprice=3385.60,atr=  10.03,sl=3372.60
date: 1180921.00,mp=   1.00,entryprice=3385.60,atr=  10.29,sl=3372.20
date: 1180921.00,mp=   1.00,entryprice=3385.60,atr=  10.38,sl=3372.20
date: 1180921.00,mp=   1.00,entryprice=3385.60,atr=  10.67,sl=3371.80
date: 1180921.00,mp=   1.00,entryprice=3385.60,atr=  10.54,sl=3371.80
date: 1180925.00,mp=   1.00,entryprice=3385.60,atr=  10.71,sl=3371.60
date: 1180925.00,mp=   1.00,entryprice=3385.60,atr=  12.12,sl=3369.80
date: 1180925.00,mp=   1.00,entryprice=3385.60,atr=  12.07,sl=3370.00
date: 1180925.00,mp=   1.00,entryprice=3385.60,atr=  12.12,sl=3369.80
date: 1180925.00,mp=   1.00,entryprice=3385.60,atr=  12.12,sl=3369.80
date: 1180925.00,mp=   1.00,entryprice=3385.60,atr=  11.91,sl=3370.20
date: 1180925.00,mp=   1.00,entryprice=3385.60,atr=  11.55,sl=3370.60
date: 1180925.00,mp=   1.00,entryprice=3385.60,atr=  11.36,sl=3370.80
date: 1180925.00,mp=   1.00,entryprice=3385.60,atr=  11.19,sl=3371.00
date: 1180925.00,mp=   1.00,entryprice=3385.60,atr=  10.95,sl=3371.40
date: 1180925.00,mp=   1.00,entryprice=3385.60,atr=  10.80,sl=3371.60
date: 1180925.00,mp=   1.00,entryprice=3385.60,atr=  10.86,sl=3371.40
date: 1180925.00,mp=   1.00,entryprice=3385.60,atr=  10.68,sl=3371.80
date: 1180925.00,mp=   1.00,entryprice=3385.60,atr=  10.43,sl=3372.00
date: 1180925.00,mp=   1.00,entryprice=3385.60,atr=  10.33,sl=3372.20

MF
#6

Mark Fric

20.09.2019 08:25

yes, we made it differently. 


we originally wanted to make it as same as MetaTrader has it, but then decided to leave it like it was - SL/PT will be computed and set every bar.

 

The reason is that in EasyLanguage you cannot store value to a gloval variable, so when you'll stop the strategy and then start it again you'd lose the values of SL/PT that were stored in memory and opened trades would have no SL/PT.

e
#7

eastpeace

22.09.2019 09:28

MC has global variables, gvsetnamedint and gvgetnamedint.


And even if you don't use global variables, the EL language can lock the atr value, or stop loss, profit target. Assign the values when a condition  will only be satisfied once.


Here is an example.



vars:
 ma1(0),ma2(0),longcond(False),atrvalue(0),atrvalue2(0),
 sl(0),pt(0),sl2(0),pt2(0);
 
 
ma1 = Average(c,10);
ma2 = Average(c,30);

longcond = ma1 cross above ma2;


atrvalue = AvgTrueRange(20);  // updated bar by bar

if longcond then begin

 buy 1 contract next bar at market;
end;

if marketposition=1 then begin

 if barssinceentry = 0 then begin  // The condition is only satisfied once, so the values always equal to the value calculated for this time.
  atrvalue2 = AvgTrueRange(20); // never change
  sl = entryprice - 3 * atrvalue; //never change
  pt = entryprice + 5 * atrvalue; // never change
  
  //sl2 = entryprice - 3 * atrvalue2;
  //pt2 = entryprice + 5 * atrvalue2;

 end;
 
 sell 1 contract next bar at sl stop;
 sell 1 contract next bar at pt limit;


end;


//check the sl,pt value  

if Date>=1190826 { and Date<=1180806} then begin
 print("date: ",date," ,time=",time,",mp=",marketposition,",entryprice=",entryprice,",atr1=",atrvalue,",atr2-",atrvalue2," ,sl=",sl," ,pt=",pt);
end



MF
#8

Mark Fric

23.09.2019 13:22
global variables are available in MultiCharts, but not in Tradestation, our code must be universal.


And locking variables as you suggest doesn't work if you disable and re-enable the startegy on chart, or when an error disables the strategy an dyou have to reenable them.


I don't think having it like we do this is a big deal, it is a standard way that is used in EasyLanguage.


e
#9

eastpeace

22.10.2019 10:58

Hello, Mark.


I have checked this issue. Even we don't use global variables, we can set the stop loss as a fixed value in multicharts. And it will not changed when the strategy is reactived.


And there is multicharts engine now, it should be suitable for MC, not TS.


Some strategies using atr sl or trailing sl  have more difference between in MC and SQ. But the ones with pips are good now. There is  some problem in the strategy development with atr SL, PT, Trailing stop.


At last. I think that the stop with accurate ATR support is very important. It should restrain the RR ratio, but now SL,PT are floating, And some instruments in futures market have very different price moving range and different volatility, even though they are relevant. So the strategy using ATR Sl/PT support are prefered for these commodity, so we can do additional market test.



b
#10

bentra

30.12.2019 21:45
Voted for this task.
CG
#11

Chris G

26.11.2021 18:08
Voted for this task.

Votes: +2

Drop files to upload

or

choose files

Max size: 5MB

Not allowed: exe, msi, application, reg, php, js, htaccess, htpasswd, gitignore

...
Wait please