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

[amibroker] Re: getting values from multiple stocks at once



PureBytes Links

Trading Reference Links

Dave
The AFL below uses ABTool plugin (the free one).  In AA set current 
stock to a stock with as few holes as possible (an Index is good) 
and set the desired range.  Set the sWatchlist to the watchlist 
containing the stocks you want to scan and the depth to rank (ie top 
10 stocks = 10).  Ranking calculation can be anything as long as it 
returns a Pscore eg WL_Close = Foreign( sTicker, "C",1);
	PScore = WL_Close; will give you top X closes etc.
Using Top 10 as an example, the ATC will contain the value of the 
10th highest rank value based on your ranking calculation.  To use 
the top 10 ranked stocks in another analysis, calculate the 
individual ranking again and compare to ATC using a foreign function 
(ie. buycond = iif(rank >= foreign("~top20ROC",C),1,0);) etc.  
Watch for line returns in the post upsetting comments.

If your intention is to backtest based on ranking Fred's Portfolio 
Trader does exactly that.  There is a slight learning curve but it 
worth the effort.
Andrew



//////////////////////////////////////////////////////////////////
//  Add To Composite RankValue based on Ranking calculation.
//////////////////////////////////////////////////////////////////

// ****************************************************************
// APPLY TO - CURRENT STOCK -  Set to an index.
// RANGE - FROM -  1997  TO - "Today",
// SCAN
// ****************************************************************


xxABtoolInit();
#pragma nocache

// #include "afl/AP_Functions.afl";

//////////////////////////////////////////////////////////////////
// 	     User Variables - enter here                       //
/////////////////////////////////////////////////////////////////
	sWatchlist = 0;  	// set to desired watchlist.
	Rank_No = 20;		// set the depth to rank to.

// ----------------------------------------------------------
// AddToComposite name
// ----------------------------------------------------------

ATCName = "~top20ROC";

//////////////////////////////////////////////////////////
// 		         Ranking Calculation                     //
//////////////////////////////////////////////////////////

function TradeRank(sTicker)
{
		WL_Close = Foreign( sTicker, "C",1);
		PScore = ROC(WL_Close,20);

return PScore;
}

//////////////////////////////////////////////////////////
//    End Ranking Calculation                           //
//////////////////////////////////////////////////////////

/////////////////////////////////////////////////////////////////
//            End User Variables                               //
/////////////////////////////////////////////////////////////////
	StartBar = LastValue( ValueWhen( Status("firstbarinrange"), 
BarIndex() ) );
	FinishBar = LastValue( ValueWhen( Status("lastbarinrange"), 
BarIndex() ) );	
	RankValue = 0;  // initialise Rank Value array
	List = GetCategorySymbols( categoryWatchlist, sWatchlist);
   // ---------------------------------------
   // Create Ranking Table
   // ---------------------------------------
   sRank = xxTableCreate();

   // Initialize Ranking Columns
   // Use loop to add columns to cover # of bars ranked.
   i = StartBar;
   while (i <= FinishBar)
   {
    xxTableColumnAdd("Ranking",     1, sRank);
    i = i + 1;
   }

	for (j=0; (sTicker = StrExtract( List,j)) != ""; j++)
		{
		Rank = TradeRank(sTicker);
		k = StartBar;
		i = 0;
		while (k <= Finishbar)
			{
			xxTableDataAddNum(Rank[k], j, i, sRank);
			i = i + 1;
			k = k + 1;
			}
		}

		// ----------------------------------
		// Sorting rank calculations
		// ----------------------------------
		
		k = StartBar;
		i = 0;
		while (k <= Finishbar)
			{
			xxTableSort(sRank, i, False, True); 
			RankValue[k] = xxTableDataGet(Rank_No-1, i, 
sRank);
			i = i + 1;
			k = k + 1;
			}

	// ---------------------------------------
	// clean up - delete srank table
	// ---------------------------------------		
	xxTableDelete(srank);

AddToComposite(rankvalue, ATCName, "x");

Buy=Sell=1;
Filter=1;
AddColumn(RankValue, "Rank value",1.0); 

		


------------------------ Yahoo! Groups Sponsor ---------------------~-->
Buy Ink Cartridges or Refill Kits for Your HP, Epson, Canon or Lexmark
Printer at Myinks.com. Free s/h on orders $50 or more to the US & Canada. http://www.c1tracking.com/l.asp?cid=5511
http://us.click.yahoo.com/l.m7sD/LIdGAA/qnsNAA/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/