Code help – Simple strategy
Forums › ProRealTime English forum › ProOrder support › Code help – Simple strategy
- This topic has 15 replies, 4 voices, and was last updated 5 years ago by shug.
-
-
03/30/2016 at 5:52 AM #4595
Hi guys,
This one is driving me nuts. I’m trying to code a very simple strategy:
“If close within 10% of range – buy on open next day. Then sell on first profitable opening.”
This is what I got so far:
123456789101112131415161718192021222324252627282930313233varrange = high - lowvarinrange = low + (varrange/10)//Check if yesterday closed within 10%IF close > low AND close < varinrange AND intrade = 0 THENvarbuy = 1intrade = 1price = openELSEvarbuy = 0ENDIF//Check if open above paid priceIF open > price AND intrade = 1 THENvarsell = 1intrade = 0price = 0ELSEvarsell = 0ENDIF// Conditions to enter long positionsIF NOT LongOnMarket AND varbuy = 1 THENBUY 50 CONTRACTS AT MARKETENDIF// Conditions to exit long positionsIf LongOnMarket AND varsell = 1 THENSELL AT MARKETENDIF1I got as far as to enter correctly, but I cant exit without at least a days delay. I seems that PRT doesnt work “in the moment”, it looks at an entire bar and takes action the next day. Is there a way to exit as it happens? In this case I want to get out as soon as we open with a win.
03/30/2016 at 8:13 AM #4597Hello Noren,
You are right, each condition are tested only once at each new bar open. This Prorealtime engine behaviour will be soon changed to a whole new way to deal with it : each condition will be tested on every timeframe bars. From the one you are trading to the smaller one below it.
What you can do here with your strategy is to simply test if the price goes up or down at each new bar. So if you are long and price has raised up, exit your position and vice-versa.
03/30/2016 at 8:38 AM #4600Hi Nicolas,
I’m not following, how will that help me? Wouldnt I still get a one day delay with PRT’s behavior?
Even if price goes up, it could still close below. I need to confirm that open above entry before I could do anything and exit immediately once that happens… Perhaps its just not possible without those engine changes…
Cheers
03/30/2016 at 8:51 AM #4605It will help your strategy not to wait an entire day (if you are trading on a daily basis) to see what happened. The backtest engine will loop all timeframes from the daily one to the 1 minute one to test each of your strategy conditions.
03/30/2016 at 9:24 AM #460603/30/2016 at 9:35 AM #4608While your strategy is running on a Daily timeframe (1 bar = 1 day = 24 hours), on backtest, all your conditions will be tested like this :
Backtest of the X bar on Daily TF -> conditions tested on each 1 hour bar -> conditions spotted on the Nth bar of the day -> conditions tests on each minute of the Nth bar previously spotted -> Found the minute where the conditions occured -> Execute SL/TL/TRAILING or anything else on the prices of the bar -> Go on next Y bar on Daily TF -> and so on ..
03/30/2016 at 12:21 PM #462003/30/2016 at 12:52 PM #4626Your code don’t have to be adapted to this future improvement of the backtest engine. Actually, if you want to deal with your orders, you can browse all the instructions available in the online prorealtime documentation, Probacktest specific, here : http://www.prorealcode.com/documentation/category/probacktest/
Also, if you want to store your price entry whenever you enter the market, you just have to create new variables and store the open price. Then, make conditional loop for anything you want around this data.
Hope, that I understand clearly your question 🙂
03/30/2016 at 6:20 PM #4657Nicolas I feel that there is a bit of a language barrier here 🙂
I know that there is online documentation, i’ve read it, but i’m still stuck. Could you please provide an actual code example that implements what you are saying. That will allow me to study your solution in a different way.
Cheers
03/30/2016 at 6:59 PM #4658Ok, please find below your strategy modified :
12345678910111213141516varrange = high - lowvarinrange = low + (varrange/10)//Check if yesterday closed within 10%conditionBUY = close > low AND close < varinrange// Conditions to enter long positionsIF NOT LongOnMarket AND conditionBUY THENBUY 10 CONTRACTS AT MARKETopenprice = openENDIF// Conditions to exit long positionsIf LongOnMarket AND Close-openprice>0 THENSELL AT MARKETENDIF03/30/2016 at 7:10 PM #466003/30/2016 at 7:29 PM #466303/31/2016 at 10:12 AM #468405/09/2018 at 4:18 PM #70082Nicolas Hi
I have just came across your code above, (the one you helped Jerry with), I’m no programmer but I thought it would have been simple to modify the same code for just sell trades, couldn’t be further from the truth. What my missing? Its not a massive code, could you share the same code for just sell trades? That would be great if you could. I’m in the process of learning the coding language and the platform most of my trading for the last 7 years is through cmc markets but i’m looking at joining I.G Index to get access to the prorealtime platform.
05/09/2018 at 8:06 PM #70108Hi Shug, below might be the code for SellShort?
Results for Long and Short attached on DAX @ 5 min TF spread = 2 on 10k bars only!
I only did 5 min TF out of interest, the strategy (I believe? ) is for Daily TF?
123456789101112131415161718DEFPARAM CUMULATEORDERS = Falsevarrange = high - lowvarinrange = High -(varrange/10)//Check if yesterday closed within 10%conditionSellShort = close < High AND close > varinrange// Conditions to enter long positionsIF NOT Shortonmarket AND conditionSellshort THENSellShort 10 CONTRACTS AT MARKETopenprice = openENDIF// Conditions to exit long positionsIf Shortonmarket AND openprice - Close >0 THENExitShort AT MARKETENDIF -
AuthorPosts