Welcome to NexusFi: the best trading community on the planet, with over 150,000 members Sign Up Now for Free
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 for basic access, or support us by becoming an Elite Member -- see if you qualify for a discount below.
-- Big Mike, Site Administrator
(If you already have an account, login at the top of the page)
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.
Broker: Tradestation/Tradestation, NinjaTrader, FXCM and Tallinex
Trading: ES, CL, EUR/USD, TF
Posts: 173 since Aug 2009
Thanks Given: 105
Thanks Received: 61
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.
#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
#region Properties [Description("")] [Category("Parameters")] public int StopWin { get { return stopWin; } set { stopWin = Math.Max(1, value); } } [Description("")] [Category("Parameters")] public int StopLoss { get { return stopLoss; } set { stopLoss = Math.Max(1, value); } }
[Description("")] [Category("Parameters")] public int MaxL1 { get { return maxL1; } set { maxL1 = Math.Max(0, value); } }
[Description("")] [Category("Parameters")] public int MinL1 { get { return minL1; } set { minL1 = Math.Min(0, value); } } [Description("")] [Category("Parameters")] public int MaxL5 { get { return maxL5; } set { maxL5 = Math.Max(0, value); } }
[Description("")] [Category("Parameters")] public int MinL5 { get { return minL5; } set { minL5 = Math.Min(0, value); } } [Description("")] [Category("Parameters")] public int MaxL15 { get { return maxL15; } set { maxL15 = Math.Max(0, value); } }
[Description("")] [Category("Parameters")] public int MinL15 { get { return minL15; } set { minL15 = Math.Min(0, value); } } #endregion } }
#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
#region Properties
[Description("")]
[Category("Parameters")]
public int StopWin
{
get { return stopWin; }
set { stopWin = Math.Max(1, value); }
}
[Description("")]
[Category("Parameters")]
public int StopLoss
{
get { return stopLoss; }
set { stopLoss = Math.Max(1, value); }
}
[Description("")]
[Category("Parameters")]
public int MaxL1
{
get { return maxL1; }
set { maxL1 = Math.Max(0, value); }
}
[Description("")]
[Category("Parameters")]
public int MinL1
{
get { return minL1; }
set { minL1 = Math.Min(0, value); }
}
[Description("")]
[Category("Parameters")]
public int MaxL5
{
get { return maxL5; }
set { maxL5 = Math.Max(0, value); }
}
[Description("")]
[Category("Parameters")]
public int MinL5
{
get { return minL5; }
set { minL5 = Math.Min(0, value); }
}
[Description("")]
[Category("Parameters")]
public int MaxL15
{
get { return maxL15; }
set { maxL15 = Math.Max(0, value); }
}
[Description("")]
[Category("Parameters")]
public int MinL15
{
get { return minL15; }
set { minL15 = Math.Min(0, value); }
}
#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?
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
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?
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.
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
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.
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.
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.