Saturday 21 September 2013

Stockastic Momentum Index AFL

I have given below an indicator which can be of great use both for intraday traders and delivery based traders. this indicator will tell us the current strength of a given stock. When the indicator crosses 60 levels and takes a slight turn downwards, we can expect the stock to lose its value. one can short it or sell the stock if  already bought.Similarly when the indicator below the -60 levels and shows an upward trend, it is and indication  that the stock is gaining strength. one can buy it  on the long side.

It would be advisable to use this along with other indicators for confirmation, because relying on one indicator is not always good. 

I hope you will enjoy using the Stochastic Momentum Index AFL 


////////////////////////////////////// the code begins below this line/////////////////////////////////////////

SECTION_BEGIN("SMI - Stochastic Momentum Index");
SetChartBkColor( colorBlack ) ;
LookBack    = Optimize("LookBack",Param("Lookback", 10, 1, 100 ),1,30,1);
Smooth1     = Optimize("Smooth 1",Param("Smooth 1", 3, 1, 100 ),1,30,1);
Smooth2     = Param("Smooth 2", 3, 1, 20 );
Trigger     = Param("Signal", 3, 1, 10);
PP            = ParamField("Price field",-1);
SMILevel    = Param("Level", 60, 10, 60, 10);

HH = HHV( H, LookBack );
LL = LLV( L, LookBack );


StoMom = 100 * EMA( EMA( PP - 0.5 * ( HH + LL ), Smooth1 ), Smooth2 ) /
( 0.5 * EMA( EMA( HH - LL, Smooth1 ), Smooth2 ) );
StoSig = MA(StoMom,3);

Buy = Cover = Cross(StoMom,StoSig);
Short = Short = Cross(StoSig,StoMom);

Plot (StoMom,_DEFAULT_NAME(),IIf(StoMom> Ref(StoMom,-1),colorBrightGreen,colorRed), styleLine+styleThick);
Plot (StoSig,"",colorWhite,styleLine+styleNoLabel);
Plot(0,"",31,styleNoLabel);
Plot( SMILevel,"",colorOrange,styleDashed);
Plot(-SMILevel,"",colorOrange,styleDashed); 

Sunday 15 September 2013

Amibroker AFL for Commodity trading

Today I would like to post an AFL that i found suitable for Commodities trading. This AFL plots valid entry points and stop loss. However I suggest the stop loss can be followed suiting individual risk levels.I systematically adopted and traded with discipline this could be good tool for trading commodities. This AFL is also applicable in other markets too. But I find it more comfortable in commodity trading.

I am not the author of this AFL. I found it in the internet, and modified it to look better.

Happy trading

/////////////////////////////////////////// the code is posted below this line////////////////////


TimeFrameSet(inDaily);
DayHigh = LastValue(H);
DayLow = LastValue(L);
TimeFrameRestore();

Title = EncodeColor(colorBlue)+ "Commodities Trading System" + " - " + Name() + " - " + EncodeColor(colorRed)+ Interval(2) + EncodeColor(colorBlue) +
" - " + Date() +" - "+"\n" +EncodeColor(colorBlue) +"Op-"+O+" "+"Hi-"+H+" "+"Lo-"+L+" "+
"Cl-"+C+" "+ "Vol= "+ WriteVal(V)+"\n"+


prev=AMA2(C,1,0);
d=IIf(C>Ref(Max(Max(H,Ref(H,-20)),Max(Ref(H,-10),Ref(H,-15))),-1),Min(Min(L,Ref(L,-20)),Min(Ref(L,-10),Ref(L,-15))),
IIf(C<Ref(Min(Min(L,Ref(L,-20)),Min(Ref(L,-10),Ref(L,-15))),-1),Max(Max(H,Ref(H,-20)),Max(Ref(H,-10),Ref(H,-15))),PREV));
a=Cross(Close,d);
b=Cross(d,Close);
state=IIf(BarsSince(a)<BarsSince(b),1,0);
s=state>Ref(state,-1);
ss=state<Ref(state,-1);
sss=state==Ref(state,-1);
col=IIf(state == 1 ,51,IIf(state ==0,4,1));
Plot(C,"",Col,64);
Buy = s;
Sell = ss;
shape = Buy * shapeUpArrow + Sell * shapeDownArrow;
PlotShapes(shape, IIf(Buy,colorGreen,colorRed), 0, IIf(Buy,Low,High));
PlotShapes(IIf(Buy, shapeSquare, shapeNone),colorGreen, 0, L, Offset=-10);
PlotShapes(IIf(Buy, shapeSquare, shapeNone),colorLime, 0,L, Offset=-20);
PlotShapes(IIf(Buy, shapeUpArrow, shapeNone),colorWhite, 0,L, Offset=-15);
PlotShapes(IIf(Sell, shapeSquare, shapeNone),colorRed, 0, H, Offset=20);
PlotShapes(IIf(Sell, shapeSquare, shapeNone),colorOrange, 0,H, Offset=30);
PlotShapes(IIf(Sell, shapeDownArrow, shapeNone),colorWhite, 0,H, Offset=-25);


dist = 2.9*ATR(10);
dist1= 2.9*ATR(10);
for( i = 0; i < BarCount; i++ )
{
if( Buy[i] )
{
PlotText( "\nBuy:" + L[ i ] + "\nSL= " + (L[i]*0.9975), i, L[ i ]-dist[i], colorGreen, colorWhite );
}
if( Sell[i] )
{
PlotText( "Sell:" + H[ i ] + "\nSL= " + (H[i]*1.0025), i, H[ i ]+dist1[i], colorRed, colorWhite );
}
}



_SECTION_BEGIN("swing1");
no=20;
res=HHV(H,no);
sup=LLV(L,no);
avd=IIf(C>Ref(res,-1),1,IIf(C<Ref(sup,-1),-1,0));
avn=ValueWhen(avd!=0,avd,1);
supres=IIf(avn==1,sup,res);
a=Cross(C,supres);
b=Cross(supres,C);
style = a * styleStaircase + b * styleStaircase;
PlotShapes(a,style, IIf(a,colorGreen,colorRed), 0, IIf(a,Low,High));
Plot(supres,"Swing",colorYellow,styleStaircase);
_SECTION_END();



_SECTION_BEGIN("trend");
uptrend=PDI(20)>MDI(10)AND Signal(29)<MACD(13);
downtrend=MDI(10)>PDI(20)AND Signal(29)>MACD(13);
Plot( 2, /* defines the height of the ribbon in percent of pane width */"ribbon",
IIf( uptrend, colorGreen, IIf( downtrend, colorRed, 0 )), /* choose color */
styleOwnScale|styleArea|styleNoLabel, -0.5, 100 );
_SECTION_END();

Buy = s AND a AND uptrend ;
Short = ss AND b AND downtrend ;
Sell = ss AND b AND downtrend ;
Cover = s AND a AND uptrend ;

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

Filter=Buy OR Sell;
Filter= Cover OR Short;

AddColumn( Buy, "Buy", 1);
AddColumn(Sell, "Sell", 1);
AddColumn(Close,"Close",1.2);
AddColumn(Volume,"Volume",1.0);


// Plot the Buy and Sell arrows.
shape = Buy * shapeUpArrow + Sell * shapeDownArrow;
PlotShapes(IIf(Buy, shapeSquare, shapeNone),colorGreen, 0, L, Offset=-10);
PlotShapes(IIf(Buy, shapeSquare, shapeNone),colorLime, 0,L, Offset=-20);
PlotShapes(IIf(Buy, shapeUpArrow, shapeNone),colorWhite, 0,L, Offset=-15);
PlotShapes(IIf(Sell, shapeSquare, shapeNone),colorRed, 0, H, Offset=20);
PlotShapes(IIf(Sell, shapeSquare, shapeNone),colorOrange, 0,H, Offset=30);
PlotShapes(IIf(Sell, shapeDownArrow, shapeNone),colorWhite, 0,H, Offset=-25);


//plot volume graph

// set background gradient colours
SetChartBkGradientFill( ParamColor("BgTop", ColorRGB( 172,172,172 )),ParamColor("BgBottom", ColorRGB( 102,136,187 )),ParamColor("titleblock",ColorRGB( 255,255,255 )));

GraphXSpace = 5;