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

[amibroker] Re: AFL formula for CCI



PureBytes Links

Trading Reference Links

Dave -

Was up early, so I tackled this.  If I understand what you're after, 
there is a way to do it with one loop using a "switch" to gate the 
lookbacks for each tick.  It depends on the MA() function taking a 
variable lookback (which I think may not be documented).

Here's the code with some test output.  For test, the CCIx lookback 
cycles between 20 and 24 and the normal CCI of those values are 
output for comparison to CCIx.  It matches with some roundoff 
allowance.

-- Bruce

// Generate a variable lookback that cycles 20,21,22,23,24,20,21 ....
base = 20;
period = Close - Close + base;
period = (period + (Cum(1) % 5));

// Get the max period for later
maxperiod = LastValue(Highest(period));

// This works because MA takes a variable lookback
SMATP = MA(Avg, period);

MD = Close - Close;
for(i = 0; i < maxperiod; i++) {
	// This is the key - only add terms where i < lookback for 
each tick
    MD = MD + abs(SMATP - Ref(Avg, -i)) * (i < period);
}
MD = MD / period;

CCIx = (Avg - SMATP) / (0.015 * MD);

// Output some stuff for proof
Filter = 1;
AddColumn(CCIx, "CCIx", 8.4);
AddColumn(base, "BASE", 5.0);
AddColumn(period, "PERIOD", 5.0);
AddColumn(CCI(base), "CCI(base)", 8.4);
AddColumn(CCI(base + 1), "CCI(base + 1)", 8.4);
AddColumn(CCI(base + 2), "CCI(base + 2)", 8.4);
AddColumn(CCI(base + 3), "CCI(base + 3)", 8.4);
AddColumn(CCI(base + 4), "CCI(base + 4)", 8.4);


--- In amibroker@xxxxxxxxxxxxxxx, "Dave Merrill" <dmerrill@xxxx> 
wrote:
> thanks tomasz, but it's still not working when I pass an array for 
the CCIx
> period, which is the end result I'm looking for. I still get the 
same error
> "Error 3. Condition in IF, WHILE, FOR statements has to be Numeric 
or
> Boolean type."
> 
> any further ideas?
> 
> here my actual code:
> 
> ==============
> function MeanDev(array, mean, range) {
>  result = 0;
>  for(i = range; i < BarCount; i++) {
>   result[i] = 0; // the mean is not 'moving' over the range 
(outside the
> loop)
>   tm = mean[i];
>   for(j = 0; j < range; j++) {
>    result[i] = result[i] + abs(array[i - j] - tm);
>   }
>   result[i] = result[i] / range;
>  }
>  return result;
> }
> function CCIx(period) {
>  SMATP = MA(Avg, period);
>  MD = MeanDev(Avg, SMATP, period);
>  result = (Avg - SMATP) / (0.015 * MD);
>  return result;
> }
> ==============
> 
> dave
> 
>   The mean deviation in CCI requires that SMATP does NOT move over 
the
> period.
>   In your
>   MD = Sum(Abs(SMATP - avg), period);
> 
>   SMATP is moving.
> 
>   You can not code itwith SUM or MA. You must use looping for that.
> 
> 
>   function MeanDev( array, mean, range )
>   {
>     result = 0;
> 
>      for( i = range ; i < BarCount; i++ )
>      {
>         result[ i ] = 0;
>         // the mean is not 'moving' over the range (outside the 
loop)
>         tm = mean[ i ];
>         for( j = 0; j < range; j++ )
>         {
>           result[ i ] = result[ i ] + abs( array[ i - j ] - tm );
>         }
> 
>       result[ i ] = result[ i ]/range;
>     }
> 
>     return result;
>   }
> 
>   n = 20;
> 
>   SMATP = MA(Avg,n);//1,2
> 
>   MD = MeanDev( Avg, SMATP, n );
> 
>   CCIx = (Avg - SMATP) / (0.015 * MD);//5,6,7
> 
>   Plot(CCIx,"CCIx",colorGreen,styleLine);
> 
>   Plot(CCI(n),"CCI",colorRed,styleLine);
> 
> 
>   Best regards,
>   Tomasz Janeczko
>   amibroker.com
>     ----- Original Message -----
>     From: Dave Merrill
>     To: amibroker@xxxxxxxxxxxxxxx
>     Sent: Sunday, October 12, 2003 7:24 AM
>     Subject: RE: [amibroker] AFL formula for CCI
> 
> 
>     thanks for jumping in graham.
> 
>     first, I think AB's Avg is (H+L+C)/3, so we don't need to 
calculated it
> again.
> 
>     second, I think the problem we're both having is the mean 
deviation
> code. my loop version sums, over 20 days, the absolute values of the
> difference between TODAY'S SMATP and the typical price of that day.
> 
>     this non-loop version is close, not the results it gives, but
> conceptually:
> 
>       MD = Sum(Abs(SMATP - avg), period);
> 
>     what's wrong I think is that for each day being summed, it uses 
the
> SMATP value for that day, same as it uses the avg value for that 
day. but as
> I said above, it needs the avg for each day, but the SMATP value 
used should
> always be from the current day, the one being calculated, not the 
value on
> each of the previous (Period) days.
> 
>     any more ideas? not just you graham, but anyone?
> 
>     dave
>       This isn't an exact match, I tried to check against the 
description in
> AB
>       help files but couldn't quite follow it.
>       I am missing something, just not certain what.
> 
>       TP = (H+L+C)/3;
>       SMATP = MA(TP,20);
>       MD = MA(abs(TP - SMATP),20);
>       CCIx = (TP - SMATP) / (0.015 * MD);
> 
>       Plot(CCIx,"CCIx",colorGreen,styleLine);
>       Plot(CCI(20),"CCI",colorRed,styleLine);
> 
>       Cheers,
>       Graham
>       http://groups.msn.com/ASXShareTrading
>       http://groups.msn.com/FMSAustralia
> 
>       -----Original Message-----
>       From: Dave Merrill [mailto:dmerrill@x...]
>       Sent: Sunday, 12 October 2003 12:22 PM
>       To: amibroker@xxxxxxxxxxxxxxx
>       Subject: [amibroker] AFL formula for CCI
> 
> 
>       re the earlier discussion of using arrays as paremters to 
built-in
>       indicators, I need a version of the CCI that can take an 
array for its
>       period.
> 
>       here's a description of how the 20-period CCI is calculated, 
from
>       StockCharts.com:
>         1) Calculate today's Typical Price (TP) = (H+L+C)/3 where H 
= high;
> L =
>       low, and C = close.
>         2) Calculate today's 20-day Simple Moving Average of the 
Typical
> Price
>       (SMATP).
>         3) Calculate today's Mean Deviation. First, calculate the 
absolute
> value
>       of the difference between today's SMATP and the typical price 
for each
> of
>       the past 20 days. Add all of these absolute values together 
and divide
> by 20
>       to find the Mean Deviation.
>         4) The final step is to apply the Typical Price (TP), the 
Simple
> Moving
>       Average of the Typical Price (SMATP), the Mean Deviation and a
> Constant
>       (.015) to the following formula:
>             CCI = (Typical Price - SMATP) / (.015 x Mean Deviation)
> 
>       according to my tests, this matches the built-in CCI exactly:
>         MD = 0;      // MD is Mean Deviation
>         for(i = 0; i < period; i++) {
>           MD = MD + Abs(SMATP - Ref(avg, -i));
>         }
>         MD = MD / period;
> 
>       can anyone see how to do the same thing with AFL array 
processing
> instead of
>       the loop?
> 
>       thanks,
> 
>       dave
> 
> 
> 
>       Send BUG REPORTS to bugs@xxxx
>       Send SUGGESTIONS to suggest@xxxx
>       -----------------------------------------
>       Post AmiQuote-related messages ONLY to: 
amiquote@xxxxxxxxxxxxxxx
>       (Web page: http://groups.yahoo.com/group/amiquote/messages/)
>       --------------------------------------------
>       Check group FAQ at:
>       http://groups.yahoo.com/group/amibroker/files/groupfaq.html
> 
>       Your use of Yahoo! Groups is subject to
> http://docs.yahoo.com/info/terms/
> 
> 
> 
>       Send BUG REPORTS to bugs@xxxx
>       Send SUGGESTIONS to suggest@xxxx
>       -----------------------------------------
>       Post AmiQuote-related messages ONLY to: 
amiquote@xxxxxxxxxxxxxxx
>       (Web page: http://groups.yahoo.com/group/amiquote/messages/)
>       --------------------------------------------
>       Check group FAQ at:
> http://groups.yahoo.com/group/amibroker/files/groupfaq.html
> 
>       Your use of Yahoo! Groups is subject to the Yahoo! Terms of 
Service.
> 
> 
> 
>     Send BUG REPORTS to bugs@xxxx
>     Send SUGGESTIONS to suggest@xxxx
>     -----------------------------------------
>     Post AmiQuote-related messages ONLY to: amiquote@xxxxxxxxxxxxxxx
>     (Web page: http://groups.yahoo.com/group/amiquote/messages/)
>     --------------------------------------------
>     Check group FAQ at:
> http://groups.yahoo.com/group/amibroker/files/groupfaq.html
> 
>     Your use of Yahoo! Groups is subject to the Yahoo! Terms of 
Service.
> 
> 
>         Yahoo! Groups Sponsor
>               ADVERTISEMENT
> 
> 
> 
> 
>   Send BUG REPORTS to bugs@xxxx
>   Send SUGGESTIONS to suggest@xxxx
>   -----------------------------------------
>   Post AmiQuote-related messages ONLY to: amiquote@xxxxxxxxxxxxxxx
>   (Web page: http://groups.yahoo.com/group/amiquote/messages/)
>   --------------------------------------------
>   Check group FAQ at:
> http://groups.yahoo.com/group/amibroker/files/groupfaq.html
> 
>   Your use of Yahoo! Groups is subject to the Yahoo! Terms of 
Service.


------------------------ Yahoo! Groups Sponsor ---------------------~-->
Rent DVDs Online - Over 14,500 titles.
No Late Fees & Free Shipping.
Try Netflix for FREE!
http://us.click.yahoo.com/JYdFFC/XP.FAA/ySSFAA/GHeqlB/TM
---------------------------------------------------------------------~->

Send BUG REPORTS to bugs@xxxxxxxxxxxxx
Send SUGGESTIONS to suggest@xxxxxxxxxxxxx
-----------------------------------------
Post AmiQuote-related messages ONLY to: amiquote@xxxxxxxxxxxxxxx 
(Web page: http://groups.yahoo.com/group/amiquote/messages/)
--------------------------------------------
Check group FAQ at: http://groups.yahoo.com/group/amibroker/files/groupfaq.html 

Your use of Yahoo! Groups is subject to http://docs.yahoo.com/info/terms/