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;