Oil 15 minutes meanreverting strategy

Viewing 15 posts - 1 through 15 (of 49 total)
  • Author
    Posts
  • #43014 quote
    Francesco78
    Participant
    Master

    Hello, I created this thread after the contribution of Juan Salas.

     

    Juan, can you please post here your results and your code?

    Many thanks!

    #43032 quote
    Juan Salas
    Participant
    Master

    Hi Francesco,

    Thanks. I am attaching the code:

    //crude oil 15 min strategy
     
    DEFPARAM cumulateOrders = False
    
    // Dias de la semana
    IF DayOfWeek = 0 OR Dayofweek = 6 THEN
    tradeok = 0
    ELSE
    tradeok = 1
    ENDIF
    // Friday 22:00 Close ALL operations.
    IF DayOfWeek = 5 AND time = 220000 THEN
    SELL      AT MARKET
    EXITSHORT AT MARKET
    ENDIF
    
    Fridaynight = Dayofweek = 5 AND time>220000
    
    //parameter definition
    period = 6
    fastav = average[4](close)
    slowav = average[50](close)
    maoscillator = fastav-slowav
     
    fullness = (Dclose(0)-Dopen(0))/abs(Dhigh(0)-Dlow(0))
    avfullness = summation[period](fullness)/period
    avfullnessthreshold = 0.38
    
    enrojoalcista= (tradeprice(1)-close)>35*pipsize
    enrojobajista= (close-tradeprice(1))>35*pipsize
    
    cl = maoscillator>0
    cl = cl and avfullness < -avfullnessthreshold
    
    cs = maoscillator<0
    cs = cs and avfullness > avfullnessthreshold
    
    
    // Largos
    IF NOT ONMARKET AND cl AND tradeok=1 AND NOT Fridaynight THEN
    BUY 1 CONTRACT AT MARKET
    ENDIF
    
    // Cortos
    IF NOT ONMARKET AND cs AND tradeok=1 AND NOT Fridaynight THEN
    SELLSHORT 1 CONTRACT AT MARKET
    ENDIF
    
    // Salida Largos
    IF LONGONMARKET AND enrojoalcista  AND (open-close)>=30*pipsize AND open>close THEN
    SELL AT MARKET
    ENDIF
    
    
    // Salida cortos
    IF SHORTONMARKET AND enrojobajista AND (close-open)>=30*pipsize AND open<close THEN
    EXITSHORT AT MARKET
    ENDIF
    
    ////trailing stop function
    trailingstart = 25 //trailing will start @trailinstart points profit
    trailingstep = 30 //trailing step to move the "stoploss"
    
    //reset the stoploss value
    IF NOT ONMARKET THEN
    newSL=0
    ENDIF
    
    //manage long positions
    IF LONGONMARKET THEN
    //first move (breakeven)
    IF newSL=0 AND close-tradeprice(1)>=trailingstart*pipsize THEN
    newSL = tradeprice(1)+trailingstep*pipsize
    ENDIF
    //next moves
    IF newSL>0 AND close-newSL>=trailingstep*pipsize THEN
    newSL = newSL+trailingstep*pipsize
    ENDIF
    ENDIF
    
    //manage short positions
    IF SHORTONMARKET THEN
    //first move (breakeven)
    IF newSL=0 AND tradeprice(1)-close>=trailingstart*pipsize THEN
    newSL = tradeprice(1)-trailingstep*pipsize
    ENDIF
    //next moves
    IF newSL>0 AND newSL-close>=trailingstep*pipsize THEN
    newSL = newSL-trailingstep*pipsize
    ENDIF
    ENDIF
    
    //stop order to exit the positions
    IF newSL>0 and tradeok=1 THEN
    SELL AT newSL STOP
    EXITSHORT AT newSL STOP
    ENDIF
    //************************************************************************
    
    
    //set target pprofit 30
    set stop ploss 90
    
    Francesco78, Nicolas, reb and 4 others thanked this post
    #43033 quote
    Juan Salas
    Participant
    Master
    #43037 quote
    Juan Salas
    Participant
    Master

    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).

    Best,

    Juan.

    Francesco78 thanked this post
    #43038 quote
    Juan Salas
    Participant
    Master

    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,

    Juan

    Francesco78 thanked this post
    #43039 quote
    Francesco78
    Participant
    Master

    Thank you Juan Salas!

    have a safe flight 🙂

    #43191 quote
    victormork
    Participant
    Veteran

    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:

    // Salida Largos
    IF LONGONMARKET AND enrojoalcista  AND (open-close)>=30*pipsize AND open>close THEN
    SELL AT MARKET
    ENDIF
    
    // Salida cortos
    IF SHORTONMARKET AND enrojobajista AND (close-open)>=30*pipsize AND open<close THEN
    EXITSHORT AT MARKET
    ENDIF
    #43293 quote
    Juan Salas
    Participant
    Master

    @visctormork,

    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,

    Thanks,

    Juan

    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.

    Francesco78, victormork and Wilko thanked this post
    #43309 quote
    victormork
    Participant
    Veteran

    Hi Juan,

    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.

    Candlefullness-crude-1H.itf Skärmavbild-2017-08-17-kl.-07.03.02.png Skärmavbild-2017-08-17-kl.-07.03.02.png
    #43344 quote
    Despair
    Blocked
    Master

    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. 🙁

    #43345 quote
    Despair
    Blocked
    Master

    @Nicolas: I think something is wrong with the forum. I tried now several times to attach a file but it doesn’t show up and obviously Victor had the same problem. His file(s) don’t show.

    #43347 quote
    Juan Salas
    Participant
    Master

    Hi Victor,

    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.

    Thanks,

    Juan

    #44108 quote
    Despair
    Blocked
    Master

    Hi Juan,

    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.

    victormork and Juan Salas thanked this post
    #44178 quote
    Juan Salas
    Participant
    Master

    Hi Despair,

    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.

    Thanks,

    Juan

    #44179 quote
    Despair
    Blocked
    Master

    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.

    Juan Salas and Nicolas thanked this post
Viewing 15 posts - 1 through 15 (of 49 total)
  • You must be logged in to reply to this topic.

Oil 15 minutes meanreverting strategy


ProOrder: Automated Strategies & Backtesting

New Reply
Author
Summary

This topic contains 48 replies,
has 13 voices, and was last updated by stockdemon
8 years, 4 months ago.

Topic Details
Forum: ProOrder: Automated Strategies & Backtesting
Language: English
Started: 08/11/2017
Status: Active
Attachments: 21 files
Logo Logo
Loading...