Count total "Open Long positions" in system

Hi all,
I faced with problem that I didnt know how system count total number of "Open Long Position" when building a trading system. I used it with rebalance and alert purpose.
I think that function cant receive BUY-SELL data in its outside .
My code is very long and has some other function, so I cant put whole code into this StaticVar, can someone help me to solve this problem.
Thank you in advance!

wlnum1 = "myWL";
wlnum = CategoryFind(wlnum1,categoryWatchlist);
List = CategoryGetSymbols( categoryWatchlist, wlnum );

//Buy = Sell = 0;
BI = BarIndex();
BI_BUY         =  ValueWhen( BUY, bi );
BI_SELL        =  ValueWhen( SELL, bi ) ;
Open_LONG1     =  IIf(BI_BUY > BI_SELL,1,0);

if( Status( "stocknum" ) == 0 )
	StaticVarRemove( "~number*" );	
    for( n = 0; ( Symbol = StrExtract( List, n ) )  != "";  n++ )
	//global BUY,SELL;
        //local BI ,BI_BUY , BI_SELL,Open_LONG1,Closed_Position;
        //local Buy_price ,Sell_price, Stoploss_Price;
        SetForeign ( symbol );
		BI = BarIndex();
        BI_BUY         =  ValueWhen( BUY, bi );
		BI_SELL        =  ValueWhen( SELL, bi ) ;
		Open_LONG1  =  BI_BUY > BI_SELL;
		_Trace("BUY " + BUY);
        _TRACE("SELL " + SELL);
        _TRACE("Open_LONG1 " + Open_LONG1);
        //StaticVarSet( "~number" , Open_LONG1 );
        StaticVarAdd( "~number" ,  Open_LONG1 );

symbol = Name();
Open_Long_comp = StaticVarGet( "~number" );
Filter = Open_LONG1;


Please refer to:


Thanh you @Cougar for your support.
But what i really want is in scan or explore, not backtester, because of some other condition related number of "Open Long" Position.
I think that the best solution is using "global / local " into "StaticVar" function like code below.

VariableA = 5; // implict global variable

function Test()
   local VariableA;  // explicit local variable with the same identifier as global
   global VariableB; // explicit global variable not defined earlier
                     // may be used to return more than one value from the function

   VariableA = 99;
   VariableB = 333;

VariableB = 1; // global variable

"Before function call";
"VariableA = " + VariableA;
"VariableB = " + VariableB;


"After function call";
"VariableA = " + VariableA + " (not affected by function call )";
"VariableB = " + VariableB + " (affected by the function call )"

Why would you do that when there is such a flexible CBT in place?

Counting total Open Positions is indicative that you are doing Portfolio Backtesting. You are saying that you intend to do Portfolio Backtesting from Exploration !!! Why?


Counting "open positions" from an Exploration is typically not a good idea, as @Cougar has rightfully pointed out. During an actual backtest, there are many reasons that a symbol with a Buy signal may not actually result in a new trade being opened, for example insufficient capital, max number of positions already open, etc. So at best, you could count the number of positions that could potentially be open.

The code you posted references Buy and Sell variables, and yet you've never assigned those. Clearly that won't work. If you want to count the number of positions that could potentially be open, you could simply do something like this inside your symbol-by-symbol for() loop:

myBuy = // your entry logic here
mySell = // your exit logic here
inPos = Flip(myBuy, mySell);
StaticVarAdd("~number", inPos);

Note that there is no reason to use global and local variable declarations.

Outside the if( Status( "stocknum" ) == 0 ) code you can retrieve the ~number static variable and do all the usual stuff to create Exploration output.


Thank you @mradtke,
You pointed out and solved my problem