NexusFi: Find Your Edge


Home Menu

 





MTF - Determining the TF


Discussion in NinjaTrader

Updated
    1. trending_up 1,242 views
    2. thumb_up 0 thanks given
    3. group 2 followers
    1. forum 2 posts
    2. attach_file 0 attachments




 
Search this Thread
  #1 (permalink)
 patfaninba 
Moved to Portugal
 
Experience: Advanced
Platform: NT and TOS
Trading: Futures - primarily NQ and ES
Frequency: Many times daily
Posts: 28 since Jan 2019
Thanks Given: 5
Thanks Received: 7

Hello,

I am working on a custom multi time frame indicator that has many plots across different time frames..

Some plots should appear only if they belong to a time frame higher than the time frame of the base chart..

in other words, lets say I have plots that are based on 1 hour, 4 hour and day time frames.. if the base chart is on 4 hour time frame, the lines based on 1 hour time frame should be suppressed.

Given both time frames are user selectable options at run time...I have 2 questions..from within the indicator,

1. How do I check what the plot's time frame is?
2. How do I check what the chart's base time frame is?

Note: using the example i cited above, I have created 3 data series using AddDataSeries function in OnStateChange / Configure. The plots were created using the wizard.

Appreciate your help in advance.


Started this thread Reply With Quote

Can you help answer these questions
from other members on NexusFi?
Bond Market Rout -- Worst Week Since Russias 2022 Invasi …
Treasury Notes and Bonds
Irans Dual Probability: Guns Quiet at 99.95% While Forma …
Prediction Markets & Event Contracts
April CPI Preview: +3.7% YoY Expected at 8:30 AM ET -- C …
Traders Hideout
Probability Collapse: Bitcoin $150k Craters from 15% to …
Prediction Markets & Event Contracts
CFTC Launches Innovation Task Force for AI Trading Syste …
Traders Hideout
 
Best Threads (Most Thanked)
in the last 7 days on NexusFi
Sober Journey With S&P
24 thanks
2026 Jlab journal
10 thanks
Algo automated / semi-automated trading anyone?
6 thanks
Lady Vols Primer: Trading Volatility Journal
6 thanks
Trying to learn Volume and price action correlation
5 thanks
  #2 (permalink)
 
Barz's Avatar
 Barz 
Kansas City, MO/US
 
Experience: Beginner
Platform: NinjaTrader
Trading: Futures
Posts: 18 since Jul 2018
Thanks Given: 36
Thanks Received: 15


patfaninba View Post
Hello,

I am working on a custom multi time frame indicator that has many plots across different time frames..

Some plots should appear only if they belong to a time frame higher than the time frame of the base chart..

in other words, lets say I have plots that are based on 1 hour, 4 hour and day time frames.. if the base chart is on 4 hour time frame, the lines based on 1 hour time frame should be suppressed.

Given both time frames are user selectable options at run time...I have 2 questions..from within the indicator,

1. How do I check what the plot's time frame is?
2. How do I check what the chart's base time frame is?

Note: using the example i cited above, I have created 3 data series using AddDataSeries function in OnStateChange / Configure. The plots were created using the wizard.

Appreciate your help in advance.

A bit late, but perhaps this will help others who stumble on this thread.

First, note that the primary (or base) series is always the 0th series in the BarsArray property of a NinjaScript.

Next, the trick is to convert the bar period for each series to a TimeSpan so that we can easily compare them.

Here is the full strategy code (for NT8). Should be similar for an Indicator:

 
Code
using System;
using System.Windows.Media;
using NinjaTrader.Data;

//This namespace holds strategies in this folder and is required. Do not change it.
namespace NinjaTrader.NinjaScript.Strategies.Barz.Sandbox.FuturesIO
{
    public class NexusFi_DetermineTimeFrame : Strategy
    {
        private TimeSpan[] _PeriodSpans;

        protected override void OnBarUpdate()
        {
            if (CurrentBar < BarsRequiredToTrade) { return; }

            // Conditionally populate plots based on whether the period is less
            // than or greater than the primary period.
            if (BarsInProgress == 0)
            {
                if (_PeriodSpans[1] > _PeriodSpans[0])
                {
                    Values[0][0] = Closes[1][0] + 20;
                }
                if (_PeriodSpans[2] < _PeriodSpans[0])
                {
                    Values[1][0] = Closes[2][0] - 20;
                }
            }

        }
        protected override void OnStateChange()
        {
            if (State == State.SetDefaults)
            {
                Name = "NexusFi_DetermineTimeFrame";

                // This creates a plot whose current value can be set in
                // OnBarUpdate using Values[0][0] = 25.1.
                AddPlot(Brushes.Yellow, "Larger Period");

                // This creates a plot whose current value can be set in
                // OnBarUpdate using Values[1][0] = 25.1.
                AddPlot(Brushes.Red, "Smaller Period");
            }
            else if (State == State.Configure)
            {
                // Ensure a 4 hour series has been added to your chart to serve
                // as the primary (base) series.

                // Add day chart here
                AddDataSeries(BarsPeriodType.Day, 1);

                // Add the 60 minute chart here
                AddDataSeries(BarsPeriodType.Minute, 60);
            }
            else if (State == State.DataLoaded)
            {
                _PeriodSpans = new TimeSpan[BarsArray.Length];

                for (int i = 0; i < BarsArray.Length; i++)
                {
                    BarsPeriod period = BarsArray[i].BarsPeriod;
                    Print(i + ": Period=" + period.BarsPeriodType.ToString()
                        + ", TimeSpan=" + period.ToTimeSpan());

                    // Capture the period for each series as a time span so we
                    // can easily do comparisons later.
                    _PeriodSpans[i] = period.ToTimeSpan();
                }

            }
        }

    }

    public static class BarsPeriodExtensions
    {
        public static TimeSpan ToTimeSpan(this BarsPeriod period)
        {
            switch (period.BarsPeriodType)
            {
                case BarsPeriodType.Second:
                    return TimeSpan.FromSeconds(period.Value);
                case BarsPeriodType.Minute:
                    return TimeSpan.FromMinutes(period.Value);
                case BarsPeriodType.Day:
                    return TimeSpan.FromDays(period.Value);
                case BarsPeriodType.Week:
                    return TimeSpan.FromDays(7 * period.Value);
                default:
                    throw new ArgumentException("BarsPeriodType not supported for conversion to TimeSpan: "
                        + period.BarsPeriodType);
            }
        }
    }
}


Reply With Quote
  #3 (permalink)
 patfaninba 
Moved to Portugal
 
Experience: Advanced
Platform: NT and TOS
Trading: Futures - primarily NQ and ES
Frequency: Many times daily
Posts: 28 since Jan 2019
Thanks Given: 5
Thanks Received: 7


Thank you Barz


Started this thread Reply With Quote




Last Updated on October 5, 2019


© 2026 NexusFi®, s.a., All Rights Reserved.
Av Ricardo J. Alfaro, Century Tower, Panama City, Panama, Ph: +507 833-9432 (Panama and Intl), +1 888-312-3001 (USA and Canada)
All information is for educational use only and is not investment advice. There is a substantial risk of loss in trading commodity futures, stocks, options and foreign exchange products. Past performance is not indicative of future results.
About Us - Contact Us - Site Rules, Acceptable Use, and Terms and Conditions - Downloads - Top
no new posts