Accepting the use of a loop, here is a variation ala the original,
that works as intended, but without using a function:

{
        RangeLoopDemo_01.afl

        by Progster, responding to:  
http://finance.groups.yahoo.com/group/amibroker/message/134007

}

RangeTest = Optimize("RangeTest", 1, 1, 4, 1);

Array = Close;          //      choose an array for division into ranges

// ActualRange = 0;

//      Set some range division values.
RDV1 = 3000;
RDV2 = 3500;
RDV3 = 4000;
RDV4 = 4500;

//      Note:  Range1-4 are ARRAYS, not Scalars
Range1 = IIf(Array > 0 AND Array <=  RDV1, 1, 0);
Range2 = IIf(Array > RDV1 AND Array <= RDV2, 1, 0);
Range3 = IIf(Array > RDV2  AND Array <= RDV3, 1, 0);
Range4 = IIf(Array > RDV4, 1, 0);

//      Tracing an array is invalid, unless you are in a loop and setting
indexes
/*
_TRACE( "Array: " + NumToStr(Array, 1.1, 0) ) ;
_TRACE( "Range1: " + NumToStr(Range1, 1.0, 0) ) ;
_TRACE( "Range2: " + NumToStr(Range2, 1.0, 0) ) ;
_TRACE( "Range3: " + NumToStr(Range3, 1.0, 0) ) ;
_TRACE( "Range4: " + NumToStr(Range4, 1.0, 0) ) ;
*/

//      This works as intended (but somewhat slow)
for( idx = 0; idx < BarCount; idx++ ) 
{ 
        //      These traces work.  Uncomment to see it.
        /*
        _TRACE( "Array[" +  NumToStr(idx, 1.0) + "]: " + NumToStr(Array[idx],
1.2, 1) ) ;
        _TRACE( "Range1[" +  NumToStr(idx, 1.0) + "]: " +
NumToStr(Range1[idx], 1.2, 1) ) ;
        _TRACE( "Range2[" +  NumToStr(idx, 1.0) + "]: " +
NumToStr(Range2[idx], 1.2, 1) ) ;
        _TRACE( "Range3[" +  NumToStr(idx, 1.0) + "]: " +
NumToStr(Range3[idx], 1.2, 1) ) ;
        _TRACE( "Range4[" +  NumToStr(idx, 1.0) + "]: " +
NumToStr(Range4[idx], 1.2, 1) ) ;
        */

        if (Range1[idx] == 1) ActualRange[idx] = 1;
        if (Range2[idx] == 1) ActualRange[idx] = 2;
        if (Range3[idx] == 1) ActualRange[idx] = 3;
        if (Range4[idx] == 1) ActualRange[idx] = 4;
        
        // _TRACE( "ActualRange[" +  NumToStr(idx, 1.0) + "]: " +
NumToStr(ActualRange[idx], 1.0, 1) ) ;

}       

Short = Cover = 0 ;
  
Buy = (ActualRange == RangeTest) ;

Sell = BarsSince(Buy) > 3;




--- In [email protected], "progster01" <progs...@...> wrote:
>
> I think that (at least part of) the issue with the original code is that
> 
>         Range1 = Array > 0       AND Array <= 1.1000;
>         Range2 = Array > 1.1000  AND Array <= 1.2000;
>         Range3 = Array > 1.2000  AND Array <= 1.3000;
>         Range4 = Array > 1.3000;
> 
> Range1-4 are actually ARRAYS, but are subsequently treated as if they
> were Scalars.
> 
> In my testing so far, treating an ARRAY like a scalar seems to
> silently use the last value of the ARRAY - leading to no end of
> confusion if you don't catch the original error in the first place!
> 
> 
> --- In [email protected], "ozzyapeman" <zoopfree@> wrote:
> >
> > Hello, hoping someone can point out the general flaw in logic
here. Even
> > though I've been working with AFL for six months now, array vs scalars
> > can still be confusing. All I'm trying to do is pass an array to a
> > function that tests it's range. The program  then sets a Buy according
> > to that range. An optimization is  performed to find the "best" range
> > for a given period.
> > 
> > Of course this is not my actual trading system, but merely a test of
> > concept for much a more complicated function.
> > 
> > By definition, the Close price has to fall into one of the four ranges
> > defined in the function. I am using Forex, but any symbol will do, and
> > would fall into one of the four ranges. Therefore, running the
> > Optimization should generate some trades, as the Buy condition will be
> > true eventually, as we cycle through the "RangeTest" variable for each
> > bar. But no trades are generated.
> > 
> > Traces indicate that the Close array is not being cycled through.
> > 
> > Shouldn't the following code work, without having to get into a
Barcount
> > loop? If not, where is the flaw? Any help much appreciated.
> > 
> > 
> > RangeTest = Optimize("RangeTest", 1, 1, 4, 1);
> > 
> > 
> >      function RangeFind(Array)
> >      {
> >          ActualRange = 0;
> > 
> >          Range1 = Array > 0       AND Array <= 1.1000;
> >          Range2 = Array > 1.1000  AND Array <= 1.2000;
> >          Range3 = Array > 1.2000  AND Array <= 1.3000;
> >          Range4 = Array > 1.3000;
> > 
> >          for( n = 1; n <=4; n++)
> >          {
> > 
> >            RangeN = VarGet( "Range"+ NumToStr(n, 1.0,0) );
> > 
> >            if (RangeN) ActualRange = n;
> > 
> >          }
> > 
> >        return ActualRange;
> >      }
> > 
> > 
> > Buy = RangeFind(Close) == RangeTest;
> > 
> > Sell = BarsSince(Buy) > 6;
> >
>


Reply via email to