Stop Loss/Target Profit linked to ATR
Forums › ProRealTime English forum › ProOrder support › Stop Loss/Target Profit linked to ATR
- This topic has 12 replies, 2 voices, and was last updated 5 years ago by
fdetomas.
-
-
05/07/2019 at 12:20 PM #97836
Hi,
I am not very familiar with coding but I am trying to learn PRT as quickly as I can and I am benefiting of the “simplified creation” in the meantime.
The PRT code below has been created through simplified creation but I have a problem with the ATR indicator because I would like to have the Stop Loss/Target Profit defined with an ATR value that is calculated at the time the position is started, and does not change as long as the position is open.
At the moment, in my code below, the Stop Loss/ Target Profit are linked to ATR, but that indicator is trailing with the current price.
Would anyone be able to tell me how to code a Stop Loss/Target Profit linked to the ATR calculated at the time the position is started?
Any help is very much appreciated
My System1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556// Definition of code parametersDEFPARAM CumulateOrders = False // Cumulating positions deactivated// The system will cancel all pending orders and close all positions at 0:00. No new ones will be allowed until after the "FLATBEFORE" time.DEFPARAM FLATBEFORE = 080000// Cancel all pending orders and close all positions at the "FLATAFTER" timeDEFPARAM FLATAFTER = 180000// Prevents the system from placing new orders on specified days of the weekdaysForbiddenEntry = OpenDayOfWeek = 6 OR OpenDayOfWeek = 0// Conditions to enter long positionsindicator1 = BollingerUp[20](close)c1 = (close > indicator1)indicator2 = RSI[14](close)c2 = (indicator2 > 70)IF (c1 AND c2) AND not daysForbiddenEntry THENBUY 1 SHARES AT MARKETENDIF// Conditions to exit long positionsindicator3 = BollingerDown[20](close)c3 = (close < indicator3)indicator4 = RSI[14](close)c4 = (indicator4 < 30)IF c3 AND c4 THENSELL AT MARKETENDIF// Conditions to enter short positionsindicator5 = BollingerDown[20](close)c5 = (close < indicator5)indicator6 = RSI[14](close)c6 = (indicator6 < 30)IF (c5 AND c6) AND not daysForbiddenEntry THENSELLSHORT 1 SHARES AT MARKETENDIF// Conditions to exit short positionsindicator7 = BollingerUp[20](close)c7 = (close > indicator7)indicator8 = RSI[14](close)c8 = (indicator8 > 70)IF c7 AND c8 THENEXITSHORT AT MARKETENDIF// Stops and targetsindicator9 = averagetruerange[14]indicator10 = averagetruerange[14]*2SET STOP LOSS indicator9SET TARGET PROFIT indicator1005/07/2019 at 1:28 PM #97842You need to store the values at the same time as the order is placed and then ensure that if you are on the market those instructions are not used again even if the entry conditions become true again.
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859// Definition of code parametersDEFPARAM CumulateOrders = False // Cumulating positions deactivated// The system will cancel all pending orders and close all positions at 0:00. No new ones will be allowed until after the "FLATBEFORE" time.DEFPARAM FLATBEFORE = 080000// Cancel all pending orders and close all positions at the "FLATAFTER" timeDEFPARAM FLATAFTER = 180000// Prevents the system from placing new orders on specified days of the weekdaysForbiddenEntry = OpenDayOfWeek = 6 OR OpenDayOfWeek = 0// Conditions to enter long positionsindicator1 = BollingerUp[20](close)c1 = (close > indicator1)indicator2 = RSI[14](close)c2 = (indicator2 > 70)IF not longonmarket and (c1 AND c2) AND not daysForbiddenEntry THENBUY 1 SHARES AT MARKETindicator9 = averagetruerange[14]indicator10 = averagetruerange[14]*2ENDIF// Conditions to exit long positionsindicator3 = BollingerDown[20](close)c3 = (close < indicator3)indicator4 = RSI[14](close)c4 = (indicator4 < 30)IF c3 AND c4 THENSELL AT MARKETENDIF// Conditions to enter short positionsindicator5 = BollingerDown[20](close)c5 = (close < indicator5)indicator6 = RSI[14](close)c6 = (indicator6 < 30)IF not shortonmarket and (c5 AND c6) AND not daysForbiddenEntry THENSELLSHORT 1 SHARES AT MARKETindicator9 = averagetruerange[14]indicator10 = averagetruerange[14]*2ENDIF// Conditions to exit short positionsindicator7 = BollingerUp[20](close)c7 = (close > indicator7)indicator8 = RSI[14](close)c8 = (indicator8 > 70)IF c7 AND c8 THENEXITSHORT AT MARKETENDIF// Stops and targetsSET STOP LOSS indicator9SET TARGET PROFIT indicator101 user thanked author for this post.
05/07/2019 at 5:24 PM #9785405/07/2019 at 11:34 PM #97877I have tested the code for the Stop Loss/Target Profit ATR and it works perfectly.
The only small issue now is that when a signal to close a position is generated, only the closing position trade is produced. I would have expected the code to generate two trades simultaneously, the closing position trade and the trade for the new position.
At the moment, when a trade is closed, the new position in the opposite direction is only triggered if an additional signal is generated during a successive time frame.
I tried to modify the code inputting “SELL 2 SHARES AT MARKET” among the conditions to exit long positions, but that did not work.
I would appreciate if someone could help me in sorting this out.
05/08/2019 at 8:18 AM #97882Try removing:
1DEFPARAM CumulateOrders = FalseThis instruction prevents any orders going to market at the close of a bar if you already have an open position. Cumulate positions can also be avoided by using the following in your conditions which we are now doing:
123not onmarketnot longonmarketnot shortonmarket1 user thanked author for this post.
05/08/2019 at 8:50 AM #97884Thanks for the reply.
Removing “DEFPARAM CumulateOrders = False” didn’t work.
My issue is actually the opposite, I need two positions to be started at the same time whenever there is a Buy(Exit) or Sell(Exit) signal triggered. In other words, the Buy(Exit) and the Buy(Entry) trades should happen at the same time.
As can be seen in the attached Printscreen, at the moment the code produces the Exit order but not the Entry one, which is generated only in a following cycle if an additional Buy(Entry) or Sell(Entry) signal is generated.
I am trying to find a way to have the Exit and Entry orders generated at the same time and I would really appreciate any advice on how to do it.
05/08/2019 at 9:02 AM #97888I do not fully understand your last post but maybe the following helps:
If a strategy sends a SELL order to the market to exit a long position but at the same time also sends a BUY order to the market then they cancel each other out and nothing happens. If they didn’t cancel each other out then you would just be closing a trade and opening a trade and paying the spread every time.
Strategies can never be long and short at the same time so any order to enter in an opposite direction to the current open positions closes all trades.
1 user thanked author for this post.
05/08/2019 at 9:17 AM #97892Many thanks for the quick reply.
I need to be able to generate two SELL orders at the same time; one SELL order to the market to exit a long position and another SELL order to start a new position.
At the moment, the second SELL order, the one that starts the new position, is generated only in a successive cycle.
The attached Printscreen shows that the SELL(Exit) order is generated at 5:05 and the SELL(Entry) is generated only at 5:15.
I would like to be able to generate the SELL(Exit) and SELL(Entry) at the same time to get the same price.
Thanks
05/08/2019 at 9:52 AM #97895What you have described is what will happen whenever a SELLSHORT order is sent to the market while you have a long position open. All long positions will be closed and a short position opened at the same time.
I can only assume that your strategy is closing the short position because either the stop loss or target profit are hit and not because short entry conditions have been met. My platform is not open at the moment for me to test anything.
I notice that you have instructions to close a long trade that are identical to your instructions to open a short trade. Try removing them like this:
1234567891011121314151617181920212223242526272829303132333435363738// Definition of code parameters// The system will cancel all pending orders and close all positions at 0:00. No new ones will be allowed until after the "FLATBEFORE" time.DEFPARAM FLATBEFORE = 080000// Cancel all pending orders and close all positions at the "FLATAFTER" timeDEFPARAM FLATAFTER = 180000// Prevents the system from placing new orders on specified days of the weekdaysForbiddenEntry = OpenDayOfWeek = 6 OR OpenDayOfWeek = 0// Conditions to enter long positionsindicator1 = BollingerUp[20](close)c1 = (close > indicator1)indicator2 = RSI[14](close)c2 = (indicator2 > 70)IF not longonmarket and (c1 AND c2) AND not daysForbiddenEntry THENBUY 1 SHARES AT MARKETindicator9 = averagetruerange[14]indicator10 = averagetruerange[14]*2ENDIF// Conditions to enter short positionsindicator5 = BollingerDown[20](close)c5 = (close < indicator5)indicator6 = RSI[14](close)c6 = (indicator6 < 30)IF not shortonmarket and (c5 AND c6) AND not daysForbiddenEntry THENSELLSHORT 1 SHARES AT MARKETindicator9 = averagetruerange[14]indicator10 = averagetruerange[14]*2ENDIF// Stops and targetsSET STOP LOSS indicator9SET TARGET PROFIT indicator101 user thanked author for this post.
05/08/2019 at 2:15 PM #97963I modified the code replacing as follow:
Conditions to exit long positions
SELL AT MARKET was replaced with SELLSHORT 1 SHARES AT MARKET
Conditions to exit short positions
EXITSHORT AT MARKET was replaced with BUY 1 SHARES AT MARKET
As I wanted, when a SELLSHORT order is sent to the market while there is a long position open, all long positions are closed and a short position is opened at the same time.
However, the problem I have now is that my STOP LOSS and TARGET PROFIT conditions are not triggered when the SELLSHORT is inserted in the “Conditions to exit long positions” (the same happens with BUY 1 SHARES AT MARKET inserted in the “Conditions to exit shorts positions”).
I think that the cause may be the “not on market” condition. As the code sees an open position, it jumps my indicator9 and indicator10.
Is there any way to have the code going through STOP LOSS and TARGET PROFIT conditions also after a position is opened with a SELLSHORT command?
Thanks
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263// Definition of code parametersDEFPARAM CumulateOrders = False // Cumulating positions deactivated// The system will cancel all pending orders and close all positions at 0:00. No new ones will be allowed until after the "FLATBEFORE" time.DEFPARAM FLATBEFORE = 080000// Cancel all pending orders and close all positions at the "FLATAFTER" timeDEFPARAM FLATAFTER = 180000// Prevents the system from placing new orders on specified days of the weekdaysForbiddenEntry = OpenDayOfWeek = 6 OR OpenDayOfWeek = 0// Conditions to enter long positionsindicator1 = BollingerUp[20](close)c1 = (close > indicator1)indicator2 = RSI[14](close)c2 = (indicator2 > 70)IF not longonmarket and (c1 AND c2) AND not daysForbiddenEntry THENBUY 1 SHARES AT MARKETindicator9 = averagetruerange[14]indicator10 = averagetruerange[14]*2ENDIF// Conditions to exit long positionsindicator3 = BollingerDown[20](close)c3 = (close < indicator3)indicator4 = RSI[14](close)c4 = (indicator4 < 30)IF c3 AND c4 THENSELLSHORT 1 SHARES AT MARKET// previously was SELL AT MARKETENDIF// Conditions to enter short positionsindicator5 = BollingerDown[20](close)c5 = (close < indicator5)indicator6 = RSI[14](close)c6 = (indicator6 < 30)IF not shortonmarket and (c5 AND c6) AND not daysForbiddenEntry THENSELLSHORT 1 SHARES AT MARKETindicator9 = averagetruerange[14]indicator10 = averagetruerange[14]*2ENDIF// Conditions to exit short positionsindicator7 = BollingerUp[20](close)c7 = (close > indicator7)indicator8 = RSI[14](close)c8 = (indicator8 > 70)IF c7 AND c8 THENBUY 1 SHARES AT MARKET// previously was EXITSHORT AT MARKETENDIF// Stops and targetsSET STOP LOSS indicator9SET TARGET PROFIT indicator1005/08/2019 at 10:02 PM #97999I don’t understand. Did you try my code in the previous post? It opens a long or a short position if entry conditions are met. It fixes take profit at whatever ATR * 2 was at the time of entry and stop loss at whatever ATR was at the time of entry. If your conditions to reverse the trade (which are identical to your conditions to close a trade) are met then it closes the trade and opens one in the opposite direction. It only allows one position to be open at any time. Is this not what you have asked for?
Here is a cleaner version of it with all the ‘rubbish’ removed:
12345678910111213141516171819202122232425262728DEFPARAM FLATBEFORE = 080000DEFPARAM FLATAFTER = 180000daysForbiddenEntry = OpenDayOfWeek = 6 OR OpenDayOfWeek = 0// Conditions to enter long positionsc1 = close > BollingerUp[20]c2 = RSI[14] > 70IF not longonmarket and (c1 AND c2) AND not daysForbiddenEntry THENBUY 1 SHARES AT MARKETsl = averagetruerange[14]tp = averagetruerange[14]*2ENDIF// Conditions to enter short positionsc3 = close < BollingerDown[20](close)c4 = RSI[14] < 30IF not shortonmarket and (c3 AND c4) AND not daysForbiddenEntry THENSELLSHORT 1 SHARES AT MARKETsl = averagetruerange[14]tp = averagetruerange[14]*2ENDIF// Stops and targetsSET STOP LOSS slSET TARGET PROFIT tp1 user thanked author for this post.
05/09/2019 at 8:38 AM #98021I’ve just realised that this is a strategy discussion in the ProBuilder forum which is for indicator discussions. I will move it to the ProOrder forum. Please try to post in the correct forum with future topics.
1 user thanked author for this post.
05/09/2019 at 4:14 PM #98097I have now tried and tested your “simplified” code and it works fine and as expected; the problems which affected the previous version have now been resolved.
Thanks so much for your help and apologies for having posted in the wrong forum
-
AuthorPosts
Find exclusive trading pro-tools on