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

[amibroker] Re: Normalized Rank Code - Problem, but almost there


  • To: amibroker@xxxxxxxxxxxxxxx
  • Subject: [amibroker] Re: Normalized Rank Code - Problem, but almost there
  • From: "acesheet" <acesheet@xxxxxxxxx>
  • Date: Sat, 20 Dec 2003 10:43:54 -0800
  • In-reply-to: <bs1v8e+iu99@xxxxxxxxxxx>

PureBytes Links

Trading Reference Links

Bruce and all,

Here's an interesting code that I believe works very well. The 
procedure is to run the first 'setup' part as a Scan in AA. The 
second part is a 10 point curve fit of the Cumulative Probability 
Distribution which can be used as a Relative Strength Rank. You can 
run an Exploration on a WL or Group to find high RS stocks.

Here's the first part:
//----------------------------------------------------
//-------- CPD Data Collector, v1.0 ------------------
//-------- Baseline ROC Data for Normalized ----------
//-------- Ranks based on Cum Prb Distr (CPD) --------
//----------------------------------------------------
// -ace, 12/20/03
//----------------------------------------------------
// Can only be used on current day for the moment, although
// you could probably get away with using each distribution 
// for a longer period of time.
//----------------------------------------------------
// AA Settings:
// 1) 'Current Stock'
// 2) Select a stock or index with more data bars than the
//    total number of stocks in the sample group. So if you are
//    using 3000 stocks make sure the current Ticker has at least
//    3001 days of data in it.
// 3) Set 'n last quotations' where n=1.
// 4) Scan. It might take a couple of minutes depending upon your
//    computer and the size of your WL or Group
//-----------------------------------------------------------
// First seed the temporary variable for ROC(C,260) with the ROC's 
// of all the stocks in in Group0 (or whatever group you want)
//-----------------------------------------------------------
// Set your category here:
list = CategoryGetSymbols( categoryGroup, 0 ); 
ROC260=Null;
x=Null;
y=Null;m=0;
Count=0;
AddToComposite(y,"~yrank","C");
AddToComposite(x,"~xroc","C");
for( i = 0; ( sym = StrExtract( list, i ) ) != ""; i++ )
{
   Val = Nz(Foreign( sym, "Close" ));
   n=Nz(IIf(Val!=0,1,0));
   Count=Count+n;
   ROC260[i+1]=LastValue(ROC(Val,260));
}
k=(BarCount-1-LastValue(Count));
roc260=Ref(ROC260,-k+1); // This is correct
//AddToComposite(ROC260,"~roc260","C",1); // troubleshoot
//AddToComposite(Count,"~roc260count","C",1); // troubleshoot
//-----------------------------------------------------------
// First Loop i goes through percentile ranks in 
// increments of 10 from 1 to 99
// x = the ROC(C,260) that corresponds to rank y 
// y = % Rank in 10% Increments
//-----------------------------------------------------------
m=LastValue(Count);
for( i = 0; i<=10; i++ )
{    
   y[i]=i*10;
   if(i<1) y[i]=1;
   if(i>9) y[i]=99;
   x[i]=LastValue(Percentile( ROC260, m-1, y[i] ));
}
k=BarCount-1-11;
//-----------------------------------------------------------
// Shift values to BarCount-1 and record them
//-----------------------------------------------------------
AddToComposite(Ref(y,-k),"~yrank","C",1);
AddToComposite(Ref(x,-k),"~xroc","C",1);
//-----------------------------------------------------------
// I'm trying to store x & y in arrays to use for later screens.
// The (x,y) pair should roughly represent the cumulative probability
// of the database
//-----------------------------------------------------------

Buy=0;
// End of the data collector
************************************************

//----------------------------------------------------
//-------- RS Rank 10pt Curve Fit, v1.0 --------------
//-------- Calc'd with data collected ----------------
//----for CPD using ROC(C,260) for category sym's ----
//----------------------------------------------------
// -ace, 12/20/03
//----------------------------------------------------
// AA Settings:
// 1) Set your filter to whatever you like
// 2) I suggest setting 'n last quotations', n=1
// 3) Run an Exploration
//----------------------------------------------------
// Can only be used on current day for the moment, although
// you could probably get away with using each distribution 
// for a longer period of time.
//----------------------------------------------------
// Retrieve the CPD data (11 points)
//----------------------------------------------------
x=Foreign("~xroc","C");
y=Foreign("~yrank","C");
//----------------------------------------------------
// Determine slope and intercept for 10 line segments
//----------------------------------------------------
m=0;
b=0;
// There are 10 points on the curve
k=BarCount-1-11;
for( i = BarCount-1-11; i<BarCount-1; i++ )
{
  m[i]=(y[i+1]-y[i])/(x[i+1]-x[i]); // Slope
  b[i]=y[i+1]-m[i]*x[i+1];          // Intercept
}
//----------------------------------------------------
// z is the sample data for comparison to the database
// n this case today's ROC(C,260)
//----------------------------------------------------
z=LastValue(ROC(C,260));
//----------------------------------------------------
// Assign RS based on curve fit.
// rs is the relative strength rank from CPD data
//----------------------------------------------------
rs=IIf(z<x[k],1,
  IIf(z>=x[k] AND z<x[k+1],m[k]*z+b[k],
  IIf(z>=x[k+1] AND z<x[k+2],m[k+1]*z+b[k+1],
  IIf(z>=x[k+2] AND z<x[k+3],m[k+2]*z+b[k+2],
  IIf(z>=x[k+3] AND z<x[k+4],m[k+3]*z+b[k+3],
  IIf(z>=x[k+4] AND z<x[k+5],m[k+4]*z+b[k+4],
  IIf(z>=x[k+5] AND z<x[k+6],m[k+5]*z+b[k+5],
  IIf(z>=x[k+6] AND z<x[k+7],m[k+6]*z+b[k+6],
  IIf(z>=x[k+7] AND z<x[k+8],m[k+7]*z+b[k+7],
  IIf(z>=x[k+8] AND z<x[k+9],m[k+8]*z+b[k+8],
  IIf(z>=x[k+9] AND z<x[k+10],m[k+9]*z+b[k+9],99)))))))))));
//----------------------------------------------------
// Find all stocks in the group that outperformed 90% 
// of the sample group
//----------------------------------------------------
Filter= rs>90;

AddColumn(rs,"CPD Rank",3.0);
AddColumn(ROC(C,260),"ROC",3.0);
//----------------------------------------------------
// Just for kicks this shows you the roc data (10 pts)
// so you can visually mak sure the AFL script is doing 
// what you think it should be doing. Comment it out 
// if you don't care to check it.
//----------------------------------------------------
/*
AddColumn(x[k]," 1",3.0);
AddColumn(x[k+1],"10",3.2);
AddColumn(x[k+2],"20",3.2);
AddColumn(x[k+3],"30",3.2);
AddColumn(x[k+4],"40",3.2);
AddColumn(x[k+5],"50",3.2);
AddColumn(x[k+6],"60",3.2);
AddColumn(x[k+7],"70",3.2);
AddColumn(x[k+8],"80",3.2);
AddColumn(x[k+9],"90",3.2);
AddColumn(x[k+10],"99",3.2);
*/

I used a 260 day ROC for the data in this calculation, you could set 
it up for any period you like and really any property you like or 
run several and combine them. I think this opens up a whole new way 
of ranking your possible trades. At the very least it gives one a 
way to completely normalize them.

Enjoy.

-ace




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 

Yahoo! Groups Links

To visit your group on the web, go to:
 http://groups.yahoo.com/group/amibroker/

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/