MTF : amount of orders drops dramatically if I divide the strategy in 2 TF?

Viewing 15 posts - 1 through 15 (of 19 total)
  • Author
    Posts
  • #90866 quote
    stefou102
    Participant
    Veteran

    Hello,

    I’m trying to develop a strategy on the Dax, TF15min. I have some encouraging results, but wanted to improve the position management thank to the MTF. My idea was simply to take position on a signal given on the TF15min, and exit the position on the UT5min.

    When I backtest the strategy only on the TF15min , I get in two years several hundred of trades. Logically, adding a code snippet for managing existing position on the TF5min shouldn’t impact the number of trades, or marginally. However, I get down to only 5 trades on the same period. Can someone confirm they have the same behaviour, or do I do something wrong?

     

    Here below a very simple strategy that aims to explain my problem. If you run this code on the TF15min, you get hundred of trades, If you run the same one on the TF5min but with the code in comment uncomment, you get far less trades…

     

    DEFPARAM FlatAfter = 215900
    ONCE positionsize=1
    
    //timeframe (15 minutes, updateonclose)
    
    myMA=average[7](close)
    
    tradetime=time >=090000 and time < 171500 and dayofweek<>0
    
    c1=close>myMA
    c2= myMA>myMA[1] and myMA[1]<myMA[2]
    
    cs1=close<myMA
    cs2= myMA<myMA[1] and myMA[1]>myMA[2]
    
    if tradetime then
    // -- execution logic -----
    if c1 and c2 and not longonmarket then
    BuyLevel=High+1.5*pointsize
    
    stopbougie=low-2*pointsize
    buy positionSize contracts at buyLevel STOP
    set stop loss stopbougie
    endif
    
    if cs1 and cs2 and not shortonmarket then
    SellLevel=Low-1.5*pointsize
    
    stopbougie=high+2*pointsize
    sellshort positionSize contracts at SellLevel STOP
    set stop loss stopbougie
    endif
    
    endif
    
    if longonmarket then
    sell at stopbougie stop
    elsif shortonmarket then
    exitshort at stopbougie stop
    endif
    
    //timeframe (5 minute, updateonclose)
    //myMA5=average[7](close)
    //if longonmarket and myMA5<myMA5[1] then
    //sell at market
    //elsif shortonmarket and myMA5>myMA5[1] then
    //exitshort at market
    //endif
    #90872 quote
    GraHal
    Participant
    Master

    Mmm weird … I think I am getting the opposite of what you say??

    I get loads more trades on 5m TF??

    Stef.jpg Stef.jpg Stef-1.jpg Stef-1.jpg
    #90875 quote
    stefou102
    Participant
    Veteran

    Did you uncomment the code snippet here below when you tested on the 5min?

    //timeframe (15 minutes, updateonclose)

    ……………..

    //timeframe (5 minute, updateonclose)
    //myMA5=average[7](close)
    //if longonmarket and myMA5<myMA5[1] then
    //sell at market
    //elsif shortonmarket and myMA5>myMA5[1] then
    //exitshort at market
    //endif
    #90876 quote
    robertogozzi
    Moderator
    Master

    I agree with GraHal, add a “0” to your initial capital.

    #90880 quote
    stefou102
    Participant
    Veteran

    I test it on my demo account, and indeed working fine.

    But on my live account, which is activated for MTF, this doesn’t work! So here is the bug. Nicolas?

    #90882 quote
    Nicolas
    Keymaster
    Master

    First, try to GRAPH your buylevel and selllevel with dummy trading conditions that don’t trigger any order.

    Do it with and without “timeframe (15 minutes, updateonclose)”, in order to see if the levels are calculated the same way and to incriminate the TIMEFRAME instruction or not.

    #90883 quote
    stefou102
    Participant
    Veteran

    Ok, I have been able to isolate the code line which is causing trouble. Nothing to do with Demo or Live account, sorry my mistake. The code I provided here above was an example, which was very different from most of the original code with which I have some trouble. So the code here above works fine, the code here below doesn’t work because in the conditions to take a position, I added “barindex-tradeindex(1)>=3”, which is a pretty standard code snippet. Any idea why this condition doesn’t work with the MTF?

     

    DEFPARAM FlatAfter = 215900
    ONCE positionsize=1
     
    timeframe (15 minutes, updateonclose)
     
    myMA=average[7](close)
     
    tradetime=time >=090000 and time < 171500 and dayofweek<>0
     
    c1=close>myMA
    c2= myMA>myMA[1] and myMA[1]<myMA[2]
     
    cs1=close<myMA
    cs2= myMA<myMA[1] and myMA[1]>myMA[2]
     
    if tradetime then
    // -- execution logic -----
    if c1 and c2 and not longonmarket and barindex-tradeindex(1)>=3 then
    BuyLevel=High+1.5*pointsize
     
    stopbougie=low-2*pointsize
    buy positionSize contracts at buyLevel STOP
    set stop loss stopbougie
    endif
     
    if cs1 and cs2 and not shortonmarket and barindex-tradeindex(1)>=3then
    SellLevel=Low-1.5*pointsize
     
    stopbougie=high+2*pointsize
    sellshort positionSize contracts at SellLevel STOP
    set stop loss stopbougie
    endif
     
    endif
     
    if longonmarket then
    sell at stopbougie stop
    elsif shortonmarket then
    exitshort at stopbougie stop
    endif
     
    timeframe (5 minute, updateonclose)
    myMA5=average[7](close)
    if longonmarket and myMA5<myMA5[1] then
    sell at market
    elsif shortonmarket and myMA5>myMA5[1] then
    exitshort at market
    endif
    #90884 quote
    GraHal
    Participant
    Master

    With code immediately above I get results below.

    15 min TF results are with the lines of code out … via  // inserted.

    5 min with code in … via  // removed

    3rd image is on 5 min TF with code out via … // inserted again

    Sref-2.jpg Sref-2.jpg Stef-3.jpg Stef-3.jpg Stef-4.jpg Stef-4.jpg
    #90891 quote
    robertogozzi
    Moderator
    Master

    BarIndex and TradeIndex are determined by the lowest TF (which is the MAIN or Default one). So in your case it’s the index to bars in the 5-minute TF, no matter where you place that line!

    If you need to count bars elapsed in the 15-minute TF you should add your own variables to mimick BarIndex and TradeIndex or replace 3 with 9 (three 5-minute bars are required for each 15-minute bar).

    #90892 quote
    stefou102
    Participant
    Veteran

    Thx Robertogozzi for the info, didn’t know. However, multiplying by 3 the factor doesn’t change the issue. Storing the barindex of the trade seems to work better (via the onmarket not onmarket snippet).

    So this code seems now to work fine. However, the fact that tradeindex function isn’t working smoothly with MTF remains a bug for me, or do I miss something?

    DEFPARAM FlatAfter = 215900
    ONCE positionsize=1
     
    timeframe (15 minutes, updateonclose)
     
    myMA=average[7](close)
     
    tradetime=time >=090000 and time < 171500 and dayofweek<>0
     
    c1=close>myMA
    c2= myMA>myMA[1] and myMA[1]<myMA[2]
     
    cs1=close<myMA
    cs2= myMA<myMA[1] and myMA[1]>myMA[2]
     
    if tradetime then
    // -- execution logic -----
    if c1 and c2 and not longonmarket and barindex-mybar>=9 then
    BuyLevel=High+1.5*pointsize
     
    stopbougie=low-2*pointsize
    buy positionSize contracts at buyLevel STOP
    set stop loss stopbougie
    endif
     
    if cs1 and cs2 and not shortonmarket and barindex-mybar>=9 then
    SellLevel=Low-1.5*pointsize
     
    stopbougie=high+2*pointsize
    sellshort positionSize contracts at SellLevel STOP
    set stop loss stopbougie
    endif
     
    endif
     
    if longonmarket then
    sell at stopbougie stop
    elsif shortonmarket then
    exitshort at stopbougie stop
    endif
     
    if onmarket and not onmarket[1] then
    mybar=barindex
    endif
    
    timeframe (5 minute, updateonclose)
    myMA5=average[7](close)
    if longonmarket and myMA5<myMA5[1] then
    sell at market
    elsif shortonmarket and myMA5>myMA5[1] then
    exitshort at market
    endif
    
    #90893 quote
    robertogozzi
    Moderator
    Master

    BarIndex and TradeIndex DO work smoothly… according to the default TF.

    If you use 9, instead of 3 you can keep using TRADEINDEX, my suggestions were ORed, but this won’t affect the strategy, so you can leave it as is now.

    #90895 quote
    stefou102
    Participant
    Veteran

    Ok but if it works smoothly, can you rework a code using tradexindex and which would work under MTF ? Because I can’t, and the workaround with storing the barindex of the trade is actually not working exactly as the tradeindex function.

    #90896 quote
    GraHal
    Participant
    Master

    Here you are stefou102 … might encourage you .. I get attached!

    I’ll set it going on Demo Forward Test

    Thank You for Sharing

    Stef-5.jpg Stef-5.jpg Stef-6.jpg Stef-6.jpg
    #90899 quote
    robertogozzi
    Moderator
    Master

    Yes, just forget your custom variable and just change the numeric constant:

    barindex-tradeindex(1)>=9  //or barindex-tradeindex >=9

    it would be 12 in a 1-hour TF or 60 in a 1-hour TF with a 1-minute DEFAULT TF, and so on…

    MTF requires that you change the way you think of TF’s. The main one, the one setting the pace, is the LOWEST which guarantees that ANY TF used will at least be executed once when its own bar closes and that’s why all TF’s must be multiple of the default (or main) one.

    If, in your case, the 15-minute TF would set the pace, how could ProOrder guarantee that the 1-minute code is executed every minute? Instead, letting the 1-minute TF set the pace, your 15-minute code will be executed every minute and it’s granted it will be executed when the 15-minute bar closes!

    This has some drawbacks:

    • you have less history data
    • you have to do some math to count elapsed bars

    But advantages are far more!

    MTF is just a new feature, which you can take advantage of or not. You may still write strategies with a single TF because MTF may not be beneficial to them, while other strategies you write do improve when using MTF.

    But if tou want to use MTF you must write your code in accordance with it.

    #90901 quote
    stefou102
    Participant
    Veteran

    Well Understood, but I maintain that using

    barindextradeindex(1)>=9 

    doesn’t work in my code, almost no position are taken anymore…Or are the instruction to place orders always to be inserted in the lowest TF??

    Carefull Grahal, but the code I posted was just to solve the problem, there is no way it can be profitable.

Viewing 15 posts - 1 through 15 (of 19 total)
  • You must be logged in to reply to this topic.

MTF : amount of orders drops dramatically if I divide the strategy in 2 TF?


ProOrder: Automated Strategies & Backtesting

New Reply
Author
author-avatar
stefou102 @stefou102 Participant
Summary

This topic contains 19 replies,
has 5 voices, and was last updated by GraHal
7 years ago.

Topic Details
Forum: ProOrder: Automated Strategies & Backtesting
Language: English
Started: 02/08/2019
Status: Active
Attachments: 7 files
Logo Logo
Loading...