Hi there, I am having an issue and don’t know how to move forward with this, I want for the code to set my stop loss when long in the lowest of the past 2 candles and when shorting the market on the highest of the last 2 candles.
I am using function Highest but not getting the expected results, also the TP is not corresponding with the instructions I am giving. I am really new to programing though! my code is:
thank you so much in advance 🙂
// 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 = 173000
st1= lowest[2](low)
tp1= st1*2
st2= highest[2](high)
tp2= st2*2
// Conditions to enter long positions
indicator1 = ExponentialAverage[200](close)
c1 = (close > indicator1)
indicator2 = RSI[14](close)
c2 = (indicator2 > 50)
indicator3 = CALL "ENGULFING"
c3 = (indicator3 > 0.5)
IF c1 AND c2 AND c3 THEN
BUY 1 SHARES AT MARKET
SET STOP pLOSS st1
SET TARGET pPROFIT tp1
ENDIF
// Conditions to enter short positions
indicator4 = ExponentialAverage[200](close)
c4 = (close < indicator4)
indicator5 = RSI[14](close)
c5 = (indicator5 < 50)
indicator6 = CALL "ENGULFING"
c6 = (indicator6 < -0.5)
IF c4 AND c5 AND c6 THEN
SELLSHORT 1 SHARES AT MARKET
SET STOP pLOSS st2
SET TARGET pPROFIT tp2
ENDIF
Substitute figures in your code below and I think you will see 2 reasons it’s not working?
st1= lowest[2](low)
tp1= st1*2
So on DJI if lowest[2](low) is, for example, 35,000 so you be exiting at 35000 points loss or 70,000 points profit at lines 27 and 28.
Is it because I am only entreing the value of the market and not the points value right? I have tried from different angles one of them beeing this
st2= (highest[2](high)-open[0])
but results don’t differ, in my mind that would subtract those hypothetical 35,000 from the opening of the current candle (candle that the trade is open) so I could get the number of points that its needed but the results are the same, I did try to look into many codes now to see if anyone would use this kind of stop but didn’t found one so far or found many, way more complex to my knowledge..
is there any book on the Pro Realt Time code or so? besides the courses?
Is it because I am only entreing the value of the market and not the points value right? I have tried from different angles one of them beeing this
st2= (highest[2](high)-open[0])
or
st2= (highest[2](high)-close[1])
but results don’t differ, in my mind that would subtract those hypothetical 35,000 from the opening of the current candle (candle that the trade is open) so I could get the number of points that its needed but the results are the same, I did try to look into many codes now to see if anyone would use this kind of stop but didn’t found one so far or found many, way more complex to my knowledge..
is there any book on the Pro Realt Time code or so? besides the courses and besides the manual PRT gives us
Want to learn more about it but have barely any base in coding
also didn’t know how to edit the post or to delete the other one
Is it because I am only entreing the value of the market and not the points value right?
Yes
If you wanted to use the value of the market, you would need to use something like below …
I say something like as I started with barely any base in coding also 6 years ago and even now I rely on the syntax checker in the code editor to put me straight.
If you use …
GRAPH ST1
GRAPH TP1
then you will see the values as a separate area under the positions after backtest.
IF c1 AND c2 AND c3 THEN
BUY 1 SHARES AT MARKET
SEll at st1 Stop
SEll at tp1 Limit
ENDIF
These may help you, but they have not been updated to current version 11, but nonetheless are very useful?
https://www.prorealtime.com/en/pdf/probacktest.pdf
https://www.prorealtime.com/en/help-manual/quick-tour
PS
We get 5 mins to edit a post after posting
Or you could do …
If Longonmarket Then
SEll at st1 Stop
SEll at tp1 Limit
ENDIF
yeah I get it this is quite hard but it’s a really interesting tool to make us not lose so much time in backtesting stuff, especially the part where we can add variables and it automatically finds the best one.
I am really sorry But I was looking at another thing: I was looking to set the stop (while shorting the market) at the highest point of the last 2 candles and vice versa, not the high of the market.
I hope you have been getting good results with it! it’s already been a long time in coding, hope to trace a similar path.
I have read that on another post about the 5 minutes to edit still don’t find where to
thank you so much for you time
PS: I got it now the Edit part!
SET STOP pLOSS (and SET TARGET pPROFIT, as well), need a difference in price exèpressed as pips, NOT a price, otherwise, as suggested by GraHal, it will NEVER be hit and, in live trading, your strategy will be stopped due to a too far stop loss.
SET STOP pLOSS and SET TARGET pPROFIT should only be set ONCE per trade. To change the SL while on market you should use a pending order.
Hi Roberto, first of all thank you so much for THE great works and strategies that you have shared!
“need a difference in price exèpressed as pips, NOT a price”
yeah, I am getting that now but still can’t figure out how to make it, and perhaps I am also expressing myself the wrong way..
the objective is every single time it triggers a buy order long – the code should calculate what is the amount of pips between the opening of the candle that trade opened (or the close from the candle before trade opens) and the lowest pricepoint in the 2 last candles – in other words, I want to open a position and define my stop loss at the lowest point of the wave we are in. That will change in a trade by trade basis.
thats is why I thought:
lowest [2] (low) – (open[0])
or
lowest [2] (low) – (close[1]) //meaning the lowest of the two last candles (price) minus the opening of the entry candle or the close of the candle before opening trade would give me an amount of pips for the stoploss
SET STOP pLOSS (or LOSS) is not a trailing STOP, su using SET STOP LOSS ABS(lowest [2] (low) – (open[0])) or SET STOP pLOSS ABS(lowest [2] (low) – (open[0])) / PipSize won’t follow the price as it rises (if you opened a LONG trade), but will always modify your original distance from the Entry Price. It actually could become bigger, in case of some huge bars!
Use a STOP pending order:
// 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 = 173000
IF Not OnMarket THEN
st1= (close - lowest[2](low)) / PipSize
tp1= st1*2
myStopLoss = lowest[2](low)
st2= (highest[2](high) - close) / PipSize
tp2= st2*2
myStopLoss = highest[2](high)
ELSE
IF LongOnMarket THEN
myStopLoss = max(myStopLoss,lowest[2](low))
SELL AT myStopLoss STOP
ELSE
myStopLoss = min(myStopLoss,highest[2](high))
EXITSHORT AT myStopLoss STOP
ENDIF
ENDIF
// Conditions to enter long positions
indicator1 = ExponentialAverage[200](close)
c1 = (close > indicator1)
indicator2 = RSI[14](close)
c2 = (indicator2 > 50)
indicator3 = 1//CALL "ENGULFING"
c3 = (indicator3 > 0.5)
IF c1 AND c2 AND c3 AND Not OnMarket THEN
BUY 1 SHARES AT MARKET
SET STOP pLOSS st1
SET TARGET pPROFIT tp1
ENDIF
// Conditions to enter short positions
indicator4 = ExponentialAverage[200](close)
c4 = (close < indicator4)
indicator5 = RSI[14](close)
c5 = (indicator5 < 50)
indicator6 = 1//CALL "ENGULFING"
c6 = (indicator6 < -0.5)
IF c4 AND c5 AND c6 AND Not OnMarket THEN
SELLSHORT 1 SHARES AT MARKET
SET STOP pLOSS st2
SET TARGET pPROFIT tp2
ENDIF
//
graphonprice TradePrice(1)
graphonprice myStopLoss coloured(255,0,0,255)
oh man thank you so much for the amazing effort and I am understanding further the issue “”but will always modify your original distance from the Entry Price””. Especially this part since in reality, I don’t want a trailing stop loss, rather a fixed one but at the same time, I don’t want it to increase.
I have attached an example of what I would love to get as a result. In layman terms is just a simple entry with a fixed stop on the highest point of the two candles before the opening, and a Take profit of double that amount of pips, it either goes one way or the other without moving anything, nor the stopL neither the TP.
This is such a journey to even get one simple thing done, but such an interesting thing because on the other hand spares us months of manual work.
once again thank you so much for you time and sorry if miss explain myself
There you go:
// 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 = 173000
st1 = abs(close - lowest[2](low))
tp1 = st1*2
st2 = abs(close - highest[2](high))
tp2 = st2*2
IF OnMarket AND Not OnMarket[1] THEN
st1 = abs(TradePrice - lowest[2](low[1]))
tp1 = st1*2
st2 = abs(TradePrice - highest[2](high[1]))
tp2 = st2*2
IF LongOnMarket THEN
SET STOP LOSS st1
SET TARGET PROFIT tp1
ELSE
SET STOP LOSS st2
SET TARGET PROFIT tp2
ENDIF
ENDIF
// Conditions to enter long positions
indicator1 = ExponentialAverage[200](close)
c1 = (close > indicator1)
indicator2 = RSI[14](close)
c2 = (indicator2 > 50)
indicator3 = CALL "ENGULFING"
c3 = (indicator3 > 0.5)
IF c1 AND c2 AND c3 AND Not OnMarket THEN
BUY 1 SHARES AT MARKET
SET STOP LOSS st1
SET TARGET PROFIT tp1
ENDIF
// Conditions to enter short positions
indicator4 = ExponentialAverage[200](close)
c4 = (close < indicator4)
indicator5 = RSI[14](close)
c5 = (indicator5 < 50)
indicator6 = CALL "ENGULFING"
c6 = (indicator6 < -0.5)
IF c4 AND c5 AND c6 AND Not OnMarket THEN
SELLSHORT 1 SHARES AT MARKET
SET STOP LOSS st2
SET TARGET PROFIT tp2
ENDIF
thank you so much!! you just made my day!
and thank you once again for the time and dedication for both of you
cheers