Hello,
I am trying optimize in CBT. Based on google search results (cbt optimize site:amibroker.com) I saw that it should work but I did not find any details. I tried an optimize inside and outside the CBT but I receive the error message:
You have not specified any variables for optimization
Do you have any hints (or links)?
Example:
SetBacktestMode( backtestRegularRaw );
//======SETTINGS====================
SetOption( "PortfolioReportMode", 0 ); // 0 - trade list 1 - detailed log 2 - summary
SetOption( "GenerateReport", 1 ); // 0 suppress generation of report 1 force generation of full report
SetOption( "InitialEquity", 100000 );
RoundLotSize = 1;
SetTradeDelays( 0, 1, 1, 1 );
maxopenpos = 10 ;
SetOption( "MaxOpenPositions", maxopenpos );
SetPositionSize( 100 / maxopenpos, spsPercentOfEquity );
SetOption( "CommissionMode" , 3 );
SetOption( "CommissionAmount" , 0.01 );
SetOption( "holdminbars", 10 );
SetOption( "MinShares", 1 ); //
PositionScore = 1000 + ROC( Close, 100 );
Buy = Ref( C > ma( C, 100 ), -1 ) ;
Sell = C < ma( C, 100 ) ;
SetCustomBacktestProc( "" );
if( Status( "action" ) == actionPortfolio )
{
bo = GetBacktesterObject(); // Get backtester object
bo.PreProcess(); // Do pre-processing
stopmin = Optimize( "Stop", 0.2, 0.1, 0.4, 0.1 ) ;
for( i = 0; i < BarCount; i++ ) // Loop through all bars
{
for( sig = bo.GetFirstSignal( i ); sig; sig = bo.GetNextSignal( i ) )
{
// Loop through all signals at this bar
Posopen = bo.FindOpenPos( sig.Symbol );
if( sig.IsEntry() )
{
bo.EnterTrade( i, sig.Symbol, sig.IsLong(), sig.Price, sig.PosSize );
}
bo.UpdateStats( i, 1 );
if( sig.IsExit() )
bo.ExitTrade( i, sig.Symbol, sig.Price );
}
bo.HandleStops( i ); // Handle programmed stops at this bar
for( trade = bo.GetFirstOpenPos(); trade; trade = bo.GetNextOpenPos() ) //loop through open positions
//======STOP LOSS====================
if( trade.BarsInTrade > 1 AND trade.GetPrice( i, "L" ) <= trade.EntryPrice * ( 1 - stopmin ) )
{
bo.ExitTrade( i, trade.Symbol, trade.GetPrice( i, "C" ), 2 ); // (1 - regular exit, 2 - max. loss, 3 - profit, 4 - trail, 5 - N-bar, 6 - ruin)
}
bo.UpdateStats( i, 1 ); // Update MAE/MFE stats for bar
bo.UpdateStats( i, 2 ); // Update stats at bar's end
} // End of for loop over bars
bo.PostProcess(); // Do post-processing
// Here we add custom metric to backtest report
}
Thanks,
Peter