Calculate (average) price range of the day over x days

Questions about MultiCharts and user contributed studies.
evdl
Posts: 401
Joined: 19 Jan 2011
Location: Netherlands
Has thanked: 85 times
Been thanked: 124 times

Calculate (average) price range of the day over x days

Postby evdl » 04 Jul 2012

I like to calculate the price range of the day, and this for every day, x days back. And then calculate an average price range over x days. The price range = highest trade - lowest trade.

This to determine the price variation of a symbol. And you can use this to adjust your stoploss and profit target.

I came up with this code (little bit borrowed here and there ;)). It looks like it does not get the highest and lowest values (I compared this to Yahoo finance historical data). What am I doing wrong?

Code: Select all

Inputs:
plot_right_corner(true),
print_indicator(false),
filename("C:\users\evdl\documents\" + "price range " + getsymbolname + ".txt"),
TicksOffSetTop(25),
Text_FontSize(12),
Text_FontName("Lucida Sans Typewriter"),
Text_DisplayBorder(True),
Text_BGColor(black),
Text_FontColor(white);

Variables:
count(0),
average_pricerange(0),
txtstr_average_pricerange(""),
txtstr_day_pricerange(""),
closeday(0),
highofday(0),
lowofday(0),
price_range(0),
init(true),
textstring(""),
dateformat_open(""),
oneMinute(eltimetodatetime(1)),
headerStr(""),
BoxStr(""),
textObj(0);

Array:
day_pricerange[](0);

{========== end of declarations ==========}

if date > date[1] then begin

// increment the count at the beginning of a new day
count = count +1;

highofday = highd(0);
lowofday = lowd(0);
price_range = highofday - lowofday;
closeday = closed(1);
dateformat_open = formatdate("dd-MM-yyyy", eldatetodatetime(date));

// expand the array by one
Array_setmaxindex(day_pricerange, count);

// post data to array
day_pricerange[count] = price_range;

// calculate the average day gap (price range)
average_pricerange = averagearray(day_pricerange, count);

end;

// Plotting data in text box in right corner of the chart-----------------------------------------
If plot_right_corner = true then begin

// Text in de textbox
txtstr_day_pricerange = text("Price range", price_range);
txtstr_average_pricerange = text("Average price range", average_pricerange);

Headerstr = Text((getsymbolname),(close-closeday), newline, NewLine);
Boxstr = Text(txtstr_day_pricerange, newLine,
txtstr_average_pricerange);

// Create textbox
once begin
textobj = Text_new_s(juliantodate(getappinfo(airightdispdatetime)),
Getappinfo(airightdispdatetime) - oneminute,
getappinfo(aihighestdispvalue) + ((Minmove / PriceScale) * ticksOffSetTop),
Text(headerStr));

// Textbox formatting
value81 = Text_SetStyle(TextObj, 1, 0);
value82 = Text_SetFontName(TextObj, Text_FontName);
value84 = Text_SetBorder(TextObj, Text_DisplayBorder);
value85 = Text_SetSize(TextObj, Text_FontSize);
value86 = Text_SetBGColor(TextObj, Text_BGColor);
value87 = Text_SetColor(TextObj, Text_FontColor);

end;

// Update the textbox location and values
value90 = Text_SetLocation_s(TextObj,
JulianToDate(GetAppInfo(airightDispDateTime)),
datetime2eltime_s(GetAppInfo(airightDispDateTime) - oneMinute),
GetAppInfo(aihighestDispValue) - ((MinMove / PriceScale) * TicksOffSetTop));

value91 = Text_SetString(TextObj, Text(HeaderStr, boxStr));
end;

{----------------------------End text box ------------------------------}

// print to file
if print_indicator then begin

// only with activating indicator start print to file
If init then begin
init = false; {prevents indicator from printing to file more than once}
FileDelete(filename);
Fileappend(filename,"Datum; High; Low; Price range; Avg price range"+ Newline);
end;

// Text formatting in file
if date <> date[1] then begin

textstring = dateformat_open + ";";
textstring = textstring + numtostr(highofday,3) + ";";
textstring = textstring + numtostr(lowofday,3) + ";";
textstring = textstring + numtostr(price_range,3) + ";";
textstring = textstring + numtostr(average_pricerange,3) + ";";
Fileappend(filename, textstring + newline);
end;
end;

User avatar
JoshM
Posts: 2195
Joined: 20 May 2011
Location: The Netherlands
Has thanked: 1544 times
Been thanked: 1565 times
Contact:

Re: Calculate (average) price range of the day over x days

Postby JoshM » 04 Jul 2012

I like to calculate the price range of the day, and this for every day, x days back. And then calculate an average price range over x days. The price range = highest trade - lowest trade.

This to determine the price variation of a symbol. And you can use this to adjust your stoploss and profit target.

I came up with this code (little bit borrowed here and there ;)). It looks like it does not get the highest and lowest values (I compared this to Yahoo finance historical data). What am I doing wrong?
What kind of output did you get that 'doesn't look good'? Is the chart you applied this indicator to also based on Yahoo data (with correct session settings)? Have you used print() statements to check if the HighD and LowD functions work correctly with the parameter specified?

If I may suggest I think it would be a good idea to "dumb down" the code to the basics (so no textbox and file export yet) and then build on it, with Print() statements to check if everything works as expected.

For example:

Code: Select all

Variables:
highOfDay(0), lowOfDay(99999);

if (BarStatus(1) = 2) then begin

if (SessionLastBar = True) then begin

Print(FormatDate("dd-MM-yyyy", ELDateToDateTime(Date)),
" - Daily high: ", NumToStr(highOfDay, 2),
" Daily low: ", NumToStr(lowOfDay, 2));

highOfDay = 0;
lowOfDay = 999999;

end; //: Last tick of day

highOfDay = MaxList(highOfDay, High);
lowOfDay = MinList(lowOfDay, Low);

end; //: OnBarClose
After testing that segment you can add the dynamic array, add the output of that to the Print statement to check, and then calculate the average.

(Don't get me wrong, I'm not saying that this how you should do it. I'm merely saying this because, looking from your code example, you might got a little sidetracked with all the code before checking the basics.)

:)

SP
Posts: 465
Joined: 06 Feb 2006
Has thanked: 36 times
Been thanked: 286 times

Re: Calculate (average) price range of the day over x days

Postby SP » 04 Jul 2012

Do you use intraday charts ? Your code only calculates the range of the first bar of the day with
"if date > date[1] then begin...", not the range of the complete day.

Also keep in mind that the array size of HighD and LowD is limited to 50 days back.

evdl
Posts: 401
Joined: 19 Jan 2011
Location: Netherlands
Has thanked: 85 times
Been thanked: 124 times

Re: Calculate (average) price range of the day over x days

Postby evdl » 04 Jul 2012

Yes, I use intraday charts (1 minute data). After the tip from JoshM, I started to do only the basics.

I noticed that if I use date = currentdate, the highd and lowd is working but only for the current day. The other days on the chart are not used. If I use date > date[1], I got values that are sometimes near the highd and lowd values (probably open of the day and the last close)

So it can be indeed that the values I got are the highd and lowd of only the firstbars. How can I code it, that it will look at the range of the complete day and also get the price ranges of x days back?

User avatar
JoshM
Posts: 2195
Joined: 20 May 2011
Location: The Netherlands
Has thanked: 1544 times
Been thanked: 1565 times
Contact:

Re: Calculate (average) price range of the day over x days

Postby JoshM » 04 Jul 2012

So it can be indeed that the values I got are the highd and lowd of only the firstbars. How can I code it, that it will look at the range of the complete day and also get the price ranges of x days back?
I showed you a code example in post #2 that didn't used HighD and LowD and thus doesn't have that limitation -- and is better suited for your goal. Why then want to use HighD and LowD? :)

evdl
Posts: 401
Joined: 19 Jan 2011
Location: Netherlands
Has thanked: 85 times
Been thanked: 124 times

Re: Calculate (average) price range of the day over x days

Postby evdl » 04 Jul 2012

Thanks JoshM and SP. With both your inputs I got the code to work.

Here is the working code, to calculate the current price range and the average price range of all the days that are on the chart (without the limitation of 50 days for highd and lowd).

Code: Select all

Inputs:
plot_right_corner(true),
print_indicator(false),
filename("C:\users\evdl\documents\" + "price range " + getsymbolname + ".txt"),
TicksOffSetTop(25),
Text_FontSize(12),
Text_FontName("Lucida Sans Typewriter"),
Text_DisplayBorder(True),
Text_BGColor(black),
Text_FontColor(white);

Variables:
count(0),
price_range(0),
average_pricerange(0),
closeday(0),
highofday(0),
lowofday(0),
highofday_trigger(0),
lowofday_trigger(0),
txtstr_average_pricerange(""),
txtstr_day_pricerange(""),
init(true),
textstring(""),
dateformat_open(""),
dateformat_close(""),
timeformat_starttime(""),
timeformat_endtime(""),
oneMinute(eltimetodatetime(1)),
headerStr(""),
BoxStr(""),
textObj(0);

Array:
day_high[] (0),
day_low[] (0);

{========== end of declarations ==========}

closeday = closed(1);
highofday = highd(0);
lowofday = lowd(0);
dateformat_open = formatdate("dd-MM-yyyy", eldatetodatetime(date));
highofday_trigger = 0;
lowofday_trigger = 99999;

If (barstatus(1) = 2) then begin

// increment the count at the beginning of a new day
count = count + 1;

// expand the array by one
Array_setmaxindex(day_high, count);
Array_setmaxindex(day_low, count);

// post data to array
day_high[count] = maxlist(highofday_trigger,highd(0));
day_low[count] = minlist(lowofday_trigger, lowd(0));

// calculate the day price range
price_range = day_high[count] - day_low[count];

// calculate the average day price range
average_pricerange = average(price_range, count);

// print statements
{Print("Date: ",formatdate("dd-MM-yyyy ",eldatetodatetime(date)),
" high ", day_high[count],
" low ", day_low[count],
" pricerange ", price_range,
" average price range", average_pricerange,
"/", count:0:0, " bars");}

end;

// Plotting data in textbox at the right on the chart-------------------------------------------------
If plot_right_corner = true then begin

// Text in the box
txtstr_day_pricerange = text("Price range", price_range);
txtstr_average_pricerange = text("Average price range", average_pricerange);

Headerstr = Text((getsymbolname),(close-closeday), newline, NewLine);
Boxstr = Text(txtstr_day_pricerange, newLine,
txtstr_average_pricerange);

// Create textbox
once begin
textobj = Text_new_s(juliantodate(getappinfo(airightdispdatetime)),
Getappinfo(airightdispdatetime) - oneminute,
getappinfo(aihighestdispvalue) + ((Minmove / PriceScale) * ticksOffSetTop),
Text(headerStr));

// Textbox formatting
value81 = Text_SetStyle(TextObj, 1, 0);
value82 = Text_SetFontName(TextObj, Text_FontName);
value84 = Text_SetBorder(TextObj, Text_DisplayBorder);
value85 = Text_SetSize(TextObj, Text_FontSize);
value86 = Text_SetBGColor(TextObj, Text_BGColor);
value87 = Text_SetColor(TextObj, Text_FontColor);

end;

// Update the textbox location and values
value90 = Text_SetLocation_s(TextObj,
JulianToDate(GetAppInfo(airightDispDateTime)),
datetime2eltime_s(GetAppInfo(airightDispDateTime) - oneMinute),
GetAppInfo(aihighestDispValue) - ((MinMove / PriceScale) * TicksOffSetTop));

value91 = Text_SetString(TextObj, Text(HeaderStr, boxStr));
end;

{----------------------------End text box ------------------------------}

// print to file
if print_indicator then begin

// Only print to file when applying indicator (only updates new values to file at barinterval
// With the setting in the inputs are "false" it stops printing updates also.
If init then begin
init = false; {prevents the indicator from printen all the time}
FileDelete(filename);
Fileappend(filename,"Datum; High; Low; Price range; Avg price range"+ Newline);
end;

// Text formatting in file
If (barstatus(1) = 2) then begin

textstring = dateformat_open + ";";
textstring = textstring + numtostr(day_high[count],3) + ";";
textstring = textstring + numtostr(day_low[count],3) + ";";
textstring = textstring + numtostr(price_range,3) + ";";
textstring = textstring + numtostr(average_pricerange,3) + ";";
Fileappend(filename, textstring + newline);
end;
end;
Use at own risk ;).

Greetings Edwin.

evdl
Posts: 401
Joined: 19 Jan 2011
Location: Netherlands
Has thanked: 85 times
Been thanked: 124 times

Re: Calculate (average) price range of the day over x days

Postby evdl » 04 Jul 2012

Just one usability comment: When using the code with a couple of years of minute data in my case, it can take some time to calculate the values.

If you only want to have these values for analysis purpose (and get it to a excel file), you best can use a higher timeframe (for example 30 minutes in the symbol settings of the chart). The average values will not differ from the minute timeframe, but it is processed much quicker.


Return to “MultiCharts”