Friday 26 June 2015

Turtle trading system AFL for Aamibroker

Turtle trading system
// asfatul.alam@yahoo.com










pds = 20;
MAFAST = EMA( Close, 20 );
MASLOW = EMA( Close, 40 );

DonchianUpper = HHV( Ref( H, -1 ), pds ); 
DonchianLower = LLV( Ref( L, -1 ), pds ); 
DonchianMiddle = ( DonchianUpper + DonchianLower ) / 2;

UpTrend = C > ( LLV( L, 20 ) + 2 * ATR( 10 ) ) AND EMA( Close, 20 ) > EMA( Close, 40 );
DnTrend = C < ( HHV( H, 20 ) - 2 * ATR( 10 ) ) AND EMA( Close, 20 ) < EMA( Close, 40 );
Color = IIf( UpTrend, colorBlue, IIf( DnTrend, colorOrange, colorCustom11 ) );


Plot( C, "Price", Color, styleBar | styleThick );
Plot( DonchianUpper, "Donchian U", ParamColor( "DU Color", colorBlue ), ParamStyle( "DU Style", styleLine ) );
Plot( DonchianMiddle, "Donchian M", ParamColor( "DM Color", colorBrightGreen ), ParamStyle( "DM Style", styleNoLine ) );
Plot( DonchianLower, "Donchian L", ParamColor( "DL Color", colorRed ), ParamStyle( "DL Style", styleLine ) );

Title = WriteVal( DonchianUpper, 1.2 ) + WriteVal( DonchianLower, 1.2 ) + WriteVal( DonchianMiddle, 1.2 );

Buy = High > Ref( HHV( High, 20 ), -1 ) AND MAFAST > MASLOW; 

Short = Low < Ref( LLV( Low, 20 ), -1 ) AND MAFAST < MASLOW; 




_SECTION_BEGIN( "Chandelier Exit" );

SetBarsRequired( 50, 50 );

Multiple = Param( "Multiple", 3, 0.5, 10, 0.1 ); 
ATRPeriods = Param( "ATR Periods", 20, 1, 50, 1 ); 

stopArray = Null;
atrArray = ATR( ATRPeriods );
HHArray = Null;
LLArray = Null;
exitArray = Null;
trendDirection = 0;

for ( i = 0; i < BarCount; i++ )
{
    if ( Short[i] )
    {
     
        stopArray[i] = Low[i] + ( Multiple * atrArray[i] );
        LLArray[i] = Low[i]; 
        trendDirection = 0 - 1; 
    }

    if ( Buy[i] )
    {
    
        stopArray[i] = High[i] - ( Multiple * atrArray[i] );
        HHArray[i] = High[i]; 
        trendDirection = 1; 
    }

    exitArray[i] = 0;

    if ( trendDirection > 0 )
    {
        
        if ( trendDirection > 1 )
        {
         
            if ( Low[i] < stopArray[i-1] )
            {
               
                trendDirection = 0; 
                exitArray[i] = 1;
            }
            else
            {
              
                if ( High[i] > HHArray[i-1] )
                    HHArray[i] = High[i];
                else
                    HHArray[i] = HHArray[i-1];

         
                stopArray[i] = HHArray[i] - ( Multiple * atrArray[i] );
            }
        }

        trendDirection = trendDirection + 1;
    }

    if ( trendDirection < 0 )
    {
       
        if ( trendDirection < 0 - 1 )
        {
            
            if ( High[i] > stopArray[i-1] )
            {
              
                trendDirection = 0;
                exitArray[i] = 0 - 1;
            }
            else
            {
               
                if ( Low[i] < LLArray[i-1] )
                    LLArray[i] = Low[i];
                else
                    LLArray[i] = LLArray[i-1];


                stopArray[i] = LLArray[i] + ( Multiple * atrArray[i] );
            }
        }

        trendDirection = trendDirection - 1;
    }

    if ( trendDirection == 0 )
    {
        stopArray[i] = 0;
        LLArray[i] = 0;
        HHArray[i] = 0;
    }
}

Sell = Cover = exitarray;

Buy = ExRem( Buy, Sell );
Sell = ExRem( Sell, Buy );
Short = ExRem( Short, Cover );
Cover = ExRem( Cover, Short );

PlotShapes( Buy*shapeUpArrow, colorBrightGreen, 0, Low );
PlotShapes( Short*shapeDownArrow, colorRed, 0, High );
PlotShapes( abs( exitArray )*shapeHollowCircle, colorYellow, 0, ValueWhen( stopArray, stopArray, 1 ), 0 );
Plot( stopArray, "Chand", ParamColor( "Chand Color:", colorYellow ), ParamStyle( "Chand Style", styleDashed ) );

_N( Title = EncodeColor( colorYellow ) + StrFormat( "{{NAME}} - {{INTERVAL}} - {{DATE}}--Turtle_System_Rev_A \n OP= %g Hi= %g Lo= %g CL= %g (%.1f%%) \n Vol= " + WriteVal( V, 1.0 ) + "\n" + " {{VALUES}}", O, H, L, C, SelectedValue( ROC( C, 1 ) ) ) );//=cnb



_SECTION_END(); 

No comments:

Post a Comment

Thanks