how to perform a backtest using 2 ts?

#21
dear Trash,
I've studied your TS and amended it a little bit. Unfortunately it still buy same symbol in the same period.
using "_trade()" functions I've been able to understand the problem comes from the following instruction: sig.Price = -1 that should remove the overlapping trade from portfolio but it doesnt work.
Is there another way to delete a trade?
"sig.PosScore = 0" means 'never entry' or just gives low priority?


Here you are the complete TS:

// 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

systemnum = 10;
SetBacktestMode( backtestRegular);

SetOption( "InitialEquity", 100000 );
SetOption( "FuturesMode", 0 );
SetOption( "MaxOpenPositions", systemnum * 1 );
SetOption( "AllowPositionShrinking", 1 );
SetOption( "ExtraColumnsLocation", 1 );

membership = "";
enterlong = exitlong = 0;
entershort = exitshort = 0;
size = 20;

if ( InWatchList( WL1 ) )
{
enterlong = Cross( C, MA( C, 20 ) );
exitlong = Cross( MA( C, 20 ), C );
entershort = 0;
exitshort = 0;

SetPositionSize( size/2, spsPercentOfEquity );
membership = "System1";
}

if ( InWatchList( WL2 ) )
{
enterlong = Cross( C, MA( C, 100 ) );
exitlong = Cross( MA( C, 100 ), C );
entershort = 0;
exitshort = 0;

SetPositionSize( size/2, spsPercentOfEquity );
membership = "System2";
}

Buy = Ref( enterlong, -1 );
Sell = Ref( exitlong, -1 );

Short = Ref( entershort, -1 );
Cover = Ref( exitshort, -1 );

BuyPrice = SellPrice = Open;
ShortPrice = CoverPrice = Open;

StaticVarSetText( "WL_Membership" + Name(), membership );
StaticVarSet( "WL_RSI" + Name(), RSI( 15 ) );

SetCustomBacktestProc("");
/* Custom-backtest procedure follows */
if ( Status( "action" ) == actionPortfolio )
{
bo = GetBacktesterObject();
bo.PreProcess(); // Do pre-processing (always required)

OpenSymbol = "";

for ( i = 0; i < BarCount; i++ ) // Loop through all bars

{
OpenSymbol = "";

for ( OpenPos = bo.GetFirstOpenPos(); OpenPos; OpenPos = bo.GetNextOpenPos() )
{
// create list of symbols and remove composite prefixes
OpenSymbol += StrReplace( StrReplace( Openpos.Symbol, "~System1_", "" ), "~System2_", "" )+",";
}

for ( sig = bo.GetFirstSignal( i ); sig; sig = bo.GetNextSignal( i ) )
{ if (sig.isentry())
{ sigsymbol = StrReplace( StrReplace( sig.Symbol, "~System1_", "" ), "~System2_", "" );
if (StrFind(Opensymbol, sigsymbol))
{ sig.Price = -1;
}
}
bo.ProcessTradeSignals( i ); // Process trades at bar (always required)
}
}
//#####################################################################################

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.PostProcess();
}



Thank you so much for your help!
Cippo
 

Attachments

Last edited:

Similar threads