/* Done by Rajandran R */
/* Author of www.marketcalls.in */
_SECTION_BEGIN("SuperTrend");
SetBarsRequired(100000,0);
GraphXSpace = 15;
SetChartOptions(0,chartShowArrows|chartShowDates);
Plot( C, "Close", ParamColor("Color", colorBlack ), styleNoTitle |
ParamStyle("Style") | GetPriceStyle() );
_N(Title = StrFormat("{{NAME}} - {{INTERVAL}} {{DATE}} Open %g, Hi %g, Lo %g,
Close %g (%.1f%%) {{VALUES}}", O, H, L, C, SelectedValue( ROC( C, 1 ) ) ));
PositionSize = 100000;
Factor=Optimize("Factor",2,2,10,1);
Pd=Optimize("ATR Periods",11,1,20,1);
Up=(H+L)/2+(Factor*ATR(Pd));
Dn=(H+L)/2-(Factor*ATR(Pd));
iATR=ATR(Pd);
TrendUp=TrendDown=Null;
trend[0]=1;
changeOfTrend=0;
flag=flagh=0;
for (i = 1; i <BarCount-1; i++) {
TrendUp[i] = Null;
TrendDown[i] = Null;
trend[i]=1;
if (Close[i]>Up[i-1]) {
trend[i]=1;
if (trend[i-1] == -1) changeOfTrend = 1;
}
else if (Close[i]<Dn[i-1]) {
trend[i]=-1;
if (trend[i-1] == 1) changeOfTrend = 1;
}
else if (trend[i-1]==1) {
trend[i]=1;
changeOfTrend = 0;
}
else if (trend[i-1]==-1) {
trend[i]=-1;
changeOfTrend = 0;
}
if (trend[i]<0 && trend[i-1]>0) {
flag=1;
}
else {
flag=0;
}
if (trend[i]>0 && trend[i-1]<0) {
flagh=1;
}
else {
flagh=0;
}
if (trend[i]>0 && Dn[i]<Dn[i-1]){
Dn[i]=Dn[i-1];
}
if (trend[i]<0 && Up[i]>Up[i-1])
{ Up[i]=Up[i-1];
}
if (flag==1)
{ Up[i]=(H[i]+L[i])/2+(Factor*iATR[i]);;
}
if (flagh==1)
{ Dn[i]=(H[i]+L[i])/2-(Factor*iATR[i]);;
}
if (trend[i]==1) {
TrendUp[i]=Dn[i];
if (changeOfTrend == 1) {
TrendUp[i-1] = TrendDown[i-1];
changeOfTrend = 0;
}
}
else if (trend[i]==-1) {
TrendDown[i]=Up[i];
if (changeOfTrend == 1) {
TrendDown[i-1] = TrendUp[i-1];
changeOfTrend = 0;
}
}
}
Plot(TrendUp,"Trend",colorGreen);
Plot(TrendDown,"Down",colorRed);
Buy = trend==1;
Sell=trend==-1;
Buy=ExRem(Buy,Sell);
Sell=ExRem(Sell,Buy);
Short=Sell;
Cover=Buy;
BuyPrice=ValueWhen(Buy,C);
SellPrice=ValueWhen(Sell,C);
ShortPrice=ValueWhen(Short,C);
CoverPrice=ValueWhen(Cover,C);
Title = EncodeColor(colorWhite)+ "Super Trend AFL code from www.marketcalls.in"
+ " - " + Name() + " - " + EncodeColor(colorRed)+ Interval(2) +
EncodeColor(colorWhite) +
" - " + Date() +" - "+"\n" +EncodeColor(colorRed) +"Op-"+O+" "+"Hi-"+H+"
"+"Lo-"+L+" "+
"Cl-"+C+" "+ "Vol= "+ WriteVal(V)+"\n"+
EncodeColor(colorLime)+
WriteIf (Buy , " GO LONG / Reverse Signal at "+C+" ","")+
WriteIf (Sell , " EXIT LONG / Reverse Signal at "+C+"
","")+"\n"+EncodeColor(colorYellow)+
WriteIf(Sell , "Total Profit/Loss for the Last Trade Rs."+(C-BuyPrice)+"","")+
WriteIf(Buy , "Total Profit/Loss for the Last trade
Rs."+(SellPrice-C)+"","");
PlotShapes(IIf(Buy, shapeSquare, shapeNone),colorGreen, 0, L, Offset=-40);
PlotShapes(IIf(Buy, shapeSquare, shapeNone),colorLime, 0,L, Offset=-50);
PlotShapes(IIf(Buy, shapeUpArrow, shapeNone),colorWhite, 0,L, Offset=-45);
PlotShapes(IIf(Short, shapeSquare, shapeNone),colorRed, 0, H, Offset=40);
PlotShapes(IIf(Short, shapeSquare, shapeNone),colorOrange, 0,H, Offset=50);
PlotShapes(IIf(Short, shapeDownArrow, shapeNone),colorWhite, 0,H, Offset=-45);
_SECTION_END();
RequestTimedRefresh( 1 );
ibc = GetTradingInterface("IB");
Transmit = ParamToggle("Transmit","OFF|ON",0);
Reset = ParamTrigger("Reset All","RESET");
BuyTrigger = ParamTrigger("Place Buy order","BUY");
SellTrigger = ParamTrigger("Place Sell order","SELL");
LastTrade = StaticVarGetText("LastTrade");
BuyOrderID = StaticVarGetText("BuyOrderID");
SellOrderID = StaticVarGetText("SellOrderID");
BuyPending = ibc.IsOrderPending(BuyOrderID);
SellPending = ibc.IsOrderPending(SellOrderID);
IBPosSize = ibc.GetPositionSize( Name() );
BuyStatus = ibc.GetStatus( BuyORderID, True );
SellStatus = ibc.GetStatus( SellORderID, True);
if( BuyTrigger )
{
if( LastTrade == "Sell" OR LastTrade == "" )
{
if( SellStatus == "Filled" OR SellStatus == "" )
{
BuyOrderID= ibc.ModifyOrder( BuyOrderID, Name(), "Buy", 100, "MKT", 0, 0, "Day", Transmit);
StaticVarSetText("BuyOrderID",BuyOrderID);
StaticVarSetText("LastTrade", "Buy");
SetChartBkColor( colorBrightGreen ) ;
}
}
}
else if( SellTrigger )
{
if( LastTrade == "Buy" OR LastTrade == "" )
{
if( BuyStatus == "Filled" OR BuyStatus == "" )
{
SellORderID = ibc.ModifyOrder( SellORderID , Name(), "Sell", 100, "MKT", 0, 0, "Day", Transmit);
StaticVarSetText("SellOrderID",SellOrderID);
StaticVarSetText("LastTrade", "Sell");
SetChartBkColor( colorRed ) ;
}
}
}
else if( Reset )
{
StaticVarSetText("BuyOrderID","");
if( BuyPending ) ibc.CancelOrder( BuyOrderID );
StaticVarSetText("SellOrderID","");
if( SellPending ) ibc.CancelOrder( SellOrderID );
StaticVarSetText("LastTrade", "");
ibc.CloseAllOpenPositions();
}
LastTWSMsg = ibc.getLastError( 0 );
//BuyStatus = WriteIf( BuyPending, BuyOrderID+", Status: "+BuyStatus,"");
//SellStatus= WriteIf( SellPending, SellOrderID+", Status: "+SellStatus,"");
Title = "\n"+
"Last TWS Error Msg: "+LastTWSMsg+"\n"+
" BuyOrderID: "+BuyOrderID+" "+BuyStatus+"\n"+
" SellOrderID: "+SellOrderID+" "+SellStatus+"\n"+
" Last Trade: "+LastTrade+"\n"+
" TWS Position Size: "+NumToStr(IBPosSize,1.0,False);
Plot(C,"Close",colorBlack,styleBar);