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

[amibroker] CBT Help to Exit Positions



PureBytes Links

Trading Reference Links

Need help with CBT.  Want to buy from a watchlist on First of Month with delay =1, MaxOpenPos of 3 and Worst1 of 6.  This works fine.

On other than First of Month, want to sell a ticker if ranking drops to below Worst2 which is 10 and not replace ticker until next First of Month.  I can not get this to work.  Sell example code below.  Help appreciated.

***********
// Rotation_with_MidLevelCBT_v2.0.afl

// An example using Mid Level CBT

MaxOpenPos = 3;
Worst1 = 6; 
Worst2 = 10; 

BuyPrice = SellPrice = ShortPrice = CoverPrice = Close;

Delay = 1;
SetTradeDelays( Delay, Delay, Delay, Delay );

SetOption("MaxOpenPositions", MaxOpenPos  ); 
SetOption("WorstRankHeld", Worst2 ); 
EnableRotationalTrading( True ); 
PositionSize = -100/GetOption("MaxOpenPositions");

firstofmonth = Month() != Ref( Month(), -1 );
firstofmonth2 = Ref(firstofmonth,-Delay);// Need for Mid Level Backtester

Rank = 1/RSI(14); 
PositionScore = rank;
//PositionScore = IIf( firstofmonth, Rank, scoreNoRotate );

//  THIS IS MID LEVEL BACKTESTER
//    Want to buy on First of Month with delay =1, MaxOpenPos of 3
//    and Worst1 of 6.
//    On other than First of Month, want to sell a ticker if ranking
//    drops to below Worst2 which is 10 and not replace ticker
//    until next First of Month

SetOption("UseCustomBacktestProc", True );
//SetOption("UseCustomBacktestProc", False );

barDates = DateTime();

// FirstOfMonth with MaxOpenPos of 3 and Worst1 of 6.
// This section of code works just fine.
if( Status("action") == actionPortfolio ) 
{ 
   bo = GetBacktesterObject(); 
   bo.PreProcess(); 

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

     _TRACE(NumToStr(barDate, formatDateTime) + " Top " + Worst1 + " Symbols: " + TopSymbols ); 

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

      } 
    } 


// On other than FirstOfMonth, want to sell a ticker if ranking falls below
// worst2 of 10 and not replace it until next FirstOfMonth. THE FOLLOWING
// DOES NOT WORK AS IT ROTATES INTO THE NEXT TICKER RATHER THAN JUST
// SELLING THE TICKER.
    if( NOT firstofmonth2[ bar ] ) 
    { 
      TopSymbols = ""; 
      for( i = 0, sig = bo.GetFirstSignal( bar ); 
           sig AND i < Worst1; 
           sig = bo.GetNextSignal( bar ), i++ ) 
      { 
       TopSymbols += sig.Symbol + ","; 
      } 

     _TRACE(NumToStr(barDate, formatDateTime) + " Top " + Worst2 + " Symbols: " + TopSymbols ); 

     for( pos = bo.GetFirstOpenPos(); pos; pos = bo.GetNextOpenPos() ) 
      { 
         if( NOT StrFind( TopSymbols, pos.Symbol + "," ) ) 
          { 
            _TRACE("Exiting position not found in top list: " + pos.Symbol ); 
             // exit the position, but do NOT replace position
            bo.ExitTrade( bar, pos.Symbol, pos.GetPrice( bar, "C" ), 8 ); // modify price field if needed here 
          } 

      } 
    } 

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

// The 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/