Here is a snippet that adds some noise to 2 different strategies with the intention to find the one to stick to .
noise applied to :
- trade prices - by adding a variable spread to entry price
- execution - by skipping entries and random position closing
the code first is run in optimization to collect the trade lists and perform MC then in exploration do display results. The output is a matrix of metrics ( return and drawdown ) , respective strategy and variations.
Enjoy.

SetOption( "priceboundchecking", false );
SetOption("ExtraColumnsLocation", 1 );
variations = 3;
strategies = 2;
j = Optimize( "variation", 0, 0, variations - 1, 1 );
k = Optimize( "strategy", 0, 0, strategies - 1, 1 );
Buy = Sell = Short = Cover = 0;
switch( k ) // strategies
{
case 0 : // strategy #1
Buy = Cross( C, Ref( High , -1 ) );
ApplyStop( stopTypeNBar, stopModeBars, 1 );
break ;
case 1: // strategy #2
Buy = Cross( C, Ref( C, -1 ) );
ApplyStop( stopTypeNBar, stopModeBars, 1 );
break ;
}
switch( j )// variations
{
case 0: //randomimze entry price
buyprice *= 1 + 0.002 * mtRandomA();
break ;
case 1:// skip entry
Buy *= mtRandomA() < 0.2;
break ;
case 2: // randomly close position
Sell = mtRandom() < .1;
break ;
}
PositionSize = 10000;
if( Status( "actionex" ) == 13 )
{
StaticVarRemove( "*" );
StaticVarSet( "counter" , 0 );
}
runs = strategies * variations ;
SetCustomBacktestProc( "" );
if( Status( "action" ) == actionPortfolio )
{
bo = GetBacktesterObject();
bo.Backtest( 1 );
bo.ListTrades();
list = StaticVarGet( "temp" );
if( IsNull( list ) )
list = Matrix( 100000 , runs , - 100 ) ;
i = 0;
counter = StaticVarGet( "counter" );
//_TRACEf ( "strategy %.f\t variation %.f", k , j ) ;
for( trade = bo.GetFirstTrade(); trade; trade = bo.GetNextTrade() )
{
list[i][counter] = trade.GetPercentProfit;
i++;
}
StaticVarSet( "temp", list );
if( counter == runs - 1 )
{
StaticVarSet( "TradeLists", list );
StaticVarRemove( "temp" );
}
StaticVarAdd( "counter", 1 );
}
list = StaticVarGet( "TradeLists" ) ;
if( list )
{
m = MxGetSize( list, 0 ) ;
n = MxGetSize( list, 1 );
// MC parameters
metrics = Matrix( n , 4 );
Random_samples = 100;
simulations = 100;
rank = 25;
j = strategy = var= write = 0;
for( ; j < n; j ++ )
{
item = MxGetBlock( list , 0, m - 1, j , j ) ;
len = MxSum( item != -100 ) ;
item = MxGetBlock( item , 0, len - 1, 0 , 0 ) ;
item = item / 100 + 1;
results = Matrix( simulations , 2 ) ;
i = 0;
for( ; i < simulations ; i ++ )
{
e = maxe = 1;
mdd = 0;
count = 0 ;
while( count < Random_samples )
{
r = floor( mtRandom() * len ) ;
e *= item[r][0];
maxe = Max( maxe, e ) ;
mdd = Max( mdd , maxe / e - 1 );
count ++;
}
results [i][0] = e;
results [i][1] = -mdd;
}
results = MxSortRows( results , True , 0 );
//results = MxSort ( results , 1 );
p = floor( rank / 100 * simulations );
metrics[j][0] = results[p][0];
metrics[j][1] = results[p][1];
if( J % variations ){
write = 1;
var ++;
}
else
if( write )
{
strategy ++ ;
write = var = 0;
}
metrics[j][2] = strategy;
metrics[j][3] = var;
}
_TRACE( "25-th Percentile {Return25, MaxDD25, Strategy, Variation}" );
_TRACE( MxToString( metrics ) );
SetOption( "NoDefaultColumns", True );
Filter = 1;
m = MxGetSize ( metrics , 0 );
n = MxGetSize ( metrics , 1 );
columns = "25-th Return,25-th MaxDD,strategy,variation";
for ( i = 0 ; i < n; i ++ )
AddColumn( null, StrExtract ( columns , i ) );
for( i = 0; i < m; i++ )
{
row = StrFormat( "%.2f\t%.2f\t%.f\t%.f", metrics [i][0], metrics[i][1],metrics[i][2], metrics[i][3] );
AddRow( row );
}
}