IF Condition Not Executed While Exploring

I am working a condition where the target will be calculated based on a variable value

Variable is DTRORB

dtr_orb=(orbdiff/newdayopen)*100;

The issue is that the if condition is never executed for the case where the variable dtr_orb>1.23

Although I can see the value of the variable >1.23 if put a definitive exploration.

if(SelectedValue(dtr_orb)>1.23)
{
Filter=Buy Or Sell
}

if(SelectedValue(dtr_orb)<1.23)
{
Filter=Condition1 Or Condition2
}

It always goes in the if loop for (dtr_orb)<1.23 and never in the (dtr_orb)>1.23, even if the value is > 1.23 (from the exploration results)

Could you please guide what could be a resolution for such a case.

dtr_orb is a numeric value

Firstly, you should plan all the test cases.
The first unhandled scenario arises when dtr_orb == 1.23. Atleast one of the if() should handle it >= or <=.

The above can be handled with a 3rd block using else

Secondly, you should explain why you are using SelectedValue() in Exploration, as it is equivalent to LastValue() in AA Exploration.
and, if dtr_orb is of type numeric, then what is the use SelectedValue() or LastValue() ?

How are you checking it? but I suspect this method isn't the same as what the value is being returned by SelectedValue()
Code an ELSE block, so that instead of assuming you are sure that you handle all scenarios.
You may easily be getting a NULL value on the last bar, or a #DIV/0 etc which would be the cause.

// dtr_orb as you say is of type Numeric
if( dtr_orb > 1.23 ) {

}
else if ( dtr_orb < 1.23 ) {

}
else {
    // Where you can handle anomalies
    // including when dtr_orb == 1.23
}

The best approach.

Edit: You can still clarify if you mean Numeric in context of NOT ARRAY or an array of numerals. The variable in your post is still ambiguous.

You are doing this incorrectly.

Filter is array variable and exploration outputs results based on last call of Filter (formula is getting processed from top to bottom). That's why in your code you get results only when dtr_orb < 1.23 as it is in last (bottom) if statement.

Now, proper way for getting getting check for both conditions (and no matter if dtr_orb is array or not) are these examples (using IIf function):

dtr_orb = (orbdiff/newdayopen)*100;

// if dtr_orb > 1.23 then ... else if dtr_orb <= 1.23 then ...
FilterCond = IIf( dtr_orb > 1.23, Buy OR Sell, Condition1 Or Condition2);

Filter = FilterCond AND Status("lastbarinrange");

bkColor = IIf( dtr_orb > 1.23, colorDarkGreen, colorDarkRed);
AddColumn( dtr_orb, "dtr_orb", 1.2, colorLightGrey, bkColor );

Or if you want to get no output or different output if dtr_orb is equal to 1.23 then

dtr_orb = (orbdiff/newdayopen)*100;

FilterCond = IIf( dtr_orb > 1.23, Buy OR Sell, IIf( dtr_orb < 1.23, Condition1 Or Condition2, 0 ));

Filter = FilterCond AND Status("lastbarinrange");

bkColor = IIf( dtr_orb > 1.23, colorDarkGreen, IIf( dtr_orb < 1.23, colorDarkRed, colorBlack));
AddColumn( dtr_orb, "dtr_orb", 1.2, colorLightGrey, bkColor );

As you can see in both examples I am adding Status("lastbarinrange") to Filter to output at last bar of selected range. If you do want to output for entire history then remove AND Status("lastbarinrange") from Filter.


If you want to output something based on SelectedValue e.g. getting datetime element at last bar of selected range e.g. in column header...

/// code of before...

dtSelect_str = DateTimToStr( SelectedValue(DateTime) );
bkColor = IIf( dtr_orb > 1.23, colorGreen, colorRed);
AddColumn( dtr_orb, "dtr_orb @" + dtSelect_str, 1.2, colorLightGrey, bkColor );

Are you sure it is single number but not array? This name newdayopen rather suggests it to be an array (e.g. if it is something like newdayopen = ValueWhen(newday, Open); ).

So since you seem to be unsure (and I do not know as you did not add full code) please check type of dtr_orb via typeof operator yourself. E.g. trace log output

_TRACE( "dtr_orb is " + typeof( dtr_orb ) );

No, you are wrong. It is not equivalent. SelectedValue in AA returns last bar of selected AA range.

4 Likes

I meant Filter in your example looks to be array type (because of Buy/Sell).
Of course Filter can be type number or array depending on what is assigned to.

If Filter variable is type number and

  1. if that number assigned to Filter is a number not being zero then exploration outputs results for all bars of set analysis range. Examples:
    Filter = 1;,
    Filter = True;,
    Filter = -1;,
    Filter = LastValue(C);,
    Filter = LastValue(Buy);// if sig of last bar of array is true,
    Filter = SelectedValue(Buy);//if sig of last bar of selected range is true,
    etc...
  2. If the number assigned to Filter is number being equal to zero then there is not any result list output (if not having added any AddRow lines but just AddColumn ones). Examples:
    Filter = 0;,
    Filter = False;,
    Filter = LastValue(Buy);// if sig of last bar of array is false,
    Filter = SelectedValue(Buy);// if sig of last bar of selected range is false,
    etc...

If Filter variable is type array and

  1. if it is not zero then
    1.1. if it is array returning true(1) / false(0) (e.g. Filter = MACD() > 0; ) then exploration outputs results where array condition is true.
    1.2. if it is an array not changing between true and false for entire analysis range (e.g. Filter = Close; and Close being not zero) then exploration outputs results for that entire set analysis range. So then it is resulting in same output as if using Filter = 1;.
  2. If Filter array is equal to zero for entire analysis range (e.g. Filter = Close - Close; or Filter = RSI() > 200;, ... etc.) then there is not any result output (if not having added any AddRow lines but just AddColumn ones). So results are the same ones as if using Filter = 0;.

As for how to create exploration please refer to this link. It has several Filter examples.

2 Likes

Thank you so much for your guidance, the variable dtr_orb is of Array type.

Thank you for your guidance, shall go through the documentation as suggested.