Monday, 18 November 2013

Amibroker AFL for Stochastic Oscillator based trading



Trading shares using Stochastic Oscillator
It is possible to do trading based on stochastic index. By using %D and %K average of 3 and the period of 15, we can get some reliable Buy and Sell signal. Some time has to be spent identifying suitable shares to trade using this system. Because shares with high volatility can generate confusing buy sell signals.  I have attached a screen view of Apollotyre as an example of selecting a share that can offer regular opportunities to trade with.
Interpretation of stochastic levels can be tricky. Because due to the nature of the stochastic oscillator, the indicator will be in the oversold region for a longer period, when the stock is in uptrend. Similarly during downtrend it will continue to be in the oversold levels for a longer period. The chart window gives us the values of stochastic levels. If we can use these values sensibly some money can be made.

 
In the properties window it is possible to fine tune the system by adjusting the %Davg, %Kavg and the Periods. I request the readers to try out variations and evolve a trading system that can deliver.
For executing short term trades this could another useful Amibroker tool.

The code is simple and posted below this line /////////////////////////////////////////////
Title = EncodeColor(16)+  Title = Name() +
"   " + EncodeColor(16) + "{{INTERVAL}}  " + EncodeColor(16) +
"  " + Date() +"  "+"\n" +EncodeColor(16) +"Open-"+EncodeColor(29)+O+EncodeColor(16)+" High-"+EncodeColor(29)+H+
"  "+EncodeColor(29)+"Low-"+EncodeColor(16)+L+"  "+EncodeColor(29)+"Close-"+EncodeColor(16)+C+
"  "+EncodeColor(29)+ "Volume= "+EncodeColor(16)+ WriteVal(V)+"\n"+

_SECTION_BEGIN("Stochastic %D%K");
//indicator criteria
periods = Param( "Periods", 15, 1, 200, 1 );
Ksmooth = Param( "%K avg", 3, 1, 200, 1 );
Dsmooth = Param( "%D avg", 3, 1, 200, 1 );
KsDs = StochD( periods , Ksmooth, DSmooth );
Plot( KsDs , _DEFAULT_NAME(), colorRed , ParamStyle("Style") );

periods = Param( "Periods", 15, 1, 200, 1 );
Ksmooth = Param( "%K avg", 3, 1, 200, 1 );
Ks = StochK( periods , Ksmooth);
Plot( Ks , _DEFAULT_NAME(), colorBlue, ParamStyle("Style") );

// criteria buy/short
up = Cross(Ks,KsDs);
down = Cross(KsDs,Ks);

Buy = Cover = up;
Short = Sell = down;
_SECTION_END();

// change the questionmarks the the value which is used to plot the indicator
base_array= ks ;

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

PlotShapes( Buy * shapeUpTriangle, colorBrightGreen, 0);
PlotShapes( Short * shapeDownTriangle, colorOrange, 0);
PlotShapes( Sell * shapeSmallCircle, colorRed, 0,base_array,Offset=25);
PlotShapes( Cover * shapeSmallCircle, colorDarkGreen, 0,base_array,Offset=-25);

ApplyStop(stopTypeLoss, stopModePercent, Optimize( "stopTypeLoss", 2.5, 2.5, 2.5, 0.5 ) ,2, True );
ApplyStop(stopTypeProfit , stopModePercent, Optimize( "stopTypeProfit ", 2.5, 2.5, 2.5, 0.1 ) ,2, True );

////////////////////////////// HIGHLY IMPORTANT ////////////////////
//ACTIVATE STOPS IN SETTINGS
e = Equity(1,0); /* Highly Important!!. "Removes all extra signals, evaluates
stops AND writes BACK signals to Sell/Cover arrays". As it should be!!*/

PlotShapes( Buy* shapeUpArrow , colorBlack, 0);
PlotShapes( Short* shapeDownArrow , colorBlack, 0);
/*
1 - regular exit
2 - Max. loss
3 - profit target
4 - trailing
5 - ruin stop
6 - n-bar stop
*/

PlotShapes(IIf(Cover==2, shapeCircle, shapeNone),colorRed,0,base_array,0); //stoploss
PlotShapes(IIf(Cover==3, shapeCircle, shapeNone),colorBrightGreen,0,base_array,0); //profit target
PlotShapes(IIf(Sell==2, shapeCircle, shapeNone),colorRed,0,base_array,0); //stoploss
PlotShapes(IIf(Sell==3, shapeCircle, shapeNone),colorBrightGreen,0,base_array,0); //profit target
////////////////////////////////////////////////////////////////////////////////
OffsetBuy = Param("Offset_Buy (green)", -9, -25, 25, 1 );
OffsetSell = Param("Offset_Sell (blue)", 2, -25, 25, 1 );
OffsetShort = Param("Offset_Short (red)", 7, -25, 25, 1 );
OffsetCover = Param("Offset_Cover (orange)", -4, -25, 25, 1 );

OND=Ref(O,1); //OPEN NEXT DAY
base_arrayBUY=base_array+OffsetBuy ; //just for offset
base_arraySELL=base_array+OffsetSell ; //just for offset
base_arraySHORT=base_array+OffsetShort ; //just for offset
base_arrayCOVER=base_array+OffsetCover ; //just for offset
for( i = 0; i < BarCount; i++ )
{
if( Buy[i] ) PlotText( "      Buy " + OND[ i ], i, base_arrayBUY[ i ], colorDarkGreen );
if( Sell[i] AND NOT Short[i] ) PlotText( "  Sell " + OND[ i ], i, base_arraySELL[ i ], colorBlue);
if( Short[i] ) PlotText( "Short " + OND[ i ], i, base_arraySHORT[ i ], colorRed);
if( Cover[i] AND NOT Buy[i] ) PlotText( "SELL " + OND[ i ], i, base_arrayCOVER[ i ], colorOrange);
}
////////////////////////////////////////////////////////////////////////////////
_SECTION_BEGIN("trending ribbon");
GraphXSpace=20;
uptrend=Buy;
downtrend=Short;
Plot( 3, "ribbon",IIf( uptrend, colorGreen, IIf( downtrend, colorRed, colorOrange )), styleOwnScale|styleArea|styleNoLabel, -0.5, 100 );
_SECTION_END();
/////////////////////////////////////End of code ///////////////////////////////////////////

Best of Luck

No comments:

Post a Comment