Problem with aiHighestDispValue  [SOLVED]

Questions about MultiCharts and user contributed studies.
PD Quig
Posts: 191
Joined: 27 Apr 2010
Location: San Jose
Has thanked: 67 times
Been thanked: 10 times

Problem with aiHighestDispValue

Postby PD Quig » 26 Apr 2014

I wrote a simple indicator to test plotting a value in the upper left hand corner of the chart and opened a can of worms. It keeps driving a floating point error.

Here's the code:

Code: Select all

{************************************************************************************************************************************************************
inputs and variable initialization section
************************************************************************************************************************************************************}

[RecoverDrawings = false];

inputs:
SignalBarStopBuffer (0.5), { an incremental buffer amount beyond the initial calculated stop }
PositionSize (2),
Text_Color (white),
Font_Size (14);

variables:
intrabarpersist Risk_Plot_Vert (0),
intrabarpersist Risk_Plot_Horz (0),
intrabarpersist Text_Risk (0),
Risk (0),
Text_Risk_str ("");


{*************************************************************************************************************************************************************
risk calculation
*************************************************************************************************************************************************************}

Risk = round(((absvalue(high - low) + SignalBarStopBuffer) * PositionSize * 100) + (PositionSize * 2 * (10 + 2.01)),0);


{*************************************************************************************************************************************************************
plot section
*************************************************************************************************************************************************************}

Risk_Plot_Vert = round(GetAppInfo(aiHighestDispValue), 1); { gets the top of the chart screen }
// Risk_Plot_Vert = high + 5;
Risk_Plot_Horz = CalcTime(DateTime2ELTime(GetAppInfo(aiLeftDispDateTime)), 5); { gets the leftmost bar on the chart }

Text_Risk_str = "Risk: $" + numtostr(Risk,0);

if LastBarOnChart then once begin { initialize text }

Text_Risk = Text_New(Date,Risk_Plot_Horz,Risk_Plot_Vert - 1.0,Text_Risk_str);
Text_SetColor(Text_Risk,Text_Color);
Text_SetSize(Text_Risk,Font_Size);
Text_SetAttribute(Text_Risk,1,true);
Text_SetStyle(Text_Risk,0,1);
end;

{ update text string value and location }

begin

Text_SetString(Text_Risk, "Risk: $" + numtostr(Risk,0));
Text_SetLocation(Text_Risk, date, Risk_Plot_Horz, Risk_Plot_Vert - 1.0);
end;

When I comment out the line of the plot section that contains aiHighestDispValue and substitute the line immediately below, all is good. Then, when I comment out the substitute line and re-compile, it still works--with the plot now in the upper left corner as desired.

The problem occurs when I turn the indicator off and then on again. The error floating point error gets thrown for the indicator and an error also gets thrown for the (completely unrelated) signal as well. At that point the only way to continue is to close and re-open the workspace.

I have used the aiHighestDispValue function in a number of other code streams without encountering this problem. Thanks in advance for any help.

PDQ
Attachments
After_turning_signal_on.png
(108.4 KiB) Downloaded 503 times
After_cycling_indicator.png
(151.66 KiB) Downloaded 505 times
Risk_2.png
(76.66 KiB) Downloaded 499 times
Risk_1.png
(78.04 KiB) Downloaded 492 times

User avatar
TJ
Posts: 7740
Joined: 29 Aug 2006
Location: Global Citizen
Has thanked: 1033 times
Been thanked: 2221 times

Re: Problem with aiHighestDispValue  [SOLVED]

Postby TJ » 26 Apr 2014

Try this:

Code: Select all


if LastBarOnChart then
Risk_Plot_Vert = round(GetAppInfo(aiHighestDispValue), 1);

PD Quig
Posts: 191
Joined: 27 Apr 2010
Location: San Jose
Has thanked: 67 times
Been thanked: 10 times

Re: Problem with aiHighestDispValue

Postby PD Quig » 27 Apr 2014

Thanks, TJ. That did it. Not sure why I got away with not using the LastBarOnChart conditional in the past, but I'll use it going forward.

PDQ

User avatar
TJ
Posts: 7740
Joined: 29 Aug 2006
Location: Global Citizen
Has thanked: 1033 times
Been thanked: 2221 times

Re: Problem with aiHighestDispValue

Postby TJ » 27 Apr 2014

ps.

on your "initialize text" section,
you do not need the LastBarOnChart,
because you only need to run the initialization once during loading of the indicator.
Removing the LastBarOnChart should make your code run a bit faster.

PD Quig
Posts: 191
Joined: 27 Apr 2010
Location: San Jose
Has thanked: 67 times
Been thanked: 10 times

Re: Problem with aiHighestDispValue

Postby PD Quig » 27 Apr 2014

thanks for that, too.

User avatar
TJ
Posts: 7740
Joined: 29 Aug 2006
Location: Global Citizen
Has thanked: 1033 times
Been thanked: 2221 times

Re: Problem with aiHighestDispValue

Postby TJ » 27 Apr 2014

thanks for that, too.
You are welcome.
Good trading to you.


Return to “MultiCharts”