Supertrend Autotrading Help Needed

#1
Can anybody help me to automate trading using this Supertrend Indicator. I am using Amiboker with IB data feed and IBcontroller is running.

Appreciate any help..

Supetrend Indicator :

Code:
/* 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();
 
#3
What is the accuracy level? If you are looking for automate trading, then u need to have either Now or Nest trader platform along with, Nest plus subscription with Nestplus plugin which you need to pay omnysis. Check out Globaldatafeed.in
 
#4
I have Nest Trader with Nest Plus.

I will buy the Nest Plus Plugin from Omnesys, once I get this AFL automated. Presently I am testing it on my paper trading account with IB.

Thanks for reply..
 

mastermind007

Well-Known Member
#5
Rohan

If you are with IB, you are already with a superior ATS environment. AmiBroker also has native level support for IB, which I can see that you are already aware of ..

Do not bother with NOW or NEST for ATS.

Do you already have paid up subscription for IB data. Generally speaking, approach to configure IB is different to configuring NEST/NOW ATS
 
Last edited:
#6
I have borrowed the paper trading account from a friend just for testing my strategy. I cant use it for trading. I want to test my strategy on IB paper trading account and then use it on NEST trader. Also the brokerage and margins of my broker (zerodha) seem to be better than IB, who require huge capital. SO I will be finally using NEST trader only.

What I am trying to do is combine the above indicator with the Alternating Trades code given below (taken from amibroker site). I tried copy pasting the alternating trades code below the supertrend indicator. But it doesnt seem to work. Orders are not going to IB.

Alternating Trades code :
Code:
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);
I have tested the above code with some simple crossover indicators. It works fine. But it doesnt seem to work on this Supertrend Indicator.
Any help is highly appreciated..
 

Intraday Higher Leverage

Save up to 90% in brokerage and get higher leverage for intraday trades.

Name:Phone:
Email:City:
State:
Are you a day trader?