First of all, I would like to express my gratitude to @Peppe, @RocketPower, @Tomasz, and a few other Amibroker experts. I would like to repost the exercise I learned from you all for the purpose of studying, which involves ranking by total value and then further sorting by IBD. I kindly ask for your help in reviewing and correcting my exercise so that I can improve my learning.
I sincerely appreciate your support.
My code is below :
#pragma sequence(scan,explore)
InputWatchlistNum = GetOption( "FilterIncludeWatchlist" );
list = CategoryGetSymbols( categoryWatchlist, InputWatchlistNum );
ListQty = StrCount(list, ",") + 1;
if (Status("action") == actionScan)
{
if( Status( "stocknum" ) == 0 )
{
StaticVarRemove( "rank*" );
StaticVarRemove( "value*" );
StaticVarRemove( "r*" );
for( n = 0; ( symbol = StrExtract( List, n ) ) != ""; n++ ) {
SetForeign( symbol );
value = C * MA(Ref(V,-1),20) ;
RestorePriceArrays();
StaticVarSet( "value" + symbol, value ); // write ranked values to a static variable
}
StaticVarGenerateRanks( "rank", "value", 0, 1224 );
}
if( Status( "stocknum" ) == 0 )
{
StaticVarRemove( "RS_*" );
StaticVarRemove("RSraw_*");
StaticVarRemove("RankRSraw*_");
for( n = 0; ( symbol = StrExtract( List, n ) ) != ""; n++ )
{
SetForeign (Symbol,0);
RSraw = 0;
// relative strength IBD style
ThreeMthRS = 0.4*(C/Ref(C,-5));
SixMthRS = 0.2*(C/Ref(C,-(2*5)));
NineMthRS = 0.2*(C/Ref(C,-(3*5)));
TwelveMthRS = 0.2*(C/Ref(C,-(4*5)));
RSraw = ThreeMthRS + SixMthRS + NineMthRS + TwelveMthRS;
RestorePriceArrays();
StaticVarSet("RSraw_" + Symbol, RSraw,True);
RSraw = StaticVarGet ("RSraw_" + Name());
}
StaticVarGenerateRanks("Rank", "RSraw_", 0, 1224);
if( Status( "stocknum" ) == 0 )
for (n = 0; (Symbol = StrExtract(list, n)) != ""; n++)
{
Rank = StaticVarGet ("RankRSraw_" + Symbol);
rankvalue = StaticVarGet("rankvalue"+symbol);
RSpctile = IIf(rankvalue <=500, 100 - 100*Rank/ListQty,-10000);
StaticVarSet("RS_" + Symbol, RSpctile, True);
}
}
_exit(); // Exit nothing to do
}
symbol = Name();
RankC = StaticVarGet ("RS_" + Name());
_SECTION_BEGIN( "VIX" );
Period = Param("Period (days)", 30, 10, 100, 1);
AnnualFactor = sqrt(252);
Price = Close;
Returns = log(Price / Ref(Price, -1));
Volatility = StDev(Returns, Period) * AnnualFactor * 100;
_SECTION_END();
Rank2 = StaticVarGet( "rank1" + "value" + symbol );
RankC = StaticVarGet ("RS_" + Name());
rankvalue = StaticVarGet("rankvalue" + symbol);
bars=5;
AddTextColumn(Name(),"Stocks");
AddTextColumn(FullName(),"Company");
for ( i = 0; i < bars; i++ )
{
newday=Ref(day(),-i);
newmonth=Ref(Month(),-i);
newyear=Ref(Year(),-i);
rs=Rankvalue;
rs=Ref(RS,-i);
rs1 = rankC;
rs1 = Ref(rankc,-i);
Filter = V > 1000;
SetOption( "NoDefaultColumns", True );
AddColumn(RS,""+newday+"/"+newmonth+"/"+newyear,1.2,colorDefault);
AddColumn(rs1,"Profit",1.2);
AddColumn(Ref(C,-i),"Price",1.2);
AddColumn(Ref(V,-i),"Volume",1.0);
AddColumn(Ref(C,-i)*Ref(V,-i),"Total Value",1.0);
AddColumn(Ref(RSI(14),-i), "RSI",1.2);
AddColumn(Volatility,"VIX",1.2);
}
if( Status( "Action" ) == actionExplore ) SetSortColumns( 3,4 );