Find the total buy setups each day

When trading with multiple positions from a watchlist I'm trying to find a simple way to count the total number of buy setups that I have each day. In my example below, when I run an exploration, I fail to get the correct results for the total each day. Any help or advice would be greatly appreciated.

MaxPositions = 100;
SetPositionSize( 100 / MaxPositions , spsPercentOfEquity );
SetOption( "MaxOpenPositions", MaxPositions );

SetTradeDelays( 1, 1, 1, 1 );
BuyPrice = SellPrice = ShortPrice = CoverPrice = Open;

SellLimit = 60;

// Remove Static Variables
StaticVarRemove( "TotalBuySetup*" );
StaticVarRemove( "BuySetup*" );

SymList = CategoryGetSymbols( categoryWatchlist, GetOption( "FilterIncludeWatchlist" ) );

for( i = 0; ( tkSym = StrExtract( SymList, i ) ) != ""; ++i )
{
    if( SetForeign( tkSym ) )
    {
        Indicator = RSI();
        BuyLimit = 40;
        BuySetup = Indicator < BuyLimit;
        StaticVarAdd( "TotalBuySetup", BuySetup );
        RestorePriceArrays();
    }
    StaticVarSet( "BuySetup" + tkSym, BuySetup );
}

BuySetupSV = StaticVarGet( "BuySetup" + Name() );
TotalBuySetupSV = StaticVarGet( "TotalBuySetup" );

Buy = BuySetupSV;
Sell = Indicator > SellLimit;
Short = Cover = 0;

//Exploration
Filter = 1;
AddColumn( RSI(), "RSI", 1.2 );
AddColumn( BuySetupSV, "BuySetupSV", 1.0 );
AddColumn( TotalBuySetupSV, "TotalBuySetupSV", 1.0 ); 
AddColumn( IIF( Buy, 'B', ' ' ), "Buy", formatChar, colorDefault, IIf( Buy, colorLime, colorDefault ) );
AddColumn( IIF( Sell, 'S', ' ' ), "Sell", formatChar, colorDefault, IIf( Sell, colorRed, colorDefault ) );
SetSortColumns( 2, -4 );

Every time you process a new symbol, you are removing your two static variables, and therefore losing the total count and also the signals from any symbols processed prior to the current symbol. Try putting everything from your "Remove Static Variables" comment through the end of the "for" loop inside an if block like this:

if (Status("stocknum") == 0)
{
}

That way all of that code will only be executed one time for your Exploration rather than once for every symbol.

3 Likes

@Marcel,

Please take a look here at last post here

You do not need StaticVarAdd at all since you iterate through list of symbol if single symbol is selected. Also you should add stocknum == 0 check as in sample code of link.

Following is just quick modification (so possibly needs further lookup):

MaxPositions = 100;
SetPositionSize( 100 / MaxPositions , spsPercentOfEquity );
SetOption( "MaxOpenPositions", MaxPositions );

SetTradeDelays( 1, 1, 1, 1 );
BuyPrice = SellPrice = ShortPrice = CoverPrice = Open;

// loop invariant code outside of loop
BuyLimit = 40;
SellLimit = 60;

wl_number = GetOption( "FilterIncludeWatchlist" );
SymList = CategoryGetSymbols( categoryWatchlist, wl_number );

if ( Status( "stocknum" ) == 0 ) {
	// Remove Static Variables
	StaticVarRemove( "TotalBuySetup*" );
	StaticVarRemove( "BuySetup*" );

	Sum_setup = 0;

	for( i = 0; ( tkSym = StrExtract( SymList, i ) ) != ""; ++i )
	{
		SetForeign( tkSym );
			Indicator = RSI();			
			BuySetup = Indicator < BuyLimit;			
		RestorePriceArrays();

		Sum_setup += Nz(BuySetup);
		StaticVarSet( "BuySetup" + tkSym, BuySetup );		
	}
	
	StaticVarSet( "TotalBuySetup", Sum_setup);
}

BuySetupSV = StaticVarGet( "BuySetup" + Name() );
TotalBuySetupSV = StaticVarGet( "TotalBuySetup" );

Buy = BuySetupSV;
Sell = Indicator > SellLimit;
Short = Cover = 0;

//Exploration
Filter = 1;
AddColumn( RSI(), "RSI", 1.2 );
AddColumn( BuySetupSV, "BuySetupSV", 1.0 );
AddColumn( TotalBuySetupSV, "TotalBuySetupSV", 1.0 ); 
AddColumn( IIF( Buy, 'B', ' ' ), "Buy", formatChar, colorDefault, IIf( Buy, colorLime, colorDefault ) );
AddColumn( IIF( Sell, 'S', ' ' ), "Sell", formatChar, colorDefault, IIf( Sell, colorRed, colorDefault ) );
SetSortColumns( 2, -4 );
4 Likes

Thank you so much @mradtke and @fxshrat, you are both Amibroker geniuses! I tried it using both methods and I can get the results I was looking for. First, here is the code I made with Matt's advice:

MaxPositions = 100;
SetPositionSize( 100 / MaxPositions , spsPercentOfEquity );
SetOption( "MaxOpenPositions", MaxPositions );

SetTradeDelays( 1, 1, 1, 1 );
BuyPrice = SellPrice = ShortPrice = CoverPrice = Open;

// loop invariant code outside of loop
BuyLimit = 40;
SellLimit = 60;

if( Status( "stocknum" ) == 0 )
{
// Remove Static Variables
    StaticVarRemove( "TotalBuySetup*" );
    StaticVarRemove( "BuySetup*" );

    SymList = CategoryGetSymbols( categoryWatchlist, GetOption( "FilterIncludeWatchlist" ) );

    for( i = 0; ( tkSym = StrExtract( SymList, i ) ) != ""; ++i )
    {
        if( SetForeign( tkSym ) )
        {
            Indicator = RSI();
            BuySetup = Indicator < BuyLimit;
            StaticVarAdd( "TotalBuySetup", BuySetup );
            RestorePriceArrays();
        }
        StaticVarSet( "Indicator" + tkSym, Indicator );
        StaticVarSet( "BuySetup" + tkSym, BuySetup );
    }
}
IndicatorSV = StaticVarGet( "Indicator" + Name() );
BuySetupSV = StaticVarGet( "BuySetup" + Name() );
TotalBuySetupSV = StaticVarGet( "TotalBuySetup" );

Buy = BuySetupSV;
Sell = IndicatorSV > SellLimit;
Short = Cover = 0;

//Exploration
Filter = 1;
AddColumn( RSI(), "RSI", 1.2 );
AddColumn( BuySetupSV, "BuySetupSV", 1.0 );
AddColumn( TotalBuySetupSV, "TotalBuySetupSV", 1.0 );
AddColumn( IIF( Buy, 'B', ' ' ), "Buy", formatChar, colorDefault, IIf( Buy, colorLime, colorDefault ) );
AddColumn( IIF( Sell, 'S', ' ' ), "Sell", formatChar, colorDefault, IIf( Sell, colorRed, colorDefault ) );
SetSortColumns( 2, -4 );

And here is the code that I made with fxshrat's advice:

MaxPositions = 100;
SetPositionSize( 100 / MaxPositions , spsPercentOfEquity );
SetOption( "MaxOpenPositions", MaxPositions );

SetTradeDelays( 1, 1, 1, 1 );
BuyPrice = SellPrice = ShortPrice = CoverPrice = Open;

// loop invariant code outside of loop
BuyLimit = 40;
SellLimit = 60;

wl_number = GetOption( "FilterIncludeWatchlist" );
SymList = CategoryGetSymbols( categoryWatchlist, wl_number );

if( Status( "stocknum" ) == 0 )
{
    // Remove Static Variables
    StaticVarRemove( "TotalBuySetup*" );
    StaticVarRemove( "BuySetup*" );

    Sum_setup = 0;

    for( i = 0; ( tkSym = StrExtract( SymList, i ) ) != ""; ++i )
    {
        SetForeign( tkSym );
        Indicator = RSI();
        BuySetup = Indicator < BuyLimit;
        RestorePriceArrays();
        Sum_setup += Nz( BuySetup );
        StaticVarSet( "Indicator" + tkSym, Indicator );
        StaticVarSet( "BuySetup" + tkSym, BuySetup );
    }
    StaticVarSet( "TotalBuySetup", Sum_setup );
}
IndicatorSV = StaticVarGet( "Indicator" + Name() );
BuySetupSV = StaticVarGet( "BuySetup" + Name() );
TotalBuySetupSV = StaticVarGet( "TotalBuySetup" );

Buy = BuySetupSV;
Sell = IndicatorSV > SellLimit;
Short = Cover = 0;

//Exploration
Filter = 1;
AddColumn( RSI(), "RSI", 1.2 );
AddColumn( BuySetupSV, "BuySetupSV", 1.0 );
AddColumn( TotalBuySetupSV, "TotalBuySetupSV", 1.0 );
AddColumn( IIF( Buy, 'B', ' ' ), "Buy", formatChar, colorDefault, IIf( Buy, colorLime, colorDefault ) );
AddColumn( IIF( Sell, 'S', ' ' ), "Sell", formatChar, colorDefault, IIf( Sell, colorRed, colorDefault ) );
SetSortColumns( 2, -4 );

And here is a copy of the exploration that I get using either method. On 1/5/15, I have 3 x 1 buy setups in the buysetup column which gives a 3 in the TotalBuySetup column. Then on 1/6/15, I have 9 x 1 buy setups giving a 9 in the TotalBuySetup column:
image

3 Likes

Thanks for the code Fxshrat! I´m looking for something similar for counting signals, but I'm having a weird issue using this code. When applying to a WL with few signals all is Ok. Explorer reports 9 Total Buy and 9 Signals, BUT when there are more signals both counts differs greatly and the difference is bigger as much signals are.

In this capture there are 22 Total Buy but only 20 signals.
2022-07-25 20_47_15-AmiBroker

any idea what might be going on?

I found the issue. It requires deactivating QuickAFL for Ipad&Align to work properly. Now is working fine
thanks!

2 Likes