Can I specify the symbol name explicitly in Custom backtester

Hello from India wonderful people.

I had a simple query, is it possible to specify which symbol you wish to run the custom backtest on?

I am writing a code to evaluate the performance of a strategy on all symbols in a watchlist. and see which ones perform best. I know this can be done through Individual backtest feature. But I want to save the equity curves too and then plot them all together. The afl to plot different equity curves is ready(picture attached). But I dont think the custom backtester(posting below) works as should.
scripts
Thanks so much guys, love this forum.

#include<Working/Price.afl>

list = CategoryGetSymbols( categoryWatchlist, 5 );
length = 167;
//Using optimize function to walk through each script. It will become evident why I'm using this and not a basic backtest with watchlist filters
script = Optimize("Script number", 136, 1, length, 1);

//get symbol to pass to SetForeign function
symbol = StrExtract( list, script-1 );
sym = StrExtract(symbol, 0, '-');

//the strategy. It's inverse of turtle. Counter-intuitive but works for certain script
sl_mult = 3;
tp_mult = 10;
tr_mult = 2;
Buy = Short = 0;
SetForeign(symbol);
	f_c = C;
	f_H = H;
	f_L = L;
	f_ATR50 = ATR(50);
	len = 10;
	Buy = Cross(LLV(Ref(f_L, -1), len), f_C);
	Short = Cross(f_C, HHV(Ref(f_H, -1), len));
	Sell = 0;
	Cover = 0;
	ApplyStop(stopTypeLoss, stopModePoint, sl_mult * f_ATR50,True );
	ApplyStop(stopTypeProfit, stopModePoint, tp_mult * f_ATR50,True );	
	ApplyStop(stopTypeTrailing, stopModePoint, tr_mult * f_ATR50,True );	
	
        // Ok. can we add this inside SetForeign scope? 
        // I am doing this so that I can save the equity curve of every script in a new symbol and then display them all in one chart. and find the scripts which gives best result for the strategy
       // its not working as its supposed to because I think what the backtester is doing is taking the buy/sell signals from different scripts and putting it on the script that is "current" in the backtester. Can I specify the script in backtester object?
	SetCustomBacktestProc("");
	if( Status("action") == actionPortfolio )
	{
		_TRACEF(NumToStr(LastValue(f_H)) + sym);
		bo = GetBacktesterObject();
		bo.Backtest();
		AddToComposite( bo.EquityArray,
						"~~~STRATEGY" + sym, "X",
						atcFlagDeleteValues | atcFlagEnableInPortfolio );
	}	
RestorePriceArrays();	

PlotShapes(IIf(Buy, shapeUpArrow, Null), C_green_light, 0, f_L, -30);
PlotShapes(IIf(Short, shapeDownArrow, Null), C_red_light, 0, f_H, -30);


1 Like

Sorry., this AFL can't work. Thanks

ok. thanks for that. I understand this doesn't work.

I want to know how can I achieve symbol-wise equity curves for a particular strategy. Can you help me with that?

Hi @ninjasoar,
please valid your amibroker license before asking for something in this forum.


That said, I'm not sure what you are really asking for. If you simply want to keep equity curves for other use, why don't you use backtesting on all tickers in your (watch?)list to produce equity curves ? No need for SetForeign()/RestorePriceArrays(). Then you can access produced equity curves very simply using Foreign() read https://www.amibroker.com/guide/a_addtocomposite.html
An alternative: store your curves in arrays with StaticVarSet() and retrieve them with StaticVarGet()
Good luck

1 Like