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:
IF Hour[0]=14 THEN
tested=0
ENDIF
IF Hour[0] = 15 AND Minute[0]=30 AND tested =0 THEN
IF Low[1]>Low[2] and High[1]<High[2] THEN
tested =1
BUY 1 CONTRACT AT High[2] STOP
SELL 1 CONTRACT At Low[2] STOP
ENDIF
ENDIF
SET TARGET PROFIT (High[2]-High[1])*2
SET STOP LOSS (High[2]-High[1])
IF ONMARKET THEN
//Close the other order
ENDIF
IF HOUR[0]= 21 AND Minute[0]= 30 THEN
EXITSHORT AT MARKET
EXITLONG AT MARKET
ENDIF
But 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.
- 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?
like this
after 21:30 closes in gain
IF Hour=14 THEN
tested=0
ENDIF
IF Hour= 15 AND Minute=30 AND tested =0 THEN
IF Low[1]>Low[2] and High[1]<High[2] THEN
tested =1
BUY 1 CONTRACT AT High[2] STOP
SELL 1 CONTRACT At Low[2] STOP
ENDIF
ENDIF
SET TARGET PROFIT (High[2]-High[1])*2
SET STOP LOSS (High[2]-High[1])
IF ONMARKET THEN
//Close the other order
ENDIF
IF HOUR= 21 AND Minute= 30 THEN
if close<tradeprice then
EXITSHORT AT MARKET
endif
if close>tradeprice then
sell AT MARKET
endif
ENDIF
Pending orders are fine:
IF Time=140000 THEN
tested=0
ENDIF
IF Time = 153000 AND tested =0 AND Not OnMarket THEN
IF Low[1]>Low[2] and High[1]<High[2] THEN
tested=1
BUY 1 CONTRACT AT High[2] STOP //LONG order
SELLSHORT 1 CONTRACT At Low[2] STOP //SHORT order
ENDIF
ENDIF
SET TARGET PROFIT (High[2]-High[1])*2
SET STOP LOSS (High[2]-High[1])
IF ONMARKET THEN
//Close the other order
ENDIF
IF Time=213000 THEN
EXITSHORT AT MARKET //close Short trades, if any
SELL AT MARKET //close Long trades, if any
ENDIF
you 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.
Thanks. 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.
Backtests 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:
IF OnMarket THEN
EXITSHORT AT MARKET //close Short trades, if any
SELL AT MARKET //close Long trades, if any
ENDIF
IF Time=140000 THEN
tested=0
ENDIF
IF Time = 153000 AND tested =0 AND Not OnMarket THEN
IF Low[1]>Low[2] and High[1]<High[2] THEN
tested=1
BUY 1 CONTRACT AT High[2] STOP //LONG order
SELLSHORT 1 CONTRACT At Low[2] STOP //SHORT order
ENDIF
ENDIF
SET TARGET PROFIT (High[2]-High[1])*2
SET STOP LOSS (High[2]-High[1])
IF ONMARKET THEN
//Close the other order
ENDIF
IF Time=213000 THEN
EXITSHORT AT MARKET //close Short trades, if any
SELL AT MARKET //close Long trades, if any
ENDIF
I added 4 lines at the beginning, so that every 30 minutes any open trade is closed.
It’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?
Also, 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.
Your 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.
Arbu 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 THEN
SELL 1 CONTRACT AT (TRADEPRICE + 2*RANGE) LIMIT
SELL 1 CONTRACT AT (TRADEPRICE – RANGE) STOP
ELSIF SHORTONMARKET THEN
EXITSHORT 1 CONTRACT AT (TRADEPRICE – 2*RANGE) LIMIT
EXITSHORT 1 CONTRACT AT (TRADEPRICE + RANGE) STOP
ENDIF
Sorry for the code as text but it seems I have lost the <insert code> button !
Another 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.