# Calculate Index from Watchlist

Hi,

I want calculate Index base Avarege Volume of watchlist for two watchlist. But result the same both of watchlist with this Index. Maybe, my code not correct. Please help me fix code! Thanks very much!

``````numwlnames=2;
wlname1 = "Satthep";// set watchlist
wlname2 = "Bank";// set watchlist

scan = Status("actionEx") == actionScan;

for ( i = 1; i <= numwlnames; i++ ) {
// call wachlist's text variable(s)
wlname = VarGetText( "wlname" + i );
if(scan) {
inWL = InWatchListName( wlname );
if( inWL )  {
if(Status("stocknum") == 0)
StaticVarRemove("~RSI*");
//_TRACE( "Calling StaticVarAdd in Scan" );
}
}
svVTotal = StaticVarGet("~VTotal");
svVcount = StaticVarGet("~Vcount");
IndexV=svVTotal/svVcount;
VarSet("IndexV"+i, SelectedValue(IndexV));
}

Filter = 1;

``````

@Mr_tran, it seems to me that there is an issue due to the `StaticVarRemove()` line.

Are you "clearing" the correct static variables?

You code is not correct on several occasions.

You should remove static vars not inside loop.
And you should remove correct variables.

Also you are calling static vars in scan.
You should call them after scan and on single symbol since you just get average of entire WL and at last bar of range.

Also change to

``````StaticVarAdd("~VTotal"+i, V);
``````

If you want to get separate averages per WL.
Similar if calling static vars.

``````/// Corrected code
/// https://forum.amibroker.com/t/calculate-index-from-watchlist/26244/3
numwlnames=2;
wlname1 = "Satthep";// set watchlist
wlname2 = "Bank";// set watchlist

scan = Status("actionEx") == actionScan;

if( scan )
{
if( Status( "stocknum" ) == 0 ) {
StaticVarRemove( "~VTotal*" );
StaticVarRemove( "~Vcount*" );
}

for( i = 1; i <= numwlnames; i++ )
{
// call wachlist's text variable(s)
wlname = VarGetText( "wlname" + i );
inWL = InWatchListName( wlname );
if( inWL )
{
}
}
}

Filter = 0;
SetOption("NodefaultColumns",1);
AddColumn( Null, "Avg. Volume", 1.2 );

if( Status( "stocknum" ) == 0 ) {
for( i = 1; i <= numwlnames; i++ )
{
svVTotal = StaticVarGet( "~VTotal"+i );
svVcount = StaticVarGet( "~Vcount"+i );
IndexV = SelectedValue(svVTotal / svVcount);
}
}
``````

@fxshrat I try scan by your code but has error. How to fix this code???

@beppe Thanks so much!

``````/// Corrected code
/// https://forum.amibroker.com/t/calculate-index-from-watchlist/26244/3
numwlnames=2;
wlname1 = "Satthep";// set watchlist
wlname2 = "Bank";// set watchlist

scan = Status("actionEx") == actionScan;

if( scan )
{
if( Status( "stocknum" ) == 0 ) {
StaticVarRemove( "~VTotal*" );
StaticVarRemove( "~Vcount*" );
}

for( i = 1; i <= numwlnames; i++ )
{
// call wachlist's text variable(s)
wlname = VarGetText( "wlname" + i );
inWL = InWatchListName( wlname );
if( inWL )
{
}
}
}

Filter = 0;
SetOption("NodefaultColumns",1);
AddColumn( Null, "Avg. Volume", 1.2 );

if( Status( "stocknum" ) == 0 ) {
for( i = 1; i <= numwlnames; i++ )
{
wlname = VarGetText( "wlname" + i );
svVTotal = StaticVarGet( "~VTotal"+i );
svVcount = StaticVarGet( "~Vcount"+i );
IndexV = SelectedValue(svVTotal / svVcount);
}
}
``````

@fxshrat When i scan and then Explorer this fixcode. Value of Avg.Volume alway equal to one Value in spite of I tried to change Frome to date

In fact, I want calculate Index Volume of each watchlist in one day to guess smart money to each watchlist base on change Volume between another days.
Example template:

YOU were chosing `SelectedValue` not me.
In analysis `SelectedValue` gets last value of range. SelectedValue is single element (of array). Array may consist of multiple elements.

https://www.amibroker.com/guide/h_understandafl.html

So you have to remove `SelectedValue` function if you want to output array.

``````/// Corrected code
/// https://forum.amibroker.com/t/calculate-index-from-watchlist/26244/3
numwlnames = 2;
wlname1 = "Satthep";// set watchlist
wlname2 = "Bank";// set watchlist

scan = Status("actionEx") == actionScan;

if( scan )
{
if( Status( "stocknum" ) == 0 ) {
StaticVarRemove( "~VTotal*" );
StaticVarRemove( "~Vcount*" );
}

for( i = 1; i <= numwlnames; i++ )
{
// call wachlist's text variable(s)
wlname = VarGetText( "wlname" + i );
inWL = InWatchListName( wlname );
if( inWL )
{
}
}
}

Filter = 1;

for( i = 1; i <= numwlnames; i++ )
{
wlname = VarGetText( "wlname" + i );
svVTotal = StaticVarGet( "~VTotal"+i );
svVcount = StaticVarGet( "~Vcount"+i );
IndexV = svVTotal / svVcount;
}
``````

@fxshrat Thanks!

IF I want only add column when it satisfies the condition Buy but error 10 when Explorer

``````for( i = 1; i <= numwlnames; i++ )
{
wlname = VarGetText( "wlname" + i );
svVTotal = StaticVarGet( "~VTotal"+i );
svPTotal = StaticVarGet( "~PTotal"+i );
svVcount = StaticVarGet( "~Vcount"+i );
IndexV = svVTotal / svVcount;
IndexP = svPTotal / svVcount;
Dotbien= IndexV/(Ref(IndexV,-1)+1e-9)>1.3 AND IndexV/MA(IndexV,9)>1.2 AND IndexP>Ref(IndexP,-1);
Tang=MA(IndexV,3)>MA(IndexV,5) AND NOT Dotbien AND IndexP>Ref(IndexP,-1) AND IndexV>Ref(IndexV,-1) ;
Tichluy= IndexV>Ref(IndexV,-1) AND Ref(IndexV,-1)>Ref(IndexV,-2) AND NOT Tang AND IndexP>Ref(IndexP,-1);
TextList = "\nDotBien\nTang\nTichluy";
TextSelector = 1*Dotbien+2*Tang+3*Tichluy;
Color=IIf(Dotbien OR Tang OR Tichluy, colorLime,colorDefault);

}
}
``````

You are exceeding Barcount range. `i` is not part of a BarCount loop.

You are doing this incorrectly.
You should add `Buy` to `Filter` variable.

Remove `if(Buy[i])` statement completely.

This one is not properly too:

``````TextSelector = 1*Dotbien+2*Tang+3*Tichluy;
``````

You should use power of two sequence. 1, 2, 4, 8, 16, ...