Coding Multi Time Frame (MTF) Indicators with NinjaTrader
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.
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.
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.
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.
Last edited by Fat Tails; April 10th, 2011 at 01:42 PM.
The following 32 users say Thank You to Fat Tails for this post:
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:
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.
Last edited by Fat Tails; April 10th, 2011 at 04:57 PM.
The following 17 users say Thank You to Fat Tails for this 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.
The following 14 users say Thank You to Fat Tails for this 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.
The following user says Thank You to TempletonPeck for this post:
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.
The following 2 users say Thank You to Fat Tails for this post:
Favorite Futures: Index,Currency and Energy futures
Posts: 1,701 since Jan 2010
Thanks: 501 given,
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.
The following user says Thank You to perryg for this post:
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.
The following 2 users say Thank You to Fat Tails for this post:
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
The following 6 users say Thank You to Fat Tails for this post: