Breakeven in CBT

I need to scaleout an open position at the time of portfolio backtesting. I cannot ascertain the price at which to breakeven neither can i ascertain the quantity of shares to scaleout (for breakeven) at the time of indicator processing. Both these have to be done by CBT. So I need to use openPosition.Shares and OpenPosition.EntryPrice to acertain the breakeven price as well as quantity during CBT. This can only be done at the time of portfolio backtesting. Now I can easily calculate the breakevenPrice and BreakevenQty, using above, but how do i trigger a scaleout call? Since there is no phase1 signal for this I am not able to call my usual scale function. see below

   for( i = 0; i < BarCount; i++ )
    {
      for( sig = bo.GetFirstSignal( i ); sig; sig = bo.GetNextSignal( i ) )
        {
         //no phase-1 signal on bar-125 where breakeven is to occur
          scaleout();
        }
    }

lets say for stockA the breakeven is to happen on bar number 125. But since there is no phase-1 signal on this bar (because breakeven parameters cannot be calculated in indicator) for this stock the above loop will not enter bar 125.

You did not include entire formula. You did not say what backtest mode you are using. You did not provide what stops did you define via ApplyStop.

Unfortunately your question isn't clear enough and does not provide all necessary details to give you an answer. Please follow this advice: How to ask a good question

As @Tomasz points out, it would be helpful to see the rest of your code. However, it might help you to know that while Signals often indicate that it's time to enter, exit, or scale a trade, your CBT is free to call bo.EnterTrade(), bo.ExitTrade(), and bo.ScaleTrade() even when there is no Signal at all.

1 Like

Knowledge base contains article that shows how to call ScaleTrade without signals from first phase.

3 Likes

Thank you @mradtke and @Tomasz. The solution, as you rightly pointed out, is to iterate through the open positions and breakeven when the condition is met. Below is the psuedo-code.

bo = GetBacktesterObject();

for( i = 0; i < BarCount; i++ )
{
//iterate through open positions
    for( openpos  = bo.GetFirstOpenPos();  openpos ;  openpos  = bo.GetNextOpenPos() )
    {
//calculate breakeven price
        reqdScaleOutPrice = <>;
        reqdPosSizeToScaleOut = <>;
        reqdBreakevenTriggerPrice = <>;

        if( openpos.GetPrice( i, "H" ) > reqdBreakevenTriggerPrice )        
            bo.ScaleTrade( i, openpos.symbol, false, reqdScaleOutPrice, reqdPosSizeToScaleOut );
        
    }
}

Breakeven action assumes that you have an open position.