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 186,462 views
    2. thumb_up 743 thanks given
    3. group 87 followers
    1. forum 424 posts
    2. attach_file 166 attachments




 
 

Coding Multi Time Frame (MTF) Indicators with NinjaTrader

 
 
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,102

I am still relying on NinjaTrader to code my indicators and have recently played around with adding secondary bar series to indicators, so I just want to share my experience and show what currently can be done and what can't be done.

Of course I hope that others will share their experience as well, allowing to better exploit the full potential that lies within this option which has recently been introduced with NinjaTrader 7.

With the first post I just want to summarize, what I have in mind, and what potential improvements can be made.


Increasing the Resolution of Indicators

Some indicators do not show correct values, because the information required to calculate them is not contained with the primary bar series. By adding a secondary high definition bar series, the indicators can be improved. There are three of them that come to my mind immediately.

The current session's VWAP:

If you calculate the volume weighted average price for a 30 min bar series, there is only one value after 30 minutes which could be the typical or any other price calculated from Open, High, Low and Close. It is obvious that this value will be far from reality and that the calculated value will be false. It would be much better to calculate the VWAP from a secondary 1 minute bar series, which would reduce the error to a minimum for all practical purposes.

The opening range indicator:

If you read "The Logical Trader" by Mark Fisher, you will not that he suggests an opening range of 20 minutes for ES. How can you display this range on a 15 min chart? The answer is: You can't. Unless you calculate the opening range from a secondary 1 min data bar series.

Volume profiles:

Let us assume that you want to display a market profile for the current session on a 17 minute chart by using one of the volume zones indicators (such as the dValueArea indicator). The problem which you will encounter here: You cannot calculate 30 min TPOs from 17 minute data. Also you cannot calculate any mode for the volume distribution of the current session from 17 min data, but you need higher resolution data. This again would be a case for a multi time frame indicator.


Displaying Higher Time Frame Signals on Your Chart

This is practically the opposite of the first way to use a secondary bar series. It is technically impossible to display smaller time frame signals on any chart, as the resolution of the chart, that is the grid defined by the visible bar series does not allow it. This limits the use of smaller bar series to cases, where more precision is needed to display some of the more complex indicators. Also smaller time frame traders can be considered as noise traders, so I am rather interested in what the larger time frame traders are doing.

Some of the indicators that are used are already MTF indicators as they tell us something about the actions of the other time frame traders. These indicators use reference points outside the visible fraction of the chart. So although indicator values are mostly calculated from price, and just represent a reduced version of price, it is their time frame transfer capability that makes them interesting for the trader.

Existing indicators include market profile, daily, weekly and monthly pivots, average daily range, fibonacci confluence indicators, which all add some information from the more distant past.

But now, with adding data series, anything should be possible. Moving averages, oscillators, divergence indicators, actually the whole set of NinjaTrader default indicators as a MTF version allowing you to calculate them on a 60 min bar series, but display the results on a 157 tick or 6 range chart.

However, there is a number of traps, which have to be avoided, when creating such indicators. My intention is to document some of those traps with a simple multi time frame EMA. I also want to release a modified version of the SuperTrend indicator.

Started this thread

Can you help answer these questions
from other members on NexusFi?
Better Renko Gaps
The Elite Circle
Deepmoney LLM
Elite Quantitative GenAI/LLM
Futures True Range Report
The Elite Circle
ZombieSqueeze
Platforms and Indicators
The space time continuum and the dynamics of a financial …
Emini and Emicro Index
 
Best Threads (Most Thanked)
in the last 7 days on NexusFi
Get funded firms 2023/2024 - Any recommendations or word …
59 thanks
Funded Trader platforms
36 thanks
NexusFi site changelog and issues/problem reporting
25 thanks
GFIs1 1 DAX trade per day journal
19 thanks
The Program
18 thanks
 
 
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,102


One of my first indicators, which I have modified, is the Opening Range Indicator. This is an experimental version, so whoever uses it, is supposed to give some feedback whether it works as expected. The indicator has the following features:

-> it loads a separate 1-minute bar series which is used to calculate the opening range
-> the use of a session template including the RTH session for which you want to display the opening range is compulsory
-> it should work for any full minute opening range on all minute, range, volume and tick charts
-> it has an option to display the RTH open or the first 1-minute bar of the session
-> it colors the opening range, the opacity can be selected also allowing to unselect the fill color for the range
-> it also allows to display the night session range, or alternatively the range of the European or Asian session

Some general notes on secondary bar series:

(1) The session template:

The secondary bar series uses the same session template as the primary bar series. Another template cannot be used.

(2) The lag problem:

OnBarUpdate() always calls the secondary bar series after the primary bar series. This is one of the fundamental problems of MTF indicators. Let us assume that your primary bar series uses 15 minute bars and the secondary bar series uses 1-min bars. This means that when the first 15-minute bar closes, only the information generated by the prior 14 1-min bars is available. The last 1-minute bar will pass unnoticed by the 15-minute bar! This problem introduces an additional lag, as the full information will only be disclosed with the first tick of the second 15-minute bar.

This is very annoying for historical data. Historical bars are 1-tick bars, so the information from the last 1-minute bar of the first 15 minutes can only be displayed when the second 15 minute bar is completed, which is after 30 min. For real-time data this is less of a problem, as the first tick should arrive pretty fast after the first 15 min bar closed. An exception is the situation at the session close.

(3) The one-tick repainting option

Because of this lag problem, some of my MTF indicators have an option to repaint the prior bar after the first tick of the consecutive bar has arrived. This makes sense for visual display of historical bars and also for the visual display of real-time data. When running strategies, this option should be turned off, as repainting falsifies data in the way it arrives, so you would need to accept the lag introduced by the way OnBarUpdate() works.

The on tick repainting option should also be used with the last bar of a session, as otherwise you would need to wait until the new session starts to display the values calculated from the last secondary bar of the old session.

(4) Checking for bars

For any MTF indicator it is necessary to check for bars. The minimum shoud be BarsRequired = 0, which indicates that you do not attempt to run it on an empty bar series. OnBarUpdate() should always include a line as per below:

 
Code
if(CurrentBars[0] < BarsRequired || CurrentBars[1] < BarsRequired)
	return;
Attached is the experimental opening range indicator (labelled X1). Please report any issues. Further suggestions are welcome. An official version of this indicator will go to the download section after testing.

Attached Thumbnails
Click image for larger version

Name:	Opening Range MTF.jpg
Views:	1442
Size:	90.4 KB
ID:	36276  
Attached Files
Elite Membership required to download: OpeningRangeV32MTFX1.zip
Started this thread
 
 
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,102

This is another experimental indicator. It allows to display any EMA on your chart, for example you can display

- a 3 range EMA on a 275 tick chart
- a 60 min EMA on a 200 volume chart
- a 60 min EMA on a 15 min chart


Allows to plot indicators based on different bar types and time frames on evenly spaced charts

You can also display multi time frame indicators by adding a second data series to your chart. For example, if you trade a tick chart you can add a 60 min bar series and then display an EMA(9) calculated from that 60 min series. The only problem you will encounter is that your bars will be unevenly spaced. With a multi time frame indicator, you avoid this uneven spacing. You typically pay with a little lag, which can be neglected for larger time frames.


Three modes to display the EMA

The indicator has three modes to demonstrate the behavior of the dual bar series indicator.

StrategicMode: The first bar series collects the values from the secondary series with the little lag already described. The chart attached shows this slightly lacking series in orange.

FirstTickMode: To avoid the lag for minute bars with identical time stamps, the indicator is allowed to repaint the indicator value for the prior bar, after the first tick of the current bar has been registered. The chart below shows the first tick mode in red. You will not that the lag has been reduced.

Visual Mode: To make it easier to use the indicator visually, the indicator interpolates the distance between two consecutive bars of the secondary bar series. The interpolation algorithm works in real-time and simulates the CalculateOnBarClose = false mode for the indicator relative to the secondary bar series. It repaints the current leg of the indicator back to the prior bar of the secondary series. The interpolation adapts itself to equidistant or non-equidistant charts.


Something to play with

I have attached the indicator. It is a protected assembly, which will only run on NT 7.0.1000.3 or later, but which is licence free. The chart below has the indicator applied three times. The blue line is the EMA in VisualMode, which is easier to use than the step functions that can be typically seen in other MTF indicators.

It is the first indicator of this type that I have coded, so please report any issues.

Attached Thumbnails
Click image for larger version

Name:	CL 05-11 (3 Min) 11_04_2011.jpg
Views:	1461
Size:	108.3 KB
ID:	36329  
Attached Files
Elite Membership required to download: VisualEMAX1.zip
Started this thread
 
 
TempletonPeck's Avatar
 TempletonPeck 
Germany
 
Experience: Beginner
Platform: NinjaTrader
Broker: Mirus Futures/Zen-Fire
Posts: 200 since Feb 2010
Thanks Given: 1,138
Thanks Received: 202

First of all thanks for your continual improvements.

Just for clarification:
Because of the lag problem it is right that the indicator shows a OR High of 1,4497 from 14:50:01-14:55 CET. ( I have chosen an 30 min opening range from 14:20 - 14:50 CET). The new high 1,4499 occured between 14:49 and 14:50 (last 1-minute bar).
So the new OR High was displayed after the next 5 minute bar closed at 14:55:01.

Attached Thumbnails
Click image for larger version

Name:	6E 06-11 (5 Min)  12_04_201101.jpg
Views:	622
Size:	84.7 KB
ID:	36418   Click image for larger version

Name:	6E 06-11 (5 Min)  12_04_201102.jpg
Views:	524
Size:	86.2 KB
ID:	36419  
Thanked by:
 
 
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,102


TempletonPeck View Post
First of all thanks for your continual improvements.

Just for clarification:
Because of the lag problem it is right that the indicator shows a OR High of 1,4497 from 14:50:01-14:55 CET. ( I have chosen an 30 min opening range from 14:20 - 14:50 CET). The new high 1,4499 occured between 14:49 and 14:50 (last 1-minute bar).
So the new OR High was displayed after the next 5 minute bar closed at 14:55:01.


Thanks for pointing this out. I also noticed the same thing.

If you use the indicator in CalculateOnBarClose = true (default setting), which you probably did, then this is what happens

14:49 -> the (incomplete opening range is calculated from the secondary bar series, which is 1 minute data)
14:50 -> the developing opening range is displayed for the period 14:20 - 14:49
14:50 -> the complete opening range is calculated via the secondary bar series
14:50 + 1 tick -> nothing, because OnBarUpdate() is not triggered
14:55 -> the result of the calculation made at 14:50 is now displayed

To NinjaTrader Developers: It is really a pity that BarSeries 0 is called prior to BarSeries 1.

Now this problem is very specific to your setup. It does not occur

- if the indicator is applied to tick, range or volume charts (time stamp of prior bar cannot be equal)
- if the duration of the opening range is not an integer multiple of the primary bar series (minute bars)
- for all consecutive bars of the chart

So for this problem to occur you need

- to select minute bars
- to select a bar period such that the opening range is an integer multiple of that bar period
- and watch out for that single bar for which the result will be displayed with a lag

In this specific case, you better use the simple version of the opening range indicator instead of the multi time frame version.

But you are absolutely right and did hit that little nail on its head.

Started this thread
Thanked by:
 
 
perryg's Avatar
 perryg 
Rechovot
 
Experience: Advanced
Platform: NinjaTrader
Broker: CQG
Trading: Index,Currency and Energy futures
Posts: 1,644 since Jan 2010
Thanks Given: 508
Thanks Received: 6,288


Fat Tails View Post
This is another experimental indicator. It allows to display any EMA on your chart, for example you can display

- a 3 range EMA on a 275 tick chart
- a 60 min EMA on a 200 volume chart
- a 60 min EMA on a 15 min chart


Allows to plot indicators based on different bar types and time frames on evenly spaced charts

You can also display multi time frame indicators by adding a second data series to your chart. For example, if you trade a tick chart you can add a 60 min bar series and then display an EMA(9) calculated from that 60 min series. The only problem you will encounter is that your bars will be unevenly spaced. With a multi time frame indicator, you avoid this uneven spacing. You typically pay with a little lag, which can be neglected for larger time frames.


Three modes to display the EMA

The indicator has three modes to demonstrate the behavior of the dual bar series indicator.

StrategicMode: The first bar series collects the values from the secondary series with the little lag already described. The chart attached shows this slightly lacking series in orange.

FirstTickMode: To avoid the lag for minute bars with identical time stamps, the indicator is allowed to repaint the indicator value for the prior bar, after the first tick of the current bar has been registered. The chart below shows the first tick mode in red. You will not that the lag has been reduced.

Visual Mode: To make it easier to use the indicator visually, the indicator interpolates the distance between two consecutive bars of the secondary bar series. The interpolation algorithm works in real-time and simulates the CalculateOnBarClose = false mode for the indicator relative to the secondary bar series. It repaints the current leg of the indicator back to the prior bar of the secondary series. The interpolation adapts itself to equidistant or non-equidistant charts.


Something to play with

I have attached the indicator. It is a protected assembly, which will only run on NT 7.0.1000.3 or later, but which is licence free. The chart below has the indicator applied three times. The blue line is the EMA in VisualMode, which is easier to use than the step functions that can be typically seen in other MTF indicators.

It is the first indicator of this type that I have coded, so please report any issues.

Fats Tails,
I am excited at what I see. Is it possible to put together a similar indicator for SMA. I use both EMA and SMA and this way I can test entry points using this idea. So far the EMA looks good in the Visual mode.

Thanked by:
 
 
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,102


perryg View Post
Fats Tails,
I am excited at what I see. Is it possible to put together a similar indicator for SMA. I use both EMA and SMA and this way I can test entry points using this idea. So far the EMA looks good in the Visual mode.

Yes, it is possible to apply this concept to most of the NinjaTrader indicators. The only problem is, that it is really tricky. It has taken me five fruitless attempts of coding, before I managed to code that EMA, and I am still not sure that everything is working properly. I do not want to code a bunch of indicators that do not work. So I have decided to give this one a longer test, before proceeding.


Some Specific Problems

Here are the difficulties that I have encountered (for those who want to further explore the subject).

(1) Different logic of adding values for the historical and the real-time part for the chart. On the historical part you will only find one-tick bars. This means that OnBarUpdate() calls lots of bars of the primary bar series and occasionally one bar of the secondary series. For real-time data OnBarUpdate() calls alternatively bar series 0 and the secondary bar series. Also I have found some exceptions.

(2) Problems with the hybrid bar.

When switching from historical to real-time data, NinjaTrader creates a hybrid bar from historical and real-time data. Sometimes this bar does not behave as expected

(3) Different logic for time stamps on minute and tick bars. The lag problem, which can be solved with repainting, only occurs, if two fixed-period bars (minute or second bars) have the same time-stamp. When using MTF indicator with a combination of minute/tick or tick/tick series, the problem does not exist. As volume and range bars are being built from ticks, they show the same behavior.

(4) Lag compensation, if possible for the combination of two minute or second based bar series.

(5) Interpolation of the results. This requires a different algorithm for charts with equidistant and non-equidistant bar spacing. Also the CalcOnBarClose = false mode needs to be simulated for the last leg of the secondary bar series.


Testing is still needed.

To summarize: I am not sure that all this works correctly, so it needs some further testing. Expect a few minor bugs.

Started this thread
Thanked by:
 
 
PHXtrader's Avatar
 PHXtrader 
Boston
 
Experience: Intermediate
Platform: Jigsaw
Trading: NQ
Posts: 84 since Apr 2011
Thanks Given: 48
Thanks Received: 76

This is awesome! Thanks so much for taking the time to do this. I'm using it on range charts and will def let you know if I see any bugs.


THANKS AGAIN.

Visit my NexusFi Trade Journal
Thanked by:
 
 
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,102


Following request, I have added the option to calculate the multi time frame EMA from Renko bars.

So, if you think that you need a tick chart, but want to calculate your EMA from 2-Renko bars, here comes your indicator.

The chart shows

- a primary series of tick bars (lime and red candlesticks)
- a secondary series of 2-Renko bars (gray and black candlesticks)

The secondary bar series is only used to check the multi time frame indicator.

Green: EMA(5) directly calculated from secondary bar series
Blue: EMA(5) calculated for the 2-Renko bars via multi time frame indicator and then inserted with the candles of the primary bar series.
Red: Same as blue but in first tick mode.
Yellow: Same as red but in strategy mode (covered by the red first tick mode EMA).

Indicator is attached. It is a protected assembly, which will only work with NT 7.0.1000.3 or later

Attached Thumbnails
Click image for larger version

Name:	ES 06-11 (30 Tick) _ ES 06-11 (2 Renko)  10_05_2011.jpg
Views:	936
Size:	110.7 KB
ID:	38372  
Attached Files
Elite Membership required to download: VisualEMAX2.zip
Started this thread

 



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