// 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 = 2;
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, 50 ) );
exitlong = Cross( MA( C, 50 ), 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
{
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 ) )
{
sigsymbol = StrReplace( StrReplace( sig.Symbol, "~System1_", "" ), "~System2_", "" );
RSIvar = StaticVarGet( "WL_RSI" + sig.Symbol );
// iterate through opensymbol list
for ( num = 0; ( symbol = StrExtract( OpenSymbol, num ) ) != ""; num++ )
{
// if signal symbol is part of open symbol list then don't enter
//if( StrFind( symbol, sigsymbol ) && sig.IsLong )
//sig.Price = -1;
// or
// if signal symbol is not part of open symbol list then check position score for new entry
if ( StrFind( symbol, sigsymbol ) == 0 && sig.IsLong )
sig.PosScore = 1000 - RSIvar[i];
else
sig.PosScore = 0;
}
}
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();
}