The test code below is supposed to initialize two arrows above price on the last bar of a Renko chart. Ultimately the objective is to be able to move either arrow and have the code pick up data from the bars that they point to and perform other calculations.
Instead, after a random number of bars (it probably isn't random but I haven't been able to figure out a pattern) the two arrows are replotted on the latest bar. The previous arrows are still visible on the chart until I click on the chart (see file attached below).
Also, I am using a "once begin" to clear the print log, and this block of code is being entered more than once. When I comment out the ClearPrintLog block, it appears that instead of bar number incrementing by one, the bar number jumps backward and then resumes incrementing (see output file attached). Also, the LastBarOnChart_s value keeps jumping between FALSE and TRUE.
As a final bizarre clue, if I comment out the "Up_Bar_Low" variable assignment line, the code works as I expected--the arrows plot once and only once and I can move them around and retrieve the bar number they point to.
I must be missing something obvious.
Code: Select all
{****************************************************************************
Name : $Test_Arrow_Plotter_s
Description :
Last Modified Date : 10/28/2014 -- test placing arrows above or below bars to get data from bars
*****************************************************************************}
{ system parameter section }
[RecoverDrawings = false];
[LegacyColorValue = false];
{*****************************************************************************
variable initialization section
*****************************************************************************}
variables:
Up_Arrow_Color (blue),
Down_Arrow_Color (red),
Up_Arrow (0),
Down_Arrow (0),
intrabarpersist Down_Bar_High (0),
intrabarpersist Up_Bar_Low (0),
intrabarpersist Up_Arrow_Bar (0),
intrabarpersist Down_Arrow_Bar (0),
intrabarpersist ID_Arw_Up (0),
intrabarpersist ID_Arw_Down (0),
ID (0);
{******************************************************************************
plot initial arrows
******************************************************************************}
if LastBarOnChart_s then once begin
{ initialize arrows above the last bar on the chart }
Up_Arrow = Arw_New_s(date, time_s, close + 2, false);
Arw_SetSize(Up_Arrow, 7);
Arw_SetColor(Up_Arrow, Up_Arrow_Color);
Arw_SetStyle(Up_Arrow, 10);
Down_Arrow = Arw_New_s(date, time_s, close + 3, true);
Arw_SetSize(Down_Arrow, 7);
Arw_SetColor(Down_Arrow, Down_Arrow_Color);
Arw_SetStyle(Down_Arrow, 10);
end;
{*****************************************************************************
find location of arrows
*****************************************************************************}
begin
ID = Arw_GetFirst(1); { study-drawn arrows only }
while ID > 0 begin
if Arw_GetColor(ID) = Up_Arrow_Color then begin { up arrow }
ID_Arw_Up = ID;
Up_Arrow_Bar = Arw_GetBarNumber(ID);
end;
if Arw_GetColor(ID) = Down_Arrow_Color then begin { down arrow }
ID_Arw_Down = ID;
Down_Arrow_Bar = Arw_GetBarNumber(ID);
end;
ID = Arw_GetNext(ID, 1); { study-drawn arrows only }
end;
end;
Up_Bar_Low = low[(BarNumber - Up_Arrow_Bar)]; { assign low of up bar to variable }
{*************************************************************************
diagnostic print section
*************************************************************************}
once begin
ClearPrintLog;
end;
Print(
"Bar Number= ", (BarNumber + MaxBarsBack),
" time_s= ", time_s,
" Up_Arrow_Bar= ", Up_Arrow_Bar + MaxBarsBack,
" Down_Arrow_Bar= ", Down_Arrow_Bar + MaxBarsBack,
" LastBarOnChart_s= ", LastBarOnChart_s
);