hi,

A post from TJ that could help you. Best regards


separate exits using pyramiding

You could do separate exits using pyramiding code but you would need to 
write a loop
and track which entry matches which exit and set sizes accordingly.

For this particular application, I think that custom backtester is 
better suited and leads to simpler and
shorter code and actual code would look as follows:


Sell=Short=Cover=0; // real rules are defined inside custom backtest proc

Buy = IIf( Close > Open, sigScaleIn, 0 ); // entry rule

SetCustomBacktestProc(""); // enable custom backtest

if( Status("action") == actionPortfolio )
{
   // actual backtest routine
   // (low-level)

   bo = GetBacktesterObject();

  // actual backtest loop
   bo.PreProcess();

   for( i = 0; i < BarCount; i++ )
   {
      // first update backtest stats and handle stops
       bo.UpdateStats( i, 0 );
       bo.HandleStops( i );
      
       _TRACE("Bar " + i );
       for( sig = bo.GetFirstSignal(i); sig; sig = bo.GetNextSignal(i) )
       {
         if( sig.IsEntry() OR sig.IsScale() )
         {
          _TRACE("ENTER " + sig.Symbol );
          bo.EnterTrade( i, sig.Symbol, True, sig.Price, 5000 /* $5000 
into one trade */);
         }
       }  

      
      for( OpenPos = bo.GetFirstOpenPos(); OpenPos; OpenPos = 
bo.GetNextOpenPos() )
      {
         // exit positions if their age is > 5 bars and they are profitable
         if( OpenPos.BarsInTrade >= 10 )
         {
             // EXIT WHEN POSITION IS 10 bars old
             _TRACE("EXIT " + OpenPos.Symbol );
             bo.ExitTrade( i, OpenPos.Handle, OpenPos.GetPrice( i, "O" ) );
          }
      }

       bo.UpdateStats( i, 2 );
   }
      
   bo.PostProcess();
}


_TRACEs lines are just for extra debuggin info. You may remove them to 
get more speed.


Tomasz Janeczko





John Bishop a écrit :
> 
> 
> Hi,
> 
> I am trying to write a trading system that scales into buys and sells by 
> using one rule for the initial buy, and then further scales into the 
> position if the C of the current bar is < the last buy price.
> 
> I get how to scale in using one rule but I'm having trouble figuring out 
> how to tell Amibroker to use one rule for the initial buy and then 
> another rule (C < Last BuyPrice) to determine whether to scale in.
> 
> Thank you very much for your help,
> 
> John
> 
> 


Reply via email to