Just getting started with an opening range strat, not as easy as I had imagined.
Forums › ProRealTime English forum › ProBuilder support › Just getting started with an opening range strat, not as easy as I had imagined.
- This topic has 11 replies, 4 voices, and was last updated 1 year ago by Arbu.
-
-
12/28/2022 at 5:54 PM #206417
To get myself acquainted with ProRealCode I thought I’d write a trial strategy. I’m finding it a lot harder than I had expected. The glossary just doesn’t provide the explanations that I would expect. Anyway, my strategy is to look at the opening range of the S&P 500 for the first 30 minutes. If it stays within that for the following 30 minutes then I would set an order to buy as soon as it broke above the range and an order to sell if it goes below. There would be a stop loss at the opposite side of the range, and a take profit at two times the size of the range. Only one of the orders should be triggered. At the end of the day, any open position should be closed. I got this far:
1234567891011121314151617181920212223IF Hour[0]=14 THENtested=0ENDIFIF Hour[0] = 15 AND Minute[0]=30 AND tested =0 THENIF Low[1]>Low[2] and High[1]<High[2] THENtested =1BUY 1 CONTRACT AT High[2] STOPSELL 1 CONTRACT At Low[2] STOPENDIFENDIFSET TARGET PROFIT (High[2]-High[1])*2SET STOP LOSS (High[2]-High[1])IF ONMARKET THEN//Close the other orderENDIFIF HOUR[0]= 21 AND Minute[0]= 30 THENEXITSHORT AT MARKETEXITLONG AT MARKETENDIFBut it plainly won’t work because I can’t understand how to set the take profits and stop losses on my orders, I don’t know how to cancel one order once the other has been triggered, and I don’t know how to close the positions at the end of the day if they are still open (EXITSHORT exists, but EXITLONG seemingly doesn’t). Can anyone give me some help?
Thanks.
12/28/2022 at 6:42 PM #206423- BUY is to be used to enter Long positions
- SELL is to be used to exit Long positions
- SELLSHORT is to be used to enter Short positions
- EXITSHORT is to be used to exit Short positions
Which timeframe are you using?
12/28/2022 at 6:48 PM #206424like this
after 21:30 closes in gain123456789101112131415161718192021222324252627IF Hour=14 THENtested=0ENDIFIF Hour= 15 AND Minute=30 AND tested =0 THENIF Low[1]>Low[2] and High[1]<High[2] THENtested =1BUY 1 CONTRACT AT High[2] STOPSELL 1 CONTRACT At Low[2] STOPENDIFENDIFSET TARGET PROFIT (High[2]-High[1])*2SET STOP LOSS (High[2]-High[1])IF ONMARKET THEN//Close the other orderENDIFIF HOUR= 21 AND Minute= 30 THENif close<tradeprice thenEXITSHORT AT MARKETendifif close>tradeprice thensell AT MARKETendifENDIF12/29/2022 at 11:52 PM #206478- BUY is to be used to enter Long positions
- SELL is to be used to exit Long positions
- SELLSHORT is to be used to enter Short positions
- EXITSHORT is to be used to exit Short positions
Which timeframe are you using?
OK, thanks. Should I be trying to place orders (if so, perhaps you can point me to where the terminology for this is), or should I just check on a tick by tick basis whether the desired price has been reached?
I was going to use a 30 minute timeframe, so wrote my code accordingly.
12/30/2022 at 3:30 AM #206480Pending orders are fine:
1234567891011121314151617181920212223IF Time=140000 THENtested=0ENDIFIF Time = 153000 AND tested =0 AND Not OnMarket THENIF Low[1]>Low[2] and High[1]<High[2] THENtested=1BUY 1 CONTRACT AT High[2] STOP //LONG orderSELLSHORT 1 CONTRACT At Low[2] STOP //SHORT orderENDIFENDIFSET TARGET PROFIT (High[2]-High[1])*2SET STOP LOSS (High[2]-High[1])IF ONMARKET THEN//Close the other orderENDIFIF Time=213000 THENEXITSHORT AT MARKET //close Short trades, if anySELL AT MARKET //close Long trades, if anyENDIFyou should only make sure that your current price is lower than the entry price (for Long trades) or higher than the entry price (for Short trades), and that it is equal or greater than the distance required by the broker for S&P 500.
12/30/2022 at 1:33 PM #206522Thanks. I chose the 30 minute timescale so that I could refer to the Max price in the candlestick two periods before. When I run the backtest I have this selected, and I also check the box to run on a tick-by-tick basis (see attachment). But trades only ever seem to get closed out at the end of a 30 minute period. What am I doing wrong?
Also, is there a way to save my backtests? I was expecting a save button in the backtest toolbar, but there isn’t one there.
12/30/2022 at 4:01 PM #206532Backtests cannot be saved. You can run it again. You can export the details you need to an eXcel file by dragging them and dropping them into a spreadsheed.
Your trades are not closed after 30 minutes as there’s no provision for that. I have added it:
12345678910111213141516171819202122232425262728IF OnMarket THENEXITSHORT AT MARKET //close Short trades, if anySELL AT MARKET //close Long trades, if anyENDIFIF Time=140000 THENtested=0ENDIFIF Time = 153000 AND tested =0 AND Not OnMarket THENIF Low[1]>Low[2] and High[1]<High[2] THENtested=1BUY 1 CONTRACT AT High[2] STOP //LONG orderSELLSHORT 1 CONTRACT At Low[2] STOP //SHORT orderENDIFENDIFSET TARGET PROFIT (High[2]-High[1])*2SET STOP LOSS (High[2]-High[1])IF ONMARKET THEN//Close the other orderENDIFIF Time=213000 THENEXITSHORT AT MARKET //close Short trades, if anySELL AT MARKET //close Long trades, if anyENDIFI added 4 lines at the beginning, so that every 30 minutes any open trade is closed.
12/30/2022 at 4:50 PM #206537It’s not the results of the backtests that I want to save, but the code that I have written. I suppose I can just copy it to a notepad and save it there, but I’m surprised that ProRealTime doesn’t seem to allow me the opportunity to save these scripts as files directly.
I don’t want trades to be closed out at the end of 30 minute periods. I want them to be closed out as soon as the stop or take profit level is hit, as would be normal in reality. So they should be closed out during these periods. How do I manage that?
12/30/2022 at 5:17 PM #206538Also, you say that pending orders are fine. But the manual says “Limit and stop orders with specific levels are valid for one bar by default starting at the open of the
next bar. They are canceled if not executed.” This is not how a pending order normally works. If they do get cancelled then they are useless and I have to keep testing the price at each iteration.12/30/2022 at 5:36 PM #206541Your work is saved on PRT’s servers, but not until you sut down your platform. To overcome this, I often save my work by exporting the ITF file to my download folder as soon as I have made some major modifications/additions to my code. Even 2-3 times per hour, it’s not so time consuming.
Pending orders are ALWAYS cancelled each bar, so you have to keep placing them again and again until you no longer need them.
12/30/2022 at 7:15 PM #206549Arbu said: “There would be a stop loss at the opposite side of the range, and a take profit at two times the size of the range”.
I think the opening range should be HIGH[2] – LOW[2] . Your calculated range HIGH[2] – HIGH[1] is incorrect , isnt it ?. If so :
RANGE = HIGH[2] – LOW[2]
The exit orders could be something like this:
IF LONGONMARKET THENSELL 1 CONTRACT AT (TRADEPRICE + 2*RANGE) LIMIT
SELL 1 CONTRACT AT (TRADEPRICE – RANGE) STOPELSIF SHORTONMARKET THENEXITSHORT 1 CONTRACT AT (TRADEPRICE – 2*RANGE) LIMITEXITSHORT 1 CONTRACT AT (TRADEPRICE + RANGE) STOPENDIFSorry for the code as text but it seems I have lost the <insert code> button !
12/30/2022 at 7:19 PM #206551Another issue is that if I want to run the backtest over a time period of a month, say, I can only do this on the 30 minute timescale or greater. Which is not a great representation of reality. I did use MT4 a few years ago, and, as I recall, it didn’t have this issue, and it also allowed me to save files as scripts. I’m thinking I might give up with this and go back to using that instead.
-
AuthorPosts
Find exclusive trading pro-tools on