PDA

View Full Version : Multi-Timeframe use in a Strategy


Freddie
04-13-2007, 02:51 PM
Hi all,

Please be easy on me this is only my second strategy. The first was much easier and very easy to program using the Strategy Anlayzer. Here goes;

I have an indicator that when a signal is received on the 30 minute bar you enter a trade. That works fine in the Analyzer. Now what I'd like to do is add another condition that when the 30 minute signal is received, the same indicator is run on the 2 minute chart of the same instrument and if I get a signal on that as well then I enter the trade. Long timeframe projects general direction and short timeframe the entry.

Is there a way to do this in the Strategy Analyzer? I think it has to do with "Input Series" but I cannot find a way to add an additional input series.

thx in advance for the help,

Fred

NinjaTrader_Dierk
04-14-2007, 12:21 AM
Sorry, this is not supported right now. Indicators (e.g. on market analyzer columns) only support one timeframe.

Freddie
04-14-2007, 03:05 AM
Dierk,

If it is not supported then what is the "Multi-Time Frame & Instruments" in the help section for NT6? It states "A NinjaScript strategy supports multi-time frame and instruments in a single strategy." Is that only if you write it in a strategy but cannot be done in the Strategy Analyzer unless youapply the written strategy as part of the conditions in the Strategy Analyzer?

Corrrect me if I'm wrong here as I'm pretty green on this programming but it looks like you can use multi timeframes and more than one instrument in a strategy. Is that correct?

But what you are telling me is that it can't be done or run in the Analyzer?

Still quite confused.

Freddie

NinjaTrader_Dierk
04-14-2007, 05:11 AM
Oops, my apologies. My comment was about market analyzer.

Yes, multi timeframes and multi instruments on a strategy are supported in strategy analyzer.

Does the sample provided in the docs make sense to you?

Freddie
04-16-2007, 09:53 AM
The sample in the docs does make sense I think. You need to somehow define a second data series if I recall. Not sure how to do that in the Analyzer?

Do you understand clearly what I'm trying to do? I'll try to explain again:

I currently have strategy setup using an indicator (call itMyIndicator)on 30 minute bars and the strategy runs correctly and the results look correct. Now what I'd like to do is add to the strategy another condition that "MyIndicator" must also have a buy signal on the 2 minute bars of the same instrument used for the 30 minute signal. So basically, I want the longer timeframe (30 min) to signal a buy and have it confirmed by the shorter timeframe (2min) using the same indicator on both the 30 and 2 minute bars of the same instrument.

I hope this is clear. At this point I'm not sure how I need to do this. Do I need somehow to define a second data series in the indicator or do I somehow define itbyeditingthe existing strategy?

Thx,

NinjaTrader_Dierk
04-16-2007, 10:05 AM
Please check out again the docs. The sample shows you
- how to add addtional series with a different timeframe to your strategy
- how to run indicators in your strategy on these different series

This is exactely what you need to do: http://www.ninjatrader-support.com/HelpGuideV6/MultiTimeFrameInstruments.html

Freddie
04-16-2007, 04:15 PM
Dierk,

I read the article and put in the Add statement and then I've tried numerous ways to get it to work and can't. Maybe you can help. Basically I want to repeat the "If" statement again but to be run on the second data series which in this case is the 5 minute dataseries that I added in the intialize. EdsPivotSMA is an indicator that I wrote that plots 2 lines, LongSMA and Short SMA. LongMAPeriods and ShortMAPeriods are variables that I can change to optimize the indicator.

Hopefully, you can provide me some ideas of what to try. Thanks,

Here's the code:

protected override void Initialize()

{

SetTrailStop("", CalculationMode.Ticks, TrailingStop, false);

Add(PeriodType.Minute,5);

CalculateOnBarClose = false;

}

/// <summary>

/// Called on each bar update event (incoming tick)

/// </summary>

protected override void OnBarUpdate()

{

// Condition set 1

if (Close[0] > EdsPivotSMA(LongMAPeriods, ShortMAPeriods).LongSMA[0] && Close[0] > EdsPivotSMA(LongMAPeriods, ShortMAPeriods).ShortSMA[0]&& EdsPivotSMA(LongMAPeriods, ShortMAPeriods).ShortSMA[0] > EdsPivotSMA(LongMAPeriods, ShortMAPeriods).LongSMA[0])



// Want to add the next conditons, similar to above if statement but applied to 5 minute bar

// (ie. BarsArray[1]) rather than to the default data series.















{

EnterLongLimit(DefaultQuantity, Close[0], "Buy Long");

ExitShortLimit(Close[0], "Cover Short", "");

NinjaTrader_Dierk
04-16-2007, 07:25 PM
As the article suggests with it's sample
CCI(BarsArray[1], 20)[0] you need to run your indicator on the secondary series like
EdsPivotSMA(BarsArray[1], LongMAPeriods, ShortMAPeriods).LongSMA[0]
BarsArray[0] = primary series (default)
BarsArray[1] = secondary series added in Initialize

Please let us know what you wanted to see clarified in the docs.

Thanks

Freddie
04-17-2007, 12:51 AM
Thx Dierk,

I'll give it a try. I was using EdsPivotSMA((BarsArray[1], LongMAPeriods), (BarsArray[1],ShortMAPeriods)).LongSMA[0] as I was not sure how to exactly add the BarsArray expression.

NinjaTrader_Dierk
04-17-2007, 01:08 AM
Great. We'll try to clarify in the docs.

Freddie
04-17-2007, 02:56 AM
That seemed to work ok for me but got another question on a slightly different topic but for the same strategy. I've set up the strategy to only calculate on close. From my results it appears that the strategy is only calculating when the default input series (which in my case is the 30 minute data series) rather than on both the 30 minute and shorter data series of 5 minutes. What is happening is it is only picking up signals when both the long signal and short signal are both given on the 30 minute close. Ideally I'd like to have the 30 minute signal checked (say at 10 am) and then if the 5 minute bar closes with a buy signal at 10:10am that I would buy then.

To have this happen would I need to change the default data series to the shorter one or do I need to somehow trigger a variable in the strategy to keep track of the long signals from each of the longer and shorter timeframes?

Thx for all your help Dierk. BTW where is Bamburg? My brother-in-law works in Cologne (Koln) and we visited there 2 years ago. Nice city.

NinjaTrader_Dierk
04-17-2007, 03:07 AM
OnBarUpdate is triggered for each series where a bar closes (if CalculateOnBarClose=true) in your case on every bar of the 30min and on every bar of the 5 min series.

Please confirm by:
Print(BarsInProgress.ToString() + " " + Time[0]);
which prints the timestamp of every closed bar.

NinjaTrader_Ray
04-17-2007, 03:14 AM
Yes you would need to make the 5 min series the primary series if that is the time frame you need to execute orders against. You can check values of the larger time frame within the context of the primary series.

Bamberg is several hours SE of Koln.

Ray

Freddie
04-17-2007, 03:41 AM
First, I'm getting this message when I run the analyzer:

"Order for strategy '----' was placed in incorrect 'BarsinProgress' context. Order ignored.



Second, I added the print line you requested at the top of the OnBarUpdae section of the strategy. It shows 2 timestamps for every 5 minutes and 4 timestamps on the 1/2 hour. Not sure why 4 on the 1/2 hour; shouldn't it still be just 2?

Ray,

So if I understand you correctly, if I'm executing off of the 5 minutes signal, which is my case, then that should be the primary series and the 30 minute should be the secondary series. So just got to flip all the program around then.



Fred

Freddie
04-17-2007, 03:43 AM
1 other thing on the timestamps that I just noticed:

On the 4 that occur each 1/2 hour, the first item in the line on 2 of them is a 1, and the first item in the line of 2 of them is a 0. If this helps at all.

NinjaTrader_Dierk
04-17-2007, 03:47 AM
Please post your strategy, so we can take a look.

Thanks

Freddie
04-17-2007, 03:52 AM
Here it is:

#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

// This namespace holds all strategies and is required. Do not change it.

namespace NinjaTrader.Strategy

{

/// <summary>

/// Enter the description of your strategy here

/// </summary>

[Description("Enter the description of your strategy here")]

[Gui.Design.DisplayName("Pivot MA using Multi Timeframes")]

public class PivotMAMultiTimeFrame : Strategy

{

#region Variables

// Wizard generated variables

private int longMAPeriods = 3; // Default setting for LongMAPeriods

private int shortMAPeriods = 1; // Default setting for ShortMAPeriods

private int trailingStop = 10; // Default setting for TrailingStop

// User defined variables (add any user defined variables below)

#endregion

/// <summary>

/// This method is used to configure the strategy and is called once before any strategy method is called.

/// </summary>

protected override void Initialize()

{

SetTrailStop("", CalculationMode.Ticks, TrailingStop, false);

Add(PeriodType.Minute,5);

CalculateOnBarClose = true;

}

/// <summary>

/// Called on each bar update event (incoming tick)

/// </summary>

protected override void OnBarUpdate()

{

int LTFLong = 0;

int STFLong =0;

int TimeCheck = 0;

Print(BarsInProgress.ToString() + " " + Time[0]);



// Condition set Long

if (Close[0] > EdsPivotSMA(LongMAPeriods, ShortMAPeriods).LongSMA[0]

&& Close[0] > EdsPivotSMA(LongMAPeriods, ShortMAPeriods).ShortSMA[0]

&& EdsPivotSMA(LongMAPeriods, ShortMAPeriods).ShortSMA[0] > EdsPivotSMA(LongMAPeriods, ShortMAPeriods).LongSMA[0])

LTFLong = 1;



if (Closes[1][0] > EdsPivotSMA(BarsArray[1],LongMAPeriods,ShortMAPeriods).LongSMA[0]

&& Closes[1][0] > EdsPivotSMA(BarsArray[1],LongMAPeriods, ShortMAPeriods).ShortSMA[0]

&& EdsPivotSMA(BarsArray[1],LongMAPeriods, ShortMAPeriods).ShortSMA[0] >

EdsPivotSMA(BarsArray[1],LongMAPeriods, ShortMAPeriods).LongSMA[0])

STFLong =1;



if (ToTime(Time[0]) > ToTime(9, 30, 0) && ToTime(Time[0]) <= ToTime(15, 30, 0))

TimeCheck = 1;

else

TimeCheck = 0;



{

if(LTFLong == 1 )

if(STFLong == 1)

if(TimeCheck == 1)

EnterLongLimit(DefaultQuantity, Close[0], "Buy Long");

ExitShortLimit(Close[0], "Cover Short", "");

Print(BarsInProgress.ToString() + " " + Time[0]);

}

// Condition set Short

if (Close[0] < EdsPivotSMA(LongMAPeriods, ShortMAPeriods).LongSMA[0]

&& Close[0] < EdsPivotSMA(LongMAPeriods, ShortMAPeriods).ShortSMA[0]

&& EdsPivotSMA(LongMAPeriods, ShortMAPeriods).ShortSMA[0] < EdsPivotSMA(LongMAPeriods, ShortMAPeriods).LongSMA[0])



if (Closes[1][0] < EdsPivotSMA(BarsArray[1],LongMAPeriods,ShortMAPeriods).LongSMA[0]

&& Closes[1][0] < EdsPivotSMA(BarsArray[1],LongMAPeriods, ShortMAPeriods).ShortSMA[0]

&& EdsPivotSMA(BarsArray[1],LongMAPeriods, ShortMAPeriods).ShortSMA[0] <

EdsPivotSMA(BarsArray[1],LongMAPeriods, ShortMAPeriods).LongSMA[0])



if (ToTime(Time[0]) > ToTime(9, 30, 0)

&& ToTime(Time[0]) <= ToTime(15, 30, 0))

{

ExitLongLimit(Close[0], "Exit Long", "Buy Long");

EnterShortLimit(DefaultQuantity, Close[0], "Enter Short");

}



}

#region Properties

[Description("")]

[Category("Parameters")]

public int LongMAPeriods

{

get { return longMAPeriods; }

set { longMAPeriods = Math.Max(1, value); }

}

[Description("")]

[Category("Parameters")]

public int ShortMAPeriods

{

get { return shortMAPeriods; }

set { shortMAPeriods = Math.Max(1, value); }

}

[Description("")]

[Category("Parameters")]

public int TrailingStop

{

get { return trailingStop; }

set { trailingStop = Math.Max(1, value); }

}

#endregion

}

}

Freddie
04-17-2007, 05:14 AM
Guys,

I've solved the double reading on the 1/2 hour but still am getting the error message.

NinjaTrader_Dierk
04-17-2007, 06:50 AM
The problem is that your have 2 series on the same instrument. You then only can place orders on the frist series for this instrument.

You need to do something like:
if (BarsInProgress != 0)
return;

... since order placements on BarsInProgress==1 will throw the error you saw.

Freddie
04-17-2007, 07:12 AM
Yes this solved the error problem. Based on your comment that I can only place orders on the first series that I'll have to modify my program as I want to place orders based on the shorter time frame.

One other question; when the Analyzer runs and determines the entry and exit points, does it use the tick data that I have stored for the instrument or what data does it use.

thx,

Fred

NinjaTrader_Dierk
04-17-2007, 07:17 AM
What do you mean by "tick data stored"?

There are three different potential data sources:
- historical tick data downloaded from a provider like eSignal: it will be used provided you have a "tick based" timeframe
- historical tick data you recorded as you had a chart running (Tools->Options->Data->Store realtime bar data checked): it will be used provided you have a "tick based" or "minute based" timeframe
- data your recorded using the market replay recorder (Tools->Options->Data->Run market replay recorder checked): will not be used for backtesting

Freddie
04-17-2007, 07:23 AM
I meant the tick data that I've downloaded and stored from my data supplier in real time.

NinjaTrader_Dierk
04-17-2007, 07:25 AM
So option (a) -> will be used for your "tick timeframe" strategy

FireFly
05-30-2007, 04:04 AM
Can Multiple-Timeframes also be used within an indicator, so that for example 1m and 5m timeframes are both used in the calculation?

NinjaTrader_Dierk
05-30-2007, 05:48 AM
Not possible

NinjaTrader_Ray
05-30-2007, 08:02 AM
Although it is possible in a strategy to have mixed time frames and instruments. Additional information can be found at the link below.

http://www.ninjatrader-support.com/HelpGuideV6/MultiTimeFrameInstruments.html