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

[amibroker] CBT Shuffle



PureBytes Links

Trading Reference Links

CBT Discussion.

I have been using CBT.  I have read the documentation and presentations.  These provide some help.  Some.  I was surprise to find that IsEntry() and others have no meaning with rotational trading.  This was very confusing to me.  I do not understand the reasoning but I am learning how to get around this.  I was also surprised to find few examples to help.

I wanted to use CBT to normally rotate on the first of the month, but also to either (1) exit and rotate or (2) exit to cash, if the ranking of a position feel below a certain level during the month.   I started out with exit and rotate, but I did not like this as AB treated each market day the same.  Again, I wanted rotation of the first of the month with exit/rotate on any other day if the ranking for a position fell below a certain level.  I was ending up with rotation on any day of the month with exit/rotation on other than the first of the month if ranking fell below a certain level.  Not what I wanted.

In time, I figured out how to do what I needed.  Here is the CBT code.

SetOption("UseCustomBacktestProc", True );
// for rotational trading afl
// targdate2 is one day delay from first of month
// worst_held_rank1 is 6 and worst_held_rank3 is 10
//
barDates = DateTime();

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

   for( bar = 0; bar < BarCount; bar++ ) 
   {
    barDate = barDates[bar];
    if( targdate2[ bar ] )
    { 
      TopSymbols = ""; 
      for( i = 0, sig = bo.GetFirstSignal( bar ); 
           sig AND i < Worst_Held_Rank1; 
           sig = bo.GetNextSignal( bar ), i++ ) 
      { 
       TopSymbols += sig.Symbol + ","; 
      } 

     OpenPos = "";
     OpenPosCnt = 0;
     for( pos = bo.GetFirstOpenPos(); pos; pos = bo.GetNextOpenPos() ) 
      {
       OpenPos += pos.Symbol + ",";
       OpenPosCnt++;
         if( NOT StrFind( TopSymbols, pos.Symbol + "," ) ) 
          { 
            _TRACE(NumToStr(barDate, formatDateTime) + " Exiting position not found in top list: " + pos.Symbol ); 
            bo.ExitTrade( bar, pos.Symbol, pos.GetPrice( bar, "C" ), 9 ); // modify price field if needed here 
          } 

      }
      _TRACE(NumToStr(barDate, formatDateTime) + " Top " + Worst_Held_Rank1 + " SYMBOLS: " + TopSymbols ); 
      _TRACE( NumToStr(barDate, formatDateTime) + " OpenPos( " + OpenPosCnt + " ):" + OpenPos );
 
    }
    
    if( NOT targdate2[ bar ] ) 
    { 
      TopSymbols = ""; 
      for( i = 0, sig = bo.GetFirstSignal( bar ); 
           sig AND i < Worst_Held_Rank3; 
           sig = bo.GetNextSignal( bar ), i++ ) 
      { 
       TopSymbols += sig.Symbol + ","; 
      } 
    
    OpenPos = "";
    OpenPosCnt = 0;
    for( pos = bo.GetFirstOpenPos(); pos; pos = bo.GetNextOpenPos() ) 
      {
       OpenPos += pos.Symbol + ",";// List of those positions now open
       OpenPosCnt++;
         if( NOT StrFind( TopSymbols, pos.Symbol + "," ) ) 
          { 
            _TRACE(NumToStr(barDate, formatDateTime) + " Exiting position not found in top list: " + pos.Symbol ); 
            bo.ExitTrade( bar, pos.Symbol, pos.GetPrice( bar, "C" ), 8 ); // modify price field if needed here 
          } 
    
      }
      _TRACE(NumToStr(barDate, formatDateTime) + " Top " + Worst_Held_Rank3 + " SYMBOLS: " + TopSymbols ); 
      _TRACE( NumToStr(barDate, formatDateTime) + " OpenPos( " + OpenPosCnt + " ):" + OpenPos );

    TradePos = "";
    TradePosCnt = 0;
    KeepDate = False;
    for( tra = bo.GetFirstTrade(); tra; tra = bo.GetNextTrade() ) 
      {
       TradePos += tra.Symbol + ",";// List of those positions now closed
       TradePosCnt++;
       if( tra.ExitDateTime == barDate )
        {
        KeepDate = True;// Is date we need to EnterTrade
        _TRACE( NumToStr(barDate, formatDateTime) + "KEEPDATE(" + KeepDate + ") " + tra.Symbol + ": " + NumToStr(tra.EntryDateTime, formatDateTime) + " " + NumToStr(tra.ExitDateTime, formatDateTime ) );
        }
      }
      //bo.UpdateStats( bar, 2 );
      _TRACE( NumToStr(barDate, formatDateTime) + " TradePos( " + TradePosCnt + " ):" + TradePos );
             
     for( sig = bo.GetFirstSignal( bar ); sig; sig = bo.GetNextSignal( bar ) )
      {
       // sig.IsEntry is always -1. It is of no value in Rotational afl
       //if(sig.IsEntry()) //Does not work in Rotational Trading
	 {
         ThePrice[bar] = sig.Price;// Needed later for EnterTrade
         sig.Price = -1;//forces no rotation on non-targdates
         if(KeepDate)
	 //if( OpenPosCnt < Max_Open_Positions )
         if( NOT StrFind( OpenPos, sig.Symbol + "," ) )
          {
          bo.EnterTrade(bar, sig.Symbol, True, ThePrice[bar], sig.PosSize, sig.PosScore);
          _TRACE( NumToStr(barDate, formatDateTime) + " OpenPos( " + OpenPosCnt + " ):" + OpenPos + " Sym: " + sig.Symbol + "Pri: " + ThePrice[bar]
          + " Sco: " + sig.PosScore );
          }
         }
      }
      
      
    }

     bo.ProcessTradeSignals( bar ); 
   } 
    bo.PostProcess(); 
} 

_SECTION_END();





------------------------------------

**** IMPORTANT PLEASE READ ****
This group is for the discussion between users only.
This is *NOT* technical support channel.

TO GET TECHNICAL SUPPORT send an e-mail directly to 
SUPPORT {at} amibroker.com

TO SUBMIT SUGGESTIONS please use FEEDBACK CENTER at
http://www.amibroker.com/feedback/
(submissions sent via other channels won't be considered)

For NEW RELEASE ANNOUNCEMENTS and other news always check DEVLOG:
http://www.amibroker.com/devlog/

Yahoo! Groups Links

<*> To visit your group on the web, go to:
    http://groups.yahoo.com/group/amibroker/

<*> Your email settings:
    Individual Email | Traditional

<*> To change settings online go to:
    http://groups.yahoo.com/group/amibroker/join
    (Yahoo! ID required)

<*> To change settings via email:
    mailto:amibroker-digest@xxxxxxxxxxxxxxx 
    mailto:amibroker-fullfeatured@xxxxxxxxxxxxxxx

<*> To unsubscribe from this group, send an email to:
    amibroker-unsubscribe@xxxxxxxxxxxxxxx

<*> Your use of Yahoo! Groups is subject to:
    http://docs.yahoo.com/info/terms/