PDA

View Full Version : Multi time frame and WMA


ddurocher
05-22-2010, 05:04 AM
I am working with a 10 minute chart and trying to add a second time frame of 30 minutes.

In my primary time frame, one condition I am checking for is

Close[0] > WMA(High, LookBackPeriods)[0]

I am trying to check for something similar in the secondary time frame but have not been able to figure out the correct syntax for the WMA. My last attempt was this:

Closes[1][0] > WMA(High, LookBackPeriods)BarsArray[1][0]

I think the portion before the greater than is OK, but regardless, I can't get seem figure this out.

Thanks,

David

NinjaTrader_Ben
05-22-2010, 06:05 AM
Hello,

Remember the different time frames will be called at different times:
http://www.ninjatrader-support.com/HelpGuideV6/MultiTimeFrameInstruments.html

Try using BarsInProgress and Print("30 min") statements to better understand when the bars are being called and in what order. You also may want to print the various bar indicator values in all BarsInProgress blocks to see how those change too. It can be confusing, but once you sort it out its pretty easy.

ddurocher
05-26-2010, 10:12 AM
That's all well and good. If I could get the code to compile I could try some of those things. In the meantime I could use some help figuring out the proper syntax to reference the WMA high in my secondary time frame. As far as I can tell "Closes[1][0] is fine with the system. I cannot figure out the correct syntax for the second half of my statement to confirm that allows me to confirm that the close is above the WMA high in the secondary time frame.
Different things I have tried all result in errors. Sometimes a CS0021 error or a bunch of "statement expected" and "expected;" errors.

NinjaTrader_RyanM
05-26-2010, 10:29 AM
Hello ddurocher,

The simplest way to pass the high of the secondary series into WMA is through the use of Highs[1]

WMA(Highs[1], 14) will capture the 14 period WMA of the secondary series high.

An alternative is creating your own custom data series. This tutorial can help with creating a data series. (http://www.ninjatrader-support.com/HelpGuideV6/Overview24.html)

Data Series are:
1) Declared in Variables region
private DataSeries secondSeriesHigh;

2) Instantiated in Initialize() method
secondSeriesHigh = new DataSeries (this);

3) Set in OnbarUpdate()
secondSeriesHigh.Set(Highs[1][0]);

The set statement above will use Highs (http://www.ninjatrader-support.com/HelpGuideV6/Highs.html) to access the secondary series high.

You can then pass this value into the series expected for the WMA indicator.
if (Closes[1][0] > WMA(secondSeriesHigh, LookBackPeriods)[0])

ddurocher
05-27-2010, 08:56 AM
Thanks Ryan. That works great. I even played around with it trying "Lows" and "Mediums" as well.

David

NinjaTrader_RyanM
05-27-2010, 09:58 AM
Glad to hear you're able to get it working, David.

ddurocher
05-31-2010, 10:06 AM
Apparently I have not got this working yet. After testing my strategy for a few days on a simulated account with real data I noticed some unexpected behaviors. I've corrected some of them but still seeing entries when my 60 minute bars are not closing above the WMA in the 60 minute time frame.

I've been testing today with Market Replay data and also added some Print statements to my code. All the numbers are correct (10 min close, 10 min WMA high & 60 minute close) except for the 60 minute WMA high (or median, I've tested with both). In fact I can't even guess what the number might be referring to that I am seeing printed out as the supposed WMA High or Median.

I've looked through my code and I believe I've coded this properly.

Thanks,

David

NinjaTrader_Bertrand
05-31-2010, 12:53 PM
David, did you post your latest code version somewhere for review? How many days back are you loading and what's your min bars required setting? Please note the min bars have to be fullfilled for all series first and then straregy would move forward, so this might explain 'out of range' higher timeframe values perhaps.

ddurocher
05-31-2010, 05:32 PM
Is there a setting for a strategy to indicate how many days to go back? The chart I am using to watch the strategy is going back 15 days. Min bars is set to 20. I am using 14 for my WMA periods. 10 min main time frame, 60 minute for the secondary time frame. I suppose I could post part of my code due to the character limit on posts, but i'm not sure how useful that would be.

NinjaTrader_Bertrand
06-01-2010, 05:09 AM
David, the strategy would take whatever is on the chart as lookback range, so for all timeframes you have the min bars of 20 has to be fulfilled and then the strategy would start processing.

If you startup your strategy from the strategies tab, please be aware that the lookback taken in NT 6.5 is determined by the chart defaults setup under Tools > Options > Data.

ddurocher
06-01-2010, 08:40 AM
OK, on the data tab I have 15 days for minute bars. Since I'm using 10 and 60 minute timeframes and a 14 period WMA I should think that would be sufficient. I suspect my issue has to do with the use of BarsInProgress.

I've added a print statement now to see what sevaral of the values are after each 10 minute bar closes. Interestingly the WMA High, Low and Median on the 60 min timeframe are changing after each 10 minute bar closes. Aside from the fact that all these values are way off, should they be changing in 60 minute time frame after each 10 minute bar closes?

David

NinjaTrader_RyanM
06-01-2010, 09:03 AM
Hello David,

The help guide article posted by Ben is good for figuring out how different bar objects are updated.

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

If you're running into unexpected results here, share the code you're working with and what instruments it's applied to.

ddurocher
06-01-2010, 09:50 AM
Here's the code. I've been using Markey replay data the last couple of days for testing on HNU (cdn etf) I've also tested it wiuth live data on several other ETFs such as BGZ and SMN.



#region Variables
// Wizard generated variables
private int lookBackPeriods = 14; // Default setting for LookBackPeriods
private int longEntry = 150; // Default setting for LongEntry
// User defined variables (add any user defined variables below)
private DataSeries secondSeriesMed; //declaring dataseries created for passing Median of secondary time frame
private DataSeries secondSeriesHigh; //declaring dataseries created for passing High of secondary time frame
private DataSeries secondSeriesLow; //declaring dataseries created for passing Low of secondary time frame


#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()
{
// use multi time frame of 10 minutes with 60 minutes for confirmation of entry
Add(PeriodType.Minute, 60); // this is the timeframe for confirmation of entry
secondSeriesMed = new DataSeries (this); // instantiating dataseries to reference Median of secondary time frame
secondSeriesHigh = new DataSeries (this); // instantiating dataseries to reference High of secondary time frame
secondSeriesLow = new DataSeries (this); // instantiating dataseries to reference Low of secondary time frame
CalculateOnBarClose = true;
Add(WMA(High, LookBackPeriods));
Add(WMA(Low, LookBackPeriods));
ExitOnClose = false;
ExcludeWeekend = true;
SessionBegin = DateTime.Parse("09:30 AM");
SessionEnd = DateTime.Parse("04:00 PM");
}
/// <summary>
/// Called on each bar update event (incoming tick)
/// </summary>
protected override void OnBarUpdate()
{
secondSeriesMed.Set(Medians[1][0]); // use Medians to access the secondary series median
secondSeriesHigh.Set(Highs[1][0]); // use Highs to access the secondary series high
secondSeriesLow.Set(Lows[1][0]); // use Lows to access the secondary series high
// Only run on real-time data
if (Historical)
return;
// We only want to process events on our primary Bars object (index = 0) which is set when adding
// the strategy to a chart
// if (BarsInProgress != 0)
// return;
// if (BarsInProgress == 0)

// Condition set 1
if (BarsInProgress == 1
&& Position.MarketPosition == MarketPosition.Flat // if we are flat
&& Closes[0][0] > WMA(High, LookBackPeriods)[0] // Last 10 min bar closed above High WMA
&& Closes[0][0] >= Opens[0][0] // Last 10 min bar was an UP bar
&& Closes[0][1] < WMA(High, LookBackPeriods)[1] // Close of previous 10 min bar was below High WMA
&& Close[0] > WMA(secondSeriesMed, LookBackPeriods)[0]) // Last 60 min bar closed above Median WMA
// && Close[0] > WMA(secondSeriesHigh, LookBackPeriods)[0]) // Last 60 min bar closed above High WMA
{
Print("Enter Long. 10 min Close: "+ Closes[0][0] + " 10 min WMA High: " + WMA(High, LookBackPeriods)[0] + " 60 Min Close: " + Close[0] + " 60 Min WMA High: " + WMA(secondSeriesHigh, LookBackPeriods)[0] + " 60 Min WMA Med: " + WMA(secondSeriesMed, LookBackPeriods)[0] + " 60 Min WMA Low: " + WMA(secondSeriesLow, LookBackPeriods)[0]);
EnterLong(LongEntry, "EnterCloseHi");
}

// Condition set 2
if (BarsInProgress == 0 // added this to try to resolve anomolous exit
&& Position.MarketPosition == MarketPosition.Long // if we are flat
&& Close[0] < WMA(Low, LookBackPeriods)[0]) // Last bar closed below Low WMA
{
// ExitLong("Exit", "EnterCloseHi");
Print("Exit Long. 10 min Close: "+ Close[0] + " 10 min WMA Low: " + WMA(Low, LookBackPeriods)[0]);
ExitLong("", "");
}

// Condition set 2
if (BarsInProgress == 0) // added this to print values at each bar

{
Print("Print Bar. 10 min Close: "+ Close[0] + " 10 min WMA High: " + WMA(High, LookBackPeriods)[0] + " 60 Min Close: " + Closes[1][0] + " 60 Min WMA High: " + WMA(secondSeriesHigh, LookBackPeriods)[0] + " 60 Min WMA Med: " + WMA(secondSeriesMed, LookBackPeriods)[0] + " 60 Min WMA Low: " + WMA(secondSeriesLow, LookBackPeriods)[0]);
}

NinjaTrader_RyanM
06-01-2010, 11:43 AM
Hello ddurocher,

Thanks for the code.

Unfortunately we got a little off track and I apologize for any of your time used debugging. Resolving this can be done one of two ways.

1) Using the convention with DataSeries would require syncing per this reference sample:
http://www.ninjatrader.com/support/forum/showthread.php?t=3572

2) Your usage does not require creating custom series. A simpler solution may be to use the built in methods. You can pass Highs[1] directly into WMA to access the secondary series high. Same is true for Lows[1] or Medians[1].

WMA(Highs[1], 14);

ddurocher
06-01-2010, 04:14 PM
Haven't tried approach 1 yet, trying the second.

When I use Closes[1][0] I get the close not from the previous completed bar but from the bar before that. Same if I use WMA(Highs[1], 14)[0] - I get the WMA high not from the last completed bar but from the bar before that. Am I missing something obvious?

Thanks,

David

NinjaTrader_RyanM
06-01-2010, 04:23 PM
Hello David,

This probably has to do with your CalculateOnBarClose (http://www.ninjatrader-support.com/HelpGuideV6/CalculateOnBarClose1.html) settings. When set to true you will get the value of the last complete bar, not the in-process bar. To get the most recent values, change CalculateOnBarClose to false. You can hard code this property or change the setting whenever you run it.

ddurocher
06-02-2010, 05:54 AM
The problem is that I am not getting the values from the last complete bar. I am getting the bar before that, in the added time frame only.

After running this some more, this is some kind of timing issue.

For example, when I start a market replay, just after 9:30 I get my first print statement. In the primary time frame I am seeing the correct values for "Close" and "WMA High" from the last 10 minute bar of the previous day. In the added time frame I am getting values not from the last bar of the previous day, but from the 2nd last bar of the day. Then at 9:40 in my next print statement I am again seeing the correct values for my primary time frame, those from the first bar of the day. On the secondary time frame, now I am seeing the values for the last bar of the previous day.

This pattern continues on during the day ...

In testing this has caused some entries to occur 1 bar later than intended.

Thanks,

David

NinjaTrader_RyanM
06-02-2010, 08:19 AM
David, Please post the latest .cs file you're working with from Documents\NinjaTrader\bin\custom\strategy.

ddurocher
06-02-2010, 09:25 AM
The "posting rules" tell me I can post attachments but other than images and hyperlinks, I don't see how.

NinjaTrader_RyanM
06-02-2010, 09:45 AM
Dave,

Attachments are handled through individual posts. Click reply and when composing your post look for Manage Attachments button under Additional Options.

ddurocher
06-02-2010, 10:00 AM
Dave,

Attachments are handled through individual posts. Click reply and when composing your post look for Manage Attachments button under Additional Options.

I had to click on "Quote" to see those options. Reply just gave me the quick reply option which is apparently less "robust".

Maybe I'll add a smiley face while I'm here... :)

NinjaTrader_RyanM
06-02-2010, 11:03 AM
Hi David,

One thing I noticed from your strategy is that your secondary time frame is larger than the primary. This is important in version 6.5, but shouldn't matter as much in version 7.

You should modify your strategy so that secondary series is smaller than the primary series.

Please see this reference sample on Backtesting NinjaScript Strategies with an intrabar granularity (http://www.ninjatrader-support.com/vb/showthread.php?t=6652) for assistance on coding this. Below is the section that discusses the order of OnBarUpdate calls in a multi instrument strategy.

When working with multiple bar series objects it is important to understand the sequential order in which the
OnBarUpdate() method is triggered. The bars will always run with the primary first followed by the secondary and
so on.

Important: Primary bars will always execute before the secondary bar series.
If a bar is timestamped as 12:00PM on the 5min bar series, the call order between the equally timestamped 12:00PM
bar on the 1min bar series is like this:
12:00PM 5min
12:00PM 1min
12:01PM 1min
12:02PM 1min
12:03PM 1min
12:04PM 1min
12:05PM 5min
12:05PM 1min

ddurocher
06-02-2010, 11:30 AM
Sorry I'm a bit confused by your response. Just to be clear (if you could not tell from the cs) I am using version 6.5.

First you said my "secondary time frame is larger". Yes I agree, it is.

Then you said I should modify so that my "primary series" is"smaller" than the larger. Isn't it already???

I'm guesing you mean that my higher time frame, 30 min, should be my primary rather than the 10 min time frame? Also you seem to be suggesting that the way I have it now is preferable for 6.5?

NinjaTrader_RyanM
06-02-2010, 11:43 AM
Yes, you're right. Thanks for seeking clarification.

If you have two series, a 10 minute and 30 minute:

Your 30 minute series should be the primary series. This is the series you run the strategy against.
Your 10 minute series should be the secondary series. This is the series you add in the initialize method.

This is preferred when using version 6.5 due to the timing of bar objects.

ddurocher
06-03-2010, 07:23 AM
Ryan,

This looks promising after my initial tests on market replay data this morning. I had to do some tweaking as my entry and exit conditions are on my secondary timeframe now (as compared to the example with the entry condition on the primary time frame) but my entries, exits and all print statements are now looking like I envisioned.

Thanks,

David

NinjaTrader_RyanM
06-03-2010, 08:01 AM
Glad to hear, David. Thanks for the update.