Oil 15 minutes meanreverting strategy
08/11/2017 at 11:21 AM #43014
Hello, I created this thread after the contribution of Juan Salas.
Juan, can you please post here your results and your code?
Many thanks!08/11/2017 at 4:27 PM #43032
Thanks. I am attaching the code:123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102//crude oil 15 min strategyDEFPARAM cumulateOrders = False// Dias de la semanaIF DayOfWeek = 0 OR Dayofweek = 6 THENtradeok = 0ELSEtradeok = 1ENDIF// Friday 22:00 Close ALL operations.IF DayOfWeek = 5 AND time = 220000 THENSELL AT MARKETEXITSHORT AT MARKETENDIFFridaynight = Dayofweek = 5 AND time>220000//parameter definitionperiod = 6fastav = average(close)slowav = average(close)maoscillator = fastav-slowavfullness = (Dclose(0)-Dopen(0))/abs(Dhigh(0)-Dlow(0))avfullness = summation[period](fullness)/periodavfullnessthreshold = 0.38enrojoalcista= (tradeprice(1)-close)>35*pipsizeenrojobajista= (close-tradeprice(1))>35*pipsizecl = maoscillator>0cl = cl and avfullness < -avfullnessthresholdcs = maoscillator<0cs = cs and avfullness > avfullnessthreshold// LargosIF NOT ONMARKET AND cl AND tradeok=1 AND NOT Fridaynight THENBUY 1 CONTRACT AT MARKETENDIF// CortosIF NOT ONMARKET AND cs AND tradeok=1 AND NOT Fridaynight THENSELLSHORT 1 CONTRACT AT MARKETENDIF// Salida LargosIF LONGONMARKET AND enrojoalcista AND (open-close)>=30*pipsize AND open>close THENSELL AT MARKETENDIF// Salida cortosIF SHORTONMARKET AND enrojobajista AND (close-open)>=30*pipsize AND open<close THENEXITSHORT AT MARKETENDIF////trailing stop functiontrailingstart = 25 //trailing will start @trailinstart points profittrailingstep = 30 //trailing step to move the "stoploss"//reset the stoploss valueIF NOT ONMARKET THENnewSL=0ENDIF//manage long positionsIF LONGONMARKET THEN//first move (breakeven)IF newSL=0 AND close-tradeprice(1)>=trailingstart*pipsize THENnewSL = tradeprice(1)+trailingstep*pipsizeENDIF//next movesIF newSL>0 AND close-newSL>=trailingstep*pipsize THENnewSL = newSL+trailingstep*pipsizeENDIFENDIF//manage short positionsIF SHORTONMARKET THEN//first move (breakeven)IF newSL=0 AND tradeprice(1)-close>=trailingstart*pipsize THENnewSL = tradeprice(1)-trailingstep*pipsizeENDIF//next movesIF newSL>0 AND newSL-close>=trailingstep*pipsize THENnewSL = newSL-trailingstep*pipsizeENDIFENDIF//stop order to exit the positionsIF newSL>0 and tradeok=1 THENSELL AT newSL STOPEXITSHORT AT newSL STOPENDIF//************************************************************************//set target pprofit 30set stop ploss 9008/11/2017 at 4:29 PM #4303308/11/2017 at 4:36 PM #43037
As you can see in the code, the parameters trailingstart: 25 and trailingstep: 30 determine the following:
Trailing start is the first profit locked. Once 25 is secure, the trailing will be securing profits after every 30 pips (trailing step). You can play with these two parameters and decide which one fits you better.
I hope it can be useful. I already mentioned, but I am a big fan of your hammer-negated and mean-reverting, so I have taken the liberty to customised both codes.
I am trying to see if mean-reverting is adaptable to any other instruments (Indexes, Forex, etc).
Juan.08/11/2017 at 4:42 PM #43038
I am at the airport right now, so I will try to post (maybe this weekend, or beginning of next week) results and codes for the hammer-negated in other TFs.
Have a great weekend,
Juan08/11/2017 at 5:18 PM #43039
Thank you Juan Salas!
have a safe flight 🙂08/15/2017 at 8:41 AM #43191
Hey guys, can anyone explain the logic behind the exit? Are you creating a target around 35 points? I’m referring to this part of the code:123456789// Salida LargosIF LONGONMARKET AND enrojoalcista AND (open-close)>=30*pipsize AND open>close THENSELL AT MARKETENDIF// Salida cortosIF SHORTONMARKET AND enrojobajista AND (close-open)>=30*pipsize AND open<close THENEXITSHORT AT MARKETENDIF08/16/2017 at 6:22 PM #43293
Hi, actually, the exit is defined by the trailing-start and trailing-step.
These two paragraphs in Spanish salida largos/Exit long and salida cortos=exit short are somehow something I thought it would help to mitigate the losses. I shouldn’t have labeled it as exit long/short.
In this sense, what I have tried to code is that because the Stop Loss is 90, I wanted to avoid to reach 90 in cases when is clear the operation is going to be negative. Example: when a LONG operation is losing by 35 points (enrojoalcista) and on top of that we see we have a long short candle of more than 30 pips, we sell at market. The market is showing a clear SHORT tendency, so why wait??. It is a little simple but it works in most of the cases. When operating in manual, if you see this clear short tendency, you would cut the losses. The same way, I wanted to create these two conditions to avoid reaching the stop loss of 90. In some cases, I am sure the operation would turn around and would close in positive, but are IMO the least.
I hope it clarify these two paragraphs,
P.S.- If any of you have tried this code in other values, it would be nice to share. I think Francesco did a wonderful job with hammer-negated and mean-reverting and we should try to explote these codes in other values and TFs.08/17/2017 at 6:09 AM #43309
Thanks for your explanation. I think you have a great idea and I’ll try this section of code on other systems. With regards to this system I was a little bit bothered about the short history so I tried to see if the idea works on a higher timeframe just for some better confirmation. I have attached my version of the US Crude 1H. Please not that this backtest is curve fitted and should be adjusted before trying out live. It’s more to get an understanding if the idea works well. I would also like to point out that the return on this system (even if it’s not bad) is not as good as the one on 15 min.
You must be logged in to access attached files.08/17/2017 at 11:17 AM #43344
It is so annoying that we have so little data for oil. There is plenty for several other raws but on Oil one has to go up to daily timeframe to get more data. 🙁08/17/2017 at 11:29 AM #4334508/17/2017 at 11:36 AM #43347
Thanks. I will try on US Crude 1h and will see the results. In any case thanks for Francesco’s and your understanding. I just got into PRC three months ago and learning everyday from all of you guys.
BTW, I am seeing the expressions “curve fitted” a lot, and although I know is about the equity curve, I don’t know exactly the meaning of the expression when it comes to the performance of the EC. Any explanation would be greatly appreciated.
Juan08/17/2017 at 1:09 PM #44108
Thanks for your contribution.
Curve fitting is a very big issue when developing strategies. In short it is adding too many rules to your code to make the backtest look better and better. Think of the extrem. You could code a strategy that specifies for every trades of the past exactly when it should be opened and closed. You could easily construct a backtest with 100% winning trades. But this strategy obviously has zero predictive power and putting it live would most likely only lose money.08/18/2017 at 12:06 PM #44178
Thanks for the explanation. I am afraid that I did what you said with some of the strategies, and when I put them on DEMO I noticed some discrepancies with the performance of my previous backtest, so I thought that I have adapted the code to create a clean curve on the backtest. You just confirmed my original idea, but I had no idea that it was labeled with the expression “curve fitted”. Besides the name, it is good to know that when working on strategies and adapting other systems is not about constructing the perfect curve.
Juan08/18/2017 at 12:35 PM #44179
Curve fitting is the probably the nb. 1 reason why realtime trading results differ from the backtest. The best we can da right now is utilizing WFA. I made a post not long ago labeled “WFA – Am I doing it right?” (post # 42252). This is a very short description of the way I think with my current knowledge one should optimize a strategy.