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

 
 BankRobberNT 
NYC
 
Experience: Intermediate
Platform: Ninjatrader
Broker: Interactive Brokers
Trading: forex
Posts: 38 since Aug 2011


Fat Tails View Post
The code can be used for a strategy, however - even if compared with the strategy mode of the VisualEMA - it is quite a bit different.

Accuracy on Backtests:

I will only compare it to the strategy mode. The strategy mode of the VisualEMA is specificly designed for use with historical data, and has the lag eliminated as much as possible, without repainting. On the chart below you will see that the real VisualEMA leads the indicator which was coded as above by one bar. This increases the accuracy of any backtest.

The VisualMode is even more accurate, but it repaints over the interpolation period. It should not be compared to the code snippet above. The same applies to the One-Tick-Correction mode.

CalculateOnBarClose = false:

Again the VisualEMA can be used in CalculateOnBarClose = false mode in strategy mode. There is a small lag for the first bars after the transition from historical to real-time data, but this is the price to be paid for the lag reduction on historical data. You cannot have both. The yellow indicator which follows the code above, cannot be easily used in CalculateOnBarClose = false mode, as it will neither produce a step function nor an interpolated indicator.

The posted code is just an example, an idea how it can be done. I like "drunken sailor" very funny, however let me post some charts.

Here is the setup:
On the left side we have 15min chart with regular EMA 12, 30min EMA 12, 60min EMA12 and 60min SMA20
On the right side we have 4tick Better Renko chart with VisualEMA 12period 15min(orange), 30min(Aqua Blue) 60min(Dark blue) and Visual SMA20 60min(White)
VisualEMA and VisualSMA are thicker 2pixel lines, MY code (NOT the one posted) are the 1 pixel thinner line same colors.

only 15min are set to 'Calculate on bar close = false' the rest is set to 'true'
VisualEMA and VisualSMA just lags and gives us wrong results.
My code (thinner line same color) match with charts on the left side.

Attached Thumbnails
Click image for larger version

Name:	a-ema comparision4.JPG
Views:	258
Size:	405.8 KB
ID:	55442   Click image for larger version

Name:	a-ema comparision5.JPG
Views:	198
Size:	406.9 KB
ID:	55443  

Can you help answer these questions
from other members on NexusFi?
The space time continuum and the dynamics of a financial …
Emini and Emicro Index
Deepmoney LLM
Elite Quantitative GenAI/LLM
ZombieSqueeze
Platforms and Indicators
Better Renko Gaps
The Elite Circle
Build trailing stop for micro index(s)
Psychology and Money Management
 
 
 
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


grofzr View Post
Only 15min are set to 'Calculate on bar close = false' the rest is set to 'true'
VisualEMA and VisualSMA just lags and gives us wrong results.
My code (thinner line same color) match with charts on the left side.

Your chart does not show anything, even the indicator settings cannot be seen.

The VisualSMA has three different modes:

VisualMode: No lag mode for discretionary trading.
One-Tick-Correction Mode: No lag mode for discretionary trading, step function.
StrategyMode: The strategy mode is designed for historical data (backtest) and has no lag on historical data.

There is a trade-off. The strategy mode will lag on real-time data just after the transition from historical data to real-time data. This cannot be easily changed. It can be explained by the fact that both SMA and EMA use a recursive formula.

Otherwise there is no lag (meaning the minimum lag between the close of the secondary bar and the close of the next primary bar).

Below chart showing the original EMA directly applied to a 5 min bar series (gray bars) and the VisualEMA (red), which tracks the original EMA as close as possible, given that it can only show the values via the primary bar series. The simple MTF EMA repeats its drunken sailor performance.

Attached Thumbnails
Click image for larger version

Name:	FDAX 12-11 (4 BetterRenko) _ FDAX 12-11 (5 Min)  22_11_2011.jpg
Views:	202
Size:	80.6 KB
ID:	55447  
Started this thread
Thanked by:
 
 BankRobberNT 
NYC
 
Experience: Intermediate
Platform: Ninjatrader
Broker: Interactive Brokers
Trading: forex
Posts: 38 since Aug 2011


Same setup 15 seconds chart on the right side
Only orange is calculated on bar close = false (drunken sailor)

Attached Thumbnails
Click image for larger version

Name:	a-ema comparision6JPG.JPG
Views:	184
Size:	385.4 KB
ID:	55448   Click image for larger version

Name:	a-ema comparision7JPG.JPG
Views:	184
Size:	380.6 KB
ID:	55449  
 
 
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


grofzr View Post
Same setup 15 seconds chart on the right side
Only orange is calculated on bar close = false (drunken sailor)

I would love to see something on your chart, but I can't. Could you post a chart with just two or three indicators, so that they can be compared?

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


grofzr View Post
only 15min are set to 'Calculate on bar close = false' the rest is set to 'true'
VisualEMA and VisualSMA just lags and gives us wrong results.

@ grofzr: I have further tested the VisualSMA and VisualEMA in mode CalculateOnBarClose = true. Although I could not see it on your chart, you are absolutely right.

The indicators lag, when they are set to CalculateOnBarClose = true. Thanks again for pointing this out.

Actually I had never tested them thoroughly in CalculateOnBarClose = true, as I was happy that they worked well tick by tick, when CalculateOnBarClose = false. The correction is not difficult to do, so I will update the indicators today or tomorrow.

The change will have no impact on the way the indicators display on historical data or in mode CalculateOnBarClose = false.


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

I have made the changes, now testing them:

Below is a chart which shows two bar series, 3-range bars (colored) and 5-minute bars (gray). The chart uses non-equidistant bar spacing, because there are two data series. The 5-minute bars have only been added for controlling the multi-timeframe indicators.

On the chart you can see

-> the default NinjaTrader EMA directly applied to the 5-min bars (blue) - this is the benchmark
-> the VisualEMA prior to correction in CalculateOnBarClose = true mode, which lags (red)
-> the VisualEMA after correction in CalculateOnBarClose = true mode (green), which closely follows the EMA
-> the MTF EMA, which lags on historical data, and oscillates between the two versions of the VisualEMA on real-time data (orange)


Started this thread
 
 BankRobberNT 
NYC
 
Experience: Intermediate
Platform: Ninjatrader
Broker: Interactive Brokers
Trading: forex
Posts: 38 since Aug 2011

First kudos to you for all your work on many great indicators. Yes, the main problem was on Calculate on Bar Close = true. I'd suggest you to do testing on 15, 30 and 60 minutes, because I've noticed the biggest differences there. I use code other than posted for my testing. For some reason 5 minutes has given almost identical results on both code(Visual EMA and my test code).You can see 5minute VisualEMA on my charts colored Ivory. I'll try post more charts later.

 
 
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


grofzr View Post
First kudos to you for all your work on many great indicators. Yes, the main problem was on Calculate on Bar Close = true. I'd suggest you to do testing on 15, 30 and 60 minutes, because I've noticed the biggest differences there. I use code other than posted for my testing. For some reason 5 minutes has given almost identical results on both code(Visual EMA and my test code).You can see 5minute VisualEMA on my charts colored Ivory. I'll try post more charts later.

Still testing, will post tomorrow.

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

As was discovered by @ grofzr, both indicators were lagging when set to CalculateOnBarClose = true. I admit that the original design of the indicators was adapted to CalculateOnBarClose = false setting, which is more difficult, so I somehow neglected this part.

Testing everything is near-impossible. You add range bars to volume bars, then there is a session break, although the indicator works on historical data and on real-time data, it may have a problem with the transition as NinjaTrader builds a hybrid bar from historical and real-time tick in a non-reproducible manner, there is the possibility of CalculateOnBarClose = false setting or CalculateOnBarClose = true setting, there are technical breaks to be considered where there is no data, as the NinjaTrader heuristics for a trading day does not allow to exclude those session breaks via a session template. Let us say it is sort of a large headache until it works.

I have now tested the updated versions with CalculateOnBarClose = true setting, and so far they passed the test.



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


I have made some tests with various multi-timeframe indicators, how they behave when they are called from other indicators or strategies. Let us consider three different cases:

(1) You have a multi-timeframe indicator which loads minute bars.
(2) You have a multi-timeframe indicator which loads tick bars.
(3) You have a multi-timeframe indicator which loads range bars.

Now let us proceed to perform a test. We open any chart (no importance) and first add the original MTF indicator. Then we also add the second indicator, which is calling the MTF indicator to retrieve its values. Both indicators are in CalculateOnBarClose = true and should display identical values for the indicator.

In cases (1) and (2) this works as expected. However, if you try (3) to access an indicator which loads range bars to calculate indicator values, the result is negative. The values transmitted to the calling indicator lag, and there is a growing error term between the original indicator and the indicator which accesses it.

Conclusion

You cannot access any MTF indicator built from range bars via another indicator or strategy in real.time. You can only use the original indicator. However it seems, that there are no deviations on historical data, so I am confident that you can access the MTF indicator for a backtest, but I am not sure.

I believe that the problem is related to the way range bars are built. The range bar is only completed with the first tick of the next bar, which is required to confirm its completion. This probably introduces a lag of one tick between the original indcator and the calling indicator. The gaps once created propagates to the next bar because of the recursive formula of the SMA, which was used.

If you want to reproduce the issue, I have attached two very basic indicators just for documenting it. You can test it on your machine. One indicator is a simple MTF indicator, the other one accesses it and should display the same value.


Attached Files
Elite Membership required to download: AccessTestMTF.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