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

Multi-Trame AddDataSeries issue

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

    Multi-Trame AddDataSeries issue

    I am building a MTF indicator using AddDataSeries, during which I noticed that the weekly data is not aligned with the daily chart.

    I added the Weekly data series using AddDataSeries(BarsPeriodType.Week, 1) during Configure state, and I am running the indicator on Daily chart (symbol CAT, Kinetick source).

    I noticed that the start of the week provided by the weekly data series is off by a week.

    So, for 12-Dec-2022 (Monday), the weekly open I get is from the 05-Dec-2022 and I see this repeating for 13th, 14th and 15th Dec. However, for 16th Dec, I get the correct weekly open of 12-Dec.


    Code:
        public class TestMTF : Indicator
        {
            bool currentTfUpdated = false;    // Keep track of tick of current TF is received.
            bool weeklyTfUpdated = false;    // Keep track of tick of Weekly TF.
            protected override void OnStateChange()
            {
                if (State == State.SetDefaults)
                {
                    // Skipped boilerplate code.                        
                }
                else if (State == State.Configure)
                {
                    // Adding Weekly data-series of the same instrument.
                    AddDataSeries(BarsPeriodType.Week, 1);                
                }
            }
    
            protected override void OnBarUpdate()
            {
                if( BarsInProgress == 0 ) currentTfUpdated = true;
                else if( BarsInProgress == 1 ) weeklyTfUpdated = true;
    
                if( currentTfUpdated && weeklyTfUpdated ) // Print only if we have received update of current and weekly TF.
                {
                    double weeklyOpenPrice = BarsArray[1].GetOpen(CurrentBars[1]);
                    DateTime weeklyCandleTime = BarsArray[1].GetTime(CurrentBars[1]);
                    Print(string.Format("BarsInProgress = {0}, Time[0] = {1}, Close[0] = {2}, WeeklyTime = {3}, WeeklyOpen = {4}",
                                        BarsInProgress, Time[0].ToString("yyyy.MM.dd"),
                                        Close[0].ToString(), weeklyCandleTime.ToString("yyyy.MM.dd"), weeklyOpenPrice.ToString()));
                }            
            }
    }
    ​

    Now notice the output window, for 12th Dec (2022.12.13) and 13th Dec (2022.12.14), 14th and 15th. The weekly open price is 234.8, which is actually the open price of previous Monday (5th Dec).
    If I open the weekly chart, and check the weekly open price in the data-box of 12th Dec, it is shown as 227.55 (which is correct).



    BarsInProgress = 0, Time[0] = 2022.12.09, Close[0] = 230.92, WeeklyTime = 2022.12.02, WeeklyOpen = 233.94
    BarsInProgress = 0, Time[0] = 2022.12.10, Close[0] = 227.29, WeeklyTime = 2022.12.09, WeeklyOpen = 234.8
    BarsInProgress = 1, Time[0] = 2022.12.10, Close[0] = 227.29, WeeklyTime = 2022.12.09, WeeklyOpen = 234.8
    BarsInProgress = 0, Time[0] = 2022.12.13, Close[0] = 233.06, WeeklyTime = 2022.12.09, WeeklyOpen = 234.8
    BarsInProgress = 0, Time[0] = 2022.12.14, Close[0] = 235.49, WeeklyTime = 2022.12.09, WeeklyOpen = 234.8
    BarsInProgress = 0, Time[0] = 2022.12.15, Close[0] = 234.48, WeeklyTime = 2022.12.09, WeeklyOpen = 234.8
    BarsInProgress = 0, Time[0] = 2022.12.16, Close[0] = 230.66, WeeklyTime = 2022.12.09, WeeklyOpen = 234.8

    BarsInProgress = 0, Time[0] = 2022.12.17, Close[0] = 232.72, WeeklyTime = 2022.12.16, WeeklyOpen = 227.55
    BarsInProgress = 1, Time[0] = 2022.12.17, Close[0] = 232.72, WeeklyTime = 2022.12.16, WeeklyOpen = 227.55



    What am I doing wrong and how do I fix this ?

    Attached Files

    #2
    Hello firstlanetech,

    Thanks for your post.

    I see in your script that you are printing out the BarsInProgress value, current Time, current Close price, Time of the added Weekly series, and Open price of the added Weekly series. I also see that the script is using Calculate.OnBarClose.

    Calculate.OnBarClose means the strategy will only process logic at the close of the bar.

    To test the script I opened a New > Chart window for the CAT instrument. I added a Daily data series and a Weekly Data series both with 30 Days To Load oo the chart and opened a Data Box window to compare the bar information to the prints in the NinjaScript Output window.

    After adding the TestMTF indicator you shared onto the CAT Daily data series, I see that the prints are correctly matching the bar information when comparing the prints to the Data Box.

    When viewing the December 9, 2022 candles for the Daily and Weekly series we can see a Daily bar Close price of 227.29 and a Weekly bar Open price of 234.80.

    The prints in the Output window match these values. The Daily series Close[0] print is 227.29 and the WeeklyOpen print is 234.8.

    From the Output window:
    BarsInProgress = 0, Time[0] = 2022.12.09, Close[0] = 227.29, WeeklyTime = 2022.12.09, WeeklyOpen = 234.8
    BarsInProgress = 1, Time[0] = 2022.12.09, Close[0] = 227.29, WeeklyTime = 2022.12.09, WeeklyOpen = 234.8

    When viewing the December 13, 2022 candle we can see a Daily Close price of 233.06 and the same WeeklyOpen price of 234.8. When comparing these values to the Data Box window, we see the values match as expected.

    From the Output window:
    BarsInProgress = 0, Time[0] = 2022.12.12, Close[0] = 233.06, WeeklyTime = 2022.12.09, WeeklyOpen = 234.8

    Note that it is expected we see a date of 12/09/2022 for the Weekly series candle since the next Weekly bar has not closed yet and the script is using Calculate.OnBarClose. We will continue seeing the WeeklyOpen date 12/09/2022 and WeeklyPrice of 234.8 in the Output window until the next weekly bar closes on 12/16/2022. Once the Weekly bar closes on 12/16/2022, the Weekly bar-related prints will reflect that Weekly 12/16/2022 bar's information.

    Here is a demonstration video showing the above information and how it was tested: https://brandonh-ninjatrader.tinytak...M18yMDY5OTk1NA

    Please let me know if you have further questions.
    Brandon H.NinjaTrader Customer Service

    Comment


      #3
      Thank you for taking the time to address this question.

      So to summarize, the higher TF candle data is available on lower TF only after the higher TF candle has closed.

      For example, if I am running a 15-minute chart, and I want the opening price of the current 1-hour candle, it will not be available till the 1-hour candle has closed.
      E.g. current 15-minute candle is 11:45 (to 12:00), and I want the opening price of 1-hour candle (11:00 to 12:00), it will not be available till the 12:00 (when the 1 hour candle has closed).

      Comment


        #4
        Hello firstlanetech,

        Thanks for your note.

        That is correct. When using a Calculate mode of OnBarClose, the OnBarUpdate() method of the script will only run at the close of a bar.

        If you have an additional 1-hour timeframe added to a script and you use Calculate.OnBarClose, information about this 1-hour bar will only process at the close of that 1-hour bar.

        To get intrabar information you would need to use Calculate.OnPriceChange or OnEachTick. OnPriceChange means onBarUpdate() will process once for each price change. OnEachTick means OnBarUpdate() will process on every single tick.

        Please review this help guide page for more information about Calculate: https://ninjatrader.com/support/help.../calculate.htm

        Let me know if I may further assist you.
        Brandon H.NinjaTrader Customer Service

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by DayTradingDEMON, Today, 09:28 AM
        4 responses
        21 views
        0 likes
        Last Post DayTradingDEMON  
        Started by geddyisodin, Yesterday, 05:20 AM
        9 responses
        50 views
        0 likes
        Last Post NinjaTrader_Gaby  
        Started by George21, Today, 10:07 AM
        1 response
        11 views
        0 likes
        Last Post NinjaTrader_ChristopherJ  
        Started by Stanfillirenfro, Today, 07:23 AM
        9 responses
        24 views
        0 likes
        Last Post NinjaTrader_ChelseaB  
        Started by navyguy06, Today, 09:28 AM
        1 response
        9 views
        0 likes
        Last Post NinjaTrader_Gaby  
        Working...
        X