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

  #21 (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,102

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:	501
Size:	101.1 KB
ID:	39163  
Attached Files
Elite Membership required to download: OpeningRangV33MTFX3.zip
Started this thread Reply With Quote

Can you help answer these questions
from other members on NexusFi?
How to apply profiles
Traders Hideout
ZombieSqueeze
Platforms and Indicators
Better Renko Gaps
The Elite Circle
NT7 Indicator Script Troubleshooting - Camarilla Pivots
NinjaTrader
Exit Strategy
NinjaTrader
 
Best Threads (Most Thanked)
in the last 7 days on NexusFi
Just another trading journal: PA, Wyckoff & Trends
34 thanks
Tao te Trade: way of the WLD
24 thanks
Bigger Wins or Fewer Losses?
15 thanks
GFIs1 1 DAX trade per day journal
15 thanks
Vinny E-Mini & Algobox Review TRADE ROOM
13 thanks
  #22 (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,102


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:	506
Size:	87.9 KB
ID:	39177  
Attached Files
Elite Membership required to download: VisualEMAX3.zip
Started this thread Reply With Quote
  #23 (permalink)
 
Zondor's Avatar
 Zondor 
Portland Oregon, United States
 
Experience: Beginner
Platform: NinjatraderŽ
Broker: CQG, Kinetick
Trading: Gameplay KlownbineŽ Trading of Globex
Posts: 1,333 since Jul 2009
Thanks Given: 1,246
Thanks Received: 2,731


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 NexusFi Trade Journal Reply With Quote
Thanked by:
  #24 (permalink)
 
Zondor's Avatar
 Zondor 
Portland Oregon, United States
 
Experience: Beginner
Platform: NinjatraderŽ
Broker: CQG, Kinetick
Trading: Gameplay KlownbineŽ Trading of Globex
Posts: 1,333 since Jul 2009
Thanks Given: 1,246
Thanks Received: 2,731

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 NexusFi Trade Journal Reply With Quote
Thanked by:
  #25 (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,102


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.

Started this thread Reply With Quote
  #26 (permalink)
 
Zondor's Avatar
 Zondor 
Portland Oregon, United States
 
Experience: Beginner
Platform: NinjatraderŽ
Broker: CQG, Kinetick
Trading: Gameplay KlownbineŽ Trading of Globex
Posts: 1,333 since Jul 2009
Thanks Given: 1,246
Thanks Received: 2,731

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 NexusFi Trade Journal Reply With Quote
Thanked by:
  #27 (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,102


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?

Started this thread Reply With Quote
  #28 (permalink)
 
Zondor's Avatar
 Zondor 
Portland Oregon, United States
 
Experience: Beginner
Platform: NinjatraderŽ
Broker: CQG, Kinetick
Trading: Gameplay KlownbineŽ Trading of Globex
Posts: 1,333 since Jul 2009
Thanks Given: 1,246
Thanks Received: 2,731

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 NexusFi Trade Journal Reply With Quote
  #29 (permalink)
 
Zondor's Avatar
 Zondor 
Portland Oregon, United States
 
Experience: Beginner
Platform: NinjatraderŽ
Broker: CQG, Kinetick
Trading: Gameplay KlownbineŽ Trading of Globex
Posts: 1,333 since Jul 2009
Thanks Given: 1,246
Thanks Received: 2,731

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 NexusFi Trade Journal Reply With Quote
Thanked by:
  #30 (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,102



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.

Started this thread 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