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

Re: [amibroker] How to code For loops without refering to array elements



PureBytes Links

Trading Reference Links

Hello,
 
One correction - since you are after finding the Shortest one then the loop should
start with the SMALLEST period (not the largest as in your original formula).
There were couple of other minor things that needed correction.
 
Here is corrected one:
 
// this checks whether the price is above its moving av 52 bars ago
//AND if so checks whether the Close crossed mov av during the last 52
//bars for a range of moving av periods AND return the moing av period
//value when it crosses

function
CheckMACross( period, Lookback )
{
  result =
False
;

  Cma =
MA( C
, period );

  bar =
BarCount - 1
- Lookback;

  
if( Close
[ bar  ] < Cma[ bar ] )
  {
   
while( bar < BarCount
)
    {
      
if( Close
[ bar ] > Cma[ bar ] )
      {
         result =
True
;
      }

      bar++;
    }
  }

  
return
result;
}

function
WCBelowMALine()
{
 found =
False
;
 for ( period = 3; period <= 52 AND NOT
found; period++)
 {
    found = CheckMACross( period,
52
);   
 }
 return
period;
}


 

Best regards,
Tomasz Janeczko
amibroker.com
----- Original Message -----
Sent: Monday, March 27, 2006 1:50 PM
Subject: Re: [amibroker] How to code For loops without refering to array elements

Hello,
 
AFL offers actually superset of capabilities of "general purpose"
language like Pascal as it allows both "normal" (i.e. scalar)
constructs like Pascal AND array based operations (not available in Pascal).
 
It is UP TO YOU which one you use. There are NO limitations and
of course in your case j variabel CAN be used as moving average period.
 
The only problem is YOUR SKILL, not the language.
 
Correct way to code thing like this
"// this checks whether the price is above its moving av 52 bars ago
and if so checks whether the close crossed mov av during the last 52
bars for a range of moving av periods and return the moing av period
value when it crosses"
 
is ( I am showing ONE OF MANY possible ways to code this,
specifially I am showing the way how to code it WITHOUT REF() statement
just to show you how you can use subscript operator (as in Pascal))
 
// this checks whether the price is above its moving av 52 bars ago
//AND if so checks whether the Close crossed mov av during the last 52
//bars for a range of moving av periods AND return the moing av period
//value when it crosses

function
CheckMACross( period, Lookback )
{
  result =
False
;

  Cma =
MA( C
, period );

  bar =
BarCount - 1
- Lookback;

  
if( Close
[ bar  ] < Cma[ bar ] )
  {
   
while( bar < BarCount
)
    {
      
if( Close
[ bar ] > Cma[ bar ] )
      {
         result =
True
;
      }
    }
  }

  
return
result;
}

function
WCBelowMALine()
{
 found =
False
;
 for ( period = 52; j < 3 AND NOT
found; j--)
 {
   found = CheckMACross( j,
52
);   
 }
 return
result;
}


 

Best regards,
Tomasz Janeczko
amibroker.com
----- Original Message -----
Sent: Sunday, March 26, 2006 1:44 PM
Subject: [amibroker] How to code For loops without refering to array elements

Hi all

For up trending stoks, I want to find the shortest Simple moving
average period values that do not cross the price within any 52 bar
period for a portfolio of securities. Obviously different stocks
will have different period values at any given point in time and
also they will very with time within a stock.

I started coding the following.  But it seems the variables that we
can use in a For loop can only be used for Array element and not
the  way general computer languages like Pascal work. 

I wold appreciate if someone can help me to get this code to work -
it appears that the j variable cannot be used to increment the
moving av period !!

Thanks in advance



//==================================================
// this checks whether the price is above its moving av 52 bars ago
and if so checks whether the close crossed mov av during the last 52
bars for a range of moving av periods and return the moing av period
value when it crosses

function WCBelowMALine(C)
{
result = 0;
for (j=52; j<3; j--)
{
result = IIf(Ref(C,-52) < Ref(MA(C,j),-52), -1,IIf(Hold(Cross(MA
(C,J),C),52),j+1 ,9));
}
return result;
}

//==================================================


















Please note that this group is for discussion between users only.

To get support from AmiBroker please send an e-mail directly to
SUPPORT {at} amibroker.com

For other support material please check also:
http://www.amibroker.com/support.html





SPONSORED LINKS
Investment management software Real estate investment software Investment property software
Software support Real estate investment analysis software Investment software


YAHOO! GROUPS LINKS