[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[amibroker] Re: Difference between 0 and Null for variables?



PureBytes Links

Trading Reference Links

The difference is that zero is zero, while Null represents no value at
all. If you have an array full of zeroes and another full of Nulls
then plot both arrays on a chart, the array of zeroes will give a
horizontal line at $0 while the array of Nulls won't plot anything at all.

Typically Null is used in arrays that are to be plotted on a chart to
prevent anything being plotted at those bars. For example, the
function MA(C,30) has no data for the first 30 bars, so the array will
have 30 Nulls at the start which won't show up on a chart.

The mathematical properties of Null are different to zero too. I think
to simplify array maths, any operation involving Null gives a Null
result, that way you can use MA(C,30) in an equation and the first 30
bars will still end up with Null values no matter what the equation
is. This can cause unexpected problems in loops though, where you're
testing individual array values. In particular, comparison operations
involving Null also give a Null result, so if you have the array "m30
= MA(C,30)", then:

if (m30[0] > m30[1])

is equivalent to:

if (Null)

which is always treated as false. The tricky one is:

if (m30[0] == Null)

You'd expect this to be true, but it is again equivalent to

if (Null)

and is thus false. That's why there's a function IsNull() to allow you
to test for Null.

Another tricky loop scenario is where an array value is dependent on
the previous value, something like:

m30[i] = m30[i-1] * <etc>

If you run this in a loop from bar one, the Null value will propogate
through the whole array, possibly leaving you wondering why nothing
ever shows up when you plot the result.

In general, if you have a comparison where you want a Null value
treated a particular way, write it so that Null should give the false
condition rather than the true condition. For example, if you want to
test for Close being greater than the MA above, where it should return
True for the Null MA values (ie. the first 30 bars), then:

if (Close[i] > m30[i])
  <abc...>
else
  <def...>

won't work, as it will be treated as false when m30 is Null and
<def...> will be executed. However:

if (Close[i] <= m30[i])
   <def...>
else
   <abc...>

will work. Again it will be treated as false when m30 is Null, but
this time that means <abc...> will be executed as desired.

Hope this helps.

Regards,
GP


--- In amibroker@xxxxxxxxxxxxxxx, "ozzyapeman" <zoopfree@xxx> wrote:
>
> Tried to do a search in the User's Guide for this, but can't quite
> pinpoint an explanation:
> 
> What is the difference, if any, between 0 and Null when assigning
> non-boolean variables? When should we use Null instead of plain old 0?
> 
> For example, all of the six variables below are real numbers, yet some
> are initially set to Null and others to 0.
> 
> I copied this snippet from an example AFL that works properly:
> 
> 
> // Set up variables for our entry values, as our stops will test
> // against the initial entry prices
> 
> valueAtBuy = valueAtShort = Null;
> profitLevel = profitLevel2 = Null;
> 
> 
> // Number of open contracts
> 
> longContractCount  = 0;
> shortContractCount = 0;
> 
> for (i = start; i < BarCount; i++)
> {
>



------------------------------------

Please note that this group is for discussion between users only.

To get support from AmiBroker please send an e-mail directly to 
SUPPORT {at} amibroker.com

For NEW RELEASE ANNOUNCEMENTS and other news always check DEVLOG:
http://www.amibroker.com/devlog/

For other support material please check also:
http://www.amibroker.com/support.html
Yahoo! Groups Links

<*> To visit your group on the web, go to:
    http://groups.yahoo.com/group/amibroker/

<*> Your email settings:
    Individual Email | Traditional

<*> To change settings online go to:
    http://groups.yahoo.com/group/amibroker/join
    (Yahoo! ID required)

<*> To change settings via email:
    mailto:amibroker-digest@xxxxxxxxxxxxxxx 
    mailto:amibroker-fullfeatured@xxxxxxxxxxxxxxx

<*> To unsubscribe from this group, send an email to:
    amibroker-unsubscribe@xxxxxxxxxxxxxxx

<*> Your use of Yahoo! Groups is subject to:
    http://docs.yahoo.com/info/terms/