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

[amibroker] Money management AFL



PureBytes Links

Trading Reference Links

I'm trying to write a money management AFL.  I start off with the scaling-out example in the user guide, and add just one more condition - after openinig a position, if it is initially profitable but before it hits the profit target of say 20% it appears to be turning into a loss.  In this case I want to set the stop to be the BuyPrice.

Unfortunately it doesn't work - it just doesn't exit when the price drops below the BuyPrice.

Below please find the full AFL, would anyone be able to help ?

Buy = Cross( MA( C, 10 ), MA( C, 30 ) ); 
Sell = 0;//Cross(MA(C,30), MA(C,10)); 

// the system will exit 
// 50% of position if FIRST PROFIT TARGET stop is hit 
// 50% of position is SECOND PROFIT TARGET stop is hit 
// 100% of position if TRAILING STOP is hit 
SetTradeDelays( 0, 0, 0, 0 ); /* delay entry/exit by one bar */

FirstProfitTarget = 20; // profit 
SecondProfitTarget = 40; // in percent 
TrailingStop = 25; // also in percent 

priceatbuy=0; 
highsincebuy = 0; 

exitoriginal = 0;
exitriskfree = 0;

exit = 0; 

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


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

		if (exitoriginal == 1 AND Close[i] > priceatbuy)
		{
			exitoriginal = 0;
			exitriskfree = 1;
		}

		if (exitriskfree == 1 AND Low[i] <= priceatbuy)
		{
			exit = 3;
			SellPrice[i] = priceatbuy;
			exitoriginal = 0;
			exitriskfree = 0;
		}


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

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

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

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

SetPositionSize( 20, spsPercentOfEquity ); 
SetPositionSize( 50, spsPercentOfPosition * ( Buy == sigScaleOut ) ); // scale out 50% of position
// scale out 50% of position
PlotShapes( (Buy) * shapeUpArrow , IIf( (Buy), colorGreen,colorBlue ), 0,IIf( (Buy), L,H ),-20 );
Plot( Close, "Price", IIf( Close > Open, colorGreen, colorRed ),styleCandle | styleThick );
PlotShapes( (ExRem(Sell,Buy)) * shapeSmallDownTriangle , colorLightOrange, 0,H ,-15 );

Plot( MA(C, 10), _DEFAULT_NAME(), colorGreen, styleLine);
Plot( MA(C, 30), _DEFAULT_NAME(), colorRed, styleLine);




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

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