How to calculate the exact number of stocks with condition of RSI

I want to calculate the number of stocks that satisfy the condition RSI >80 from a list of categoryGroup. However, after Explorer, the Explorer results are not the same as the results I calculated myself with Excel. How to fix this code to calculate Exactly?

wlnum = GetOption( "FilterIncludeGroup" );
List = CategoryGetSymbols( categoryGroup, wlnum );
AboveLevel = 80; // Param("TestLevel", 20, 5, 35, 1);
BelowLevel = 20;
PeriodRSI=3;

if( Status( "stocknum" ) == 0 )
{
    // cleanup variables created in previous runs (if any)
    StaticVarRemove( "~RSI*" );
    StaticVarRemove( "~RSICount*" );
	StaticVarRemove ("~RSIBelow*");
	 
    for( n = 0; ( Symbol = StrExtract( List, n ) )  != "";  n++ )
    {
        SetForeign( symbol );
        RSIVal = RSI( PeriodRSI ); // put in the RSI you are interested in
        //TestLevel = 20; // this line seems to work here or above - see line 8
        DKVol=MA(V,20)>100000 AND V>150000;
        RSIabove = RSIVal > AboveLevel AND DKVol;
        RSIbelow= RSIVal < BelowLevel AND DKVol;
        RSI80=Cross(80,RSIVal) AND DKVol;
        RSI20=Cross(RSIVal,20) AND DKVol;
        RSITang50=Cross(RSIVal,50) AND DKVol;
        RSIGiam50=Cross(50,RSIVal) AND DKVol;
        RSITang= RSIVal>Ref(RSIVal,-1) AND DKVol;
        RSIGiam= RSIVal<Ref(RSIVal,-1) AND DKVol;
               
        StaticVarAdd( "~RSIabove", RSIabove);
        StaticVarAdd( "~RSIbelow", RSIbelow);
        StaticVarAdd( "~RSI80", RSI80);
        StaticVarAdd( "~RSI20", RSI20);
        StaticVarAdd( "~RSITang50", RSITang50); 
        StaticVarAdd( "~RSIGiam50", RSIGiam50);
        StaticVarAdd( "~RSITang", RSITang);
        StaticVarAdd( "~RSIGiam", RSIGiam);
  
        //StaticVarAdd( "~RSICount", NOT IsNull( RSIabove) );

        RestorePriceArrays();
    }
}

svRSIabove = StaticVarGet( "~RSIabove" );
svRSIbelow = StaticVarGet( "~RSIbelow" );
svRSI80 = StaticVarGet( "~RSI80" );
svRSI20 = StaticVarGet( "~RSI20" );
svRSITang50 = StaticVarGet( "~RSITang50" );
svRSIGiam50 = StaticVarGet( "~RSIGiam50" );
svRSITang = StaticVarGet( "~RSITang" );
svRSIGiam = StaticVarGet( "~RSIGiam" );


///////////////
// Kich ban VNINDEX
///////////////
BanhetCP=Ref(svRSIabove,-1)>80 AND svRSIGiam50>=30 AND (svRSIGiam50-svRSITang50)>5 AND svRSIGiam>=100 AND (svRSIGiam-svRSITang)>70;
GiamCP=(Ref(svRSIabove,-1)>80 AND svRSIGiam50>=30 AND (svRSIGiam50-svRSITang50)>5 AND svRSIGiam>=100 AND (svRSIGiam-svRSITang)>70) OR 
       (svRSIabove>100 AND svRSIGiam50>=30 AND (svRSIGiam50-svRSITang50)>5 AND svRSIGiam>svRSITang) OR 
       svRSIGiam50>40 OR (svRSIabove>100 AND svRSI80>=50);
CannhacGiam=svRSIabove>80 AND svRSIGiam50>=30 AND svRSI80 >=30 AND svRSIGiam50>svRSITang50;
MuaCP= (svRSIabove<100 AND svRSITang>=100 AND (svRSITang-svRSIGiam)>70 AND svRSITang50>=30 AND (svRSITang50-svRSIGiam50)>5) OR svRSI20>=40;

TextVNI=WriteIf(BanhetCP,"Ban het CP", WriteIf(GiamCP,"Giam ty trong CP",WriteIf(CannhacGiam,"Can nhac giam ty trong CP", WriteIf(MuaCP, "Mua/Gia tang CP",""))));
ColorVNI=IIf(BanhetCP OR GiamCP,colorRed,IIf(MuaCP,colorLime,colorDefault));


Buy=Cross(RSI( PeriodRSI ),50) AND V>300000 AND C>WMA(C,40);

///////////////
// Explore
///////////////
Filter = 1 AND MA(V,20)>100000 AND V>150000;
AddColumn( Day(), "Day", 1.0 );
//AddColumn( C, "P", 1.0 );
AddColumn( V, "Volume", 1.0 );
AddColumn( Ref(RSI( PeriodRSI ),-1), "Pre_RSI14", 1.2, colorBlack, IIf(Ref(RSI( PeriodRSI ),-1) > aboveLevel, colorLime, colorDefault )) ;
AddColumn( RSI( PeriodRSI ), "RSI14", 1.2, colorBlack, IIf( RSI( PeriodRSI ) > aboveLevel, colorLime, colorDefault ) );
AddColumn( RSI( PeriodRSI )-ref(RSI( PeriodRSI ),-1), "+/-RSI", 1.2, colorBlack, IIf( RSI( PeriodRSI )>ref(RSI( PeriodRSI ),-1), colorLime, colorDefault ) );
AddColumn( svRSIabove, ">80", 1.2, colorBlack, IIf( svRSIabove > 80, colorred,IIf(svRSIabove > 60,colorYellow,colorDefault )));
AddColumn( svRSIbelow, "<20", 1.2, colorBlack, IIf( svRSIbelow >100, colorLime,IIf(svRSIbelow >80,colorYellow, colorDefault )));
AddColumn( svRSITang, "Tang", 1.2, colorBlack, IIf( svRSITang > 100 AND (svRSITang-svRSIGiam)>50 , colorLime, colorDefault ) );
AddColumn( svRSIGiam, "Giam", 1.2, colorBlack, IIf( svRSIGiam > 100 AND (svRSIGiam- svRSITang)>50 , colorLime, colorDefault ) );
AddColumn( svRSI80, "Giamduoi80", 1.2, colorBlack, IIf( svRSI80 > 50, colorred, colorDefault ) );
AddColumn( svRSIGiam50, "Giamduoi50", 1.2, colorBlack, IIf( svRSIGiam50 >= 30, colorred,IIf( svRSIGiam50 >= 20, colorYellow, colorDefault ) ));
AddColumn( svRSI20, "Tangtren20", 1.2, colorBlack, IIf( svRSI20 > 40, colorLime, colorDefault ) );
AddColumn( svRSITang50, "Tangtren50", 1.2, colorBlack, IIf( svRSITang50 >= 30, colorLime,IIf( svRSITang50 >= 20, colorYellow, colorDefault ) ));
AddTextColumn(TextVNI,"Kich ban VNI",1.2,colorDefault,ColorVNI);
AddColumn( IIf(Buy,1,0), "BuyRSI", 1.0 );

1 2

1 Like

Please help me fix code! Thanks so much!

Reduce to 1 symbol and start checking if RSI of this symbol is equal in Excel and in Amibroker.
Are they,? include one more symbol and see what happens
How about Pad & Align, is it enable in Amibroker ? Do you emulate the same in Excel ?
Too many things you need to check.

@Mr_tran I am suprised no one has yet raised the issue of your "misuse" of SetForeign (and since your code looks like it was derived from one of my earlier posts on this forum years ago I thought I'd point it out). It has been discussed numerous times on the forum, but building composite indicators with StaticVarAdd was intended to be used with SCAN.

So you may use AddToComposite or even a variation of your code with an Exploration and then numerous calls to SetForeign but perhaps you should try a variation of this code.

// run a SCAN on your chosen WL or Group, then to see your results run an EXPLORE

rsiPds 	  	= Param( "RSI Periods", 3, 2, 50, 1 );
RSIVal 	  	= RSI( rsiPds ); // could Paramaterize this too

HiLevel		= Param( "HiLevel", 80, 50, 99, 1 );
LoLevel		= Param( "LoLevel", 20, 5, 45, 1 );

if( Status( "actionEx" ) == actionScan )
{
    if( Status( "stocknum" ) == 0 )
    {
        StaticVarRemove( "~RSI*" );
    }

    StaticVarAdd( "~RSIabove", RSIVal > HiLevel);
    StaticVarAdd( "~RSIbelow", RSIVal < LoLevel);

	Cover = 0;
}

svRSIabove = StaticVarGet( "~RSIabove" );
svRSIbelow = StaticVarGet( "~RSIbelow" );

Then depending on what you would prefer for your Exploration output you can see results in your preferred way. For example (on a small watchlist of 10 symbols for debugging and I altered the rsi threshold for a more dramatic result),
image

or
image

And all of this builds upon the User Guide instructions for using StaticVarAdd
https://www.amibroker.com/guide/afl/staticvaradd.html

2 Likes

@TrendSurfer @awilson @portfoliobuilder Thanks so much!

This topic was automatically closed 100 days after the last reply. New replies are no longer allowed.