NexusFi: Find Your Edge


Home Menu

 





Multiple Time Frames


Discussion in NinjaTrader

Updated
      Top Posters
    1. looks_one spinnybobo with 6 posts (4 thanks)
    2. looks_two mwamba123 with 4 posts (0 thanks)
    3. looks_3 sam028 with 3 posts (5 thanks)
    4. looks_4 Trader Jeff with 2 posts (0 thanks)
      Best Posters
    1. looks_one NJAMC with 3 thanks per post
    2. looks_two cyrussujar with 2 thanks per post
    3. looks_3 sam028 with 1.7 thanks per post
    4. looks_4 spinnybobo with 0.7 thanks per post
    1. trending_up 12,814 views
    2. thumb_up 15 thanks given
    3. group 11 followers
    1. forum 17 posts
    2. attach_file 2 attachments




 
Search this Thread

Multiple Time Frames

  #1 (permalink)
jkinneberg
Vancouver, BC, Canada
 
Posts: 1 since Aug 2009
Thanks Given: 0
Thanks Received: 1

Have started coding in NinjaTrader. I know this is a very newbie question, and am an experienced coder just not coded algo systems.

How does one confirm logic in multiple time frames?
Want to only execute a trade if the longer timeframes, trend following indicators indicate the trade is being executed in the direction of the trend. For example the MACD on the daily and the 60 minute time frame is above a signal line, although the trade is being executed on a 5 minute time frame.

Thanks

Reply With Quote
The following user says Thank You to jkinneberg for this post:

Can you help answer these questions
from other members on NexusFi?
Request for MACD with option to use different MAs for fa …
NinjaTrader
NexusFi Journal Challenge - April 2024
Feedback and Announcements
ZombieSqueeze
Platforms and Indicators
My NT8 Volume Profile Split by Asian/Euro/Open
NinjaTrader
 
Best Threads (Most Thanked)
in the last 7 days on NexusFi
Retail Trading As An Industry
58 thanks
Battlestations: Show us your trading desks!
50 thanks
NexusFi site changelog and issues/problem reporting
47 thanks
GFIs1 1 DAX trade per day journal
32 thanks
What percentage per day is possible? [Poll]
31 thanks

  #2 (permalink)
 
sam028's Avatar
 sam028 
Site Moderator
 
Posts: 3,761 since Jun 2009
Thanks Given: 3,824
Thanks Received: 4,629

It's quite simple, just check the example below, which is using 3 timeframes:
 
Code
                            
        protected override void Initialize(){
            
SetProfitTarget(""CalculationMode.TicksstopWin);
            
SetStopLoss(""CalculationMode.TicksstopLossfalse);
            
// Add a 5 minute Bars object to the strategy
            
Add(PeriodType.Minute5); //BarsArray[1]
            // Add a 15 minute Bars object to the strategy
            
Add(PeriodType.Minute15); //BarsArray[2]            
            // Note: Bars are added to the BarsArray and can be accessed via an index value
            // E.G. BarsArray[1] ---> Accesses the 5 minute Bars object added above
            
Add(StochasticMomentumIndex(3,5));
            
CalculateOnBarClose true;
        }

        protected 
override void OnBarUpdate()
        {
            if (
BarsInProgress != 0)
                return;
            
            
// Checks  if the 5 period SMA is above the 50 period SMA on both the 5 and 15 minute time frames
            
if (StochasticMomentumIndex(BarsArray[2],3,5)[0] > maxL5 &&
                
StochasticMomentumIndex(BarsArray[1],3,5)[0] > maxL15 &&
                
StochasticMomentumIndex(3,5)[0] > MaxL1)                
            {
                
EnterShort(1"STO Short");
            }
            if (
StochasticMomentumIndex(BarsArray[2],3,5)[0] < minL5 &&
                
StochasticMomentumIndex(BarsArray[1],3,5)[0] < minL15 &&
                
StochasticMomentumIndex(3,5)[0] < minL1)                
            {
                
EnterLong(1"STO Long");
            }                            
        } 

Follow me on Twitter Reply With Quote
The following user says Thank You to sam028 for this post:
  #3 (permalink)
 
spinnybobo's Avatar
 spinnybobo 
Crete, IL/USA
 
Experience: Intermediate
Platform: NinjaTrader, Mt4
Broker: Tradestation/Tradestation, NinjaTrader, FXCM and Tallinex
Trading: ES, CL, EUR/USD, TF
Posts: 173 since Aug 2009
Thanks Given: 105
Thanks Received: 61



sam028 View Post
It's quite simple, just check the example below, which is using 3 timeframes:
 
Code
                            
        protected override void Initialize(){
            
SetProfitTarget(""CalculationMode.TicksstopWin);
            
SetStopLoss(""CalculationMode.TicksstopLossfalse);
            
// Add a 5 minute Bars object to the strategy
            
Add(PeriodType.Minute5); //BarsArray[1]
            // Add a 15 minute Bars object to the strategy
            
Add(PeriodType.Minute15); //BarsArray[2]            
            // Note: Bars are added to the BarsArray and can be accessed via an index value
            // E.G. BarsArray[1] ---> Accesses the 5 minute Bars object added above
            
Add(StochasticMomentumIndex(3,5));
            
CalculateOnBarClose true;
        }
 
        protected 
override void OnBarUpdate()
        {
            if (
BarsInProgress != 0)
                return;
 
            
// Checks  if the 5 period SMA is above the 50 period SMA on both the 5 and 15 minute time frames
            
if (StochasticMomentumIndex(BarsArray[2],3,5)[0] > maxL5 &&
                
StochasticMomentumIndex(BarsArray[1],3,5)[0] > maxL15 &&
                
StochasticMomentumIndex(3,5)[0] > MaxL1)                
            {
                
EnterShort(1"STO Short");
            }
            if (
StochasticMomentumIndex(BarsArray[2],3,5)[0] < minL5 &&
                
StochasticMomentumIndex(BarsArray[1],3,5)[0] < minL15 &&
                
StochasticMomentumIndex(3,5)[0] < minL1)                
            {
                
EnterLong(1"STO Long");
            }                            
        } 


Hi Sam

just wanted to say that I enjoyed your webinar. you should do another one on multiple time frames:-)

I was wondering if you could perhaps post all of this code? I am not sure if minL5 is something you made up or part of the Ninja library----would like to see your private variables as well as public.

I am really wanting to learn how to deal with multiple time frames on Ninja Trader. Perhaps you can post more examples as they are extremely helpful.

I am looking on specifically wanting to do the following:
Entry is based on a cross of the SMA (simple moving averages) the 5 period and 21 period on the Daily chart, AND the current price is > than the 8 period moving average of the monthly chart. If this is true, GoLong

Then, I would want my exit to be based on price so it would be intrabar for the daily, so I would have to use the 1 minute for exit-----say, if it goes up 30 ticks, then out and set stop at 30 ticks.

I actually have a more complex exit plan than this, but just want to know how to enter based on the daily/monthly-----and exit on the 1 minute or even the tick chart---maybe like a 100 tick or something.

Any information about this would be greatly appreciated. I am writing a paper for my school in computer science, and wanted to do some complex programming. Just need to know how to incorporate multiple time frames.

thank you

Spencer

Follow me on Twitter Reply With Quote
  #4 (permalink)
 
sam028's Avatar
 sam028 
Site Moderator
 
Posts: 3,761 since Jun 2009
Thanks Given: 3,824
Thanks Received: 4,629

Hi @spinnybobo,

Here is the code:

 
Code
                            

#region Using declarations
using System;
using System.ComponentModel;
using System.Diagnostics;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Xml.Serialization;
using NinjaTrader.Cbi;
using NinjaTrader.Data;
using NinjaTrader.Indicator;
using NinjaTrader.Strategy;
#endregion


namespace NinjaTrader.Strategy
{

    [
Description("SMI multi frame.")]
    public class 
SMI_mtf Strategy
    
{
        
#region Variables
        
private int stopWin 20;     // Default setting for StopWin
        
private int stopLoss 20;     // Default setting for StopLoss
        
private int minL1 = -70;         // Default setting for Min Limit 1 min
        
private int maxL1 70;         // Default setting for Max Limit 1 min
        
private int minL5 = -20;         // Default setting for Min Limit 5 min
        
private int maxL5 20;         // Default setting for Max Limit 5 min
        
private int minL15 = -10;         // Default setting for Min Limit 15 min
        
private int maxL15 10;         // Default setting for Max Limit 15 min
      
        #endregion

        
protected override void Initialize(){

            
SetProfitTarget(""CalculationMode.TicksstopWin);
            
SetStopLoss(""CalculationMode.TicksstopLossfalse);

            
// Add a 5 minute Bars object to the strategy
            
Add(PeriodType.Minute5); //BarsArray[1]
                       
            
Add(PeriodType.Minute15); //BarsArray[2]
            
Add(StochasticMomentumIndex(3,5));
            
CalculateOnBarClose true;
        }

        protected 
override void OnBarUpdate()
        {
            if (
BarsInProgress != 0)
                return;
                        
            if (
StochasticMomentumIndex(BarsArray[2],3,5)[0] > maxL5 &&
                
StochasticMomentumIndex(BarsArray[1],3,5)[0] > maxL15 &&
                
StochasticMomentumIndex(3,5)[0] > MaxL1)                
            {
                
EnterShort(1"STO Short");
            }
            if (
StochasticMomentumIndex(BarsArray[2],3,5)[0] < minL5 &&
                
StochasticMomentumIndex(BarsArray[1],3,5)[0] < minL15 &&
                
StochasticMomentumIndex(3,5)[0] < minL1)                
            {
                
EnterLong(1"STO Long");
            }                            
        }

        
#region Properties
        
[Description("")]
        [
Category("Parameters")]
        public 
int StopWin
        
{
            
get { return stopWin; }
            
set stopWin Math.Max(1value); }
        }
        [
Description("")]
        [
Category("Parameters")]
        public 
int StopLoss
        
{
            
get { return stopLoss; }
            
set stopLoss Math.Max(1value); }
        }
        
        [
Description("")]
        [
Category("Parameters")]
        public 
int MaxL1
        
{
            
get { return maxL1; }
            
set maxL1 Math.Max(0value); }
        }
        
        [
Description("")]
        [
Category("Parameters")]
        public 
int MinL1
        
{
            
get { return minL1; }
            
set minL1 Math.Min(0value); }
        }    
        [
Description("")]
        [
Category("Parameters")]
        public 
int MaxL5
        
{
            
get { return maxL5; }
            
set maxL5 Math.Max(0value); }
        }
        
        [
Description("")]
        [
Category("Parameters")]
        public 
int MinL5
        
{
            
get { return minL5; }
            
set minL5 Math.Min(0value); }
        }    
        [
Description("")]
        [
Category("Parameters")]
        public 
int MaxL15
        
{
            
get { return maxL15; }
            
set maxL15 Math.Max(0value); }
        }
        
        [
Description("")]
        [
Category("Parameters")]
        public 
int MinL15
        
{
            
get { return minL15; }
            
set minL15 Math.Min(0value); }
        }    
        
#endregion
    
}


Success requires no deodorant! (Sun Tzu)
Follow me on Twitter Reply With Quote
The following 3 users say Thank You to sam028 for this post:
  #5 (permalink)
 
spinnybobo's Avatar
 spinnybobo 
Crete, IL/USA
 
Experience: Intermediate
Platform: NinjaTrader, Mt4
Broker: Tradestation/Tradestation, NinjaTrader, FXCM and Tallinex
Trading: ES, CL, EUR/USD, TF
Posts: 173 since Aug 2009
Thanks Given: 105
Thanks Received: 61


sam028 View Post
Hi @spinnybobo,

Here is the code:

 
Code
                            


#region Using declarations
using System;
using System.ComponentModel;
using System.Diagnostics;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Xml.Serialization;
using NinjaTrader.Cbi;
using NinjaTrader.Data;
using NinjaTrader.Indicator;
using NinjaTrader.Strategy;
#endregion


namespace NinjaTrader.Strategy
{

    [
Description("SMI multi frame.")]
    public class 
SMI_mtf Strategy
    
{
        
#region Variables
        
private int stopWin 20;     // Default setting for StopWin
        
private int stopLoss 20;     // Default setting for StopLoss
        
private int minL1 = -70;         // Default setting for Min Limit 1 min
        
private int maxL1 70;         // Default setting for Max Limit 1 min
        
private int minL5 = -20;         // Default setting for Min Limit 5 min
        
private int maxL5 20;         // Default setting for Max Limit 5 min
        
private int minL15 = -10;         // Default setting for Min Limit 15 min
        
private int maxL15 10;         // Default setting for Max Limit 15 min
      
        #endregion

        
protected override void Initialize(){

            
SetProfitTarget(""CalculationMode.TicksstopWin);
            
SetStopLoss(""CalculationMode.TicksstopLossfalse);

            
// Add a 5 minute Bars object to the strategy
            
Add(PeriodType.Minute5); //BarsArray[1]
                       
            
Add(PeriodType.Minute15); //BarsArray[2]
            
Add(StochasticMomentumIndex(3,5));
            
CalculateOnBarClose true;
        }

        protected 
override void OnBarUpdate()
        {
            if (
BarsInProgress != 0)
                return;
                        
            if (
StochasticMomentumIndex(BarsArray[2],3,5)[0] > maxL5 &&
                
StochasticMomentumIndex(BarsArray[1],3,5)[0] > maxL15 &&
                
StochasticMomentumIndex(3,5)[0] > MaxL1)                
            {
                
EnterShort(1"STO Short");
            }
            if (
StochasticMomentumIndex(BarsArray[2],3,5)[0] < minL5 &&
                
StochasticMomentumIndex(BarsArray[1],3,5)[0] < minL15 &&
                
StochasticMomentumIndex(3,5)[0] < minL1)                
            {
                
EnterLong(1"STO Long");
            }                            
        }

        
#region Properties
        
[Description("")]
        [
Category("Parameters")]
        public 
int StopWin
        
{
            
get { return stopWin; }
            
set stopWin Math.Max(1value); }
        }
        [
Description("")]
        [
Category("Parameters")]
        public 
int StopLoss
        
{
            
get { return stopLoss; }
            
set stopLoss Math.Max(1value); }
        }
        
        [
Description("")]
        [
Category("Parameters")]
        public 
int MaxL1
        
{
            
get { return maxL1; }
            
set maxL1 Math.Max(0value); }
        }
        
        [
Description("")]
        [
Category("Parameters")]
        public 
int MinL1
        
{
            
get { return minL1; }
            
set minL1 Math.Min(0value); }
        }    
        [
Description("")]
        [
Category("Parameters")]
        public 
int MaxL5
        
{
            
get { return maxL5; }
            
set maxL5 Math.Max(0value); }
        }
        
        [
Description("")]
        [
Category("Parameters")]
        public 
int MinL5
        
{
            
get { return minL5; }
            
set minL5 Math.Min(0value); }
        }    
        [
Description("")]
        [
Category("Parameters")]
        public 
int MaxL15
        
{
            
get { return maxL15; }
            
set maxL15 Math.Max(0value); }
        }
        
        [
Description("")]
        [
Category("Parameters")]
        public 
int MinL15
        
{
            
get { return minL15; }
            
set minL15 Math.Min(0value); }
        }    
        
#endregion
    
}


Hi Sam

thanks for the code? you are awesome. Quick question rather than starting a new thread: Is it possible to backtest a strategy with 2 time frames? Like the ES entry on 5min and exit on 1min or tick and to backtest everything and get a chart printout?

I ask because it is not very intuitive. On the regular chart, it has a spot for entering multiple data series and bars back and everything, then adding the strategy. But on the backtesting engine, I dont see a spot for entering another data series?

The only other thing I can think of is hard coding the exact name of the data series and time frame in the actual strategy------not sure how to do this?

thanks as always

take care
Spencer

Follow me on Twitter Reply With Quote
The following user says Thank You to spinnybobo for this post:
  #6 (permalink)
 
sam028's Avatar
 sam028 
Site Moderator
 
Posts: 3,761 since Jun 2009
Thanks Given: 3,824
Thanks Received: 4,629

Sorry, but I don't see where is the problem.
Of course, MTF strategies are backtestable.
The logic for your idea is simple:
- check on which time frame you're in
->if it's the main TF, and flat, look for an entry
->if it's the other TF, and not flat, look for an exit

But I've maybe not understood your questions .

Success requires no deodorant! (Sun Tzu)
Follow me on Twitter Reply With Quote
The following user says Thank You to sam028 for this post:
  #7 (permalink)
 
NJAMC's Avatar
 NJAMC 
Atkinson, NH USA
Market Wizard
 
Experience: Intermediate
Platform: NinjaTrader 8/TensorFlow
Broker: NinjaTrader Brokerage
Trading: Futures, CL, ES, ZB
Posts: 1,970 since Dec 2010
Thanks Given: 3,037
Thanks Received: 2,394


spinnybobo View Post
Hi Sam

thanks for the code? you are awesome. Quick question rather than starting a new thread: Is it possible to backtest a strategy with 2 time frames? Like the ES entry on 5min and exit on 1min or tick and to backtest everything and get a chart printout?

I ask because it is not very intuitive. On the regular chart, it has a spot for entering multiple data series and bars back and everything, then adding the strategy. But on the backtesting engine, I dont see a spot for entering another data series?

The only other thing I can think of is hard coding the exact name of the data series and time frame in the actual strategy------not sure how to do this?

thanks as always

take care
Spencer

Hi @spinnybobo

Be sure to keep an eye on BarsInProgress. Also, when you use the EnterLong function, you may need to use the following format:

EnterLong(int barsInProgressIndex, int quantity, string signalName)

This will let you fire orders against different Instruments in your strategy based upon the BarsInProgress. By default is is 0 and fires upon your main chart/instrument.

Nil per os
-NJAMC [Generic Programmer]

LOM WIKI: NT-Local-Order-Manager-LOM-Guide
Artificial Bee Colony Optimization
Visit my NexusFi Trade Journal Reply With Quote
The following 3 users say Thank You to NJAMC for this post:
  #8 (permalink)
 
spinnybobo's Avatar
 spinnybobo 
Crete, IL/USA
 
Experience: Intermediate
Platform: NinjaTrader, Mt4
Broker: Tradestation/Tradestation, NinjaTrader, FXCM and Tallinex
Trading: ES, CL, EUR/USD, TF
Posts: 173 since Aug 2009
Thanks Given: 105
Thanks Received: 61


sam028 View Post
Sorry, but I don't see where is the problem.
Of course, MTF strategies are backtestable.
The logic for your idea is simple:
- check on which time frame you're in
->if it's the main TF, and flat, look for an entry
->if it's the other TF, and not flat, look for an exit

But I've maybe not understood your questions .

thanks Sam. I am confusing the act of starting a new chart and adding multiple data series to the chart with the strategy analyzer. In Tradestation, for example, I always had to add a data series the strategy was using to the chart in order for it to work. It seems that Ninja can add it to the base chart through the code. Just a mis-understanding of how everything works:-)

I did have one question though: Let's say I wanted to open a new 1 min chart on the ES. Lets say I wanted my code to add an SMA(8) of the 10 min ES chart so I can see it. So, it would have 1 min bars with a ES 10 min SMA(8) all done programmatically.

it seems like anything we want to add to the chart has to be done through the Add() method like Add(SMA(5)) in the Initialize(){} field. However, we cannot use BarsArray in this field----and I think we can only access multiple time frames through BarsArray---correct? ---and this field is the area where we would Add indicators to the chart.

Is it possible to add an indicator of the ES SMA(5) 10 min chart of ES 1min programmatically?
I would think this would be essential for debugging to make sure everything is written correctly from a visual perspective.

thanks
Spencer

Follow me on Twitter Reply With Quote
  #9 (permalink)
 cyrussujar 
OH
 
Experience: None
Platform: NinjaTrader
Posts: 6 since Apr 2010
Thanks Given: 3
Thanks Received: 9

This has already been discussed:
Coding Multi Time Frame (MTF) Indicators with NinjaTrader

You can also just download the indicator:
Visual EMA Multi-Timeframe

In summary, basic coding causes "stair-stepping" of higher time-frame indicators but that's actually what you want if you are designing a strategy. There is a MTF stochastic available too.

Reply With Quote
The following 2 users say Thank You to cyrussujar for this post:
  #10 (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



spinnybobo View Post
Hi Sam

thanks for the code? you are awesome. Quick question rather than starting a new thread: Is it possible to backtest a strategy with 2 time frames? Like the ES entry on 5min and exit on 1min or tick and to backtest everything and get a chart printout?

I ask because it is not very intuitive. On the regular chart, it has a spot for entering multiple data series and bars back and everything, then adding the strategy. But on the backtesting engine, I dont see a spot for entering another data series?

The only other thing I can think of is hard coding the exact name of the data series and time frame in the actual strategy------not sure how to do this?

thanks as always

take care
Spencer

Hi there,

I was curious to know if you were able to setup a strategy to backtest the 5 minute for entry and use the 1 minute for exiting using ema? I came across this thread since I am trying to accomplish a similar strategy.

Thanks,
Jeff

Reply With Quote





Last Updated on March 21, 2015


© 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