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
// Definition of code parameters
DEFPARAM 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" time
DEFPARAM FLATAFTER = 180000
// Prevents the system from placing new orders on specified days of the week
daysForbiddenEntry = OpenDayOfWeek = 6 OR OpenDayOfWeek = 0
// Conditions to enter long positions
indicator1 = BollingerUp[20](close)
c1 = (close > indicator1)
indicator2 = RSI[14](close)
c2 = (indicator2 > 70)
IF (c1 AND c2) AND not daysForbiddenEntry THEN
BUY 1 SHARES AT MARKET
ENDIF
// Conditions to exit long positions
indicator3 = BollingerDown[20](close)
c3 = (close < indicator3)
indicator4 = RSI[14](close)
c4 = (indicator4 < 30)
IF c3 AND c4 THEN
SELL AT MARKET
ENDIF
// Conditions to enter short positions
indicator5 = BollingerDown[20](close)
c5 = (close < indicator5)
indicator6 = RSI[14](close)
c6 = (indicator6 < 30)
IF (c5 AND c6) AND not daysForbiddenEntry THEN
SELLSHORT 1 SHARES AT MARKET
ENDIF
// Conditions to exit short positions
indicator7 = BollingerUp[20](close)
c7 = (close > indicator7)
indicator8 = RSI[14](close)
c8 = (indicator8 > 70)
IF c7 AND c8 THEN
EXITSHORT AT MARKET
ENDIF
// Stops and targets
indicator9 = averagetruerange[14]
indicator10 = averagetruerange[14]*2
SET STOP LOSS indicator9
SET TARGET PROFIT indicator10
You 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.
// Definition of code parameters
DEFPARAM 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" time
DEFPARAM FLATAFTER = 180000
// Prevents the system from placing new orders on specified days of the week
daysForbiddenEntry = OpenDayOfWeek = 6 OR OpenDayOfWeek = 0
// Conditions to enter long positions
indicator1 = BollingerUp[20](close)
c1 = (close > indicator1)
indicator2 = RSI[14](close)
c2 = (indicator2 > 70)
IF not longonmarket and (c1 AND c2) AND not daysForbiddenEntry THEN
BUY 1 SHARES AT MARKET
indicator9 = averagetruerange[14]
indicator10 = averagetruerange[14]*2
ENDIF
// Conditions to exit long positions
indicator3 = BollingerDown[20](close)
c3 = (close < indicator3)
indicator4 = RSI[14](close)
c4 = (indicator4 < 30)
IF c3 AND c4 THEN
SELL AT MARKET
ENDIF
// Conditions to enter short positions
indicator5 = BollingerDown[20](close)
c5 = (close < indicator5)
indicator6 = RSI[14](close)
c6 = (indicator6 < 30)
IF not shortonmarket and (c5 AND c6) AND not daysForbiddenEntry THEN
SELLSHORT 1 SHARES AT MARKET
indicator9 = averagetruerange[14]
indicator10 = averagetruerange[14]*2
ENDIF
// Conditions to exit short positions
indicator7 = BollingerUp[20](close)
c7 = (close > indicator7)
indicator8 = RSI[14](close)
c8 = (indicator8 > 70)
IF c7 AND c8 THEN
EXITSHORT AT MARKET
ENDIF
// Stops and targets
SET STOP LOSS indicator9
SET TARGET PROFIT indicator10
Thanks Vonasi, I very much appreciate your quick reply
I 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.
Try removing:
DEFPARAM CumulateOrders = False
This 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:
not onmarket
not longonmarket
not shortonmarket
Thanks 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.
I 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.
Many 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
What 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:
// 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" time
DEFPARAM FLATAFTER = 180000
// Prevents the system from placing new orders on specified days of the week
daysForbiddenEntry = OpenDayOfWeek = 6 OR OpenDayOfWeek = 0
// Conditions to enter long positions
indicator1 = BollingerUp[20](close)
c1 = (close > indicator1)
indicator2 = RSI[14](close)
c2 = (indicator2 > 70)
IF not longonmarket and (c1 AND c2) AND not daysForbiddenEntry THEN
BUY 1 SHARES AT MARKET
indicator9 = averagetruerange[14]
indicator10 = averagetruerange[14]*2
ENDIF
// Conditions to enter short positions
indicator5 = BollingerDown[20](close)
c5 = (close < indicator5)
indicator6 = RSI[14](close)
c6 = (indicator6 < 30)
IF not shortonmarket and (c5 AND c6) AND not daysForbiddenEntry THEN
SELLSHORT 1 SHARES AT MARKET
indicator9 = averagetruerange[14]
indicator10 = averagetruerange[14]*2
ENDIF
// Stops and targets
SET STOP LOSS indicator9
SET TARGET PROFIT indicator10
I 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
// Definition of code parameters
DEFPARAM 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" time
DEFPARAM FLATAFTER = 180000
// Prevents the system from placing new orders on specified days of the week
daysForbiddenEntry = OpenDayOfWeek = 6 OR OpenDayOfWeek = 0
// Conditions to enter long positions
indicator1 = BollingerUp[20](close)
c1 = (close > indicator1)
indicator2 = RSI[14](close)
c2 = (indicator2 > 70)
IF not longonmarket and (c1 AND c2) AND not daysForbiddenEntry THEN
BUY 1 SHARES AT MARKET
indicator9 = averagetruerange[14]
indicator10 = averagetruerange[14]*2
ENDIF
// Conditions to exit long positions
indicator3 = BollingerDown[20](close)
c3 = (close < indicator3)
indicator4 = RSI[14](close)
c4 = (indicator4 < 30)
IF c3 AND c4 THEN
SELLSHORT 1 SHARES AT MARKET
// previously was SELL AT MARKET
ENDIF
// Conditions to enter short positions
indicator5 = BollingerDown[20](close)
c5 = (close < indicator5)
indicator6 = RSI[14](close)
c6 = (indicator6 < 30)
IF not shortonmarket and (c5 AND c6) AND not daysForbiddenEntry THEN
SELLSHORT 1 SHARES AT MARKET
indicator9 = averagetruerange[14]
indicator10 = averagetruerange[14]*2
ENDIF
// Conditions to exit short positions
indicator7 = BollingerUp[20](close)
c7 = (close > indicator7)
indicator8 = RSI[14](close)
c8 = (indicator8 > 70)
IF c7 AND c8 THEN
BUY 1 SHARES AT MARKET
// previously was EXITSHORT AT MARKET
ENDIF
// Stops and targets
SET STOP LOSS indicator9
SET TARGET PROFIT indicator10
I 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:
DEFPARAM FLATBEFORE = 080000
DEFPARAM FLATAFTER = 180000
daysForbiddenEntry = OpenDayOfWeek = 6 OR OpenDayOfWeek = 0
// Conditions to enter long positions
c1 = close > BollingerUp[20]
c2 = RSI[14] > 70
IF not longonmarket and (c1 AND c2) AND not daysForbiddenEntry THEN
BUY 1 SHARES AT MARKET
sl = averagetruerange[14]
tp = averagetruerange[14]*2
ENDIF
// Conditions to enter short positions
c3 = close < BollingerDown[20](close)
c4 = RSI[14] < 30
IF not shortonmarket and (c3 AND c4) AND not daysForbiddenEntry THEN
SELLSHORT 1 SHARES AT MARKET
sl = averagetruerange[14]
tp = averagetruerange[14]*2
ENDIF
// Stops and targets
SET STOP LOSS sl
SET TARGET PROFIT tp
I’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.
I 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