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

[amibroker] Re: question on i-j; in a loop



PureBytes Links

Trading Reference Links

You have not showed it in your code snippet. But, presumably 'i' is the
control variable for an outer loop

That being the case; Ignore all other logic and just try to understand
what the flow control statements are doing.

e.g.

for (i = 0; i < BarCount; i++) {  // Point 1
   ...
   for (j = i + delay; j < BarCount; j++) { // Point 2
     ...
     if (...) {  // Point 3
       i = j;    // Point 4
       break;    // Point 5
     }
     ...  // Point 6
   }
   ...  // Point 7
}
...  // Point 8

Point 1:
This is the outermost loop. The flow is as follows;
   First, initialize the control variable (i = 0).
   Second, evaluate the loop test (i < BarCount).
     a. If the loop test is true we execute the loop body.
     b. If the loop test is false the loop is finished.
   Third, after the loop body has been executed, update the control
variable (i++) and repeat the Second step.

We repeat the second and third steps until the loop is finished. In the
example above we are iterating through all the bars in the data, one
after the other.

Point 2:
This is an example of an inner (or 'nested' loop). The behavior is the
same as an outer loop. In this case the control variable is being
initialized to the current value of i plus an offset (i + delay). In
other words, we are not beginning at the first bar of the data. Rather,
we are continuing where i left off (plus an offset).

Point 3:
Here you have found a meaningful condition that you have been looking
for.

Point 4:
Given that your inner loop picked up where i left off (i.e. j was
initialized using the value of i when starting the inner loop) there is
no point in going over those bars again. As such, you are bumping up the
value of i to reflect where j left off (i.e. where j found the
meaningful condition of Point 3). This new value of i will be used in
the control flow of the next iteration of the outer loop. Think of it as
two shift workers performing the same job, each picks up where the other
left off.

Point 5:
The break statement means to consider the inner loop finished, even if
the loop test still thinks that the loop should continue. The break
statement has veto power over the loop test.

Point 6:
If the meaningful condition of Point 3 has not yet been found, then the
remainder of the inner loop body would execute as normal.

Point 7:
If there was any more code in the outer loop body that came after the
inner loop, it would be executed after the inner loop finished
(regardless of whether the inner looped finished due to a break or due
to a failed loop test).

Point 8:
If there was any more code after the outer loop, it would be executed
after the outer loop.

So, in your specific case; When you remove the i=j statement, the outer
loop will redundantly examine bars that the inner loop has already
processed. By leaving the statement in place, the combined efforts of
the two loops only examine the bars once.

Mike

--- In amibroker@xxxxxxxxxxxxxxx, zeek ing <zeeking57@xxx> wrote:
>
> Hello all,
> So I have been studying this loop over and over again and have a
problem. It
> is used for sigscaleout. I am not sure what i=j does in the code. I
should
> say why it does what it does. When I remove the i=j in the code, then
the
> chart plots multiple short signals for that short. WHen the i=j is
> put back in the code then only one signal is plotted. Kind of like
what
> EXREM does. MY question is WHY????
> can anyone help??
>
>
>
> if ( Short [ i ]  )
>
>     {
>
>         priceatshort = ShortPrice[ i ];
>
>         LowsinceShort = 0;
>
>         lowsinceshort = Min( Low[ i ], lowsinceshort );
>
>          shortAdjusted[ i ] = 1;
>
>         shortPriceAdjusted[ i ] = ShortPrice[ i ];
>
>         FirstProfitTargetArrayS[ i ] =  priceatshort -
FirstProfitTargetS;
>
>
>
>         for (j = i + delay; j < BarCount; j++)
>
>         {  LowsinceShort  = Min( Low[ j ], LowsinceShort  );
>
>             lowSinceshortArray[ j ] = lowsinceshort;
>
>             FirstProfitTargetArrayS[ j ] =  priceatshort -
> FirstProfitTargetS;
>
>
>
>             if( High[ j ] >= TrailStopArrayS[ j ] )
>
>             { Cover[ j ] = 1;
>
>                 CoverPrice[ j ] = Max( Open[ j ], TrailStopArrayS[ j ]
);
>
>
>
>                 i = j;
>
>                 break;   }
>
>
>
>             if( exitType == 0 AND Low[ j ]<= (priceatshort -
> FirstProfitTargetS) )
>
>             {
>
>               // first profit target hit - scale-out
>
>                 exitType = 1;
>
>                 ShortAdjustedScale[ j ] = sigScaleOut;
>
>                 ShortPriceAdjusted[ j ] = Min( Open[ j ],
(priceatshort -
> FirstProfitTargets) );
>
>                         }
>
>
>
>               if (j == BarCount - 1)
>
>             { i = BarCount; }
>
>
>
>         }
>
>     }
>




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

**** IMPORTANT PLEASE READ ****
This group is for the discussion between users only.
This is *NOT* technical support channel.

TO GET TECHNICAL SUPPORT send an e-mail directly to 
SUPPORT {at} amibroker.com

TO SUBMIT SUGGESTIONS please use FEEDBACK CENTER at
http://www.amibroker.com/feedback/
(submissions sent via other channels won't be considered)

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

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/