how to perform a backtest using 2 ts?

#1
Dear all,


I have 2 TS that I would like to use together but giving higher priority to 1 of them. In other terms: I want to apply the first TS still no more trades are available for it. If some equity are still available, I want to buy with the second TS (if market matches its buy conditions).

Are there in Amibroker functions that can help me?

thank you

cippo
 

trash

Well-Known Member
#4
Dear all,


I have 2 TS that I would like to use together but giving higher priority to 1 of them. In other terms: I want to apply the first TS still no more trades are available for it. If some equity are still available, I want to buy with the second TS (if market matches its buy conditions).

Are there in Amibroker functions that can help me?

thank you

cippo
You can make clones of your symbol(s) i.e. with prefix ~System1_APPL if symbol is AAPL. Second system has prefix ~System2_APPL. You can create those ones programmatically via AddtoComposite in around a 1 second. Note: tilde at start of composite symbol is mandatory!


Then move your system1 and system2 cloned symbols to a watchlist.

Code:
SetBacktestMode( .... );

SetOption( "InitialEquity", 100000 );
SetOption( "FuturesMode", 0 );
SetOption( "MaxOpenPositions", 2 ); // number of systems here
SetOption( "AllowPositionShrinking", 1 );

watchlist = InWatchList( 1 );// watchlist membership
nm = Name();

if( Status( "actionex" ) == actionBacktest AND watchlist )
{
	Short = Cover = 0;
	enterlong = exitlong = 0;	

	if ( StrFind( nm, "System1" ) )// first symbol clone and system
	{
		enterlong = Cross( C, MA( C, 20 ) );
		exitlong  = Cross( MA( C, 20 ), C );
		
		SetPositionSize( 50, spsPercentOfEquity ); // 50% into first system
	}

	if ( StrFind( nm, "System2" ) )// second symbol clone and system
	{
		enterlong = Cross( C, MA( C, 50 ) );
		exitlong  = Cross( MA( C, 50 ), C );
		
		SetPositionSize( 50, spsPercentOfEquity ); // 50% into second system
	}	

	Buy  = Ref( enterlong, -1 );
	Sell = Ref( exitlong, -1 );
	
	BuyPrice = SellPrice = Open;
}
If you wanna do any special equity stuff then use Custom backtest interface of AmiBroker.



Thank you very much for your e-mail.

The difference comes from the fact that you're getting a situation where:
- both systems have an entry signal on the same bar
- one of them is skipped due to lack of funds.

Since there is no position-scoring defined that would prefer one _system_ over the other and position-sizing is identical,
then alphabetical order matters which is processed first. So – that's always SYSTEM1 symbol,
which is being entered and SYSTEM2 which is being skipped in such case.

Please make sure that “Allow position size shrinking” option is turned on, to avoid such situations.
Or add some scoring that would prefer rules A over rules B (not the ticker name, but actual rules),
so the order of signals in portfolio would be the same (as far as given set of rules is concerned).

Just to remind - if PositionScore is not defined or it has the same value for two or more symbols:
1. transaction of greater positionSize (in $) are preferred, then
2. alphabetical order
3. Long trades rather than short trades, if both occur at the same time for the same stock.

Dont understand you clearly but might try using Exrem..
No, this is totally wrong. Besides Exrem is not required in analysis.
 
Last edited:
#5
thank you very much, Trash.
You understood what I meant.

But I would like using this double system on a portfolio of symbols with only 1 open position for each symbol.

for example: first bar
SymbolA ==> match buycondition of system1 so buy for 10% of equity
SymbolB ==> match buycondition of system2 so buy for 10% of equity
SymbolC ==> doesn't match conditions for both system
SymbolD ==> doesn't match conditions for both system
...

2nd bar
SymbolA ==> position still open
SymbolB ==> position still open
SymbolC ==> doesn't match conditions for 1st system but match 2nd system condition, so buy for 10% of equity
SymbolD ==> match conditions for 1st system so buy for 10% of equity
...

in other words, for each bar I need to check the whole portfolio for 1st system (the one with higher priority), then if there are some equity still available I need to check again the whole portfolio for the second system

Another problem that I see is that with your proposal I can open a position following the rules of the 1st system and sell that position following the condition of the second system. And I would like to avoid it. What I buy with 1st system has to be sold with the 1st system. And the same for the 2nd system.


thank you

Cippo
 

trash

Well-Known Member
#6
thank you very much, Trash.
You understood what I meant.

But I would like using this double system on a portfolio of symbols with only 1 open position for each symbol.

for example: first bar
SymbolA ==> match buycondition of system1 so buy for 10% of equity
SymbolB ==> match buycondition of system2 so buy for 10% of equity
SymbolC ==> doesn't match conditions for both system
SymbolD ==> doesn't match conditions for both system
...

2nd bar
SymbolA ==> position still open
SymbolB ==> position still open
SymbolC ==> doesn't match conditions for 1st system but match 2nd system condition, so buy for 10% of equity
SymbolD ==> match conditions for 1st system so buy for 10% of equity
...

in other words, for each bar I need to check the whole portfolio for 1st system (the one with higher priority), then if there are some equity still available I need to check again the whole portfolio for the second system

Another problem that I see is that with your proposal I can open a position following the rules of the 1st system and sell that position following the condition of the second system. And I would like to avoid it. What I buy with 1st system has to be sold with the 1st system. And the same for the 2nd system.


thank you

Cippo
That's what the sample method in post #4 does (possible additional custom requirements not included)! Why do you think I have added it?
 
#7
Yes! you are right. The program does just what I meant. I had not studied it in deep and still not tested.

here:
SetOption( "MaxOpenPositions", 2 ); // number of systems here

I used 1 instead of 2, cause I want max 1 position for each system and each symbol. And it works well with 'SetOption( "MaxOpenPositions", 1 ).

Question:
I would like 1 openposition for each original symbol, too. So, if system1 opens a position for ~System1_APPL I don't want a position is open for ~System2_APPL.

in AFL it becomes something like this:

if ( StrFind( nm, "System2" ) )
{
enterlong = Cross( C, MA( C, 50 ) ) and status ~System1_"+nm not inthemarket


But I don't know how to properly write this condition: status ~System1_"+nm not inthemarket

This is one of my many lack of information about Amibroker, I have as well open a specific thread about this question without solving the problem.
http://www.traderji.com/amibroker/94473-recognize-if-out-market.html
 
Last edited:

trash

Well-Known Member
#9
Simply create a custom column using custom backtest interface. Then you see in the result list which system is used. Or as I previously mentioned create a clone of your used symbol via ATC and clones having prefix using your system names. ....

Example

Code:
// system membership
// by trash
// http://www.traderji.com/amibroker/95243-how-perform-backtest-using-2-ts.html#post1007370

WL1 = 0; // watchlist 1
WL2 = 1; // watchlist 2

SetOption( "InitialEquity", 100000 ); 
SetOption( "FuturesMode", 0 ); 
SetOption( "MaxOpenPositions", 2 ); // number of systems here 
SetOption( "AllowPositionShrinking", 1 );
SetOption( "ExtraColumnsLocation", 1 );

membership = "";

if ( InWatchList( WL1 ) )
{
    Buy = Cross( C, MA( C, 20 ) ); // buy when close crosses ABOVE moving average
    Sell = Cross( MA( C, 20 ), C ); // sell when closes crosses BELOW moving average
    Short = Cover = 0;
    
    SetPositionSize( 50, spsPercentOfEquity );
    
    membership = "System1";
}

if (  InWatchList( WL2 ) )
{
    Buy = Cross( C, MA( C, 50 ) ); // buy when close crosses ABOVE moving average
    Sell = Cross( MA( C, 50 ), C ); // sell when closes crosses BELOW moving average
    Short = Cover = 0;
    
    SetPositionSize( 50, spsPercentOfEquity );
    
    membership = "System2";
}
     

StaticVarSetText( "WL_Membership" + Name(), membership ); 

SetCustomBacktestProc("");  
/* Custom-backtest procedure follows */
if( Status("action") == actionPortfolio ) 
{ 
     bo = GetBacktesterObject();
     bo.Backtest(1); // run default backtest procedure 

    for (trade = bo.GetFirstTrade(); trade; trade = bo.GetNextTrade())
    {
        system = StaticVarGetText( "WL_Membership" + trade.Symbol );
        trade.AddCustomMetric( "System", system ); 
    }
    for (trade = bo.GetFirstOpenPos(); trade; trade = bo.GetNextOpenPos())
    {
        system = StaticVarGetText( "WL_Membership" + trade.Symbol );
        trade.AddCustomMetric( "System", system ); 
    }
    
    bo.ListTrades();
}
 
Last edited:
#10
Dear trash, thank you very much.
It works very well and does just what I meant.

May you explain me something more about your program? I'm not so used in object programs. it would be very helpful if you added some rem text to each of new rows.
I don't understand how it avoids to entry in a new position if there is another one already open for the same symbol with the other system.

thank you

cippo.
 
Last edited:

Similar threads