Supertrend Autotrading Help Needed

#11
ok
as i will be using nest plugin only.

I need the afl code to combine supertrend or nma swing with autotrading to create a fully automated system

(I was not using nest plugin as there is no simulated trading available in nest trader to test the code)
 
#14
yes
but i need the code for sending buy/sell signals to the market
ClientID = ParamStr("ClientId","000000");
Symbol= ParamStr("Symbol","NIFTY13OCTFUT");
Price=NumToStr(C,8.3,True);
Quantity=Param("Quantity",1,1,100,1);
AT = ParamToggle("AutoTrade","No,Yes");

AplliedQuantity=IIf(LastValue(Buy) AND LastValue(Cover) OR LastValue(Short) AND LastValue(Sell),Quantity*2,Quantity);
RefNumber = Nz(StaticVarGet("RefNumber"));
Checkdt=Nz(StaticVarGet("lastdt"));
dt = LastValue( DateTime() );
Cond=LastValue(Buy) OR LastValue(Short) OR LastValue(Sell) OR LastValue(Cover);

if(AT)
{
plus = CreateStaticObject("Nest.PlusApi");
if(plus)
{
plus.SetObjectName(ClientID);
if(Cond AND Checkdt != dt )
{
if(LastValue(Buy) OR LastValue(Cover)){plus.PlaceOrder("BUY",RefNumber,"NFO",Symbol,"DAY","LIMIT",AplliedQuantity,Price,0,1,"NRML",ClientID);}
if(LastValue(Short) OR LastValue(Sell)){plus.PlaceOrder("SELL",RefNumber,"NFO",Symbol,"DAY","LIMIT",AplliedQuantity,Price,0,1,"NRML",ClientID);}
StaticVarSet("RefNumber",RefNumber+1);
StaticVarSet("lastdt",dt );
}
}
}



change client id . Which data feeder r u using? check the symbol name, it should not mismatch with nest, and do u have api subscribed with omnysis or it wont work
 
#15
Thanks for the code..

I am using IB data feed now. So im testing in IB paper trading account.
I have similar code for IB. But its not working with the Supertrend Indicator.
Somehow its not picking up LastValue(Buy) and LastValue(Sell).
Trigger is not going to IBcontroller.
 

mastermind007

Well-Known Member
#16
Thanks for the code..

I am using IB data feed now. So im testing in IB paper trading account.
I have similar code for IB. But its not working with the Supertrend Indicator.
Somehow its not picking up LastValue(Buy) and LastValue(Sell).
Trigger is not going to IBcontroller.
You will have to do some debugging. Do you know how to Debug AFL?
 
#17
Yes. A little bit.
Im trying my best to debug .
Even audio signals seem to work for buy/sell.
Buy orders are not being executed.
This is my final code :
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();


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);
Plz tell me where im going wrong..
 
#18
I have subscribed to the Nest Plus Trading Plugin...
But they didnt give me any software or plugin
Just it appears on their website that you are subscribed to the plugin
But in Nest Trader it says not subscribed..
 
#19
I have subscribed to the Nest Plus Trading Plugin...
But they didnt give me any software or plugin
Just it appears on their website that you are subscribed to the plugin
But in Nest Trader it says not subscribed..
You have to subscribe for nest API not nest plugin. API is activated in nest plus which is charged 300/month by omnysis, nest to amibroker plugin i have posted in my above post, copy paste the above plugin after SuperTrend afl. change client id to your nest ID given by your broker in the ClientID=ParamStr("ClientId","000000"); in the above plugin
 
Last edited:

Similar threads