Using MTF for trailing stop

Viewing 7 posts - 1 through 7 (of 7 total)
  • Author
    Posts
  • #80547 quote
    Toto le Heros
    Participant
    Veteran

    Hi,

    First of all, big thanks again for the quality of this forum and the excellence of PRT in implementing progressively new functionalities.

    MTF (MultiTimeFrame) looks like a very promising one.

    I got the idea of using it for a purpose which is not exactly the one it was built for, as far as I understand.

    My idea would be to have a strategy opening positions in a TIME FRAME #1 (ie : 15 minutes), BUT to have an associated trailing stop function managed in a (shorter) TIME FRAME #2 (ie. : 1 minute)

    In my example 15′ should be the “default” time frame… but based on my trial it looks like it is not working because TimeFrame#2 should be a multiple of TIMEFRAME#1 (default).

    Does somebody think there would be a way to manage it so that it is working as per my expectation ?

    Any ideas welcome.

    BR,

    #80548 quote
    GraHal
    Participant
    Master

    TF # 1 – 15 mins is a multiple of TF 2 – 1 mins so either I have misunderstood what you are saying or your problem is something else??

    You would need to be running the System on a Chart TF of 1 min.

    If you are trying run on Chart TF of 15 mins then you would get the error message that you got.

    #80557 quote
    robertogozzi
    Moderator
    Master

    You cannot have a 1-minute TF to do something, then launch the default mode on a 15-minute TF, because of these two requirements:

    • all TFs need to be multiple of the default TF
    • the default (which is the main one, the one to which BarIndex, TradeIndex, history data, …. are related) TF has to be be the lowest one

    You’ll have to do the opposite, use the 15-minute TF to run your strategy and launch the default TF on a 1-minute chart to trail SL.

    Nicolas thanked this post
    #80695 quote
    Toto le Heros
    Participant
    Veteran

    Thank you for your answers. I will try to explain better my case and for this I will use a real example.

    So let’s imagine that my goal is to run a strategy which would be the following : from 9am, on DAX UT=15min, I am looking to go short as soon as the lowest and the highest of the current candle are lower than the previous highest and lowest. As soon as the condition is there, the system sets a STOP order 1 point below the close of the current candle. And then I am willing to drive this with a classical “10 points trailing stop”, on a 1 minute timeframe basis (to make a closer monitoring of my position). Clear enough ?

    Well, my 1st approach of this is to build the program in the default TF = 15′. And to add at the beginning of the program a TF=1′ section including the trailing stop function only.

    If I do this, I am unable to even run a back test : the system returns an error. Somethin like : “All timeframes in the code must be multiples of the graph’s timeframe

    So that I try it the other way around (code attached) with 1′ time frame as default (trailing stop only) and 15′ time frame including main part of the code. Running this program on the 1′ TF leads to positions taken in the 1′ timeframe in fact… (ie. : 5th September at 9.14am, 5th Sept at 11.25am, at 11.38 am, etc.)

    Is there a way to program in order to obtain what I am willing to get :

    • 15′ TF for positions opening
    • 1′ TF for trailing stop management on open positions
      DEFPARAM CumulateOrders = False // Cumul des positions désactivé
      DEFPARAM FLATAFTER = 171500
      
      //-->15 minutes TF
      timeframe(15minutes,updateonclose)
      
      noEntryBeforeTime = 090000
      timeEnterBefore = time >= noEntryBeforeTime
      noEntryAfterTime = 163000
      timeEnterAfter = time < noEntryAfterTime
      daysForbiddenEntry = OpenDayOfWeek = 6 OR OpenDayOfWeek = 0
      
      // Conditions pour ouvrir une position en vente à découvert
      c1 = high<high[1] and low<low[1]
      
      IF c1 AND timeEnterBefore AND timeEnterAfter AND not daysForbiddenEntry THEN
      SELLSHORT 1 CONTRACT AT close-1 STOP
      ENDIF
      
      // Stops et objectifs
      SET STOP pLOSS 50
      SET TARGET pPROFIT 100
      
      //-->-->default TF (1 minute)
      timeframe(default)
      //trailing stop
      trailingstop = 10
      if not onmarket then
      MINPRICE = close
      priceexit = 0
      endif
      if shortonmarket then
      MINPRICE = MIN(MINPRICE,close)
      if tradeprice(1)-MINPRICE>=trailingstop*pointsize then
      priceexit = MINPRICE+trailingstop*pointsize
      endif
      endif
      if onmarket and priceexit>0 then
      EXITSHORT AT priceexit STOP
      endif

       

    #80700 quote
    robertogozzi
    Moderator
    Master

    It’s because the code is read each minute. Since conditions on the 15-minute TF are valid 15 times, lines 16-18 will be executed each minute. You cannot rely on BARINDEX, since it always refers to the default TF (UT), so you have to work it around as follows:

    1. at line 6 insert these lines to mimick BARINDEX:

    ONCE Bar15minute = 0
    Bar15Minute = Bar15Minute + 1

    2. replace line 16 with

    IF c1 AND timeEnterBefore AND timeEnterAfter AND not daysForbiddenEntry AND TradeON THEN

    3. between line 25 and 26 insert these lines:

    ONCE TradeON = 1                              //1=trading enabled  0=trading disabled
    IF IntraDayBarIndex = 0 THEN
       TradeON = 1                                //trading enabled at the beginnin of each new day
    ENDIF
    TradeBar = Bar15Minute
    IF Not OnMarket AND TradeBar <> TradeBar[1] THEN //When not on market and the 15-minute bar changes then reenable trading
       TradeON = 1
    ENDIF
    IF OnMarket THEN
       TradeON = 0                                //disable trading once on market
    ENDIF

    This will prevent more than one trade to be entered within the same 15-minute bar.

    #80701 quote
    robertogozzi
    Moderator
    Master

    This is the complete code:

    DEFPARAM CumulateOrders = False // Cumul des positions désactivé
    DEFPARAM FLATAFTER = 171500
     
    //-->15 minutes TF
    timeframe(15minutes,updateonclose)
    ONCE Bar15minute = 0
    Bar15Minute = Bar15Minute + 1
    noEntryBeforeTime = 090000
    timeEnterBefore = time >= noEntryBeforeTime
    noEntryAfterTime = 163000
    timeEnterAfter = time < noEntryAfterTime
    daysForbiddenEntry = OpenDayOfWeek = 6 OR OpenDayOfWeek = 0
     
    // Conditions pour ouvrir une position en vente à découvert
    c1 = high<high[1] and low<low[1]
     
    IF c1 AND timeEnterBefore AND timeEnterAfter AND not daysForbiddenEntry AND TradeON THEN
    SELLSHORT 1 CONTRACT AT close-1 STOP
    ENDIF
     
    // Stops et objectifs
    SET STOP pLOSS 50
    SET TARGET pPROFIT 100
     
    //-->-->default TF (1 minute)
    timeframe(default)
    ONCE TradeON = 1                              //1=trading enabled  0=trading disabled
    IF IntraDayBarIndex = 0 THEN
    TradeON = 1                                //trading enabled at the beginnin of each new day
    ENDIF
    TradeBar = Bar15Minute
    IF Not OnMarket AND TradeBar <> TradeBar[1] THEN //When not on market and the 15-minute bar changes then reenable trading
    TradeON = 1
    ENDIF
    IF OnMarket THEN
    TradeON = 0                                //disable trading once on market
    ENDIF
    //trailing stop
    trailingstop = 10
    if not onmarket then
    MINPRICE = close
    priceexit = 0
    endif
    if shortonmarket then
    MINPRICE = MIN(MINPRICE,close)
    if tradeprice(1)-MINPRICE>=trailingstop*pointsize then
    priceexit = MINPRICE+trailingstop*pointsize
    endif
    endif
    if onmarket and priceexit>0 then
    EXITSHORT AT priceexit STOP
    endif
    Berra thanked this post
    #80702 quote
    Toto le Heros
    Participant
    Veteran

    Smart !

    Thank you very much Roberto !

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

Using MTF for trailing stop


ProOrder: Automated Strategies & Backtesting

New Reply
Author
Summary

This topic contains 6 replies,
has 3 voices, and was last updated by Toto le Heros
7 years, 5 months ago.

Topic Details
Forum: ProOrder: Automated Strategies & Backtesting
Language: English
Started: 09/15/2018
Status: Active
Attachments: No files
Logo Logo
Loading...