I am trying to implement the logic
a) If unrealised loss in any open-position exceeds maxLossOpenPos then exit that position
b) If total loss of all closed trades added to unrealised loss of all open positions exceeds MaxLossPerDay then exit all open positions and ignore any new signal for that day (i.e. no more trading that day)
My understanding of CBT is quite limited and I came up with below code but it is not working.
Please help me. Also if you could point me to a thread which is of similar topic then it will be useful to me. Thankyou
_SECTION_BEGIN("Price");
SetChartOptions(0,chartShowArrows|chartShowDates|chartWrapTitle);
_N(Title = StrFormat("{{NAME}} - {{INTERVAL}} {{DATE}} Open %g, Hi %g, Lo %g, Close %g (%.1f%%) {{VALUES}} ", O, H, L, C, SelectedValue( ROC( C, 1 ))) );
PlotOHLC( O, H, L, C, "Traded", colorRed, styleBar , Null, Null, 0, 1, 1);
_SECTION_END();
Buy = Cross( MACD(), Signal() );
Sell = Cross( Signal(), MACD() );
Short=Sell;
Cover=Buy;
// trade on next bar open
SetTradeDelays( 1, 1, 1, 1 );
BuyPrice = SellPrice = CoverPrice=ShortPrice=Open;
// trade size: 25% of current portfolio equity
SetPositionSize( 25, spsPercentOfEquity );
if( Status( "action" ) == actionPortfolio )
{
bo = GetBacktesterObject();
bo.PreProcess();
equityArray = bo.EquityArray;
MaxLossPerDay = equityArray * 5 / 100;
MaxLossPerOpenPosition = equityArray * 2 / 100;
dt = DateTime();
for( i = 0; i < BarCount; i++ )
{
if( i > 0 )
{
previousBarDate = DateTimeConvert( 0, dt[i - 1] );
thisBarDate = DateTimeConvert( 0, dt[i] );
if( previousBarDate != thisBarDate ) //new day
MaxLossPerDayHit= totalLossForTheDay = totalRealisedLossForTheDay = totalUnRealisedLossForTheDay = 0;
}
for( closedTrade = bo.GetFirstTrade(); closedTrade; closedTrade = bo.GetNextTrade() )
{
if( closedTrade == bo.GetFirstTrade() ) //if this is the first trade in list
totalRealisedLossForTheDay = closedTrade.GetProfit();
else
if( DateTimeConvert( 0, closedTrade.EntryDateTime ) != datePreviousTrade ) // if first trade of the day
totalRealisedLossForTheDay = 0;
else
totalRealisedLossForTheDay = totalRealisedLossForTheDay + closedTrade.GetProfit(); //realised profit/loss
datePreviousTrade = DateTimeConvert( 0, closedTrade.EntryDateTime );
}
for( openpos = bo.GetFirstOpenPos(); openpos ; openpos = bo.GetNextOpenPos() )
{
thisOpenPositionLoss = openpos.GetProfit();
if( thisOpenPositionLoss < ( -1 * MaxLossPerOpenPosition[i] ) )
{
bo.ExitTrade( i, openpos.Symbol, openpos.GetPrice( i, "C" ), 2 );
_TRACE( "MaxLoss Per OpenPosition Hit for instrument " + openpos.Symbol + " on " + dt );
}
else
if( totalLossForTheDay < ( -1 * MaxLossPerDay[i] ) )
{
bo.ExitTrade( i, openpos.Symbol, openpos.GetPrice( i, "C" ), 2 );
_TRACE( "MaxLoss Per Day Hit for instrument " + openpos.Symbol + " on " + dt );
}
if( openpos == bo.GetFirstOpenPos() )
totalUnRealisedLossForTheDay = openpos.GetProfit();
else
if( DateTimeConvert( 0, openpos.EntryDateTime ) != datePreviousOpenPos )
totalUnRealisedLossForTheDay = 0;
else
totalUnRealisedLossForTheDay = totalUnRealisedLossForTheDay + openpos.GetProfit(); //Unrealised profit/loss
datePreviousOpenPos = DateTimeConvert( 0, openpos.EntryDateTime );
}
totalLossForTheDay = totalRealisedLossForTheDay + totalUnRealisedLossForTheDay;
for( sig = bo.GetFirstSignal( i ); sig; sig = bo.GetNextSignal( i ) )
{
if( totalLossForTheDay < ( -1 * MaxLossPerDay[i] ) )
{
sig.PosSize = 0; //ignore the signal
_TRACE( "MAX LOSS for Day Hit. No Nore Trading For The Day. Signal for instrument " + sig.Symbol + " IGNORED on " + sig.EntryDateTime );
}
if( sig.IsEntry()) // Process long entries
bo.EnterTrade( i, sig.Symbol, True, sig.Price, sig.PosSize );
else
{
if( sig.IsExit()) // Process long exits
bo.ExitTrade( i, sig.Symbol, sig.Price );
}
}
bo.HandleStops(i);
//bo.ProcessTradeSignals( i );
}
bo.PostProcess();
}