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

Re: [amibroker] Re: 2 simple trading systems



PureBytes Links

Trading Reference Links

Greetings all --

Here is an AFL code that will implement the parabolic trailing stop for a long position.  Set the Buy independently.  Set the initial level of the trailing stop as you wish.  The code takes care of raising the stop level as the price rises.

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

// ParabolicStop.afl
//
//    This implementation is for a long position.
//
//    The parabolic trailing stop is set below the
//    entry point on the first day of long position,
//    and rises according to a formula as the price rises.
//    Unlike the traditional trailing stop, the
//    parabolic stop continues to rise even as the
//    price holds steady or drops.  Eventually, the
//    price and the parabolic stop meet, which triggers
//    an exit.
//
SetTradeDelays(0,0,0,0);

//    Trading system entry logic goes here.
//    Exit will be made by the parabolic stop.

//    For example, use moving average crossover entry.
MALen1 =     Optimize("MALen1",30,1,31,1);
MAvg = AMA(C,2/(MALen1+1));

MALen2 =    Optimize("MALen2",15,1,31,2);
Pass = C>=MA(C,MALen2);

Buy = pass AND Cross(C,MAvg);

//    The code for the Parabolic Trailing Stop begins here.
//
//    Assume that entry will be made at the close of the day the
//        buy signal is generated.
//
//    Setting TradeAtStop to 1 assumes that there is a stop
//        in place and the trade exits intraday at the stop price.
//    Setting TradeAtStop to 0 assumes that intraday exit
//        cannot take place (as in mutual fund end-of-day
//        trading) and the trade takes place at the close
//        of the signal day.

TradeAtStop = Param("TradeAtStop",0,0,1,1);

//    Set the initial stop level.
//    For this example, it is set at the Lowest Low
//        for some number of days.

LBDays = Optimize("LBDays",1,0,10,1);

//    Set the Acceleration factor and Maximum Acceleration.

IAF = Param("IAF",0.02,0.001,0.1,0.001);        // acceleration factor
MaxAF = Param("MaxAF",0.2,0.001,1.0,0.001);    // max acceleration

Psar = Close;        // initialize
mp = 0;                // flat initial conditions
Sell = 0;            // clear sell signals
af = IAF;            // initial acceleration factor
hp = High [ 0 ];
lp = Low [ 0 ];
Lp = LLV(Low,LBDays);

//    Loop through all the bars.

for( i = 2; i < BarCount; i++ )
{
    //    Check for exit from long position

    if (  (mp == 1) AND (Low[i] < Psar[i-1])  )
    {
        Sell[i] = 1;
        if (TradeAtStop)
        {
            SellPrice[i] = Psar[i-1];
        }
        else
        {
            SellPrice[i] = Close[i];
        }
        mp = 0;
    }

    //    Continuation of long position -- adjust stop

    if ( mp == 1 )
    {
        if (High[i] > Hp)
        {
            Hp = High[i];
            af = af + IAF;
            if (af > MaxAF) af = MaxAF;
        }
        psar [ i ] = psar [ i-1 ] + af * ( hp - psar [ i-1 ] );
    }
    else
    {
        //    not in a long position.
        //    value of psar is not important.
        //    set the psar level so it will plot
        //        on the price graph.

        psar[i] = Close[BarCount-1];
    }

    //    Check for new long position

    if (  (mp == 0) AND (Buy[i])  )
    {
        BuyPrice[i] = Close[i];
        Psar[i] = Lp[i];
        Hp = High[i];
        af = IAF;
        mp = 1;
    }
}

//    The code for the Parabolic Trailing Stop ends here.


Plot( Close, "Price", colorBlack, styleCandle );
Plot( MAvg, "MAvg", colorBlue,styleLine);
Plot( psar, "SAR", colorRed,
        styleDots | styleNoLine | styleThick );

Buy = ExRem(Buy,Sell);
Sell = ExRem(Sell,Buy);
//Figure 7.11 Parabolic Stop - Looping Code

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

Thanks,
Howard
www.quantitativetradingsystems.com

On Sun, Jun 29, 2008 at 3:10 PM, PW <pwraceochmotor@xxxxxxxxxxx> wrote:

or maybe this if you would like to have SAR on the signal line instead:



r1=5;r2=8;r3=9;
//ml = MACD(r1, r2);
sl = Signal(r1,r2,r3);
//Plot( ml , StrFormat(_SECTION_NAME()+"(%g,%g)", r1, r2), colorRed,
ParamStyle("MACD style") );
Plot( sl , "Signal" + _PARAM_VALUES(), ParamColor("Signal color",
colorBlue ), ParamStyle("Signal style") );

O=H=L=C=sl;

acc = Param("Acceleration", 0.1, 0, 1, 0.001 );
accm = Param("Max. acceleration", 0.11, 0, 1, 0.001 );
Plot( SAR( acc, accm ), _DEFAULT_NAME(), ParamColor( "Color",
colorCycle ), ParamStyle("Style", styleDots | styleNoLine, maskDefault
| styleDots | styleNoLine ) );


__._,_.___

Please note that this group is for discussion between users only.

To get 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

__,_._,___