Hi EZ --

The following code comes from my book, "Quantitative Trading Systems".  See
if this helps.

////////////////////////////////////////////////////////////////////


//    ScaleOut.afl
//
//    Sells part of holdings at one profit target,
//    sells remainder of holding at second profit target,
//    sells entire holding on trailing stop.
//
//    This example patterned after one in the
//        AmiBroker User's Guide.
//
SetTradeDelays(0,0,0,0);
BuyPrice = C;
SellPrice = C;

//    Use a moving average crossover to enter long positions
Buy = Cross(MA(C,5),MA(C,25));
Sell = 0;

//    Targets are in percentages.
FirstProfitTarget = 10;
SecondProfitTarget = 20;
TrailingStop = 10;

//    Scalars to keep track of prices while in trade.
PriceAtBuy=0;
HighSinceBuy = 0;
Exit = 0;


//    Loop through all the bars.
for( i = 0; i < BarCount; i++ )
{
    if (PriceAtBuy==0 AND Buy[i]==1)
    {
        PriceAtBuy = BuyPrice[i];
    }
    else
        if(PriceAtBuy > 0 )
        {
            HighSinceBuy = Max(High[i],HighSinceBuy);
            if (exit==0 AND
                High[i] >= (1 + FirstProfitTarget*0.01)
                    * PriceAtBuy)
            {
                // first profit target hit - scale-out
                Exit = 1;
                Buy[i] = sigScaleOut;
                BuyPrice[i] = (1 + FirstProfitTarget*0.01)
                    * PriceAtBuy;
            }
            if(Exit==1 AND
                High[i] >= (1 + SecondProfitTarget*0.01)
                    * PriceAtBuy)
            {
                // 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)
            {
                // 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;
                HighSinceBuy = 0;
            }
    }
}

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

//Buy = ExRem(Buy,Sell);
//Sell = ExRem(Sell,Buy);
//Figure 7.10 Scale Out

//////////////////////////////////////////////////////////////

Thanks,
Howard

On Sun, Dec 28, 2008 at 8:45 PM, ezbentley <[email protected]> wrote:

>   I copied and pasted the code from example 4 at the following url to test
> scaling out:
>
> http://amibroker.com/guide/h_pyramid.html
>
> However, it simply does not work. All positions are exited at once when the
> first profit target
> is met.
>
> The desired behavior is to exit half of the position at first profit target
> and exit the other half
> at the second profit target.
>
> It is very frustrating when the example code from the official source is
> not behaving
> correctly.
>
> Does anyone know how to correctly implement scaling out?
>
> Thanks in advance,
>
>  
>

Reply via email to