Explore to calculate the HistVol

Hello,

It is possible to do a explore to calculate the HistVol without using SetForeign (symbol) like here @fxshrat
@link Percentage Stock price nyse above the 200 day moving average

Thank you,
Carlos


// @fxshrat
// @link https://forum.amibroker.com/t/percentage-stock-price-nyse-above-the-200-day-moving-average/19377/5

// watchlist should contain all symbols included in the test
wlnum = 297; // GetOption( "FilterIncludeWatchlist" ); --> WatchList StocksIV
ListComp = CategoryGetSymbols( categoryWatchlist, wlnum ) ;

if( Status( "stocknum" ) == 0 )
{
    // cleanup variables created in previous runs (if any)
    StaticVarRemove( "rankIV*" );
    StaticVarRemove( "valuesIV*" );

    for( n = 0; ( Symbol = StrExtract( ListComp, n ) )  != "";  n++ )
    {
        SetForeign( symbol );
       
			//Indicador de Ratio de Volatilidad
			HistVol = StDev(log(close/Ref(close,-1)),10)/(StDev(log(close/Ref(close,-1)),100) + 1e-9 );
			values = HistVol;

        RestorePriceArrays();

        // write ranked values to a static variable
        StaticVarSet( "valuesIV" + symbol, values );

    }

}

// perform ranking 
StaticVarGenerateRanks( "rankIV", "valuesIV", 0, 1224 ); // normal rank mode 

symbol = Name();
values = StaticVarGet( "valuesIV" + symbol );
rank = StaticVarGet( "rankIV" + "valuesIV" + symbol );

Plot(values, "Ratio Volatilidad 10/100", colorBlueGrey, styleLine|styleThick);

// Controla el filtro de búsqueda 
// Seleccionar en 'Apply to' --> *Filter
if ( GetOption("ApplyTo") == 2 ) {

ListarComp = Param("Nº Valores a Listar",10,1,StrCount(ListComp,",")+1,1);

Filter = rank <= ListarComp ;
SetOption("NoDefaultColumns", True ); 

// exploration code for verification
AddColumn(DateTime(),"Fecha", formatDateTime);
AddTextColumn(Name(),"Ticker",0,colorDefault,colorDefault);
AddTextColumn(FullName(),"Nombre",0,colorDefault,colorDefault);

AddColumn( Nz(values), "IV",1.2,colorBlack,IIf(values>=0,colorGreen,colorRed));
AddColumn( rank, "Rank IV",1.0,colorDefault,colorDefault);

AddColumn(StrCount(ListComp,",")+1,"Nº. Comp.",1.0);

if( Status( "Action" ) == actionExplore )
    SetSortColumns( 5 ); 

} else 
	if ( Status( "action" ) == actionExplore )
		Error("Seleccionar en 'Apply to' --> *Filter --> Stocks IV");


The code of other thread was about composite.

But to run the code of post #1 without SetForeign you may do like this (Read comments at top):

/// Original at
/// @link https://forum.amibroker.com/t/explore-to-calculate-the-histvol/20062
/// Modification at
/// @link https://forum.amibroker.com/t/explore-to-calculate-the-histvol/20062/2
/// 1. Set Watchlist number in wlnum line
/// 2. Apply code to analysis and set Filter -> Watchlist -> WL number being equal to wlnum
/// 3. Enable Pad&align 
/// 4. Run Scan
/// 5. Run Explore or apply to chart pane
/// watchlist should contain all symbols included in the test
wlnum = 297; // GetOption( "FilterIncludeWatchlist" ); --> WatchList StocksIV
ListComp = CategoryGetSymbols( categoryWatchlist, wlnum );
sym_count = StrCount(ListComp,",")+1;
symbol = Name();

// Apply Scan on Filter -> Watchlist -> number being equal to wlnum
is_scan = Status( "action" ) == actionScan;
if ( is_scan ) {
	if ( Status( "stocknum" ) == 0 ) {
		// cleanup variables created in previous runs (if any)
		StaticVarRemove( "rankIV*" );
		StaticVarRemove( "valuesIV*" );
	}
	
	if ( InWatchList(wlnum) ) {
		lg = log(C/Ref(C,-1));
		HistVol = StDev(lg,10)/(StDev(lg,100) + 1e-9 );
		values = HistVol;		
		// write ranked values to a static variable
		StaticVarSet( "valuesIV" + symbol, values );
	}
}

ListarComp = Param("Nº Valores a Listar",10,1,500,1);

// If not Scan
if ( NOT is_scan ) {
	// perform ranking 
	StaticVarGenerateRanks( "rankIV", "valuesIV", 0, 1224 ); // normal rank mode 

	values = StaticVarGet( "valuesIV" + symbol );
	rank = StaticVarGet( "rankIV" + "valuesIV" + symbol );

	Plot(values, "Ratio Volatilidad 10/100", colorBlueGrey, styleLine|styleThick);

	// Controla el filtro de búsqueda 
	// Seleccionar en 'Apply to' --> *Filter
	if ( GetOption("ApplyTo") == 2 ) {
		Filter = rank <= Min(ListarComp, sym_count);

		SetOption("NoDefaultColumns", True ); 

		// exploration code for verification
		AddColumn(DateTime(),"Fecha", formatDateTime);
		AddTextColumn(Name(),"Ticker",0,colorDefault,colorDefault);
		AddTextColumn(FullName(),"Nombre",0,colorDefault,colorDefault);

		AddColumn( Nz(values), "IV",1.2,colorBlack,IIf(values>=0,colorGreen,colorRed));
		AddColumn( rank, "Rank IV",1.0,colorDefault,colorDefault);

		AddColumn(sym_count,"Nº. Comp.",1.0);

		if ( Status( "Action" ) == actionExplore )
			SetSortColumns( 5, -1 ); 
	} else 
		if ( Status( "action" ) == actionExplore )
			Error("Seleccionar en 'Apply to' --> *Filter --> Stocks IV");
}

BTW this one is not good idea

ListarComp = Param("Nº Valores a Listar",10,1,StrCount(ListComp,",")+1,1);

Filter = rank <= ListarComp ;

changed to:

ListarComp = Param("Nº Valores a Listar",10,1,500,1);

Filter = rank <= Min(ListarComp, StrCount(ListComp,",")+1);
6 Likes

Thanks for your quick response @fxshrat
With its changes it is faster 0.10 seconds.
Thank you.