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!
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;
Test();
"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.