NexusFi: Find Your Edge


Home Menu

 





MTF - Determining the TF


Discussion in NinjaTrader

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




 
Search this Thread

MTF - Determining the TF

  #1 (permalink)
 patfaninba 
san francisco, CA / USA
 
Experience: Beginner
Platform: TOS
Trading: Dow
Posts: 25 since Jan 2019
Thanks Given: 5
Thanks Received: 6

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?
What broker to use for trading palladium futures
Commodities
REcommedations for programming help
Sierra Chart
About a successful futures trader who didn´t know anyth …
Psychology and Money Management
ZombieSqueeze
Platforms and Indicators
Pivot Indicator like the old SwingTemp by Big Mike
NinjaTrader
 
  #2 (permalink)
 
Barz's Avatar
 Barz 
Kansas City, MO/US
 
Experience: Beginner
Platform: NinjaTrader
Trading: Futures
Posts: 18 since Jul 2018
Thanks Given: 35
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 FIO_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 = "FIO_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 
san francisco, CA / USA
 
Experience: Beginner
Platform: TOS
Trading: Dow
Posts: 25 since Jan 2019
Thanks Given: 5
Thanks Received: 6


Thank you Barz

Started this thread Reply With Quote




Last Updated on October 5, 2019


© 2024 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 - Privacy Policy - Downloads - Top
no new posts