[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [amibroker] Losing Trades in Custom Backtester



PureBytes Links

Trading Reference Links

Currently there is no way to do that under regular trading mode as far as I know. The second buy signal has been removed before PreProcess().
(see http://www.amibroker.com/gifs/bt_regular.gif to understand what I mean).

I have figured out a way to work around this by using rotational mode to simulate regular trading. Under rotational mode, those redundant signals are not removed.
Here is some code for your reference:

EnableRotationalTrading(); 
SetOption("WorstRankHeld",160); // this number needs to be big enough. Only 2*WorstRankHeld signals will be held by CBT each bar.
SetOption("MaxOpenPositions", 100);
SetOption("InitialEquity", 30000);
SetOption("CommissionMode", 1); //% per trade
SetOption("CommissionAmount", 0.5);
SetOption("MarginRequirement", 50);
SetOption("UsePrevBarEquityForPosSizing", True);
SetOption("MinShares", 100);

SetTradeDelays( 1, 1, 1, 1 );
RoundLotSize = 5;

.......

ApplyStop( stopTypeNBar, stopModeBars, 10); 

Sell0 = ...; // Rename sell/buy to sell0/buy0 since you can have sell/buy in rotational mode.
Buy0 = ...;
Sell0[0] = 1; // trick to remove leading sell signals
Sell0 = ExRem( Sell0, Buy0 ); 
Sell0[0] = 0;

RawScore = 100 + ......; // make sure it is greater than 2
PositionScore = 1;
for(i = 0; i < BarCount; i++)
{
        if(Buy0[i]) PositionScore[i] = RawScore[i]; 
        else if(Sell0[i]) PositionScore[i] = 2;
        else PositionScore[i] = C[i]/H[i]; //semi-random
}

SetOption("UseCustomBacktestProc", True ); 

if( Status("action")== actionPortfolio )
{ 
   bo = GetBacktesterObject(); 

   bo.PreProcess(); // Initialize backtester 

   for(bar=0; bar < BarCount; bar++) 
   { 
                bo.HandleStops(bar-1); // if use bar not bar-1, the n-bar exits have one extra bar delay. don't know why.
       for ( sig=bo.GetFirstSignal(bar); sig; sig=bo.GetNextSignal(bar) ) 
       {     
           // first handle exit signals  (PosScore = 2)
               if ((sig.PosScore == 2 OR sig.isExit()) AND sig.Price != -1 ) 
          { 
               bo.ExitTrade(bar,sig.symbol,sig.Price); 
          } 
       } 

        // update stats after closing trades 
             bo.UpdateStats(bar, 1 ); 
       
             for ( sig=bo.GetFirstSignal(bar); sig; sig=bo.GetNextSignal(bar)) 
             { 
          // Entry Signals (PosScore > 2)
          // Only one position per symbol
            if (sig.PosScore > 2 AND sig.Price != -1 AND IsNull( bo.FindOpenPos( sig.Symbol ))) 
             { 
                         // long only
              bo.EnterTrade(bar, sig.symbol, True, sig.Price, sig.PosSize, sig.PosScore,sig.RoundLotSize);
                    } 
            } 

             bo.UpdateStats(bar,1); // MAE/MFE is updated when timeinbar is set to 1. 
             bo.UpdateStats(bar,2);   
   } 
   bo.PostProcess(); // Finalize backtester 
}





  ----- Original Message ----- 
  From: C Alvarez 
  To: amibroker@xxxxxxxxxxxxxxx 
  Sent: Monday, August 07, 2006 4:04 PM
  Subject: [amibroker] Losing Trades in Custom Backtester



  I am using the Custom Backtester to iterate through the Signal list. Depending on some additional conditions that I only can know at this time, I might decide to skip a trade and set the PosSize = 0. This prevents the trade from happening, as I want.

  The problem is that if I remove a trade from the Signal list and then the Buy array has a buy for the next day, it is not showing up the Signal list.. And at this time I might want to take the trade. Any suggestions on how I can do this?

  Here is the simplified code.

  Thanks,
  Cey


  SetCustomBacktestProc("");

  if( Status("action") == actionPortfolio )
  {
          bo = GetBacktesterObject();
          bo.PreProcess(); // Initialize backtester

          for(bar=0; bar<BarCount; bar++)
          {

                  for( sig = bo.GetFirstSignal(bar); sig; sig = bo.GetNextSignal(bar) )
                  {
                          if( sig.IsEntry())
                          {
                                  if (some_condition) // don't take trade
                                  {
                                          sig.PosSize = 0;
                                  }
                          }
                  }

          bo.ProcessTradeSignals(bar); // Process current bar's signals

          }

          bo.PostProcess();
  }