NexusFi: Find Your Edge


Home Menu

 





Coding Multi Time Frame (MTF) Indicators with NinjaTrader


Discussion in NinjaTrader

Updated
      Top Posters
    1. looks_one Fat Tails with 180 posts (562 thanks)
    2. looks_two Zondor with 19 posts (29 thanks)
    3. looks_3 madLyfe with 16 posts (10 thanks)
    4. looks_4 BankRobberNT with 14 posts (16 thanks)
      Best Posters
    1. looks_one gomi with 3.7 thanks per post
    2. looks_two Fat Tails with 3.1 thanks per post
    3. looks_3 Zondor with 1.5 thanks per post
    4. looks_4 BankRobberNT with 1.1 thanks per post
    1. trending_up 189,485 views
    2. thumb_up 743 thanks given
    3. group 87 followers
    1. forum 424 posts
    2. attach_file 166 attachments




 
Search this Thread

Coding Multi Time Frame (MTF) Indicators with NinjaTrader

  #371 (permalink)
 
NJAMC's Avatar
 NJAMC 
Atkinson, NH USA
Market Wizard
 
Experience: Intermediate
Platform: NinjaTrader 8/TensorFlow
Broker: NinjaTrader Brokerage
Trading: Futures, CL, ES, ZB
Posts: 1,970 since Dec 2010
Thanks Given: 3,037
Thanks Received: 2,395

@Fat Tails,

I have a question on multiple time frames. I am looking back in time and need to figure out exactly which CurrentBars[] match. I can get close but doesn't seems to match reliable and have started to record associated CurrentBar for the other time series to see if I can get around it.

So here is what I am trying to do, the "simple" case: I have a primary 1 Min bar on the chart and add a 5 min bar as part of a strategy. I am now at bar CurrentBars[0]=1567 for the 1min bar and need to look back 13 bars to find the SMA(1min)[13] and the SMA(5min)[X]. I am hoping there is a simple or internal record of the associate between the bars so I don't need to try an create this association.

So in the above case, the 1min reference bar would be 1567-13 = 1554, the 5min initial bar CurrentBars[1] ~1567/5=313 is the 13th bar back the truncated value of 1554/5 = ~310 bars back 313-310 or 3 bars. It seems like using the 13/5 = ~2.6 might not be a reliable bars back calculation due to starting points of the time series.

Nil per os
-NJAMC [Generic Programmer]

LOM WIKI: NT-Local-Order-Manager-LOM-Guide
Artificial Bee Colony Optimization
Visit my NexusFi Trade Journal Reply With Quote

Can you help answer these questions
from other members on NexusFi?
ZombieSqueeze
Platforms and Indicators
Pivot Indicator like the old SwingTemp by Big Mike
NinjaTrader
MC PL editor upgrade
MultiCharts
REcommedations for programming help
Sierra Chart
Trade idea based off three indicators.
Traders Hideout
 
Best Threads (Most Thanked)
in the last 7 days on NexusFi
What is Markets Chat (markets.chat) real-time trading ro …
70 thanks
Spoo-nalysis ES e-mini futures S&P 500
55 thanks
Just another trading journal: PA, Wyckoff & Trends
31 thanks
Bigger Wins or Fewer Losses?
24 thanks
The Program
20 thanks
  #372 (permalink)
 
Fat Tails's Avatar
 Fat Tails 
Berlin, Europe
Market Wizard
 
Experience: Advanced
Platform: NinjaTrader, MultiCharts
Broker: Interactive Brokers
Trading: Keyboard
Posts: 9,888 since Mar 2010
Thanks Given: 4,242
Thanks Received: 27,103


NJAMC View Post
@Fat Tails,

I have a question on multiple time frames. I am looking back in time and need to figure out exactly which CurrentBars[] match. I can get close but doesn't seems to match reliable and have started to record associated CurrentBar for the other time series to see if I can get around it.

So here is what I am trying to do, the "simple" case: I have a primary 1 Min bar on the chart and add a 5 min bar as part of a strategy. I am now at bar CurrentBars[0]=1567 for the 1min bar and need to look back 13 bars to find the SMA(1min)[13] and the SMA(5min)[X]. I am hoping there is a simple or internal record of the associate between the bars so I don't need to try an create this association.

So in the above case, the 1min reference bar would be 1567-13 = 1554, the 5min initial bar CurrentBars[1] ~1567/5=313 is the 13th bar back the truncated value of 1554/5 = ~310 bars back 313-310 or 3 bars. It seems like using the 13/5 = ~2.6 might not be a reliable bars back calculation due to starting points of the time series.

I do not think that it is possible to associate the indices of two different bar series. I have always accepted that each of the bar series has its own index whatever it is. With 1 and 5 minutes things may be still easy, but if you take other timeframes you will typically experience cut-off bars at the session end.

Started this thread Reply With Quote
Thanked by:
  #373 (permalink)
 
Xav1029's Avatar
 Xav1029 
Tampa, FL
 
Experience: Beginner
Platform: NinjaTrader, Sierra Chart
Broker: Mirus Futures/Zen-Fire
Trading: 6E, M6E, 6J
Posts: 1,375 since Dec 2011
Thanks Given: 1,452
Thanks Received: 3,377



NJAMC View Post
@Fat Tails,

I have a question on multiple time frames. I am looking back in time and need to figure out exactly which CurrentBars[] match. I can get close but doesn't seems to match reliable and have started to record associated CurrentBar for the other time series to see if I can get around it.

So here is what I am trying to do, the "simple" case: I have a primary 1 Min bar on the chart and add a 5 min bar as part of a strategy. I am now at bar CurrentBars[0]=1567 for the 1min bar and need to look back 13 bars to find the SMA(1min)[13] and the SMA(5min)[X]. I am hoping there is a simple or internal record of the associate between the bars so I don't need to try an create this association.

So in the above case, the 1min reference bar would be 1567-13 = 1554, the 5min initial bar CurrentBars[1] ~1567/5=313 is the 13th bar back the truncated value of 1554/5 = ~310 bars back 313-310 or 3 bars. It seems like using the 13/5 = ~2.6 might not be a reliable bars back calculation due to starting points of the time series.

I sometimes use a "sync" series that stores current bars of one bar series in a dataseries inside the other bar series. It helps with exotic bars ,but due to NT's MTF logic you still get that 1 bar discrepancy when bars close at the exact same moment.

London Calling
Visit my NexusFi Trade Journal Reply With Quote
Thanked by:
  #374 (permalink)
 
NJAMC's Avatar
 NJAMC 
Atkinson, NH USA
Market Wizard
 
Experience: Intermediate
Platform: NinjaTrader 8/TensorFlow
Broker: NinjaTrader Brokerage
Trading: Futures, CL, ES, ZB
Posts: 1,970 since Dec 2010
Thanks Given: 3,037
Thanks Received: 2,395


Fat Tails View Post
I do not think that it is possible to associate the indices of two different bar series. I have always accepted that each of the bar series has its own index whatever it is. With 1 and 5 minutes things may be still easy, but if you take other timeframes you will typically experience cut-off bars at the session end.

@Fat Tails,

Thanks, I found another problem which you may already be aware of with the simple 1 -> 5 ratio. In the off hours, if there is 0 volume for 1 minute, you will only have 4 bars (or even less) per 5 minute bar!

I have tried to go with the sync method in an IntSeries for now. This is better, but still not right, maybe due to the NT MTF bar close issue. I have not tried this with 1 Min vs 3 Renko for example, not sure how things will work in that case. I am sure I can only go in one direction, but it might change based upon number of bars produced by each series. In this 1 Min/3Renko the number of bars/min for the renko is no longer correlated, could be more or less due to price action.

Nil per os
-NJAMC [Generic Programmer]

LOM WIKI: NT-Local-Order-Manager-LOM-Guide
Artificial Bee Colony Optimization
Visit my NexusFi Trade Journal Reply With Quote
  #375 (permalink)
 
vvhg's Avatar
 vvhg 
Northern Germany
 
Experience: Intermediate
Platform: NT
Trading: FDAX, CL
Posts: 1,583 since Mar 2011
Thanks Given: 1,016
Thanks Received: 2,824


NJAMC View Post
@Fat Tails,

Thanks, I found another problem which you may already be aware of with the simple 1 -> 5 ratio. In the off hours, if there is 0 volume for 1 minute, you will only have 4 bars (or even less) per 5 minute bar!

I have tried to go with the sync method in an IntSeries for now. This is better, but still not right, maybe due to the NT MTF bar close issue. I have not tried this with 1 Min vs 3 Renko for example, not sure how things will work in that case. I am sure I can only go in one direction, but it might change based upon number of bars produced by each series. In this 1 Min/3Renko the number of bars/min for the renko is no longer correlated, could be more or less due to price action.

One workaround for that could be to reference a bar by its time instead of its index.
There is also an indicator with an example on synchronizing bars

vvhg

Hic Rhodos, hic salta.
Reply With Quote
Thanked by:
  #376 (permalink)
symphys
Earth
 
Posts: 18 since Feb 2010
Thanks Given: 3
Thanks Received: 6

I'm a little bit lost or maybe it's getting too late but I'd like to put up some question here if I'm doing things right:
My strategy has 1 Minute Bar Chart and CalculateOnBarClose false. Now I'd like to calculate the EMA of 5 min chart and save it to a DataSeries.

So to analyse when the bar has closed I wait for FirstTickOfBar then use the last Bar (barsAgo = 1 in realtime, for historical bars this has to be 0 I guess)

 
Code
if (historical)
    barsAgo = 0;
else
    barsAgo = 1;

if (FirstTickOfBar)
{
   double ema50 = VisualEMA(50, false, false, VisualEMACalcMode.Strategy, VisualEMAPeriodType.Minute, 5, VisualEMAPriceType.Close)[barsAgo];
   emaSeries.Set(barsAgo, ema50);
}

Would this code result in correct values (realtime and histocial)?

Reply With Quote
  #377 (permalink)
 balance 
Taiwan
 
Experience: Intermediate
Platform: TOS, Matlab
Broker: IB, ToS
Trading: HSI, TF, CL
Posts: 17 since Aug 2011
Thanks Given: 5
Thanks Received: 11


vvhg View Post
One workaround for that could be to reference a bar by its time instead of its index.
There is also an indicator with an example on synchronizing bars

vvhg

In my project I have this issue all the time so here's the summary:

Most, if not all, of all bar referencing mechanism in NT is based on barsAgo, which is opposite to the internal 0-based array indexing. Since I need to record absolute indexing (array style), the following assumes absolute (0 is 1st bar) indexing:

If i is the array zero-based indexing, then its 'barsAgo' is always CurrentBar -i.

For two instruments in the same chart/indicator/strategy, assume their BarsArray index is p and q respectively,
then
the index of 'bar i (in timeframe p)' in timeframe q =

BarsArray[q].GetBar(Time[CurrentBars[p]-i])

For the reduced, and most likely case of p is the the 'current isntrument', then this is reduced to

BarsArray[q].GetBar(Time[CurrentBar-i])

This is the only way to get the correct answer.

Reply With Quote
  #378 (permalink)
 
Fat Tails's Avatar
 Fat Tails 
Berlin, Europe
Market Wizard
 
Experience: Advanced
Platform: NinjaTrader, MultiCharts
Broker: Interactive Brokers
Trading: Keyboard
Posts: 9,888 since Mar 2010
Thanks Given: 4,242
Thanks Received: 27,103


symphys View Post
I'm a little bit lost or maybe it's getting too late but I'd like to put up some question here if I'm doing things right:
My strategy has 1 Minute Bar Chart and CalculateOnBarClose false. Now I'd like to calculate the EMA of 5 min chart and save it to a DataSeries.

So to analyse when the bar has closed I wait for FirstTickOfBar then use the last Bar (barsAgo = 1 in realtime, for historical bars this has to be 0 I guess)

 
Code
if (historical)
    barsAgo = 0;
else
    barsAgo = 1;

if (FirstTickOfBar)
{
   double ema50 = VisualEMA(50, false, false, VisualEMACalcMode.Strategy, VisualEMAPeriodType.Minute, 5, VisualEMAPriceType.Close)[barsAgo];
   emaSeries.Set(barsAgo, ema50);
}

Would this code result in correct values (realtime and histocial)?

If you use a multi-timeframe indicator in a strategy, then you definitely introduce an additional lag. Compare

-> value from 5 minute bar retrieved -> projected onto primary bars -> used by strategy
-> value from 5 minute bar retrieved -> directly used by strategy

The projection is only done after the bar close of the secondary 5 min bars. For example, if you project on a 1-min bar series - most unfavourable case when primary and secondary bars have identical time stamps - then you will the result of the 5-min EMA only one bar later, if you run the strategy in COBC = true.

The indicator addresses the lag by repainting in One-Tick-Correction and Visual mode. It can be run in COBC = false. But if you use the MTF indicator in a strategy then you do not want it to repaint. Actually, I have somewhere presented an example how to access the indicator in other than strategy mode in order to let the strategy repaint, but in fact this is pretty insane.

It is also insane to let the strategy run in COBC= false, but not update the indicator values. I don't understand why you are doing this.

What about simply adding a secondary bar series of 5 min bars and applying an EMA to that series?

Started this thread Reply With Quote
Thanked by:
  #379 (permalink)
symphys
Earth
 
Posts: 18 since Feb 2010
Thanks Given: 3
Thanks Received: 6


Fat Tails View Post
What about simply adding a secondary bar series of 5 min bars and applying an EMA to that series?

Thanks for the insight. I'll be using this approach...

Reply With Quote
  #380 (permalink)
 
mk77ch's Avatar
 mk77ch 
Switzerland
 
Experience: Advanced
Platform: NinjaTrader
Broker: Dorman
Trading: Currency Futures, ES
Posts: 132 since Oct 2010
Thanks Given: 27
Thanks Received: 350


i am currently coding an indicator which is using a second dataseries.
the primary is a 1 minute chart and the second is a 5 minute.

could somebody please tell me how many bars of the second (m5) dataseries are getting loaded?
does it just load all available historical 5 minute bars or is there a way i can control this?

cheers,
mike

Reply With Quote




Last Updated on April 19, 2022


© 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