hi,

if you use two contracts then in my opinion you need only the firstProfit 
target and the trading stop.

I attached code for 2 contracts for ES futures. Also it used regular market 
hours so you will have to adjust some things,

Ed



  ----- Original Message ----- 
  From: gborrageiro 
  To: [email protected] 
  Sent: Tuesday, June 09, 2009 7:16 PM
  Subject: [amibroker] help! pyramiding - scaling out





  hi,

  I'm trying to test taking profits on a system using two standard contracts 
(notional $1mn each contract), with an exit after 5 profitable 5 pips on the 
first mn, then a trailing 10 pip stop thereafter.

  I have a coding error somewhere, as long positions work fine but short 
positions do not.

  please help!

  thx

  Buy = Cross( MA( C, 10 ), MA( C, 50 ) ); 
  Sell = 0; 
  short = Cross( MA( C, 50 ), MA( C, 10 ) ); 
  Cover = 0; 

  FirstProfitTarget = 0.0005;
  SecondProfitTarget = 0.0100;
  TrailingStop = 0.0010;

  priceatbuy = 0;
  highsincebuy = 0;
  priceatshort = 0;
  lowsinceshort = 0;

  exit = 0;

  for ( i = 0; i < BarCount; i++ )
  {
  if ( priceatbuy == 0 AND Buy[ i ] )
  {
  priceatbuy = BuyPrice[ i ];
  }

  if ( priceatbuy > 0 )
  {
  highsincebuy = Max( High[ i ], highsincebuy );

  if ( exit == 0 AND
  High[ i ] >= FirstProfitTarget + priceatbuy )
  {
  // first profit target hit - scale-out
  exit = 1;
  Buy[ i ] = sigScaleOut;
  }

  if ( exit == 1 AND
  High[ i ] >= SecondProfitTarget + priceatbuy )
  {
  // second profit target hit - exit
  exit = 2;
  SellPrice[ i ] = Max( Open[ i ], SecondProfitTarget + priceatbuy );
  }

  if ( Low[ i ] <= highsincebuy - TrailingStop )
  {
  // trailing stop hit - exit
  exit = 3;
  SellPrice[ i ] = Min( Open[ i ], highsincebuy - TrailingStop );
  }

  if ( exit >= 2 )
  {
  Buy[ i ] = 0;
  Sell[ i ] = exit + 1; // mark appropriate exit code
  exit = 0;
  priceatbuy = 0; // reset price
  highsincebuy = 0;
  }
  }
  }

  for ( i = 0; i < BarCount; i++ )
  {
  if ( priceatShort == 0 AND Short[ i ] )
  {
  priceatShort = ShortPrice[ i ];
  }

  if ( priceatShort < 0 )
  {
  lowsinceshort = Min( Low[ i ], lowsinceshort );

  if ( exit == 0 AND
  Low[ i ] <= -FirstProfitTarget + priceatShort )
  {
  // first profit target hit - scale-out
  exit = 1;
  Short[ i ] = sigScaleOut;
  }

  if ( exit == 1 AND
  Low[ i ] <= -SecondProfitTarget + priceatShort )
  {
  // second profit target hit - exit
  exit = 2;
  SellPrice[ i ] = Min( Open[ i ], -SecondProfitTarget + priceatShort );
  }

  if ( High[ i ] >= lowsinceshort + TrailingStop )
  {
  // trailing stop hit - exit
  exit = 3;
  SellPrice[ i ] = Max( Open[ i ], lowsinceshort + TrailingStop );
  }

  if ( exit >= 2 )
  {
  Short[ i ] = 0;
  Cover[ i ] = exit + 1; // mark appropriate exit code
  exit = 0;
  priceatShort = 0; // reset price
  lowsinceshort = 0;
  }
  }
  }

  SetPositionSize( 2, spsShares );
  SetPositionSize( 50, spsPercentOfPosition * ( Buy == sigScaleOut ) ); // 
scale out 50% of position
  SetPositionSize( 50, spsPercentOfPosition * ( Short == sigScaleOut ) ); // 
scale out 50% of position

  PositionSize = MarginDeposit = 1;
  NumContracts = 2;
  PositionSize = NumContracts * MarginDeposit;



  

Reply via email to