Coding Multi Time Frame (MTF) Indicators with NinjaTrader - futures io
futures io futures trading



Coding Multi Time Frame (MTF) Indicators with NinjaTrader


Discussion in NinjaTrader

Updated by Fat Tails
      Top Posters
    1. looks_one Fat Tails with 178 posts (550 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.6 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 129,472 views
    2. thumb_up 726 thanks given
    3. group 87 followers
    1. forum 420 replies
    2. attach_file 165 attachments




Welcome to futures io: the largest futures trading community on the planet, with well over 100,000 members
  • Genuine reviews from real traders, not fake reviews from stealth vendors
  • Quality education from leading professional traders
  • We are a friendly, helpful, and positive community
  • We do not tolerate rude behavior, trolling, or vendors advertising in posts
  • We are here to help, just let us know what you need
You'll need to register in order to view the content of the threads and start contributing to our community.  It's free and simple.

-- Big Mike, Site Administrator

(If you already have an account, login at the top of the page)

 
Search this Thread
 

Coding Multi Time Frame (MTF) Indicators with NinjaTrader

(login for full post details)
  #21 (permalink)
Market Wizard
Berlin, Europe
 
Experience: Advanced
Platform: NinjaTrader, MultiCharts
Broker: Interactive Brokers
Trading: Keyboard
 
Fat Tails's Avatar
 
Posts: 9,842 since Mar 2010
Thanks: 4,238 given, 26,613 received

Yes, I had the same problem with the GetNextBeginEnd() signature for the multi timeframe version of the Opening Range indicator. I have corrected it now and made a number of smaller improvements as well.

X3 means that it is the third eXperimental version of the indicator. When it is ripe, I will put it into the download section of this forum. I think it is close.

The chart show the opening range calculated from 1 minute data. The red highlighted range is the range of the first minute of trading. The night session high and low is shown as well.

The indicator should not throw any exceptions as was the case with the V32MTFX2 version.

Attached Thumbnails
Click image for larger version

Name:	ES 06-11 (15 Min)  24_05_2011.jpg
Views:	242
Size:	101.1 KB
ID:	39163  
Attached Files
Register to download File Type: zip OpeningRangV33MTFX3.zip (9.0 KB, 113 views)
Reply With Quote

Can you help answer these questions
from other members on futures io?
Macros for flatten and reverse
TradeStation
Thinkscript for scanning in Option Hacker
ThinkOrSwim
Where I can find knoxville divergence indicator for ninja trader 8?
NinjaTrader
Degiro Negative Balance Advice
Traders Hideout
Trying to locate prop firm that purchased my automated trading system
Trading Reviews and Vendors
 
Best Threads (Most Thanked)
in the last 7 days on futures io
VWAP for stock index futures trading?
35 thanks
What Is the Source of Your Edge??
23 thanks
VWAP oscillator for NT8
15 thanks
Index options vs futures options
11 thanks
Battlestations: Show us your trading desks!
10 thanks
 
(login for full post details)
  #22 (permalink)
Market Wizard
Berlin, Europe
 
Experience: Advanced
Platform: NinjaTrader, MultiCharts
Broker: Interactive Brokers
Trading: Keyboard
 
Fat Tails's Avatar
 
Posts: 9,842 since Mar 2010
Thanks: 4,238 given, 26,613 received


perryg View Post
Fat Tails,
I have used in real time the VisualSMAX3 on 1min and 5 min charts taking the moving average from a Range chart as well using typical. It is working perfectly with very good results. I have also tested the other way round of using range charts and putting a 5 min moving average onto the range chart. I can recommend the use of this indicator.
If you have time could you also add to the VisualEMA indicator the Typical as you did with the VisualSMA.

This is the enhanced version of the multi-timeframe EMA. As before you can select between the step mode, which reflects the values generated by the secondary bars and the interpolated visual mode, which smoothes out the step. This version now also allows to select the Input series, meaning that you can calculate the multi-timeframe EMA from the Close, High, Low, Median, Typical or Weighted series.

Indicator attached below.

Attached Thumbnails
Click image for larger version

Name:	CL 07-11 (5 Min)  24_05_2011.jpg
Views:	274
Size:	87.9 KB
ID:	39177  
Attached Files
Register to download File Type: zip VisualEMAX3.zip (40.1 KB, 174 views)
Reply With Quote
The following 11 users say Thank You to Fat Tails for this post:
 
(login for full post details)
  #23 (permalink)
Portland Oregon, United States
 
Experience: Beginner
Platform: NinjatraderŽ
Broker: CQG, Kinetick
Trading: Gameplay KlownbineŽ Trading of Globex
 
Zondor's Avatar
 
Posts: 1,346 since Jul 2009
Thanks: 1,245 given, 2,713 received


Thanks for all this work, FT. This is great thinking! MTF Visual EMA indicator is excellent!

Because of rampant risk asset correlation it can be beneficial to track instruments other than the one you are trading. This suggests the idea of secondary indicators that would be based not only on a different time frame, but on a different instrument.

For example, on a 200 tick chart of the CL, display in an added panel the 20 period EMA of the 3 range ES... etc. while maintaining the equidistant bar spacing of the 200 tick CL in the Price Panel.

Follow me on Twitter Visit my futures io Trade Journal Reply With Quote
The following user says Thank You to Zondor for this post:
 
(login for full post details)
  #24 (permalink)
Portland Oregon, United States
 
Experience: Beginner
Platform: NinjatraderŽ
Broker: CQG, Kinetick
Trading: Gameplay KlownbineŽ Trading of Globex
 
Zondor's Avatar
 
Posts: 1,346 since Jul 2009
Thanks: 1,245 given, 2,713 received

Fat Tails said:


Quoting 
(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.


I wrote some indicators that display a secondary data series. What I found is that when BarsInProgress =1, there is always a new value of Closes[1][0] and any variables associated with it DO update. However, the new values are NOT shown on the chart until the next tick comes in for the primary series, where BarsInProgress = 0.

For example, I was showing a plot of ^TICK on a 250 tick chart (with equal bar spacing) of YM. During the customary YM super slow periods, could see that the ^TICK plot did not respond when the time and sales showed new values of ^TICK coming in. The ^TICK plot only updated when a new value of YM printed, although diagnostic statements printing to the output window showedthat new values of Closes[1][0] were indeed all coming in when they were supposed to.

In other words, the chart display only updated when BarsInProgress was equal to 0.

The solution that worked for my indicators : FORCE the chart display to update every time a new price tick comes in for the secondary series.

 
Code
if(!Historical && BarsInProgress == 1) ChartControl.Refresh();

Follow me on Twitter Visit my futures io Trade Journal Reply With Quote
The following 3 users say Thank You to Zondor for this post:
 
(login for full post details)
  #25 (permalink)
Market Wizard
Berlin, Europe
 
Experience: Advanced
Platform: NinjaTrader, MultiCharts
Broker: Interactive Brokers
Trading: Keyboard
 
Fat Tails's Avatar
 
Posts: 9,842 since Mar 2010
Thanks: 4,238 given, 26,613 received


Zondor View Post
I wrote some indicators that display a secondary data series. What I found is that when BarsInProgress =1, there is always a new value of Closes[1][0] and any variables associated with it DO update. However, the new values are NOT shown on the chart until the next tick comes in for the primary series, where BarsInProgress = 0.

For example, I was showing a plot of ^TICK on a 250 tick chart (with equal bar spacing) of YM. During the customary YM super slow periods, could see that the ^TICK plot did not respond when the time and sales showed new values of ^TICK coming in. The ^TICK plot only updated when a new value of YM printed, although diagnostic statements printing to the output window showedthat new values of Closes[1][0] were indeed all coming in when they were supposed to.

In other words, the chart display only updated when BarsInProgress was equal to 0.

The solution that worked for my indicators : FORCE the chart display to update every time a new price tick comes in for the secondary series.

 
Code
if(!Historical && BarsInProgress == 1) ChartControl.Refresh();

Thank you for the suggestion, that makes sense.

If I refresh the chart with each incoming tick, when Barsarray[1] is processed , do you know whether that will override the chart display update interval?

I do not want to cause a NinjaTrader freeze by refreshing the chart for each incoming tick, if the market is moving fast.

Reply With Quote
 
(login for full post details)
  #26 (permalink)
Portland Oregon, United States
 
Experience: Beginner
Platform: NinjatraderŽ
Broker: CQG, Kinetick
Trading: Gameplay KlownbineŽ Trading of Globex
 
Zondor's Avatar
 
Posts: 1,346 since Jul 2009
Thanks: 1,245 given, 2,713 received

Fat Tails asked:

Quoting 
If I refresh the chart with each incoming tick, when Barsarray[1] is processed , do you know whether that will override the chart display update interval?

I do not want to cause a NinjaTrader freeze by refreshing the chart for each incoming tick, if the market is moving fast.

I am not sure whether the forced Refresh overrides the chart display interval. This could be checked by printing diagnostic messages with the Refresh times down to the millisecond to the Output Window. You could keep track of the time of the last Refresh and not allow the forced refresh to occur unless a certain number of milliseconds has elapsed since the last refresh.

 
Code
if(!Historical && BarsInProgress==1 &&  MostRecentRefreshTime.AddMilliseconds > UserSelectableMillisecondRefreshInterval) ChartControl.Refresh();
However I have NOT done this. The forced Refreshes on every update of the secondary series have not caused any problems with my indicators as long as the command is a separate statement near the end of OnBarUpdate. Have not seen any problems at all during fast markets.

Another condition you can subject it to is to only trigger if the value of what you are plotting has changed from its most recent previous value.

Follow me on Twitter Visit my futures io Trade Journal Reply With Quote
The following user says Thank You to Zondor for this post:
 
(login for full post details)
  #27 (permalink)
Market Wizard
Berlin, Europe
 
Experience: Advanced
Platform: NinjaTrader, MultiCharts
Broker: Interactive Brokers
Trading: Keyboard
 
Fat Tails's Avatar
 
Posts: 9,842 since Mar 2010
Thanks: 4,238 given, 26,613 received


Zondor View Post
Fat Tails asked:
I am not sure whether the forced Refresh overrides the chart display interval. This could be checked by printing diagnostic messages with the Refresh times down to the millisecond to the Output Window. You could keep track of the time of the last Refresh and not allow the forced refresh to occur unless a certain number of milliseconds has elapsed since the last refresh.

 
Code
if(!Historical && BarsInProgress==1 &&  MostRecentRefreshTime.AddMilliseconds > UserSelectableMillisecondRefreshInterval) ChartControl.Refresh();
However I have NOT done this. The forced Refreshes on every update of the secondary series have not caused any problems with my indicators as long as the command is a separate statement near the end of OnBarUpdate. Have not seen any problems at all during fast markets.

Another condition you can subject it to is to only trigger if the value of what you are plotting has changed from its most recent previous value.

@Zondor

I really appreciate your input here. May main concern is CPU load. Let us assume - for the worse - that I have 15 indicators on my chart, out of which 3 are multi-timeframe indicators and 5 have custom plots (for example the pivots do have custom plots that can create a bit of CPU load)

How does NinjaTrader process the indicators internally?

Process A

1. new incoming tick
2. triggers OnBarUpdate for primary bars for indicators 1 to 15
3. plots (if time elapsed smaller display update interval)
4. trigger OnBarUpdate for secondary bars of first multiseries indicator
5. plots (if time elapsed smaller display update interval)
6. trigger OnBarUpdate for secondary bars of second multiseries indicator
7. plots (if time elapsed smaller display update interval)
8. trigger OnBarUpdate for secondary bars of third multiseries indicator
9. plots (if time elapsed smaller display update interval)

Process B

same as above, but additional 14 plots within the step 2.

The code snippet meant to assure that the refresh is not executed, if the last refresh was less than n milliseconds ago. Now I cannot know the last refresh time, unless I have an indicator with an override Plot() to collect it.So I am thinking about two options here

First Option: measure the difference between the last two incoming ticks and compared it to the display update interval (this assumes that the tick speed remains constant, and may at worst trigger one additional refresh, but not 100).

Second Option: Code a separate indicator that will automatically refresh, by using a user-selectable display update interval.

What is your opinion?

Reply With Quote
 
(login for full post details)
  #28 (permalink)
Portland Oregon, United States
 
Experience: Beginner
Platform: NinjatraderŽ
Broker: CQG, Kinetick
Trading: Gameplay KlownbineŽ Trading of Globex
 
Zondor's Avatar
 
Posts: 1,346 since Jul 2009
Thanks: 1,245 given, 2,713 received

The OnBarUpdate event is only called ONCE per incoming tick. Refresh without fear!

Definition of BarsInProgress from Ninjatrader help:

An index value of the current Bars object that has called the OnBarUpdate() method. In a multi-bars script, the OnBarUpdate() method is called for each Bars object of a script. This flexibility allows you to separate trading logic from different bar events. In a single Bars script this property will always return an index value of 0 representing the primary Bars and instrument the script is running on.



(note: For primary instrument price series n=1, for second series n=2, third series n=3....nth series n=N)
(BIP=BarsInProgress, an integer returned for each OnBarUpdate event)
The OnBarUpdate event is always fired ONCE when a tick arrives. It's the same event no matter which bar series the tick comes from. The only difference is what value BIP is set to. For some reason, only when BIP=0 will OBU cause the chart to refresh. I consider this to be a Ninjatrader bug.

1. data feed sends a new incoming tick of primary instrument price series(n=1)
2. triggers OnBarUpdate in which the value of BIP is 0. All the code in OnBarUpdate executes... ONCE only.
3. BECAUSE the value of BarsInProgress is 0 the chart will refresh.
3. data feed sends a new incoming tick for instrument N
4. triggers OnBarUpdate in which the value of BIP is N-1. All the code in OnBarUpdate executes... ONCE only.
5. BECAUSE the value of BarsInProgress is NOT 0 the chart will NOT refresh. NINJA BUG
6. So we force the chart to refresh. if(!Historical && BarsInProgress >0...&& whatever)

OnBarUpdate is called ONCE each time there is a tick from ANY of the instruments, no matter how many instruments there are.

All the BarUpdate events DO occur exactly when they are supposed to. Whenever ANY of the series updates, OnBarUpdate DOES occur. OnBarUpdate events are driven directly by the different series in real time and are not dependent on each other in any way. However the APPEARANCE OF THE CHART only updates when BIP=0!

You can see this in the output window. OnBarUpdate events will fire with different values of BIP whenever any of the series generates a tick, but the chart will only refresh when BIP=0.

So far I do not see any indication that the forced refreshes cause any noticeable increase in CPU load.

Bonus! Did you know that you can index the CurrentBar expression to find where you are in each data series! Instead of CurrentBar write CurrenBars[N]. If you want to be sure you are looking at the CurrentBar of the primary series, you will have lots of "interesting' problems if you say CurrentBar instead of CurrentBars[0], because CurrentBar will keep returning values for different instruments!

Follow me on Twitter Visit my futures io Trade Journal Reply With Quote
The following 4 users say Thank You to Zondor for this post:
 
(login for full post details)
  #29 (permalink)
Portland Oregon, United States
 
Experience: Beginner
Platform: NinjatraderŽ
Broker: CQG, Kinetick
Trading: Gameplay KlownbineŽ Trading of Globex
 
Zondor's Avatar
 
Posts: 1,346 since Jul 2009
Thanks: 1,245 given, 2,713 received

The VisualEMAX with a period of 1 can be used as a proxy for the price of a series with any time frame including or other than that of the chart. But when I try to take advantage of this by runnng an indicator that calls the VisualEMAX3 I get the error:

Error on calling OnStartUp method for indicator VisualEMAX3, Object reference not set to instance of an object.

My indicator then displays nothing.

The same thing happens when I try to apply another indicator to the VisualEMAX3 using the NT7 indicator-of-an -indicator feature of the indicators dialog box.


Because the code is in a DLL I can't do anything about the error. FT, could you please consider adding try{ }catch error handlers to all of the methods of the indicator to make sure that all exceptions are handled to prevent crashing of indicator from unhandled exceptions. Thanks.

Follow me on Twitter Visit my futures io Trade Journal Reply With Quote
The following user says Thank You to Zondor for this post:
 
(login for full post details)
  #30 (permalink)
Market Wizard
Berlin, Europe
 
Experience: Advanced
Platform: NinjaTrader, MultiCharts
Broker: Interactive Brokers
Trading: Keyboard
 
Fat Tails's Avatar
 
Posts: 9,842 since Mar 2010
Thanks: 4,238 given, 26,613 received



Zondor View Post
OnBarUpdate is called ONCE each time there is a tick from ANY of the instruments, no matter how many instruments there are.

All the BarUpdate events DO occur exactly when they are supposed to. Whenever ANY of the series updates, OnBarUpdate DOES occur. OnBarUpdate events are driven directly by the different series in real time and are not dependent on each other in any way. However the APPEARANCE OF THE CHART only updates when BIP=0!

You can see this in the output window. OnBarUpdate events will fire with different values of BIP whenever any of the series generates a tick, but the chart will only refresh when BIP=0.

So far I do not see any indication that the forced refreshes cause any noticeable increase in CPU load.

I understand that OnBarUpdate() is triggered for each bars array. So if datafeed produces a tick containing price data, this will be first processed for BarsArray[0], then for the other BarsArrays. What is confusing is the order of the other bars being processed. Let us assume that I apply a multi-timeframe indicatoro to a multi-dataseries chart. If the secondary bars objects of the MTF indicators matches the secondary bars objects of the underlying multi-series chart, NinjaTrader will not recreate the bars. So there will not be two 5 min bar series for ES 06-11, but only one, as the MTF indicator will use the already existing bars array of the multiseries chart. Could this inverse the order in which the BarsInProgress are selected?

Also I have observed some rare cases, where BarsInProgress=0 have been updated twice in a row (real-time), without any update of BarsInProgress=1 (double BIP), but I was not able to reproduce it.

Guess, I just will try out some code snippets and then see how it plays out in real-time.



Zondor View Post
Bonus! Did you know that you can index the CurrentBar expression to find where you are in each data series! Instead of CurrentBar write CurrenBars[N]. If you want to be sure you are looking at the CurrentBar of the primary series, you will have lots of "interesting' problems if you say CurrentBar instead of CurrentBars[0], because CurrentBar will keep returning values for different instruments!

All my multi timeframe indicators use CurrentBars[N] for clarity. Just had a problem in a custom plot, where CurrentBar returned false values. But CurrentBars[0] also did return false values, still have no idea why.

Reply With Quote


futures io Trading Community Platforms and Indicators NinjaTrader > Coding Multi Time Frame (MTF) Indicators with NinjaTrader


December 11, 2018


Upcoming Webinars and Events
 

An Afternoon With futures io member TropicalTrader

Elite only
     



Copyright © 2020 by futures io, s.a., Av Ricardo J. Alfaro, Century Tower, Panama, +507 833-9432, info@futures.io
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.
no new posts