Number of open positions in exploration

I am trying to know the number of open positions when I run exploration. The variable "EntryPrice" is greater than zero if I have a position in that symbol. I understand that in exploration mode everything is done by symbol so I should use static variables to count the number of open positions (those with entryprice > 0).

SymList = CategoryGetSymbols(categoryWatchlist,1); //myWatchlist

StaticVarRemove( "dentro*" );

if( Status( "stocknum" ) == 0 ) //para que solo haga una pasada
{
	for( i = 0; ( symbol = StrExtract( SymList, i ) ) != ""; i++ )
	{	 
	 dentro = IIf(EntryPrice  > 0, 1, 0);	   
	 StaticVarSet("dentro_" + symbol, dentro);
	 _TRACE("Symbol is " + symbol + "      " + "   dentro = " +  StaticVarGet("dentro_" +symbol)  );
	}
}

But this is not working. I have 4 open positions and the result is this:

c19

Since you didn't show us your exploration code, there's really no way to identify your error. However, I see that you are saving a static variable for each symbol, i.e. "dentro_XXX". How does that help you determine how many positions are open? Also, where do you determine EntryPrice?

I would suggest you spend a little more time with the Help files so that you understand how AmiBroker executes an Exploration and also how to effectively use Static Variables.

Thanks for your answer. I did not include exploration because it is very long and will not help with my problem which is identifying symbols with open positions. I was trying to go step by step so the variable "dentro_XXX" should indicate if that symbol has an open position. Later in the code I can count the number of open positions if i am able to see them individually. But I am doing something wrong so I cannot see which symbols have open positions.

See this code:

// trading system
Buy = Cross(MACD(), Signal());
Sell = Cross(Signal(),MACD() );


// long
long = Flip(Buy,Sell);
EntryPrice = IIf(long, ValueWhen(Buy,C), 0);


// explore
Filter = Status( "lastbarinrange" ); 
SymList = CategoryGetSymbols(categoryWatchlist,1); 
AddColumn(EntryPrice,"EntryPrice",1.2);

StaticVarRemove( "dentro*" );

if( Status( "stocknum" ) == 0 ) //para que solo haga una pasada
{
	for( i = 0; ( symbol = StrExtract( SymList, i ) ) != ""; i++ )
	{	 
	 dentro = IIf(EntryPrice  > 0, 1, 0);	   
	StaticVarSet("dentro_" + symbol, dentro);	
	_TRACE("Symbol is " + symbol + "      " + "   dentro = " +  StaticVarGet("dentro_" +symbol)  );
	
	}
}

c20

There are 6 open positions.

How can I count them? Thanks

For the record, it's usually a bad idea to try to count "open positions" from an Exploration, because the actual trades taken during a backtest may be different than the set of entry signals you generated. For example, the back tester may not enter trades due to capital constraints, max open positions, etc. Search the forum for "backtest explore" to find lots of other posts on this topic.

But let's assume you are OK with your current approach.

There are multiple errors in your code, but the primary problem is that inside your

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

block of code, you are using the variable named EntryPrice. However, EntryPrice will have been set for the first symbol in the watchlist (i.e. stock number 0), not for every symbol in the watchlist. If you want EntryPrice to be accurate for each symbol, then you will need to calculate it inside the for loop that goes through all the symbols. You will also need a Foreign() or SetForeign() call to retrieve the data for that symbol so that you have the inputs necessary to determine potential entries and exits.

It would also make more sense to have a single static variable rather than a separate static variable for each symbol in the watchlist. Otherwise, you will need to sum up the symbol-specific variables later, which is just a waste of time and effort.

1 Like

Many thanks for your answer. Following your suggestions i was able to make it work. However I did not need to move EntryPrice inside the loop through symbols.

// trading system
Buy = Cross(MACD(), Signal());
Sell = Cross(Signal(),MACD() );

// long
long = Flip(Buy,Sell);
EntryPrice = IIf(long, ValueWhen(Buy,C), 0);

// explore
Filter = Status( "lastbarinrange" ); 
SymList = CategoryGetSymbols(categoryWatchlist,1); //watchlist n 1
AddColumn(EntryPrice,"EntryPrice",1.3);

//initialize counter
if( Status( "stocknum" ) == 0 ) StaticVarRemove("count");

//check open positions
for( i = 0; i < 11; i++)  //list has 11 symbols
{
	if( Status( "stocknum" ) == i ) 
	{		
	symbol = StrExtract( SymList, i );	
	OpenPos = IIf(EntryPrice  > 0, 1, 0);	
	_TRACE("i = " + i + "   Symbol is " + symbol + "      " + "   OpenPos = " +  OpenPos  );	
	
	//acumulate counter
	SetForeign(symbol);
		cuenta = Nz(StaticVarGet("count"));
		StaticVarSet("count", cuenta + OpenPos);
		_TRACE("Counter is " + StaticVarGet("count") ); 
	RestorePriceArrays();	
	}
}

c23

You could achieve the same result by replacing all of this:

//check open positions
for( i = 0; i < 11; i++)  //list has 11 symbols
{
	if( Status( "stocknum" ) == i ) 
	{		
	symbol = StrExtract( SymList, i );	
	OpenPos = IIf(EntryPrice  > 0, 1, 0);	
	_TRACE("i = " + i + "   Symbol is " + symbol + "      " + "   OpenPos = " +  OpenPos  );	
	
	//acumulate counter
	SetForeign(symbol);
		cuenta = Nz(StaticVarGet("count"));
		StaticVarSet("count", cuenta + OpenPos);
		_TRACE("Counter is " + StaticVarGet("count") ); 
	RestorePriceArrays();	
	}
}

With a single line:

StaticVarAdd("count", EntryPrice > 0);

However, by doing it this way your count won't be accurate until after you've processed all the symbols, so if you were to output the static "count" variable from your exploration, the value would be different for different symbols on the same bar.

1 Like