Problem with IIF and OR for trading a specific hour

Hi

I am doing a algo that only will trade specific hours (that is, not "between" 00 - 06, but rather 00, 01, 05 and 06).

The problem is that my IIF function only gives back the first of my "OR" hours. The code is;

initial = 100000;
SetOption( "InitialEquity", initial );

lev1 =  1;
lev = 1 / lev1 ;
MarginDeposit = 100000 * lev;

SetOption( "PriceBoundChecking", False );
SetOption( "ActivateStopsImmediately", True );

SetPositionSize( initial  , spsValue );

SetOption( "CommissionMode", 3 );
SetOption( "CommissionAmount", 3.7 );
spread_ic  = 0.01;

Hour_ok = IIf( Hour() == (
                   00
                   OR 01
                   OR 02
                   OR 02
                   OR 03
                   OR 04
                   OR 05
                   OR 06), 1, 0 );


t1 =  10;
t2 =  3;
t3 =  200;

tp_m = 1;
sl_m = 0.5;

bb_t = Ref( BBandtop( c, t1, t2 ), -1 );
bb_b = Ref( BBandBot( c, t1, t2 ), -1 );
e_ma = Ref( eMA( C, t3 ) , -1 );

SetTradeDelays( 0, 0, 0, 0 );
Buy =  h > bb_b AND
       o < bb_b AND
       bb_b > e_ma   AND
       Hour_ok == 1;

BuyPrice = bb_b + spread_ic ;
Sell =  Null;

short = l < bb_t AND
        O > bb_t AND
        bb_t < e_ma   AND
        Hour_ok == 1;

ShortPrice = bb_t - spread_ic;

Cover = Null; 

ApplyStop( stopTypeProfit, stopModePercent, tp_m, 1, 1, 1, 0 );
ApplyStop( stopTypeLoss, stopModePercent, SL_m , 1, 1, 1, 0 );

This code will only trade at Hour 01, and not at 02 - 06. I assume it is something wrong with the "OR" statement, but I can't figure out what.. Any ideas where to start to investigate?

I solved it, but it would be interesting to know why... the solution is;

Hour_ok = IIf( 
Hour() == 1
OR Hour() == 2
OR Hour() == 3
OR Hour() == 4
OR Hour() == 5
OR Hour() == 6
, 1, 0 );

It works, but it would be interesting if someone could elaborate somewhat on why the above code works, and the original doesn't.

OR is a logical operator that returns True when either of one of its arguments is true. In your first example, the innermost parentheses contain:

00
OR 01
OR 02
...
OR 06

Since OR is assuming you are passing logical arguments, this is equivalent to:

False
OR True
OR True
...
OR True

The result of that is True, which is to say 1. Moving out one set of parentheses, you are checking whether Hour() is equal to 1.

Your second solution correctly combines logical conditions: Hour() == X.. However, the IIf() is unnecessary, so you could simply use:

Hour_ok = Hour() == 1
          OR Hour() == 2
          OR Hour() == 3
          OR Hour() == 4
          OR Hour() == 5
          OR Hour() == 6;
4 Likes

This topic was automatically closed 100 days after the last reply. New replies are no longer allowed.