View Full Version : Create a Moving Average for Daily Range
netjms
04-17-2009, 10:28 AM
I am trying to write an indicator that will automatically calculate the current days range and create a 7 day moving average.
I have found CurrentDayOHL and with this can easily calculate the range for the current day as of the last bar, however, I am having trouble creating the 7-day moving average.
Thanks for helping the novice.
NinjaTrader_Josh
04-17-2009, 10:41 AM
netjms,
To get prior day's information you will need to use PriorDayOHLC.
netjms
04-17-2009, 02:09 PM
Ok. Reading about the PriorDayOHLC will get me the prior day's information, but how would I go beyond that to create a 7-day moving average?
Thanks
NinjaTrader_Josh
04-17-2009, 02:12 PM
netjms,
You have to do the math in your code. Get the values you want for the 7 days and then run an average calculation. Then you have the information you want.
netjms
04-17-2009, 02:21 PM
I am ok with doing the math in code to calculate the 7-day average, what I don't understand is how retrieve the data past the prior day; consider the following...
I use CurrentDayOHL to get current day information, then use PrioDayOHLC to get yesterday's information. I now have two of the 7 years values in which to caculate a moving average.
How do I go beyond yesterday's values? Do I need to manipulate "barsago" or is there additional informaiton I am not getting?
Thanks
netjms
04-17-2009, 02:22 PM
sorry I meant '...7 days" not 7 years.
NinjaTrader_Josh
04-17-2009, 02:27 PM
That is right. You need to manipulate the bars ago value for PriorDayOHLC().
For instance, let us just say that 10 bars = 1 day. On the most recent bar accessing PriorDayOHLC() with index anywhere between 0 and 10 will give you yesterday's values. When you access index between 11 and 20 you get the day before that. 21 and 30 gets you yet another day earlier. etc. etc.
You can make it easier on yourself by using GetBar() to give you which [] index to use to get a certain date.
netjms
04-24-2009, 01:02 PM
Ok. Back to this indicator. I have coded the following. I put the initial code in the initialize section because it really only needs to run 1 time when the indicator starts (is this the correct location?). However, I am having a problem with (what I think) GetBar. The code is returning 0 however, I have validated the barDate variable via print statements. I am entering a time of 16:00 hours because I want to get the daily range at the end each day in the loop. See the contents of the output window below too. In addition to returning zero, it appears the code stops (?) because I get no more output.
Any input? Thanks!
protectedoverridevoid Initialize()
{
CalculateOnBarClose = true;
Overlay = false;
PriceTypeSupported = false;
//get start date to loop through
DateTime startdate = DateTime.Now.Subtract(TimeSpan.FromDays(6));
Print ("Start Date " + startdate);
//get the prior 6 days highs & lows
for (double i = 0; i < 6; i++)
{
DateTime barDate = startdate.AddDays(i);
Print ("Bar Date " + barDate + " " + i);
Print (barDate.Year + " " + barDate.Month + " " + barDate.Day);
int barsAgo = GetBar(new DateTime(barDate.Year, barDate.Month, barDate.Day, 16, 0, 0));
Print ("barsago is " + barsAgo);
double dayrange = PriorDayOHLC().PriorHigh[barsAgo] - PriorDayOHLC().PriorLow[barsAgo];
Print ("Day ranage is " + dayrange);
RangeTotal = ++dayrange;
}
}
protectedoverridevoid OnBarUpdate()
{
double dayrange = CurrentDayOHL().CurrentHigh[0] - CurrentDayOHL().CurrentLow[0];
double avgrange = (RangeTotal + dayrange)/7;
Print("7-day Average Daily Range is " + avgrange);
double stoploss = avgrange * 1.0;
double target = avgrange * 1.5;
DrawTextFixed("avgdayrange","Average Daily Range = " + avgrange + "\r\n" +
"Stop Loss = " + stoploss + "Profit Target = " + target,TextPosition.TopLeft);
}
netjms
04-24-2009, 01:03 PM
Forget the ouput window contents...
Start Date 4/18/2009 3:03:36 PM
Bar Date 4/18/2009 3:03:36 PM 0
2009 4 18
Start Date 4/18/2009 3:03:36 PM
Bar Date 4/18/2009 3:03:36 PM 0
2009 4 18
barsago is 0
NinjaTrader_Josh
04-24-2009, 01:54 PM
netjms,
You should not place any logic in Initialize(). Please move it to OnBarUpdate(). If you only want it to execute once put in within the context of if (CurrentBar == 0).
Ralph
04-24-2009, 01:55 PM
...I am having a problem with (what I think) GetBar. ...
You are correct, netjms. GetBar() is a runtime-method and therfore should be executed in OnBarUpdate().
Regards
Ralph
netjms
04-24-2009, 07:11 PM
Thanks guys for your help. Moving the code and making come changes worked. However, I receive a 0 value for barAgo. I have reviewed the reference material online and cannot understand why. I have verified the barDate values are correct (I even hardcoded to a specific date and still got zero). My chart data has more then two weeks of data. Any ideas?
protectedoverridevoid OnBarUpdate()
{
if (CurrentBar == 0)
{
//get start date to loop through
DateTime startdate = DateTime.Now.Subtract(TimeSpan.FromDays(6));
//get the prior 6 days highs & lows
for (double i = 0; i < 6; i++)
{
DateTime barDate = startdate.AddDays(i);
int barsAgo = GetBar(new DateTime(barDate.Year, barDate.Month, barDate.Day, 16, 0, 0)); double dayrange = PriorDayOHLC().PriorHigh[barsAgo] - PriorDayOHLC().PriorLow[barsAgo];
RangeTotal = ++dayrange;
}
}
Ralph
04-25-2009, 12:47 AM
Although CurrentBar == 0 is the correct place to initiate runtime dependent things, you can't access historical data (barDate) here. At this point in time there simply is no history available yet. You need to pass your 6 days, before you can access the history of 6 days ago.
Regards
Ralph
netjms
04-25-2009, 06:35 PM
I think I understand however, I have more then 6 days of history on display in the chart. I was able to print data(to the output window) from OnBarUpdate for all the days of data in the chart (roughly 2+ weeks).
Are you saying I cannot access history in the OnBarUpdate area?
Thanks
netjms
04-25-2009, 07:57 PM
Never mind my last post. I re-read Ralph's post and I understand. Thanks
evensteven
04-26-2009, 05:48 PM
I've already asked this and you guys who are working on it know a lot more than I, but are you able to develop a price range indicator similar to the tick count whereby once the next price bar opens you can be shown a small box or something that shows a range number (1, 3, 3.5, etc. thinking ES), for the past 20 bars or so. My thinking is if I know the recent price bar range on the ES has been 3pts then it may help me with entry and stop planning. Does that make sense or even possible - or perhaps that is what you guys are working on and I don't understand it. That's highly possible. Thank you for your efforts.