Stop loss, Scale out and Trailing stop using the ATR
Forums › ProRealTime English forum › ProOrder support › Stop loss, Scale out and Trailing stop using the ATR
- This topic has 3 replies, 2 voices, and was last updated 4 years ago by robertogozzi.
-
-
03/08/2020 at 3:33 AM #121459
Hello everybody first time on this forum I hope this is the right place for this discussion.
I am having trouble coding a strategy to back test a number of different algorithms ( combination of indicators). Below is what I have so far and it is not doing what I expect or more importantly what I want. I have commented where I think it is needed to help explain what I am trying to do.
In summary I wish to have a stop loss 1.5 X the ATR at time of trade entry and when break even is reached I wish to close ½ the position and let the other half run (still with the same stop loss). When the trade reaches 2 X the ATR figure at trade entry in favour of my trade I wish to apply a trailing stop to move with the trade as profit increases, that trailing stop is the same as the original stop loss (1.5 X ATR at trade entry).
The trade is closed when either my stop loss is hit or my exit conditions (indicators) signal me to exit. You will see there is an entry condition I have named “continuation trade” this is simply to allow for re-traces (to exit the trade and then get back in if conditions are favourable).
Any help is welcome however time is getting away from me and I really need to solve this quickly so I can get testing algo’s. Thanks in advance.
Scale out123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139DEFPARAM CumulateOrders = False // Cumulating positions deactivated// Prevents the system from placing new orders on specified days of the weekdaysForbiddenEntry = OpenDayOfWeek = 0// --- settingsamount = 2 // amount of contract/lots/shares to open for each orderLot2Close = 1 // amount of contract/lots/shares to close when breakeven occursbreakeven = averagetruerange*1.5 // multiple of ATR at time of trade entry to reach no loss tradestoploss = averagetruerange*1.5 // multiple of ATR at time of trade entry to stop further loss//trailing stop functiontrailingstart = averagetruerange*2 //trailingstop will start at mulitple of ATR points profit at time of trade entry//trailing step is moving the "stoploss" as trade moves in your favour// end settings// Conditions to enter long positionsindicator1 = HullAverage[50](close)c1 = (close > indicator1)indicator2, indicator3, ignored, indicator4 = CALL "Aroon oscillator"[14]c2 = (indicator2 CROSSES OVER indicator3)indicator5 = Average[5](close)-Average[25](close)c3 = (indicator5 > 0)c4 = (indicator2 CROSSES OVER indicator4)// original entry signal Aroon zero crossIF (c1 AND c2 AND c3) AND not daysForbiddenEntry THENBUY amount CONTRACT AT MARKET// "continuation trade" signal (not opening 2nd position) Aroon line crosses upper line c4 and correct side of moving average c1 2nd confirmation indicator long c3 (long again signal)ELSIF NOT LONGONMARKET AND c4 AND c1 AND c3 AND not daysForbiddenEntry THENBUY amount CONTRACT AT MARKETstoploss = averagetruerange[14]*1.5breakeven = averagetruerange[14]*1.5trailingstart = averagetruerange[14]*2ENDIF// Conditions to exit long positionsindicator6 = HullAverage[50](close)c5 = (close < indicator6)indicator7, indicator8, ignored, indicator9 = CALL "Aroon oscillator"[14]c6 = (indicator7 CROSSES UNDER indicator8)indicator10 = Average[5](close)-Average[25](close)c7 = (indicator10 < 0)c8 = (indicator7 CROSSES UNDER indicator9)// Manage trade "scale out" at breakeven (half off the table)IF NOT ONMARKET THENbreakeven =0stoploss =0trailingstart =0newSL =0ENDIFIF LONGONMARKET AND countoflongshares= amount AND close-tradeprice[1]=> breakeven THENSELL Lot2Close CONTRACT AT MARKETENDIF// Move to "trailing stop" (trailing start) then apply trailing step (difference in close of trade price in favour of trade) to new stoploss (trailing stop)IF LONGONMARKET AND newSL=0 AND close-tradeprice(1)=> trailingstart THENnewSL = close-tradeprice(1) - stoplossENDIF//next movesIF LONGONMARKET AND newSL>0 AND close-tradeprice(1)> close-tradeprice (-1) THENnewSL = newSL+(close-tradeprice(1)-close-tradeprice(-1))ENDIF// New StoplossIF LONGONMARKET AND close-tradeprice(1)=< newSL THENSELL AT MARKET// Normal exit indicator signalsELSIF c5 OR c6 OR c7 OR c8 THENSELL AT MARKETstoploss = averagetruerange[14]*1.5breakeven = averagetruerange[14]*1.5ENDIF// Conditions to enter short positionsindicator11 = HullAverage[50](close)c9 = (close < indicator11)indicator12, indicator13, indicator14, ignored = CALL "Aroon oscillator"[14]c10 = (indicator12 CROSSES UNDER indicator13)indicator15 = Average[5](close)-Average[25](close)c11 = (indicator15 < 0)c12 = (indicator12 CROSSES UNDER indicator14)// original entry signal Aroon zero crossIF (c9 AND c10 AND c11) AND not daysForbiddenEntry THENSELLSHORT amount CONTRACT AT MARKET// "continuation trade" signal(not opening 2nd position) Aroon line crosses lower line c12 and correct side of moving average c9 2nd confirmation indicator short c11 (short again signal)ELSIF NOT SHORTONMARKET AND c12 AND c9 AND c11 AND not daysForbiddenEntry THENSELLSHORT amount CONTRACT AT MARKETstoploss = averagetruerange[14]*1.5breakeven = averagetruerange[14]*1.5trailingstart = averagetruerange[14]*2ENDIF// Conditions to exit short positionsindicator16 = HullAverage[50](close)c13 = (close > indicator16)indicator17, indicator18, indicator19, ignored = CALL "Aroon oscillator"[14]c14 = (indicator17 CROSSES OVER indicator18)indicator20 = Average[5](close)-Average[25](close)c15 = (indicator20 CROSSES OVER 0)c16 = (indicator17 CROSSES OVER indicator19)// Manage trade "scale out" at breakeven (half off the table)IF NOT ONMARKET THENbreakeven =0stoploss =0trailingstart =0newSL =0ENDIFIF SHORTONMARKET AND countofshortshares= amount AND close-tradeprice[1]=> breakeven THENEXITSHORT Lot2Close CONTRACT AT MARKETENDIF// Move to "trailing stop" (trailing start) then apply trailing step (difference in close of trade price in favour of trade) to new stoploss (trailing stop)IF SHORTONMARKET AND newSL=0 AND close-tradeprice(1)=> trailingstart THENnewSL= close-tradeprice(1)+ stoplossENDIF//next movesIF SHORTONMARKET AND newSL>0 AND close-tradeprice(1)< close-tradeprice(-1) THENnewSL = newSL-(close-tradeprice(-1)-close-tradeprice(1))ENDIF// New StoplossIF SHORTONMARKET AND close-tradeprice(1)=> newSL THENEXITSHORT AT MARKET// Normal exit indicator signalsELSIF c13 OR c14 OR c15 OR c16 THENEXITSHORT AT MARKETstoploss = averagetruerange[14]*1.5breakeven = averagetruerange[14]*1.5ENDIF// Stops and targetsSET STOP pLOSS stoplossP.S. from time to time I get an error message when I click the back
test my system button. The message reads.
Line 141 (the line number varies)
One of the following characters would be more suitable than “End of code” : null
However there is no code at this line even the line number does not appear, the code finishes two lines earlier and then the next lines number appears 1 line below where the code ends with again no code written.
Once again thanks in advance.
Regards 19robhfx60
1 user thanked author for this post.
03/08/2020 at 6:13 AM #121461Lines 30-32 should be moved just below line 26, which is the first entry (if I correctly understood your code) and lines 91-93 should be moved to line 87.
Lines 107-112 can be removed, since they are a duplicate of lines 45-50.
Lines 51 and 113 have [1] next to keyword TRADEPRICE, replace it by (1).
Negative values (-1) are NOT allowed anywhere as an index reference. If you want to refer to the value prior to (1) then you have to write (2). What do you want to do with those lines?
What use are lines 70-71 and 132-133?
You never exit upon NEWSL, as it is in the original code where you read about that trailing stop. At the end you should append these lines:1234IF NewSL > 0 THENSELL AT NewSL STOPEXITSHORT AT NewSL STOPENDIFPartial positions CANNOT be closed in strategies. I know this can be done only when backtesting though I have never used it, thus I cannot tell you how to tell your code to close, say, only one out of two positions opened. Perhaps it’s just:
12SELL 1 CONTRACT AT MARKETEXITSHORT 1 CONTRACT AT MARKET03/09/2020 at 10:20 AM #121522Hello Roberto. Thank you for the quick reply I was not expecting to get a reply so quickly. As you can tell by my code I’m new to this coding thing and that string of code was a combination of several other bits of coding from similar topics so there is some repeating myself going on there. I am aware about the closing of half positions situation, however I will be trading manually to begin with so there many ways to solve that issue . I will implement the changes you have suggested and let you know the result Thank you for your knowledge and time and again thank you for the quick reply. oh I nearly forgot any ideas about the error message thing at the end of my post?
Regards 19robhfx60
03/09/2020 at 10:57 AM #121535Tehere are so many errors… when you modify the code many of them will be corrected.
I’ve been returned an error about the HullAverage since I have v10.3 which has not a built-in HULL average. If you have v11 this will not be the error you are talking about.
-
AuthorPosts
Find exclusive trading pro-tools on