Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Multitimeframe Strategy - execution issue

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    Multitimeframe Strategy - execution issue

    I developed a strategy on 30min time frame and I want to execute it on 5min bars, but keeping all the indicators calculations on the 30min time frame.
    For simplicity, assuming the strategy is Close[0] > mythreshold,
    I did:

    protected override void Initialize()
    {
    Add(PeriodType.Minute, 5);
    }

    OnBarUpdate()
    {calculations to estimate my threshold}

    if(BarsInProgress == 1)

    // Entry Long Condition
    if (Close[0] > mythresholdHigh)

    {
    EnterLong(DefaultQuantity, "Long 5min");

    }



    // Entry Short Condition
    if (Close[0] < mythresholdLow)


    {
    EnterShort(DefaultQuantity, "Short 5min");

    }

    Then I left everything else the same, for what it concerns the Exits.

    Could you explain me where is my mistake?

    Thank you

    #2
    Hi stefy,

    One of our NinjaScript trainees will respond later today. Thank you for your patience.
    Josh P.NinjaTrader Customer Service

    Comment


      #3
      Hello,

      This is a good start!

      My changes are:

      -Put the most everything in the OnBarUpdate() block
      -Add brackets for the BarsInProgress block
      -Calculate your indicators and conditions within the BarsInProgress == 0 block.
      -Use an overload method to place the order in a different timeframe.

      Try something like this:

      protected override void Initialize()
      {

      Add(PeriodType.Minute, 5);

      }

      protected override OnBarUpdate()
      {


      if(BarsInProgress == 0)
      {

      //calculations to estimate my threshold

      // Entry Long Condition
      if (Close[0] > mythresholdHigh)
      {

      //submits to BarsInProgress 1
      EnterLong(1, DefaultQuantity, "Long 5min");

      }

      // Entry Short Condition
      if (Close[0] < mythresholdLow)
      {

      //submits to BarsInProgress 1
      EnterShort(1, DefaultQuantity, "Short 5min");

      }



      }//closes BarsInProgress
      }//closes OnBarUpdate

      Here is the link to the EnterLong() method. Take a look at the overload method for experienced programmers:
      DenNinjaTrader Customer Service

      Comment


        #4
        Ben,

        I followed your guidance step by step but the code doesn't work yet.
        I'm attaching it. Could you please have a look and tell me what's wrong there?

        Thank you
        Attached Files

        Comment


          #5
          What does not work? In general, we do not provide debug assistance, although we will look at code to see if we can point out something that we can see immediately. I took a glance at your code and did not see anything right away. I suggest debugging your code to see what is happening. I know you have posted here before so likely you have seen the debug link of info but I will post below just in case.

          RayNinjaTrader Customer Service

          Comment


            #6
            Thank you Ray.
            I actually fixed it, but that was just part of the code.

            Generally, does it matter the order of the programming code?
            I'm asking because, to program in two time frames, I had to do something like this:

            if (BarsInProgress == 0)
            {Indicator Calculation on 30min
            ExitLong Conditions (Take Profit / Stop Loss)
            ExitShort Conditions (Take Profit / Stop Loss)}

            if (BarsInProgress == 1)
            //5min bars
            {EntryLong Conditions
            EntryShort Conditions}

            As you can see, I just wanted to get my entries more promptly (on 5min bars instead of the 30min which I use for calculating indicators). I decided to keep Exit conditions and Stop Losses on the 30min time frame for simplicity.
            Nevertheless, I get something like 800 trades per security instead of the previous 200.
            I often get an Entry and its Take Profit / Stop Loss in the same bar.
            How can I avoid this?

            Thank you
            Last edited by stefy; 11-03-2008, 07:36 AM.

            Comment


              #7
              Hello,


              You will need to debug it. Try using the TraceOrders and Print() options. Check your stop loss values just prior to using them.

              Feel free to post snippets of your code where you think the bug might be and we will take a look.

              If the target or stop is filled that means price reached that price. You can't "prevent" this behavior because this is how it is supposed to work if price reaches that point. In backtesting this is more evident because there is no intarbar logic and a conservative approach is taken in filling stops to prevent overestimation of strategy performance.
              DenNinjaTrader Customer Service

              Comment


                #8
                I tried to run the strategy in Replay mode.
                I noticed that sometimes, instead of a single entry per bar as it was happening in the 30min only environment, in the 30min - 5min execution, the strategy enters and exits many times inside the same bar.
                How can I prevent this to happen?

                Thank you

                Comment


                  #9
                  Hello,

                  You could use if(FirstTickOfBar) before the execution portion.

                  Or you could use if (Position.MarketPosition != MarketPosition.Flat) to see if you are flat before entering a new order.

                  You could also set up flag variables that would flag when you want to trade and when not to. Something like this:

                  if (flagVariable)
                  {
                  EnterLong();
                  flagVariable = false;
                  }

                  if (exit condtiion)
                  {
                  ExitLong();
                  flagVariable = true;
                  }
                  DenNinjaTrader Customer Service

                  Comment

                  Latest Posts

                  Collapse

                  Topics Statistics Last Post
                  Started by Geovanny Suaza, 02-11-2026, 06:32 PM
                  0 responses
                  637 views
                  0 likes
                  Last Post Geovanny Suaza  
                  Started by Geovanny Suaza, 02-11-2026, 05:51 PM
                  0 responses
                  366 views
                  1 like
                  Last Post Geovanny Suaza  
                  Started by Mindset, 02-09-2026, 11:44 AM
                  0 responses
                  107 views
                  0 likes
                  Last Post Mindset
                  by Mindset
                   
                  Started by Geovanny Suaza, 02-02-2026, 12:30 PM
                  0 responses
                  569 views
                  1 like
                  Last Post Geovanny Suaza  
                  Started by RFrosty, 01-28-2026, 06:49 PM
                  0 responses
                  571 views
                  1 like
                  Last Post RFrosty
                  by RFrosty
                   
                  Working...
                  X