StaticVarSet in CBT

Hello,
I would like to query the shares of the OpenPosition in the CBT in order to display this values in exploration. This array should be saved via StaticVarSet. I've tried everything including SetForeign. In exploration, only a number is ever received, not an array. The last value. The correct values are shown to me via _Trace. What am I doing wrong ????

SetCustomBacktestProc("");
if (Status("action") == actionPortfolio) 
{
    cash = Null;
    OpenPosition = Null;
    StaticVarRemove("Cash");
    StaticVarRemove("GesamtWert");
    StaticVarRemove("OpenPosition");
    StaticVarRemove("PosStck_*");
    bo = GetBacktesterObject();
    bo.PreProcess();
    for (i = 0; i < BarCount; i++)
    {
        for (sig = bo.GetFirstSignal( i ); sig; sig = bo.GetNextSignal( i ) )
        {	
			if (sig.IsEntry())
			{
				prev_close = StaticVarGet("PrevClose_" + sig.Symbol);	// Works, calculate on prev. Close
				CashAvailable = bo.Cash / (MaxPos - bo.GetOpenPosQty());
				SharesNeed = CashAvailable / prev_close[ i ];
				SharesNeed = round(SharesNeed);
				sig.PosSize = -2000 - SharesNeed;
				//sig.PosSize = bo.Cash / (MaxPos - bo.GetOpenPosQty()); // works, calculate on Open Delay 0
			}
			if (sig.IsScale())
			{
				ScaleOutPercent = StaticVarGet("PosScaleOut_" + sig.Symbol);
				
				// Calculate Shares 
				//_TRACE("ScaleOutPercent  " + NumToStr(ScaleOutPercent));
				//MyTrade = bo.FindOpenPos(sig.Symbol);
				//ScaleOutPosSize = round((MyTrade.Shares * ScaleOutPercent) / 100);
				//sig.PosSize = -2000 - ScaleOutPosSize; // works
				
				// Calculate Percent
				sig.PosSize = -1000 - ScaleOutPercent; // works
			}
        }	
        bo.ProcessTradeSignals( i );
        for (openPos = bo.GetFirstOpenPos(); openPos; openPos = bo.GetNextOpenPos)
		{
			//SetForeign(openPos.Symbol);
			StaticVarSet("PosStck_" + openPos.Symbol, openPos.Shares);
			//_TRACE("PosStck  " + NumToStr(openPos.Shares) + "   " + NumToStr(i));
			//RestorePriceArrays();
		}
        OpenPosition[i] = bo.GetOpenPosQty();
        cash[ i ] = bo.cash;
    }
    bo.PostProcess();
    StaticVarSet("Cash",cash);
    StaticVarSet("GesamtWert",bo.EquityArray);
    StaticVarSet("OpenPosition",OpenPosition);
}


What you do is not creating array.

If you want array then you need to create array. E.g.

op_shares = Null;

for( i = 0; i < BarCount; i++ )	{                  
	// ....
	
	for ( op = bo.GetFirstOpenPos(); op; op = bo.GetNextOpenPos() ) {
		op_shares[i] = op.Shares;			
	}  
	
	// ....      
}

bo.PostProcess(); 

StaticVarSet("OpenPos_Shares", op_shares);

If you want to create separate arrays per symbol then you might create multi dim array.
So you would need list/number of traded symbols.

1 Like

Hello FxShrat,
I apologize. I guess I was inaccurate. In the main Barcount loop, Barcount is probably a double with defined steps. Perfect for compressing data. In the CBT, Barcount is obviously a shortint. 0, 1, 2 .... I've read several times StaticVarGet / Set would sync itself. That should be the problem here. CBT defines barcount in a different way. But ok ... then I need a matrix to get there. How does it have to be structured in order to be read by StaticVarGet? DateTime, Name (), Value ???? A hint would be good, even better a small snippet of code.

And once again I politely apologize. Your code snippet would only show the shares of the last position in the list in StaticVarGet. There may be cases where this is required. It doesn't help here.

Upper code was example snippet not copy&paste ready code you may look for.
Sorry, but this goes a bit beyond free help.

But you need to store list first.
Then create a matrix.
Then store to matrix (yes, storing datetime and shares value).
Then store to static var.
Then call static var(s) outside of backtest (e.g. chart, exploration).
Then create 1-dim array.
Then output array (plot, explorer).

All together around 50 lines of code.

Example output BT

Symbol	Trade	Date	Price	Ex. date	Ex. Price	% chg	Profit	% Profit	Contracts	Position value	Cum. Profit	# bars	Profit/bar	MAE	MFE	Scale In/Out	
AXP	Long (n-bar)	2020-06-01	96.75	2020-06-15	103.95	7.44%	144.00	7.44%	20	1935.00	144.00	12	12.00	-2.26%	19.82%	0/0
AXP	Long (n-bar)	2020-06-15	103.95	2020-06-29	94.52	-9.07%	-179.17	-9.07%	19	1975.05	487.89	11	-16.29	-11.06%	4.92%	0/0
AXP	Long (n-bar)	2020-06-29	94.52	2020-07-14	94.84	0.34%	6.72	0.34%	21	1984.92	591.55	11	0.61	-5.23%	3.58%	0/0
AXP	Long (n-bar)	2020-07-14	94.84	2020-07-28	94.42	-0.44%	-8.82	-0.44%	21	1991.64	628.24	11	-0.80	-2.69%	3.11%	0/0
AXP	Long (n-bar)	2020-07-28	94.42	2020-08-11	103.26	9.36%	185.64	9.36%	21	1982.82	730.52	11	16.88	-2.32%	10.14%	0/0
AXP	Open Long	2020-08-11	103.26	2020-08-21	96.15	-6.89%	-135.09	-6.89%	19	1961.94	799.75	10	-13.51	-7.34%	2.36%	0/0
BA	Long (n-bar)	2020-06-01	151.39	2020-06-15	190.94	26.12%	514.15	26.12%	13	1968.07	658.15	12	42.85	-4.52%	54.70%	0/0
BA	Long (n-bar)	2020-06-15	190.94	2020-06-29	194.49	1.86%	35.50	1.86%	10	1909.40	523.39	11	3.23	-12.22%	9.09%	0/0
BA	Long (n-bar)	2020-06-29	194.49	2020-07-14	179.96	-7.47%	-145.30	-7.47%	10	1944.90	446.25	11	-13.21	-12.72%	0.01%	0/0
BA	Long (n-bar)	2020-07-14	179.96	2020-07-28	170.84	-5.07%	-100.32	-5.07%	11	1979.56	527.92	11	-9.12	-6.37%	5.32%	0/0
BA	Long (n-bar)	2020-07-28	170.84	2020-08-11	180.13	5.44%	102.19	5.44%	11	1879.24	832.71	11	9.29	-10.20%	8.00%	0/0
BA	Open Long	2020-08-11	180.13	2020-08-21	167.5	-7.01%	-138.93	-7.01%	11	1981.43	660.82	10	-13.89	-7.92%	5.46%	0/0
CAT	Long (n-bar)	2020-06-01	120.5	2020-06-15	123.61	2.58%	49.76	2.58%	16	1928.00	707.91	12	4.15	-2.40%	16.13%	0/0
CAT	Long (n-bar)	2020-06-15	123.61	2020-06-29	125.25	1.33%	26.24	1.33%	16	1977.76	549.63	11	2.39	-4.53%	7.14%	0/0
CAT	Long (n-bar)	2020-06-29	125.25	2020-07-14	136.88	9.29%	186.08	9.29%	16	2004.00	632.33	11	16.92	-1.57%	9.29%	0/0
CAT	Long (n-bar)	2020-07-14	136.88	2020-07-28	138.03	0.84%	16.10	0.84%	14	1916.32	544.02	11	1.46	-5.59%	3.00%	0/0
CAT	Long (n-bar)	2020-07-28	138.03	2020-08-11	142.53	3.26%	63.00	3.26%	14	1932.42	895.71	11	5.73	-5.67%	4.34%	0/0
CAT	Open Long	2020-08-11	142.53	2020-08-21	138.43	-2.88%	-57.40	-2.88%	14	1995.42	603.42	10	-5.74	-4.83%	2.57%	0/0
CSCO	Long (n-bar)	2020-06-01	46.3	2020-06-15	45.35	-2.05%	-40.85	-2.05%	43	1990.90	667.06	12	-3.40	-5.75%	4.30%	0/0
CSCO	Long (n-bar)	2020-06-15	45.35	2020-06-29	46.15	1.76%	35.20	1.76%	44	1995.40	584.83	11	3.20	-3.13%	4.72%	0/0
CSCO	Long (n-bar)	2020-06-29	46.15	2020-07-14	46.26	0.24%	4.73	0.24%	43	1984.45	637.06	11	0.43	-1.80%	2.28%	0/0
CSCO	Long (n-bar)	2020-07-14	46.26	2020-07-28	46.28	0.04%	0.86	0.04%	43	1989.18	544.88	11	0.08	-2.29%	3.11%	0/0
CSCO	Long (n-bar)	2020-07-28	46.28	2020-08-11	47.19	1.97%	39.13	1.97%	43	1990.04	934.84	11	3.56	-1.94%	3.72%	0/0
CSCO	Open Long	2020-08-11	47.19	2020-08-21	42.25	-10.47%	-207.48	-10.47%	42	1981.98	395.94	10	-20.75	-11.57%	2.67%	0/0


Then Explorer

Ticker	Date/Time	Shares	
AXP	2020-06-01	20
AXP	2020-06-02	20
AXP	2020-06-03	20
AXP	2020-06-04	20
AXP	2020-06-05	20
AXP	2020-06-08	20
AXP	2020-06-09	20
AXP	2020-06-10	20
AXP	2020-06-11	20
AXP	2020-06-12	20
AXP	2020-06-15	19
AXP	2020-06-16	19
AXP	2020-06-17	19
AXP	2020-06-18	19
AXP	2020-06-19	19
AXP	2020-06-22	19
AXP	2020-06-23	19
AXP	2020-06-24	19
AXP	2020-06-25	19
AXP	2020-06-26	19
AXP	2020-06-29	21
AXP	2020-06-30	21
AXP	2020-07-01	21
AXP	2020-07-02	21
AXP	2020-07-06	21
AXP	2020-07-07	21
AXP	2020-07-08	21
AXP	2020-07-09	21
AXP	2020-07-10	21
AXP	2020-07-13	21
AXP	2020-07-14	21
AXP	2020-07-15	21
AXP	2020-07-16	21
AXP	2020-07-17	21
AXP	2020-07-20	21
AXP	2020-07-21	21
AXP	2020-07-22	21
AXP	2020-07-23	21
AXP	2020-07-24	21
AXP	2020-07-27	21
AXP	2020-07-28	21
AXP	2020-07-29	21
AXP	2020-07-30	21
AXP	2020-07-31	21
AXP	2020-08-03	21
AXP	2020-08-04	21
AXP	2020-08-05	21
AXP	2020-08-06	21
AXP	2020-08-07	21
AXP	2020-08-10	21
AXP	2020-08-11	19
AXP	2020-08-12	19
AXP	2020-08-13	19
AXP	2020-08-14	19
AXP	2020-08-17	19
AXP	2020-08-18	19
AXP	2020-08-19	19
AXP	2020-08-20	19
AXP	2020-08-21	19
BA	2020-06-01	13
BA	2020-06-02	13
BA	2020-06-03	13
BA	2020-06-04	13
BA	2020-06-05	13
BA	2020-06-08	13
BA	2020-06-09	13
BA	2020-06-10	13
BA	2020-06-11	13
BA	2020-06-12	13
BA	2020-06-15	10
BA	2020-06-16	10
BA	2020-06-17	10
BA	2020-06-18	10
BA	2020-06-19	10
BA	2020-06-22	10
BA	2020-06-23	10
BA	2020-06-24	10
BA	2020-06-25	10
BA	2020-06-26	10
BA	2020-06-29	10
BA	2020-06-30	10
BA	2020-07-01	10
BA	2020-07-02	10
BA	2020-07-06	10
BA	2020-07-07	10
BA	2020-07-08	10
BA	2020-07-09	10
BA	2020-07-10	10
BA	2020-07-13	10
BA	2020-07-14	11
BA	2020-07-15	11
BA	2020-07-16	11
BA	2020-07-17	11
BA	2020-07-20	11
BA	2020-07-21	11
BA	2020-07-22	11
BA	2020-07-23	11
BA	2020-07-24	11
BA	2020-07-27	11
BA	2020-07-28	11
BA	2020-07-29	11
BA	2020-07-30	11
BA	2020-07-31	11
BA	2020-08-03	11
BA	2020-08-04	11
BA	2020-08-05	11
BA	2020-08-06	11
BA	2020-08-07	11
BA	2020-08-10	11
BA	2020-08-11	11
BA	2020-08-12	11
BA	2020-08-13	11
BA	2020-08-14	11
BA	2020-08-17	11
BA	2020-08-18	11
BA	2020-08-19	11
BA	2020-08-20	11
BA	2020-08-21	11
CAT	2020-06-01	16
CAT	2020-06-02	16
CAT	2020-06-03	16
CAT	2020-06-04	16
CAT	2020-06-05	16
CAT	2020-06-08	16
CAT	2020-06-09	16
CAT	2020-06-10	16
CAT	2020-06-11	16
CAT	2020-06-12	16
CAT	2020-06-15	16
CAT	2020-06-16	16
CAT	2020-06-17	16
CAT	2020-06-18	16
CAT	2020-06-19	16
CAT	2020-06-22	16
CAT	2020-06-23	16
CAT	2020-06-24	16
CAT	2020-06-25	16
CAT	2020-06-26	16
CAT	2020-06-29	16
CAT	2020-06-30	16
CAT	2020-07-01	16
CAT	2020-07-02	16
CAT	2020-07-06	16
CAT	2020-07-07	16
CAT	2020-07-08	16
CAT	2020-07-09	16
CAT	2020-07-10	16
CAT	2020-07-13	16
CAT	2020-07-14	14
CAT	2020-07-15	14
CAT	2020-07-16	14
CAT	2020-07-17	14
CAT	2020-07-20	14
CAT	2020-07-21	14
CAT	2020-07-22	14
CAT	2020-07-23	14
CAT	2020-07-24	14
CAT	2020-07-27	14
CAT	2020-07-28	14
CAT	2020-07-29	14
CAT	2020-07-30	14
CAT	2020-07-31	14
CAT	2020-08-03	14
CAT	2020-08-04	14
CAT	2020-08-05	14
CAT	2020-08-06	14
CAT	2020-08-07	14
CAT	2020-08-10	14
CAT	2020-08-11	14
CAT	2020-08-12	14
CAT	2020-08-13	14
CAT	2020-08-14	14
CAT	2020-08-17	14
CAT	2020-08-18	14
CAT	2020-08-19	14
CAT	2020-08-20	14
CAT	2020-08-21	14
CSCO	2020-06-01	43
CSCO	2020-06-02	43
CSCO	2020-06-03	43
CSCO	2020-06-04	43
CSCO	2020-06-05	43
CSCO	2020-06-08	43
CSCO	2020-06-09	43
CSCO	2020-06-10	43
CSCO	2020-06-11	43
CSCO	2020-06-12	43
CSCO	2020-06-15	44
CSCO	2020-06-16	44
CSCO	2020-06-17	44
CSCO	2020-06-18	44
CSCO	2020-06-19	44
CSCO	2020-06-22	44
CSCO	2020-06-23	44
CSCO	2020-06-24	44
CSCO	2020-06-25	44
CSCO	2020-06-26	44
CSCO	2020-06-29	43
CSCO	2020-06-30	43
CSCO	2020-07-01	43
CSCO	2020-07-02	43
CSCO	2020-07-06	43
CSCO	2020-07-07	43
CSCO	2020-07-08	43
CSCO	2020-07-09	43
CSCO	2020-07-10	43
CSCO	2020-07-13	43
CSCO	2020-07-14	43
CSCO	2020-07-15	43
CSCO	2020-07-16	43
CSCO	2020-07-17	43
CSCO	2020-07-20	43
CSCO	2020-07-21	43
CSCO	2020-07-22	43
CSCO	2020-07-23	43
CSCO	2020-07-24	43
CSCO	2020-07-27	43
CSCO	2020-07-28	43
CSCO	2020-07-29	43
CSCO	2020-07-30	43
CSCO	2020-07-31	43
CSCO	2020-08-03	43
CSCO	2020-08-04	43
CSCO	2020-08-05	43
CSCO	2020-08-06	43
CSCO	2020-08-07	43
CSCO	2020-08-10	43
CSCO	2020-08-11	42
CSCO	2020-08-12	42
CSCO	2020-08-13	42
CSCO	2020-08-14	42
CSCO	2020-08-17	42
CSCO	2020-08-18	42
CSCO	2020-08-19	42
CSCO	2020-08-20	42
CSCO	2020-08-21	42


So it works (in this case 1 position per symbol).

1 Like

Hello FxShrat,
First of all, thank you very much for your great help.
I did not expect to receive a ready-made copy and paste solution from you. The direction to reach my goal is enough.

A matrix for this purpose would probably be very large and an additional loop would probably have to be built in over the list of all symbols. The back tester would be very slow. I have therefore limited myself to only querying the last value. In any case, it is not intended for calculations but only for information in exploration. And ... what happened before has not been changed anyway.

Nevertheless, I will test the approach with the matrix on occasion.

SetCustomBacktestProc("");
if (Status("action") == actionPortfolio) 
{
    cash = Null;
    OpenPosition = Null;
    StaticVarRemove("Cash");
    StaticVarRemove("GesamtWert");
    StaticVarRemove("OpenPosition");
    StaticVarRemove("PosStck_*");
    bo = GetBacktesterObject();
    bo.PreProcess();
    for (i = 0; i < BarCount; i++)
    {
        for (sig = bo.GetFirstSignal( i ); sig; sig = bo.GetNextSignal( i ) )
        {	
			if (sig.IsEntry())
			{
				prev_close = StaticVarGet("PrevClose_" + sig.Symbol);
				CashAvailable = bo.Cash / (MaxPos - bo.GetOpenPosQty());
				SharesNeed = CashAvailable / prev_close[ i ];
				SharesNeed = round(SharesNeed);
				sig.PosSize = -2000 - SharesNeed;
				//sig.PosSize = bo.Cash / (MaxPos - bo.GetOpenPosQty()); // works
			}
			if (sig.IsScale())
			{
				ScaleOutPercent = StaticVarGet("PosScaleOut_" + sig.Symbol);
				
				// Calculate on Shares
				//MyTrade = bo.FindOpenPos(sig.Symbol);
				//ScaleOutPosSize = round((MyTrade.Shares * ScaleOutPercent) / 100);
				//sig.PosSize = -2000 - ScaleOutPosSize; // works
				
				// Calculate on Percent
				sig.PosSize = -1000 - ScaleOutPercent; // works
			}
        }	
        bo.ProcessTradeSignals( i );
        if ( i == BarCount -1)
        {
			for (openPos = bo.GetFirstOpenPos(); openPos; openPos = bo.GetNextOpenPos)
			{
				StaticVarSet("PosStck_" + openPos.Symbol, openPos.Shares);
			}
        }
        OpenPosition[i] = bo.GetOpenPosQty();
        cash[ i ] = bo.cash;
    }
    bo.PostProcess();
    StaticVarSet("Cash",cash);
    StaticVarSet("GesamtWert",bo.EquityArray);
    StaticVarSet("OpenPosition",OpenPosition);
}

and in Exploration

AddColumn(IIf(Status("lastbarinrange"), StaticVarGet("PosStck_" + Name()), 0) , "PosStck",1.0);
AddColumn(IIf(Status("lastbarinrange"), StaticVarGet("PosStck_" + Name()), 0) * Close , "PosValue",1.2);

Thanks again