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 188,964 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

  #391 (permalink)
 
ratfink's Avatar
 ratfink 
Birmingham UK
Market Wizard
 
Experience: Intermediate
Platform: NinjaTrader
Broker: TST/Rithmic
Trading: YM/Gold
Posts: 3,633 since Dec 2012
Thanks Given: 17,423
Thanks Received: 8,425


deltason View Post
Great thread. Hard for me to phrase this question. Is there a way to create an alert or chart marker when an indicator or bar value on one time frame equals an indicator or bar value on another time frame? For example, can there be an alert/chart marker when the 5 period SMA of a 10 minute chart equals the 5 period SMA of a 20 minute chart (rounded if necessary)? Another example would be an alert/chart marker when the high/low of a 30 minute bar equals the high/low of a 20 minute bar.

Use something like this:

 
Code
// in variables region

int alertBar;

// in Initialize ()

Add (PeriodType.Minute, 10);
Add (PeriodType.Minute, 20);

CalculateOnBarClose = false;


// in OnBarUpdate

if (BarsInProgress != 0)        // all series get the update so we'll use the original chart one only
    return;

if ((int)(SMA(Closes[1],5)[0]) == (int)(SMA(Closes[2],10)[0]))
{
    if (CurrentBar != alertBar)
    {
        // draw or make a sound here

        alertBar = CurrentBar;
    }
}
That's a rough idea, you might want to use less rounding (e.g. use the Round2TickSize function) or run with different event settings but something like that should do enough. There are other ways but I like simple. (Do excuse any spelling/syntax/sematic errors, I'm only on a ChromeBook.)

ed: Just a thought, I think there are CrossAbove/CrossBelow/etc functions that could be useful in this area

Travel Well
Visit my NexusFi Trade Journal Reply With Quote
Thanked by:

Can you help answer these questions
from other members on NexusFi?
Better Renko Gaps
The Elite Circle
Increase in trading performance by 75%
The Elite Circle
Trade idea based off three indicators.
Traders Hideout
How to apply profiles
Traders Hideout
MC PL editor upgrade
MultiCharts
 
Best Threads (Most Thanked)
in the last 7 days on NexusFi
Just another trading journal: PA, Wyckoff & Trends
33 thanks
Tao te Trade: way of the WLD
24 thanks
My NQ Trading Journal
14 thanks
GFIs1 1 DAX trade per day journal
11 thanks
HumbleTraders next chapter
11 thanks
  #392 (permalink)
 
deltason's Avatar
 deltason 
Memphis, Tennessee
 
Experience: None
Platform: NinjaTrader
Trading: ES, EUR/USD
Posts: 133 since Jul 2009
Thanks Given: 1,613
Thanks Received: 122


ratfink View Post
Use something like this:

That's a rough idea, you might want to use less rounding (e.g. use the Round2TickSize function) or run with different event settings but something like that should do enough. There are other ways but I like simple. (Do excuse any spelling/syntax/sematic errors, I'm only on a ChromeBook.)

ed: Just a thought, I think there are CrossAbove/CrossBelow/etc functions that could be useful in this area

Thanks for taking the time to put that together. I am just now seeing this and will give it a go. Thanks again.

Visit my NexusFi Trade Journal Reply With Quote
Thanked by:
  #393 (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



deltason View Post
Great thread. Hard for me to phrase this question. Is there a way to create an alert or chart marker when an indicator or bar value on one time frame equals an indicator or bar value on another time frame? For example, can there be an alert/chart marker when the 5 period SMA of a 10 minute chart equals the 5 period SMA of a 20 minute chart (rounded if necessary)? Another example would be an alert/chart marker when the high/low of a 30 minute bar equals the high/low of a 20 minute bar.


For every multitimeframe question there are 3 different approaches. Please see link below for a detailed explanation.



Your first example: In the case of an SMA, you can simply tweak the indicator by adapting the period. The 5-period SMA of a 20 minute chart comes very close to a 10-period SMA of a 10-minute chart. Therefore for your first example you do not need to create any indicator that loads multiple bar series.

Your second example: This question is not clearly put, because if you use 20 min and 30 min bars, they will sometimes have a common close and they will sometimes not have a common close. In this case I would use a 10 min chart and compare the 3-bar high to the 2-bar high, or the 3-bar low to the 2-bar low. This approach beats any approach using 20 and 30 min bars.


Below is a sample chart of what can be done without adding a secondary bar series.

-> the first chart shows genuine MACDs which are calculated from composite bars (composite bars are created from primary bars, something which is possible for minute, tick and volume bars)
-> the second chart shows tweaked MACDs, which have the timeframe problem resolved by adapting the indicator lookback periods

Of course there is a small difference between the genuine and the tweaked indicators. Which one would you prefer?






Started this thread Reply With Quote
  #394 (permalink)
 
deltason's Avatar
 deltason 
Memphis, Tennessee
 
Experience: None
Platform: NinjaTrader
Trading: ES, EUR/USD
Posts: 133 since Jul 2009
Thanks Given: 1,613
Thanks Received: 122

Thanks Fat Tails. The time periods were just examples to see what could be done and to make it a bit easier for me to ask the questions. I also have an interest in comparing different fractals on charts that are not time based.

Visit my NexusFi Trade Journal Reply With Quote
Thanked by:
  #395 (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

One of the reasons that coding multi timeframe indicators is so confusing is that, for historical data, only one tick is provided per bar. Let's see how this plays out in an example with a multi time frame indicator on a one minute chart. The primary price series, Bars[0], is a one minute bar series that prints to the chart, and the Added secondary price series, Bars[1], is a one second price series of the same instrument. As shorthand, the BarsInProgress property will be abbreviated BIP.

Once per minute, a bar of the primary series will print to the chart. When that bar prints, BIP equals zero and FirstTickOfBar will be true for the primary bar series. However, the fact that the bar has a high, low and close as well as an open, clearly shows that the appearance of this bar was actually triggered by its completion, not its commencement. So, for historical data, what is called "FirstTickOfBar" is actually the LAST tick of the bar.

The very next price tick will be from the secondary series, BIP will be 1, and Closes[1][0] and Times[1][0] will be the same as the most recent Closes[0][0] and Times[0][0], since this is the last one second period within the primary bar, Bars[0][N]. Anything calculated from this tick will correctly plot at the same place in the horizontal scale as CurrentBars[0][N].

Now the problems begin. Bars[0][N] is FINISHED. The next 60 ticks of the secondary series belong to Bars[0][N+1], not to Bars[0][N]. However, Bars[0][N], is THE Current Bar as far as the chart is concerned; Bars[0][N+1] does not even exist yet. It won't show up for one more minute, until it ENDS. So any data derived from these ticks will erroneously be plotted to the most recent bar on the chart, Bars[0][N].

Understanding how to correct for this is one of the major requirements for properly coding multi time frame, or multi instrument, indicators.

"If we don't loosen up some money, this sucker is going down." -GW Bush, 2008
“Lack of proof that something is true does not prove that it is not true - when you want to believe.” -Humpty Dumpty, 2014
“The greatest shortcoming of the human race is our inability to understand the exponential function.”
Prof. Albert Bartlett
Follow me on Twitter Visit my NexusFi Trade Journal Reply With Quote
  #396 (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

Here is a post about Ehlers indicators based on time frames other than those of the Master Instrument


"If we don't loosen up some money, this sucker is going down." -GW Bush, 2008
“Lack of proof that something is true does not prove that it is not true - when you want to believe.” -Humpty Dumpty, 2014
“The greatest shortcoming of the human race is our inability to understand the exponential function.”
Prof. Albert Bartlett
Follow me on Twitter Visit my NexusFi Trade Journal Reply With Quote
Thanked by:
  #397 (permalink)
 
michaelf's Avatar
 michaelf 
Mérida, Mexico
Market Wizard
 
Experience: Intermediate
Platform: NinjaTrader 8
Broker: NinjaTrader Brokerage
Trading: MNQ
Frequency: Daily
Duration: Minutes
Posts: 559 since Mar 2011
Thanks Given: 1,434
Thanks Received: 581


Fat Tails View Post
I just want to explain another time, how the indicators work and can be correctly accessed. The explanation refers to the VisualSMA but also applies to the VisualEMA


Strategy mode

Indicator collects data from secondary bar series. When a bar of the secondary bar series is complete, the value is displayed on the chart. This typically results in a step function.


One-Tick Repaint Mode

This mode corrects a weakness of the multi-barseries concept of NinjaTrader. OnBarUpdate() will be triggered for the primary bars and then for the secondary bars. Now imagine that you have a 15 min chart and want to display an hourly SMA. Then t the full hour NinjaTrader will first run the primary bars (allowing to write values to the chart) and then the secondary bars calculating the values from the hourly bars, which it should have written to the primary 15 min bars during the prior run of OnBarUpdate(). This mean that for the bar close of the 15 min bar, the last tick of the 60 min bar cannot be taken into account, because it is not available.

The solution adopted is to repaint the value to the prior bar with the first tick of the new 15 minute bar. The indicator therefore repaints the prior bar a single time, but only uses information from the prior 60 minute bar, which is unfortunately processed after the 15 min bar.

The result also is a step function, but the lag of 1 bar is eliminated when real-time data is processed.


Visual Mode

With each incoming tick the distance between the last node - in the example above that would be the close of the hourly bar - and the current value of the indicator is repainted by using a linear interpolation. In COBC = false mode you can see the last leg moving up and down.


Accessing the indicator

In strategy mode it is pretty easy to access the indicator via another indicator or a strategy. This is possible because the indicator does not repaint. However, there is a limitation. Any multi-timeframe indicator that loads range bars, cannot be properly accessed via another indicator or strategy. Sometimes the indicator values will be vertically shifted, that is the indicator values would be false.

Therefore the VisualSMA cannot be accessed when the period type selected via the indicator parameters is set to "Range".

If you want to access the VisualSMA in One-Tick Repaint or Visual mode you would need to take into account that the indicator repaints. When the indicator is accessed in One-Tick Repaint mode, you need to access the last 2 values for your strategy and let it recalculate. When you access the indicator in Visual mode, things are more complicated, as the indicator repaints over the last swinging leg. Here you would need to call all indicator values that cover this period.

The indicator offers you a Public IntSeries, which can be accessed and which tells you how many bars it paints back for the currennt value. The IntSeries can be accessed as VisualSMA(period, false, false, thisCalcMode, thisPeriodType, thisPeriodValue, thisPriceType).Repaint[0] and then used to collect the last indicator values. You can then use the integer to loop through the bars back to the last node, which is used as anchor point.

For those who are interested I have attached a model indicator that accesses the VisualSMA and plots the values on a chart. Please copy the model indicator into the directory NinjaTrader 7 -> bin -> custom -> Indicator and compile. It will only compile, if you have the VisualSMA installed.

In case that you have installed the model indicator or any other indicator or strategy that accesses the VisualSMA, do not forget to remove those indicators/strategies first before you uninstall the assembly.

Hi @Fat Tails

A question, I try to use the VisualEMA in a strategy, build with the Strategy Wizard and I would like that the VisualEMA under "Rissing" paints the background. Right now it paint's only some single lines, what do I have to do, that it paints correctly?

Thanks for any help
Michael

Attached Thumbnails
Click image for larger version

Name:	visual.JPG
Views:	345
Size:	187.6 KB
ID:	161724  
Follow me on Twitter Visit my NexusFi Trade Journal Reply With Quote
  #398 (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


michaelf View Post
Hi @Fat Tails

A question, I try to use the VisualEMA in a strategy, build with the Strategy Wizard and I would like that the VisualEMA under "Rissing" paints the background. Right now it paint's only some single lines, what do I have to do, that it paints correctly?

Thanks for any help
Michael

The VisualEMA calculates node values according to the selected period for its internal bars and then interpolates the distance between the current value and the node. Interpolating means that it is repainting back to the last node. Repainting indicators cannot be accessed via a strategy created with the strategy wizard. However, it could be accessed via a manually coded strategy, as the number of bars that are currently repainted are exposed and can be accessed.

If you wish to access it with the strategy wizard, then you need to use the indicator in strategy mode (NOT: visual mode). In strategy mode the indicator paints a step function. Everytime a secondary bar closes the VisualEMA follows with a new value. I do not know whether you can define a rising state and a falling state for a step function with the strategy wizard. It is definitely easy to achieve, if you code manually.

Please be aware that the Visual EMA repaints in visual mode - it repaints back to the prior node - and therefore the slope derived from a new value will only be available in real time when the secondary bar has closed.

Started this thread Reply With Quote
Thanked by:
  #399 (permalink)
 Trader Jeff 
Chicago, IL United States
 
Experience: Advanced
Platform: Ninja Trader, Trader Work Station, Think or Swim
Broker: Ninja Trader, Interactive Brokers, TD Ameritrade
Trading: ES
Posts: 27 since Nov 2014
Thanks Given: 7
Thanks Received: 10

Hi,

I am setting up a strategy that uses an EMA/Cross for an entry of one timeframe and uses an EMA/Cross of another timeframe reversing for my exit/stop. Could this indicator be used in a strategy for this? And if not, would you kindly point me in the right direction of another Ninja indicator that can accomplish this if you know of one.

I guess I am basically looking for an alternative option of the general stop/loss by using another EMA Cross on a different timeframe than the primary entry for my exit.

Thanks for providing this indicator!

Jeff

Reply With Quote
  #400 (permalink)
 Trader Jeff 
Chicago, IL United States
 
Experience: Advanced
Platform: Ninja Trader, Trader Work Station, Think or Swim
Broker: Ninja Trader, Interactive Brokers, TD Ameritrade
Trading: ES
Posts: 27 since Nov 2014
Thanks Given: 7
Thanks Received: 10




Trader Jeff View Post
Hi,

I am setting up a strategy that uses an EMA/Cross for an entry of one timeframe and uses an EMA/Cross of another timeframe reversing for my exit/stop. Could this indicator be used in a strategy for this? And if not, would you kindly point me in the right direction of another Ninja indicator that can accomplish this if you know of one.

I guess I am basically looking for an alternative option of the general stop/loss by using another EMA Cross on a different timeframe than the primary entry for my exit.

Thanks for providing this indicator!

Jeff


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