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

Re: [amibroker] Scaling out example code does not work



PureBytes Links

Trading Reference Links

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 <ezbentley@xxxxxxxxx> 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,


__._,_.___

**** IMPORTANT ****
This group is for the discussion between users only.
This is *NOT* technical support channel.

*********************
TO GET TECHNICAL SUPPORT from AmiBroker please send an e-mail directly to
SUPPORT {at} amibroker.com
*********************

For NEW RELEASE ANNOUNCEMENTS and other news always check DEVLOG:
http://www.amibroker.com/devlog/

For other support material please check also:
http://www.amibroker.com/support.html

*********************************




Your email settings: Individual Email|Traditional
Change settings via the Web (Yahoo! ID required)
Change settings via email: Switch delivery to Daily Digest | Switch to Fully Featured
Visit Your Group | Yahoo! Groups Terms of Use | Unsubscribe

__,_._,___