Stopping a Buy Signal from Using an {EMPTY} Variable

I have some system code that is returning {EMPTY} for a slow 200 day moving address because a stock is fairly new and there were not enough bars for it to calculate the 200 day moving address. The only problem is it is giving me a buy signal. How can I exclude a variable from being used if it is null/empty? I tried using IsNull() and IsEmpty() but I couldn't get it to work. That stock is still showing up as a buy signal.

Code snippet of interest:

FastMA = EMA(C, 50);
SlowMA = EMA(C,200);

BuyRule1 = FastMA>SlowMA;

This is returning true when SlowMA == {EMPTY}

I tried this:

BuyRule1 = FastMA>SlowMA AND IsEmpty(SlowMA)!=1;

but that didn't work.

I also tried

BuyRule1 = FastMA>SlowMA AND IsEmpty(SlowMA)==0;

That stock is still showing up in my backtest for some reason on the same date.

I also tried:

BuyRule1 = FastMA>SlowMA AND Nz(IsEmpty(SlowMA),0)==0;

Thanks in advance.

I answered over support channel (by email):

You don't need to do ANYTHING because {Empty} or Null value when used in conditions or any expression produces Null to which is NOT a buy signal.
Null propagates through expressions. Any expression with Null component has value of Null.

BuyRule1 = FastMA > SlowMA; // this WILL NOT produce true if SlowMA is null.

Null is "NO VALUE" it is NOT a buy signal.

See:

  1. ValueWhen() and Null values
  2. In some cases, NULL values are reported as 0 (zero)

You may be misguided if you turned on "pad and align" in the Analysis settings, (and the symbol you are trying has short history but reference symbol has long history) and you are comparing with chart which obviously does not use padding. Just turn it OFF "Pad and align". Only this way you will get same output from chart and Analysis.

See also: How do I debug my formula?

And please read this:
http://www.amibroker.com/kb/2015/01/28/why-analysis-results-and-chart-output-may-differ/

2 Likes