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-time frame and dataseries problems

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

    multi-time frame and dataseries problems

    Q: how to calculate EMA3 on dataseries, that based on primary TF, but last value is taken from secondary TF? Problem is that EMA3 is updated on close of primary 1hour bars, but not inside of secondary minute bars at the moment I update dataseries there. So I cannot check signal condition "intrabar". What I am missing?

    my code:
    Code:
            protected override void Initialize()
            {			
    			Add(PeriodType.Minute, 1);
    			CalculateOnBarClose = true;			
    			BarsRequired = _ema2_period;
    			_ema1_data = new DataSeries(this, MaximumBarsLookBack.Infinite);
    			_ema2_data = new DataSeries(this, MaximumBarsLookBack.Infinite);
            }
            private bool InitIndies()
            {
                if (_init) return true;
                _ema1=EMA(_ema1_data, _ema1_period); if (_ema1==null) return false;            
                _ema2=EMA(_ema2_data, _ema2_period); if (_ema2==null) return false;
                _init=true;
                return true;
            }
    
            protected override void OnBarUpdate()
            {    
                    // we are in main dataseries 1H
                    if (BarsInProgress == 0 ) {
                        if (!InitIndies()) return;
    
                        if (CurrentBars[0] < BarsRequired) {
                            _ema1_data.Set(Closes[0][0]);
                            _ema2_data.Set(Closes[0][0]);
                            return;
                        }
                        _signaled=false;
                    } else
                    // we are in 1m dataseries
                    if (BarsInProgress == 1) {                    
                        if (!_init || (CurrentBars[0] < BarsRequired)) return;
                        _ema1_data.Set(Closes[1][0]);
                        _ema2_data.Set(Closes[1][0]);
                        if (!_signaled) {
     			// check _ema1[0] and_ema2[0] for conditions...
                        }
                    }
            }
    P.S. yes, I read manual many times.

    thanks.

    #2
    Hi Andrius,

    Originally posted by andrius View Post
    Q: how to calculate EMA3 on dataseries, that based on primary TF, but last value is taken from secondary TF?
    You define the context of updates with BarsInProgress, and you can use Closes[1] (Secondary series close) as input to your EMA.

    if (BarsInProgress == 0)
    Value.Set(EMA(Closes[1], 14)[0]);

    Our historical bar referencing model is conservative, in that you can't use smaller time frames in order to "peek" into the future. If you are not sure what bar is referenced, print the time stamp of each series to see how they interrelate.
    Ryan M.NinjaTrader Customer Service

    Comment


      #3
      This does not answer my question and do not provide solution. Your example deals with action inside primary BarArray, my question is about action inside secondary BarArray.

      Since all this question already took several emails and forum post I assume "Our historical bar referencing model is conservative" is a cowardly way to say "NO you cannot do that with NT".

      It's unfortunate this is all you get from NT support once you stuck with uncommon problem.

      Comment


        #4
        Andrius, to start out, I think I'm having the same problem.. but I don't understand the problem you're having. You never reference EMA3 in code, for example.

        My problem: trying to keep historical data for values calculated on the events of a sub-timeframe. They just.. don't tick over until the primary instrument's timeframe, so half of all values are "missing". Testing tonight, however, I believe I have an awful, but workable, solution to our problem. Consider this indicator:

        *DISCLAIMER: This method is, undoubtedly, unsupported by either me or NinjaTrader. It uses, after all, more than simple arithmetic.

        Code:
        #region Using declarations
        using System;
        using System.Diagnostics;
        using System.Drawing;
        using System.Drawing.Drawing2D;
        using System.ComponentModel;
        using System.Collections;
        using System.Xml.Serialization;
        using NinjaTrader.Data;
        using NinjaTrader.Gui.Chart;
        using CMI.NinjaTrader.Data;
        #endregion
        
        namespace NinjaTrader.Indicator {
            /// <summary>
            /// Capital Management Institute
            /// </summary>
            [Description("Capital Management Institute.")]
            public class CMITest : Indicator {
                #region Variables
        
                private DataSeries subtfDataa;
                private DataSeries subtfDatab;
        
                #endregion
        
                protected override void Initialize() {
        
                    // Get the primary instrument and readd it with a different time period.
                    Add(Instruments[0].FullName, PeriodType.Minute, 1);
                }
        
                protected override void OnStartUp() {
                    base.OnStartUp();
        
                    // Note that I'm not doing this in Initialize() because I'm not sure I can reference Closes.
        
                    // Create a dataseries using this indicator as the "parent".
                    subtfDataa = new DataSeries(this);
        
                    // Then create another indicator using the different timeframe'd data, and create
                    // a dataseries from that different timeframe'd indicator.
                    subtfDatab = new DataSeries(SMA(Closes[1], 2));
                }
        
                protected override void OnBarUpdate() {
        
                    if (BarsInProgress == 0)
                        return;
        
                    if (CurrentBar < 10)
                        return;
        
                    // On each bar close, print the time. Note that the sub timeframe dataseries is desired
                    // the same as the sub timeframe pricing data, but I suspect it will match up with
                    // every other one.
                    subtfDataa.Set(Closes[1][0]);
                    subtfDatab.Set(Closes[1][0]);
        
                    string stra = "", strb = "", strc = "";
                    for (int i = 0; i < 10; i++) {
                        stra += "; " + Closes[1][i];
                        strb += "; " + subtfDataa[i];
                        strc += "; " + subtfDatab[i];
                    }
        
                    Print(Time[0] + "   Close values" + stra);
                    Print("Close values,  primary     timeframe" + strb);
                    Print("Close values, secondary timeframe" + strc);
                    Print("");
                }
        
                // So I don't know.. do I need to dispose of data series when I'm done?
        
                #region Properties
                #endregion
            }
        }
        I added this to a chart with a 3-minute period.

        And some output:
        2012/06/07 12:21:00 Close values; 1315.5; 1315.75; 1315.5; 1315.75; 1316; 1315.5; 1314.75; 1315; 1314.75; 1314.75
        Close values, primary timeframe; 1315.5; 1315.75; 1316; 1315; 1314.5; 1315.25; 1315.25; 1315; 1315.5; 1315.25
        Close values, secondary timeframe; 1315.5; 1315.75; 1315.5; 1315.75; 1316; 1315.5; 1314.75; 1315; 1314.75; 1314.75

        2012/06/07 12:22:00 Close values; 1316.5; 1315.5; 1315.75; 1315.5; 1315.75; 1316; 1315.5; 1314.75; 1315; 1314.75
        Close values, primary timeframe; 1316.5; 1315.75; 1316; 1315; 1314.5; 1315.25; 1315.25; 1315; 1315.5; 1315.25
        Close values, secondary timeframe; 1316.5; 1315.5; 1315.75; 1315.5; 1315.75; 1316; 1315.5; 1314.75; 1315; 1314.75

        2012/06/07 12:23:00 Close values; 1316.5; 1316.5; 1315.5; 1315.75; 1315.5; 1315.75; 1316; 1315.5; 1314.75; 1315
        Close values, primary timeframe; 1316.5; 1315.75; 1316; 1315; 1314.5; 1315.25; 1315.25; 1315; 1315.5; 1315.25
        Close values, secondary timeframe; 1316.5; 1316.5; 1315.5; 1315.75; 1315.5; 1315.75; 1316; 1315.5; 1314.75; 1315

        2012/06/07 12:24:00 Close values; 1316.25; 1316.5; 1316.5; 1315.5; 1315.75; 1315.5; 1315.75; 1316; 1315.5; 1314.75
        Close values, primary timeframe; 1316.25; 1316.5; 1315.75; 1316; 1315; 1314.5; 1315.25; 1315.25; 1315; 1315.5
        Close values, secondary timeframe; 1316.25; 1316.5; 1316.5; 1315.5; 1315.75; 1315.5; 1315.75; 1316; 1315.5; 1314.75

        2012/06/07 12:25:00 Close values; 1316; 1316.25; 1316.5; 1316.5; 1315.5; 1315.75; 1315.5; 1315.75; 1316; 1315.5
        Close values, primary timeframe; 1316; 1316.5; 1315.75; 1316; 1315; 1314.5; 1315.25; 1315.25; 1315; 1315.5
        Close values, secondary timeframe; 1316; 1316.25; 1316.5; 1316.5; 1315.5; 1315.75; 1315.5; 1315.75; 1316; 1315.5

        2012/06/07 12:26:00 Close values; 1316; 1316; 1316.25; 1316.5; 1316.5; 1315.5; 1315.75; 1315.5; 1315.75; 1316
        Close values, primary timeframe; 1316; 1316.5; 1315.75; 1316; 1315; 1314.5; 1315.25; 1315.25; 1315; 1315.5
        Close values, secondary timeframe; 1316; 1316; 1316.25; 1316.5; 1316.5; 1315.5; 1315.75; 1315.5; 1315.75; 1316
        First, note the timestamps.. it's all very confusing. The timestamps increment in one-minute intervals, because I only allow OnBarUpdate to execute on the 1-minute sub-timeframe.

        The values, then. I started this block at 12:21. That means that at the end of 12:20, the primary instrument ticked, and [1] will have the same values for each because the subtimeframe ticked together with the primary at 12:21. So, in this sub-timeframe bar, 0 is set. At the end of this minute, the sub-timeframe ticks again, and the primary instrument/dataseries _does_not_.

        Because of the new tick, the second block's secondary timeframe has a new [1], close has a new [1], but the primary instrument's dataseries has the xxx.75 that it had before. This happens once more (twice total) before the minutes align again, and both the SMA indicator and the primary instrument tick at the same time (the 4th block, above).

        If you notice, the primary timeframe's values are missing 2 values between each one compared to the Closes[1]. That's because the value is updated, but is then reset on the next sub-timeframe bar close because the primary instrument hasn't ticked again yet.

        So, I think what you need to do, then, is create a dataseries around an indicator based on the bars of the sub-timeframe. Ugly. Hideous. But.. it's what's required to get NinjaTrader to work, as far as I know.

        Proper fix: allow a DataSeries to be passed a Bars object (I see a 'bar' event, but have no idea how it's used) on creation. Better yet, allow it to be passed an instrument (but I can't figure out how to get timing information from one). Only the latter would prevent the whole concept from being unnecessarily confusing.
        Last edited by DrkShadow; 06-11-2012, 06:06 PM.

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by andrewtrades, Today, 04:57 PM
        1 response
        10 views
        0 likes
        Last Post NinjaTrader_Manfred  
        Started by chbruno, Today, 04:10 PM
        0 responses
        6 views
        0 likes
        Last Post chbruno
        by chbruno
         
        Started by josh18955, 03-25-2023, 11:16 AM
        6 responses
        436 views
        0 likes
        Last Post Delerium  
        Started by FAQtrader, Today, 03:35 PM
        0 responses
        9 views
        0 likes
        Last Post FAQtrader  
        Started by rocketman7, Today, 09:41 AM
        5 responses
        20 views
        0 likes
        Last Post NinjaTrader_Jesse  
        Working...
        X