When to define numeric variables with -.999 and .999

Questions about MultiCharts and user contributed studies.
tonyt
Posts: 48
Joined: 21 May 2021
Has thanked: 20 times
Been thanked: 1 time

When to define numeric variables with -.999 and .999

Postby tonyt » 02 Sep 2022

Hello code gurus,

Can someone please explain why it is necessary or good practice to define variables using -.99999 and .99999 when calling them in a for loop or as trade signals?
Please see the following code snipets:

Code: Select all

Value1=AvgTrueRange(ADRdays); value2=-999999.; end; End; {now add the stops} if marketposition=1 then begin stop1a= EntryPrice-(stoplossADR*Value1); if stop1a > value2 then Begin value2=stop1a; stoptype="StopLoss";

Code: Select all

hh = -99999999999; ll = 99999999999; if nSess>1 then begin for value90 = 1 to nSess begin hh = maxlist(hh,ohlcvalues[1+value90*4]); ll = minlist(ll,ohlcvalues[2+value90*4]); end; end
In the first example, it seems that stop1a will always be greater than '-.999999' so that Value2 is redundant. Why do I keep seeing it in code? Is it there to prevent to prevent incorrect entries?

Any guidance is appreciated. Thank you!

User avatar
rrams
Posts: 128
Joined: 10 Feb 2011
Location: USA
Has thanked: 7 times
Been thanked: 70 times
Contact:

Re: When to define numeric variables with -.999 and .999

Postby rrams » 04 Sep 2022

In both examples the use of '-.999999' for the initial value of a variable is because the greater than condition needs to evaluate to true THE FIRST TIME in order to seed that variable. Then each new bar iteration replaces that variable with a new value only if it is greater than the previous. So, value2 is not redundant.

Did the author need to use large integers instead of just using zero? Well EntryPrice-(stoplossADR*Value1) can evaluate to a negative number so just comparing it to zero to start isn't correct. You do need to make sure your code can tolerate bad numbers for the MarketPosition, EntryDate, etc when running in realtime, but that is not why '-.999999' was used in this case.

Is it good practice? Not if it is confusing. Both code snipets can be re-written to avoid '-.999999'.


Return to “MultiCharts”