Backtesting multiple systems with different parameters

Hi

I want to backtest 5 systems together, the only difference between systems is their length of the indicator. Each system is allowed to open max 5 positions, so 5 systems together allow 25 positions.
Also i don’t want to take repeated signals from individuals systems, but as a whole system i am fine with taking repeated signals in the same stock through different individual systems.

buy1=...
buy2=...
buy3=...
buy4=...
buy5=...
Buy=buy1 or buy2 or buy3 or buy4 or buy5;

So i don’t want repeated signals from individual systems ( ie in buy1,buy2…etc)…but as a whole system if repeated signals come then its fine( For ex: buy1 can be true in stock1 , then buy2 can be true in same stock 1 after a period)

@akshay.gupta55 have you included the ExRem function in your codes?

https://www.amibroker.com/guide/afl/exrem.html

It might help to see an example of what you are coding.

To right the whole code would be tedious, but let me give a picture of what i am trying to do.
I want to run several systems of exactly same type but just different parameters (for ex 5 systems with different moving average parameters). So i want to run my top 5 optimized systems, instead just one. Basically the way i want to run is top 5 systems run independently as if were individual systems. The problem is i could write 5 different afl scripts to run, which would be fine. But i am looking for a solution in which i can run them in 1 script .

@akshay.gupta55

Have you heard of “copy and paste”? Two or three keystroke/mouse clicks too tedious?

Anyway here is a code for testing multiple system simultaneously. It can serve as a nice starting point for you to create a version suited to your needs. Perhaps when you have completed it you can “give back” and share with us or post in the User Library?

http://www.amibroker.com/members/library/detail.php?id=1227

3 Likes

@portfoliobuilder
Thanks for teaching how to " Copy and paste " . I am a novice , when it comes to Amibroker, so will definitely share if i can add value in the forum. I don’t understand the point of sarcasm here, when you know something just tell it, instead of trying to make remarks about copy and pasting. You are not being forced to reply on the forum.

1 Like

@akshay.gupta55 the comment serves three purposes, a form of humor ( I get a good laugh at least :smile:) and secondly as you wrote there are many “novices” who perhaps do not realize they can easily post the codes with “copy and paste”, and finally it may serve as a reminder to the poster that the rest of us on the forum are not foolish enough to believe that “writing the code would be too tedious”, so do not take us for fools.

Don’t be so sensitive, I did make an honest effort to find a useful code for you and am happy to help in the future if I am able.

3 Likes

That is true, and as I can see, you follow this rule:

@akshay.gupta55 ---> Przechwytywanie

@portfoliobuilder ---> Przechwytywanie

Before you tell @portfoliobuilder how he should behave here, consider who are you replying to. He was trying to help you (just like he has already helped lots of other users) instead of the fact, that you wrote, that presenting your actual code would be too tedious for you.

7 Likes

@akshay.gupta55 Back a few post ago when I originally suggested that you post some code it was because in general it helps people find a solution if they can make changes and test those changes on the original code. You do not need to reveal any secret system but can replace your Buy and Sell secrets with generic moving average crossovers or over sold RSI signals.

Too many times newbie/novice users will ask for help solving a problem but not post the code that is causing the problem. Thus no help is offered. That may not apply to your case @akshay.gupta55 but that is why it is usually a good idea to post code.

As for the “copy and paste” comment, I thought it was funny but members of this forum come from all over the world and have different ideas on humor. But @portfoliobuilder did make some other very good points. “too tedious”, really?

Let’s not forget to keep having fun on the forum

2 Likes

Thanks for help guys :peace_symbol:.

1 Like

I used idea posted by @portfoliobuilder in http://forum.amibroker.com/t/backtesting-two-or-more-strategies-simultaneously/795/7. Thanks for the code.

So following is the code which helped me in backtesting the strategies.

s = Optimize( "system", 0, 0, 4, 1 );
switch(s)
{
case 0:
Buy=buy1 ;
sell=sell1;
BuyPrice=C;
SellPrice=C;
PositionScore=roc(2);
break;

case 1:
Buy=buy2 ;
sell=sell2;
BuyPrice=C;
SellPrice=C;
PositionScore=roc(2);
break;

case 2:
Buy=buy3 ;
sell=sell3;
BuyPrice=C;
SellPrice=C;
PositionScore=roc(2);
break;

case 3:
Buy=buy4 ;
sell=sell4;
BuyPrice=C;
SellPrice=C;
PositionScore=roc(2);
break;

case 4:
Buy=buy5 ;
sell=sell5;
BuyPrice=C;
SellPrice=C;
PositionScore=roc(2);
break;
}

SetCustomBacktestProc( "" );

/* Custom-backtest procedure follows */

if( Status( "action" ) == actionPortfolio )
{
    bo = GetBacktesterObject();
    bo.Backtest(); // run default backtest procedure
    StaticVarSet( "system" + s, bo.EquityArray() );
   // Code for generating trade list in csv file 
}

I would like to generate trade list in csv file , through custom backtester. Is there a way i could do that…? I saw ListTrades() method in custom backtester , but dont know how to use it get a csv file ?

@akshay.gupta55 I don't know how to use the CBI for that but you can get your trades into a csv file via other methods. You can select all and copy and paste (:smile:) either from the AA window of results, or from the Backtest Report results page.
image

A few links that might be helpful too,
http://www.amibroker.com/kb/2014/12/10/copy-backtest-result-to-excel/

http://www.amibroker.com/kb/2014/09/18/how-to-print-result-list-from-analysis-window/

Not sure if this one is helpful but worth a look,
http://www.amibroker.com/kb/2014/09/30/gen-backtest-from-a-file/

1 Like

How to get a single equity curve for multiple systems?

You can start by looking at the links in this post from @Tomasz: Duplicate: How can I backtest a portfolio of trading systems sharing a single max position and different exits

1 Like

Hello,
I'm also looking for a multiportfolio-system to backtest two completely different systems in one portfolio with one equity curve.
In the code below is a system from the forum, I think it could fit for my requirements. My problem is that there comes the error "insufficient funds...". I know this error, but I can't find out why in this code. The size is 10 % or 20 % and there is enough equity.
If I activate the code line "size = 10" or "buy = 1" after the multiplex the system buys and sells stocks but then the requirement two systems with different parameters is not considered (attachment)
error_multiportfolio
Can someone give me a hint how to make work the code ?
Thanks for every help


function multiplex(Buy1, Sell1, BuyPrice1, SellPrice1,size1,PositionScore1, 
	Buy2, Sell2, BuyPrice2, SellPrice2, size2, PositionScore2)
{
	global Buy, Sell, PositionScore, size;
	Buy = Sell = False;
	PositionScore = 0;
	size = 0;
	BuyPrice =SellPrice = Null;
	sys = 0;
	for(i = 0; i < BarCount; i++)
	{
		switch(sys)
		{
			case 0:
			if(Buy1[i] && Buy2[i])
			{
				if(PositionScore1[i] >= PositionScore2[i])
				{
					sys = 1;
					size[i] = size1[i];
					BuyPrice[i] = BuyPrice1[i];
					PositionScore[i] = PositionScore1[i];
					Buy[i] = buy1[i];
				}
				else
				{
					sys = 2;
					size[i] = size2[i];
					BuyPrice[i] = BuyPrice2[i];
					PositionScore[i] = PositionScore2[i];
					Buy[i] = Buy2[i];
				}
			}
			else if (Buy1[i])
			{
				sys = 1;
				size[i] = size1[i];
				BuyPrice[i] = BuyPrice1[i];
				PositionScore[i] = PositionScore1[i];
				Buy[i] = Buy1[i];
			}
			else if (Buy2[i])
			{
				sys = 2; //both used at the same time;
				size[i] = size2[i];
				BuyPrice[i] = BuyPrice2[i];
				PositionScore[i] = PositionScore2[i];
				Buy[i] = Buy2[i];
			}
			break;
			case 1: //system 1 in use
			if(Sell1[i])
			{
				sys = 0;
				Sell[i] = Sell1[i];
				SellPrice[i] = SellPrice1[i];
			}
			break;
			case 2:
			if(Sell2[i])
			{
				sys = 0;
				Sell[i] = Sell2[i];
				SellPrice[i] = SellPrice2[i];
			}
		}
	}
	return;
}
				

buy1 = Ref(C,-1) > Ref(MA(C, 30),-1);
Sell1 = Ref(C,-1) < Ref(MA(C, 30),-1);
size1 = 10;
PositionScore1 = 10;
BuyPrice1 = SellPrice1 = C; 
r = RSI();
buy2 = Ref(C,-1) > Ref(MA(C, 50),-1);
Sell2 = Ref(C,-1) < Ref(MA(C, 50),-1); 
size2 = 20;
BuyPrice2 = SellPrice2 = O;
PositionScore2 = 20;

multiplex(Buy1, Sell1, BuyPrice1, SellPrice1,size1,PositionScore1, 
	Buy2, Sell2, BuyPrice2, SellPrice2, size2, PositionScore2);

//buy = 1;
//size = 10;
Short = Cover = False;
SetPositionSize(size, spsPercentOfEquity);
SetOption("MaxOpenPositions", 10 );  // 10 and 100 as max pos makes 7 point 
SetOption("InitialEquity", IE=100000);  
SetOption("AllowPositionShrinking", 0);   
SetOption("CommissionMode", 1);   
SetOption("CommissionAmount", 0);
SetOption("AllowSameBarExit", False);
SetOption("PriceBoundChecking", True);
SetOption("ActivateStopsImmediately", False);
Filter = Buy OR Sell;
AddColumn(Buy, "Buy", 7.0);
AddColumn(Sell, "sell", 7.0);
AddColumn(size, "size", 7.0);
AddColumn(positionscore, "score", 7.0);