I am trying to exit all trades when the total portfolio amount gets to a certain point. I've tried to write a custom backtest, but it does not generate any results.
Here is the code:
PosSize = 2000;
maxpos = 40; // maximum number of open positions
SetOption("InitialEquity", 100000 ); // set initial equity = 50K
SetOption( "MaxOpenPositions", maxpos );
SetPositionSize( 5.0, spsPercentOfEquity );
SetPositionSize( PosSize, spsValue );
SetTradeDelays( 0, 0, 0, 0 );
SetOption( "ActivateStopsImmediately", True );
ShortPrice = Open;
Stocks =
TimeNum() >= 093000
AND TimeNum() <= 093100;
Short = Stocks;
Cover =
TimeNum() >= 155000
AND TimeNum() <= 160000;
Buy = 0;
Sell = 0;
ApplyStop( stopTypeProfit, stopModePercent, 3.0, 1) ;
ApplyStop( stopTypeLoss, stopModePercent, 8.0, 1 );
ApplyStop( stopTypeNBar, stopModeBars, 390, ExitAtStop = 0 );
//////////////////////////////////////////////////////////////////////////
SetCustomBacktestProc("");
if (Status("action") == actionPortfolio) {
bo = GetBacktesterObject();
bo.PreProcess();
initEquity = bo.InitialEquity;
for (i = 0; i < BarCount; i++) {
if ( bo.Equity - initEquity >= 100 ) {
for (trade = bo.GetFirstOpenPos(); trade; trade = bo.GetNextOpenPos()) {
bo.ExitTrade(i, trade.Symbol, trade.GetPrice(i, "C"), 3);
}
}
bo.HandleStops(i); // Handle programmed stops at this bar
bo.UpdateStats(i, 1); // Update MAE/MFE stats for bar
bo.UpdateStats(i, 2); // Update stats at bar's end
}
//bo.Backtest(); // Run backtests
bo.PostProcess();
}
///////////////////////////////////////////////////////////////////////////