Announcement

Collapse

Looking for a User App or Add-On built by the NinjaTrader community?

Visit NinjaTrader EcoSystem and our free User App Share!

Have a question for the NinjaScript developer community? Open a new thread in our NinjaScript File Sharing Discussion Forum!
See more
See less

Partner 728x90

Collapse

Testing Multi-timeframe. Not working as expected.

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

    Testing Multi-timeframe. Not working as expected.

    Hi, I want to run a strategy in a 5 min chart (primary time frame), but checking for a condition in 1 min time frame. Specifically, the condition is that the time of day have to be 13:59. I code this simple condition to do this, but it is not working. When I run this in a 1 min chart, it works, but in 5 min chart, it don´t.

    Where is the mistake in this code?

    else if (State == State.Configure)
    {
    AddDataSeries(Data.BarsPeriodType.Minute, 1);
    }
    }

    protected override void OnBarUpdate()
    {
    if (BarsInProgress != 0)
    return;

    if (CurrentBars[0] < 0)
    return;

    // Set 1
    if (Times[1][0].TimeOfDay == new TimeSpan(13, 59, 0))
    {
    BackBrushAll = Brushes.CornflowerBlue;
    Print(@"signal");
    }

    }​

    #2
    Hello federicoo,

    Thank you for your note.

    I see you have the following BarsInProgress check:

    if (BarsInProgress != 0)
    return;​

    What this does is if OnBarUpdate() is called but it is for anything but the primary bar series, which is BarsInProgress == 0, then it will return. Since you are wanting to check the added data series, that is BarsInProgress == 1. You will need to adjust your logic to make sure you are accessing the proper data series in OnBarUpdate(). The snippet on the BarsInProgress page of the help guide shows how to separate logic based on which Bars object is calling OnBarUpdate():


    Here is another useful help guide resource regarding multi-bars scripts:


    Please let us know if we may be of further assistance.
    Emily C.NinjaTrader Customer Service

    Comment


      #3
      Thank you Emilly,

      When use strategy builder, this add the check of BarInProgress. How can I avoid that Strategy Builder put this lines checking that?

      Comment


        #4
        Hello federicoo,

        Thank you for your reply.

        Due to the limitations of the Strategy Builder, that line may not be removed or adjusted. What this would come down to is the Calculate property, which may be adjusted in the Strategy Builder on the Default Properties screen. It may also be adjusted when enabling a strategy - you can change it between On Bar Close, On Price Change, or On Each Tick. If it is set to On Bar Close, then the logic in OnBarUpdate will only be processed on the close of each bar for the primary series the strategy is added to. That is why this works on a 1-minute chart, because the code will run on the close of every 1-minute bar. On a 5-minute chart, the code will only run on the close of every 5-minute bar. If you change this to On Price Change, however, then OnBarUpdate will be processed for every price change. This would allow you to check the time of the 1-minute bars even while the 5-minute bars are still in the middle of being formed.

        I have created a strategy in the Strategy Builder that prints the time of the 1-minute added series, the 5-minute added series, and the primary series to which the strategy is applied to. You will be able to see the print statements if you open the Control Center > New > NinjaScript Output window while the strategy is enabled. With the default calculate setting of On Bar Close, you will see the print statements in the output window on the close of each bar from the chart where the strategy is added. If you change it to On Price Change, you will see the print statements every time the price changes. Here is the strategy I made to demonstrate this:
        OnBarUpdatePrintExamples_NT8.zip

        For more information about the Calculate property:


        Please let me know if I may be of further assistance.
        Emily C.NinjaTrader Customer Service

        Comment


          #5
          Thanks Emily,

          But I want to run this in strategy analyzer. It works fine in real time live data, but not working in strategy analyzer because it can't update onpricechange, I think... any ideas?

          Comment


            #6
            Hello federicoo,

            Thank you for you reply.

            In that case, you will need to work with an unlocked script and remove the BarsInProgress check that we previously discussed:
            if (BarsInProgress != 0)
            return;​​


            If you would like to test an unlocked script while keeping your original script available in the Strategy Builder, you could go to "Welcome" page of the builder, hover your mouse over the strategy in the list, then select Save As. GIve the script you plan to unlock a new name, then click Next in the builder then Unlock Code.
            NOTE: Once the code is unlocked, you can no longer use the Builder for subsequent strategy editing. This is why I suggest creating a copy of a script so that your original version can remain locked to the Strategy Builder for further edits if needed, then a new copy could be made as needed.

            Here is how I modified the OnBarUpdate portion of the script after unlocking a copy of the OnBarUpdatePrintExample script I provided in my last reply:

            protected override void OnBarUpdate()
            {
            // if (BarsInProgress != 0) <-- either comment out or delete these two lines in bold
            // return;

            if (CurrentBars[0] < 1)
            return;

            Print("BarsInProgress: " + BarsInProgress); // you can add this additional print statement if you'd like to see which BarsInProgress called OnBarUpdate when the script is running
            Print(@"The time for the 1 minute series is: " + Convert.ToString(Times[1][0].TimeOfDay));
            Print(@"The time for the 5 minute series is: " + Convert.ToString(Times[2][0].TimeOfDay));
            Print(@"The time for the primary series is: " + Convert.ToString(Times[0][0].TimeOfDay));
            }​

            For more information about BarsInProgress, please see the help guide section here:


            Please let me know if I may be of further assistance.
            Emily C.NinjaTrader Customer Service

            Comment

            Latest Posts

            Collapse

            Topics Statistics Last Post
            Started by lightsun47, Today, 03:51 PM
            0 responses
            2 views
            0 likes
            Last Post lightsun47  
            Started by 00nevest, Today, 02:27 PM
            1 response
            8 views
            0 likes
            Last Post 00nevest  
            Started by futtrader, 04-21-2024, 01:50 AM
            4 responses
            41 views
            0 likes
            Last Post futtrader  
            Started by Option Whisperer, Today, 09:55 AM
            1 response
            13 views
            0 likes
            Last Post bltdavid  
            Started by port119, Today, 02:43 PM
            0 responses
            8 views
            0 likes
            Last Post port119
            by port119
             
            Working...
            X