How to get cumulative sum of trading value in a watchlist?

Hi All,

I'm a new member of our forum. I would like to know how much trading value in a day of tickers in a specific watchlist is. Then I will add that value to the volume of a customized index.
The first step is calculating cumulative sum of trading value in that watchlist in one day, and my code is as below. For some reason which I don't know, the displayed result is not consistent as attached photo.
The correct result is 3021, but somehow some weird results (likes 3060, 3024) randomly occur.
In addition, my poor code seems not effective due to a long time of respond. I will add the result to the volume of a customized index for all days which means my code will take much longer.

Could you please tell me what wrong with my code? and how to improve it?

Thank you so much.

Inconsistent%20result|541x237

wlnum = 0; 
List = CategoryGetSymbols( categoryWatchlist, wlnum ) ;

Money = 0;
count = 0;   

if( List != "" ) 
{
	for( n = 0; ( Symbol = StrExtract( List, n ) )  != "";  n++ )
	{
		SetForeign( Symbol ); 	
		Money = Money + C*V/1e6;
		count = count + 1;
		RestorePriceArrays();
	}
}

Filter = count and InWatchList(wlnum);
AddColumn(Money,"Value",1.2);
AddColumn(count,"Count",1.2);

I have no ideas why the attached image could not appear properly.
It was uploaded from my computer. I will try to upload it again through a free website.

@bachi try using StaticVarAdd, search this forum for many examples, and the User Guide

https://www.amibroker.com/guide/afl/staticvaradd.html

// Using StaticVarAdd and run EXPLORE

// I'm going to pick a watchlist in AA window
wlnum = GetOption( "FilterIncludeWatchlist" );
List = CategoryGetSymbols( categoryWatchlist, wlnum );

if( Status( "stocknum" ) == 0 )
{
    // cleanup variables created in previous runs
    StaticVarRemove( "~SymbolCount*" );
    StaticVarRemove( "~Money*" );

    for( n = 0; ( Symbol = StrExtract( List, n ) )  != "";  n++ )
    {

        SetForeign( symbol );

        StaticVarAdd( "~Money ", C * V / 1e6 );
        StaticVarAdd( "~SymbolCount", 1 ); 
        
        RestorePriceArrays();
    }
}

svMoney  = StaticVarGet( "~Money " );
svSymbolCount = StaticVarGet( "~SymbolCount" );

///////////////
// Explore
///////////////
Filter = Status( "stocknum" ) == 0; // can use Filter settings that suit your needs
SetOption("NoDefaultColumns",True);
AddColumn(DateTime(), "Date", formatDateTime);
AddColumn( svMoney, "Money", 1.0 );
AddColumn( svSymbolCount, "Number of Stocks", 1.0 );

2 Likes

Thank you mate.

I got it with your help :smiley: