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

[amibroker] Exploration Help for a Neophyte



PureBytes Links

Trading Reference Links

I'm trying to combine two separate scans into an exploration that 
will give me:
     1.	stocks fitting into a nice up-trending linear regression    
channel and,
     2.	give me the relative strength of the sector for each of the  
selections.

I'm the furthest thing from a programmer and have shamelessly lifted 
the two programs below from the AFL Library (thanks to Pat Hargus & 
Brian Mitchell for posting the code below-would never have cranked 
that out myself)

My problem is-how do I get the Relative Strength program to take the 
IndustryID number and generate a RS value based on all the 198 
sectors in the QP3 universe (which I already have in a watchlist)?? 
Ideally, I would like to run the scan for stocks in a different 
watchlist and then sort the picks by the Sector RS value.  Since I 
use QP3 I would somehow have to "tell" the program to add "!ID" 
before the sector number.  

The (hijacked) code is below; hopefully the solution is simple (I've 
never managed to get my head around this ever-dreaded looping 
business). 

Many thanks in advance for any help rendered.

Chuck 



//                                                                   
         LR Scan


SetBarsRequired(10000,10000);
P = ParamField("Price field",-1);
Daysback = Param("Period for Linear Regression Line",50,1,100,1);
shift = Param("Look back period",0,0,100,1); 


//  =============================== Math 
Formula==============================================================
======

x = Cum(1);
lastx = LastValue( x ) - shift; 
aa = LastValue( Ref(LinRegIntercept( p, Daysback), -shift) ); 
bb = LastValue( Ref(LinRegSlope( p, Daysback ), -shift) ); 
y = Aa + bb * ( x - (Lastx - DaysBack +1 ) ); 
LRLine =  IIf( x > (lastx - Daysback) AND BarIndex() < Lastx, y, 
Null );



// ==========================  compute 1st SD 
Channel==============================================================
=

SDP = Param("Standard Deviation",1.5, 0, 6, 0.1);//Default=1.5
SD = SDP/2;

width = LastValue( Ref(SD*StDev(p, Daysback),-shift) );   // THIS IS 
WHERE THE WIDTH OF THE CHANELS IS SET  
SDU = IIf( x > (lastx - Daysback) AND BarIndex() < Lastx, y+width , 
Null ) ;
SDL = IIf( x > (lastx - Daysback) AND BarIndex() < Lastx, y-width , 
Null ) ;

//  ==========================  Compute 2d SD 
Channel==============================================================
==

SDP2 = Param("2d Standard Deviation",2.0, 0, 6, 0.1);//Default=2.0
SD2 = SDP2/2;

width2 = LastValue( Ref(SD2*StDev(p, Daysback),-shift) );   // THIS 
IS WHERE THE WIDTH OF THE CHANELS IS SET  
SDU2 = IIf( x > (lastx - Daysback) AND BarIndex() < Lastx, 
y+width2 , Null ) ;
SDL2 = IIf( x > (lastx - Daysback) AND BarIndex() < Lastx, y-
width2 , Null ) ;


// ============================ Buy/Sell 
Criteria=============================================================
=======


Uplimit=Close<SDU2;

Lolimit=Close>SDL2;

Slope=(Y-Ref(Y,-Daysback))/Ref(Y,-Daysback);

C1=Sum(Uplimit,Daysback)==Daysback;

C2=Sum(Lolimit,Daysback)==Daysback;

C3=MA(Volume,10)>80000;//Volume filter

C4=Slope>0.03;//Slope filter

C5=(Low>Ref(Low,-1)) OR (Close>Ref(Close,-1));//want price just 
starting up (today>yesterday)

C6=(C5 + Ref(Low, -2)>Ref(Low,-3)) OR  (Ref(Close,-2)>Ref(Close,-
3));//this line looks for 2 sequential days up.(knocks out ~ 15% of 
picks) 

Buy=C1 AND C2 AND C3 AND C4 AND C5 AND C6;

//Sell=0;

//========================================================Below is 
Sector Relative Strength 
code=================================================================
======

EnableRotationalTrading();
SetOption("WorstRankHeld", 5);
PositionSize = -100;
PositionScore = 0;
WatchlistNum = 1;//Default is 1
Filter=1;//default is 1;
NumColumns=0;

function CalculatePosition(st, Lt1, Lt2, Lt3, Lt4, Lt5, Lt6)
{
	score=0;

	if(st > Lt1) score++;
	if(st > Lt2) score++;
	if(st > Lt3) score++;
	if(st > Lt4) score++;
	if(st > Lt5) score++;
	if(st > Lt6) score++;
	return score;
}


// walk through the watchlist grabbing all the symbols to calculate 
RS vs ourself.
List =CategoryGetSymbols(categoryWatchlist, WatchlistNum);
for(i=0; (sym = StrExtract(List, i)) != "";i++)
{
	if(sym != Name())
	{
		f = RelStrength(sym);

		st3 = EMA(f, 3);
		st5 = EMA(f, 5);
		st8 = EMA(f, 8);
		st12 = EMA(f, 12);
		st15 = EMA(f, 15);

		Lt30 = EMA(f, 30);
		Lt35 = EMA(f, 35);
		Lt40 = EMA(f, 40);
		Lt45 = EMA(f, 45);
		Lt50 = EMA(f, 50);
		Lt60 = EMA(f, 60);

		z=BarCount - 1;
		// uncomment the following if you want to do some 
backtesting or if you like waiting around
		// a long time for the exploration to complete
		//for(z=0;z < BarCount;z++)
		{

		PositionScore[z] = PositionScore[z] + 
CalculatePosition(st3[z], Lt30[z],
Lt35[z], Lt40[z], Lt45[z], Lt50[z], Lt60[z]);

		PositionScore[z] = PositionScore[z] + 
CalculatePosition(st5[z], Lt30[z],
Lt35[z], Lt40[z], Lt45[z], Lt50[z], Lt60[z]);

		PositionScore[z] = PositionScore[z] + 
CalculatePosition(st8[z], Lt30[z],
Lt35[z], Lt40[z], Lt45[z], Lt50[z], Lt60[z]);

		PositionScore[z] = PositionScore[z] + 
CalculatePosition(st12[z], Lt30[z],
Lt35[z], Lt40[z], Lt45[z], Lt50[z], Lt60[z]);

		PositionScore[z] = PositionScore[z] + 
CalculatePosition(st15[z], Lt30[z],
Lt35[z], Lt40[z], Lt45[z], Lt50[z], Lt60[z]);

	}
}
}

//AddTextColumn(FullName(), "Name");
//AddColumn(PositionScore[BarCount - 1], "RS");


//===================================================================
=======Explore===============================

Filter=Buy;
NumColumns=12;
Column0Name="Name";
Column0=FullName();
Column1Name="Sumuplimit";
Column1=Sum(uplimit,Daysback);
Column2Name="SumLoLimit";
Column2=Sum(Lolimit,Daysback);
Column3Name="Close";
Column3=Close;
Column4Name="Slope";
Column4=Slope;
Column5Name="1YrProjEPSGrowth";
Column5=GetExtraData("Yr1ProjEPSGrowth");
Column6Name="Price/Earn Growth";
Column6=GetExtraData("PEG");
Column7Name="Yesterday's Proximity";
Column7=Low/Ref(SDL2,-1);
Column8Name="Todays Proximity";
Column8=Low/SDL2;
Column9Name="Close>50DMA";
Column9=Sum(Close>MA(50,Close),Daysback);
Column10Name="Industry Name";
Column10=IndustryID(1);
Column11Name="Industry #";
Column11=IndustryID(0);


//AddTextColumn(FullName(), "Name");
AddColumn(PositionScore[BarCount - 1], "RS");







------------------------ Yahoo! Groups Sponsor --------------------~--> 
Put more honey in your pocket. (money matters made easy).
http://us.click.yahoo.com/r7D80C/dlQLAA/cosFAA/GHeqlB/TM
--------------------------------------------------------------------~-> 

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

 
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/