Code to automatically place an order

When my Buy/Sell order gets executed, i want to automatically place my target price order and stop loss order.

I am using zerodha trader. Is it possible to do it?

Hi Mani,

Here is the Code...

Before Take to Live Check This AFL with Bar Replay

to ensure that orders are Fired correctly..

This AFL to Fire Orders from Amibroker to Nest/Zerodha Trader
Ensure U Subscribed to Plus Trading Plugin
Ensure Nest Trader is Running and U Logged in to Nest Plus
Enter Your Client ID and Order Details in Parameter Window
Enter Your Tgt Percent and SLoss Percent (Only Number) in Parameter Window
Enter Your Trade Symbol in the Parameter Window as same as it is in Nest Ex:- ACC-EQ, NIFTY15MARFUT etc
This AFL will Place a Tgt and SL order after U take Long/Short Position
If Tgt Order Hits SL Order will be canceled and Vice Versa
Insert Ur Buy&Short (Not Sell) Condition in the Specified Place
Only Define Buy and Short..DoNot Define/assign Sell&Cover as it is assigned by this AFL based on Your Tgt/SLoss Percent.


SetChartOptions(0,chartShowArrows|chartShowDates);
_N(Title = StrFormat("{{NAME}} - {{INTERVAL}} {{DATE}} Open %g, Hi %g, Lo %g, Close %g (%.1f%%) {{VALUES}}", O, H, L, C, SelectedValue( ROC( C, 1 ) ) ));
Plot( C, "Close", ParamColor("Color", colorDefault ), styleNoTitle | ParamStyle("Style") | GetPriceStyle() ); 

_SECTION_BEGIN("Nest Plugin");
//AFL to Fire Orders from Amibroker to Nest/Zerodha Trader
//Ensure U Subscribed to Plus Trading Plugin
//Ensure Nest Trader is Running and U Logged in to Nest Plus
//Enter Your Client ID and Order Details in Parameter Window
//Enter Your Tgt Percent and SLoss Percent (Only Number) in Parameter Window
//Enter Your Trade Symbol in the Parameter Window as same as it is in Nest Ex:- ACC-EQ, NIFTY15MARFUT etc
//This AFL will Place a Tgt and SL order after U take Long/Short Position
//If Tgt Order Hits SL Order will be canceled and Vice Versa
//Insert Ur Buy/Short (Not Sell) Condition in the Specified Place
//Only Define Buy and Short..DoNot Define/assign Sell/Cover as it is assigned by this AFL based on Your Tgt/SLoss Percent.
Buy=Sell=Short=Cover=0;
BarN = ValueWhen(DateNum()!= Ref(DateNum(),-1),BarIndex());
BarNo = IIf(IsEmpty(BarN),BarIndex()+1,BarIndex()-BarN+1);
NVal = "DAY";
NClient = ParamStr("ZeroDha ClientId","AB1234");
NExch = ParamList("Select Stock Exchange","NSE,NFO,MCX",0);
NTrdSymbol = ParamStr("Enter Trd Symbol Here","ACC-EQ");
NProdType = ParamList("Select Product Type","MIS,NRML,CNC",0);
NOrdType = ParamList("Select Order Type","LIMIT,MKT",0);
NQty = Param("Qty to Buy/Sell?",1,1,5000,1);
ProfitPercent = Param("Target Percentage?",1,0.25,10,0.25);
LossPercent = Param("Stop Loss Percentage?",1,0.25,10,0.25);
PriceSlip = C*0.003; //For Calculating Trigger Price for SL Orders
if (NExch == "MCX")
Decimal = 0;
else
Decimal = 1;

//-----------------------------------------------------------------\\
//--UR Buy/Short Condition Starts Here---------\\
//--This is Sample Condition ..Replace it with Your Own----\\
Buy = TimeNum()==100000;
Short = TimeNum()==110000;

//--UR Buy/Short Condition Ends Here---------\\
Buy = ExRem(Buy,Short);
Short=ExRem(Short,Buy);

BuyPrice = ValueWhen(Buy,C);
BuyNum = Sum(Buy,BarNo);
BuyBar = BarNo - ValueWhen(Buy,BarNo)+1;

ShortPrice = ValueWhen(Short,C);
ShortNum = Sum(Short,BarNo);
ShortBar = BarNo - ValueWhen(Short,BarNo)+1;

BTgtPrice = BuyPrice + (BuyPrice*(ProfitPercent/100));
BSLPrice = BuyPrice - (BuyPrice*(LossPercent/100));

STgtPrice = ShortPrice - (ShortPrice*(ProfitPercent/100));
SSLPrice = ShortPrice + (ShortPrice*(LossPercent/100));

BuyRefNo = StrFormat(Name())+"-Buy-"+NumToStr(BuyNum,1.0)+"-"+StrFormat(Now(1))+"-"+NumToStr(GetChartID(),1.0);
ShortRefNo = StrFormat(Name())+"-Short-"+NumToStr(ShortNum,1.0)+"-"+StrFormat(Now(1))+"-"+NumToStr(GetChartID(),1.0);

BuyTgtRefNo = StrFormat(Name())+"-BuyTgt-"+NumToStr(BuyNum,1.0)+"-"+StrFormat(Now(1))+"-"+NumToStr(GetChartID(),1.0);
BuySLRefNo = StrFormat(Name())+"-BuySL-"+NumToStr(BuyNum,1.0)+"-"+StrFormat(Now(1))+"-"+NumToStr(GetChartID(),1.0);

BuyString1=StaticVarGetText(StrFormat(Name())+"-Buy");
BuyString2 = StrFormat(Name())+"-Buy-"+NumToStr(BuyNum)+"-"+NumToStr(GetChartID(),1.0);

ShortString1=StaticVarGetText(StrFormat(Name())+"-Short");
ShortString2 = StrFormat(Name())+"-Short-"+NumToStr(ShortNum)+"-"+NumToStr(GetChartID(),1.0);

BTgtSell = Cross(C,BTgtPrice) AND BuyNum > 0;
BSLSell = Cross(BSLPrice,C) AND BuyNum > 0;

BTgtSell = ExRem(BTgtSell,Buy);
BSLSell = ExRem(BSLSell,Buy);

STgtBuy = Cross(STgtPrice,C) AND ShortNum > 0;
SSLBuy = Cross(C,SSLPrice) AND ShortNum > 0;

STgtBuy = ExRem(STgtBuy,Short);
SSLBuy = ExRem(SSLBuy,Short);

Sell = BTgtSell AND Sum(BSLSell,BuyBar)==0 AND BuyNum > 0 OR BSLSell AND Sum(BTgtSell,BuyBar)==0 AND BuyNum > 0;
Cover = STgtBuy AND Sum(SSLBuy,ShortBar)==0 AND ShortNum > 0 OR SSLBuy AND Sum(STgtBuy,ShortBar)==0 AND ShortNum > 0;

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

SellNum = Sum(Sell,BarNo);
CoverNum = Sum(Cover,BarNo);

BuyTgtRefNo = StrFormat(Name())+"-BuyTgt-"+NumToStr(BuyNum,1.0)+"-"+StrFormat(Now(1))+"-"+NumToStr(GetChartID(),1.0);
BuySLRefNo = StrFormat(Name())+"-BuySL-"+NumToStr(BuyNum,1.0)+"-"+StrFormat(Now(1))+"-"+NumToStr(GetChartID(),1.0);

ShortTgtRefNo = StrFormat(Name())+"-ShortTgt-"+NumToStr(ShortNum,1.0)+"-"+StrFormat(Now(1))+"-"+NumToStr(GetChartID(),1.0);
ShortSLRefNo = StrFormat(Name())+"-ShortSL-"+NumToStr(ShortNum,1.0)+"-"+StrFormat(Now(1))+"-"+NumToStr(GetChartID(),1.0);

BuyTgtString1=StaticVarGetText(StrFormat(Name())+"-BuyTgt");
BuyTgtString2 = StrFormat(Name())+"-BuyTgt-"+NumToStr(BuyNum)+"-"+NumToStr(GetChartID(),1.0);

BuySLString1=StaticVarGetText(StrFormat(Name())+"-BuySL");
BuySLString2 = StrFormat(Name())+"-BuySL-"+NumToStr(BuyNum)+"-"+NumToStr(GetChartID(),1.0);

ShortTgtString1=StaticVarGetText(StrFormat(Name())+"-ShortTgt");
ShortTgtString2 = StrFormat(Name())+"-ShortTgt-"+NumToStr(ShortNum)+"-"+NumToStr(GetChartID(),1.0);

ShortSLString1=StaticVarGetText(StrFormat(Name())+"-ShortSL");
ShortSLString2 = StrFormat(Name())+"-ShortSL-"+NumToStr(ShortNum)+"-"+NumToStr(GetChartID(),1.0);

BuyCancel1=StaticVarGetText(StrFormat(Name())+"-BuyCancel");
BuyCancel2 = StrFormat(Name())+"-BuyCancel-"+NumToStr(SellNum)+"-"+NumToStr(GetChartID(),1.0);

ShortCancel1=StaticVarGetText(StrFormat(Name())+"-ShortCancel");
ShortCancel2 = StrFormat(Name())+"-ShortCancel-"+NumToStr(CoverNum)+"-"+NumToStr(GetChartID(),1.0);

if ( LastValue(Buy) AND BuyString1 != BuyString2)
{plus = CreateStaticObject("Nest.PlusApi");
plus.SetObjectName(NClient);
plus.PlaceOrder("BUY",BuyRefNo,NExch,NTrdSymbol,NVal,NOrdType,NQty,LastValue(BuyPrice,1),0,0,NProdType,NClient);
StaticVarSetText(StrFormat(Name())+"-Buy",BuyString2 );}

if ( LastValue(Short) AND ShortString1 != ShortString2)
{plus = CreateStaticObject("Nest.PlusApi");
plus.SetObjectName(NClient);
plus.PlaceOrder("SELL",ShortRefNo,NExch,NTrdSymbol,NVal,NOrdType,NQty,LastValue(ShortPrice,1),0,0,NProdType,NClient);
StaticVarSetText(StrFormat(Name())+"-Short",ShortString2 );}

if ( LastValue(Ref(Buy,-1)) AND BuyTgtString1 != BuyTgtString2)
{plus = CreateStaticObject("Nest.PlusApi");
plus.SetObjectName(NClient);
plus.PlaceOrder("SELL",BuyTgtRefNo,NExch,NTrdSymbol,NVal,NOrdType,NQty,LastValue(Prec(BTgtPrice,Decimal),1),0,0,NProdType,NClient);
StaticVarSetText(StrFormat(Name())+"-BuyTgt",BuyTgtString2 );}

if ( LastValue(Ref(Buy,-1)) AND BuySLString1 != BuySLString2)
{plus = CreateStaticObject("Nest.PlusApi");
plus.SetObjectName(NClient);
plus.PlaceOrder("SELL",BuySLRefNo,NExch,NTrdSymbol,NVal,"SL",NQty,LastValue(Prec(BSLPrice-PriceSLip,Decimal),1),LastValue(Prec(BSLPrice,Decimal),1),0,NProdType,NClient);
StaticVarSetText(StrFormat(Name())+"-BuySL",BuySLString2 );}

if ( LastValue(Ref(Short,-1)) AND ShortTgtString1 != ShortTgtString2)
{plus = CreateStaticObject("Nest.PlusApi");
plus.SetObjectName(NClient);
plus.PlaceOrder("BUY",ShortTgtRefNo,NExch,NTrdSymbol,NVal,NOrdType,NQty,LastValue(Prec(STgtPrice,Decimal),1),0,0,NProdType,NClient);
StaticVarSetText(StrFormat(Name())+"-ShortTgt",ShortTgtString2 );}

if ( LastValue(Ref(Short,-1)) AND ShortSLString1 != ShortSLString2)
{plus = CreateStaticObject("Nest.PlusApi");
plus.SetObjectName(NClient);
plus.PlaceOrder("BUY",ShortSLRefNo,NExch,NTrdSymbol,NVal,"SL",NQty,LastValue(Prec(SSLPrice+PriceSLip,Decimal),1),LastValue(Prec(SSLPrice,Decimal),1),0,NProdType,NClient);
StaticVarSetText(StrFormat(Name())+"-ShortSL",ShortSLString2 );}

if ( LastValue(Sell) AND LastValue(BTgtSell) AND BuyCancel1 != BuyCancel2)
{plus = CreateStaticObject("Nest.PlusApi");
plus.SetObjectName(NClient);
plus.CancelOrder(BuySLRefNo);
StaticVarSetText(StrFormat(Name())+"-BuyCancel",BuyCancel2 );}

if ( LastValue(Sell) AND LastValue(BSLSell) AND BuyCancel1 != BuyCancel2)
{plus = CreateStaticObject("Nest.PlusApi");
plus.SetObjectName(NClient);
plus.CancelOrder(BuyTgtRefNo);
StaticVarSetText(StrFormat(Name())+"-BuyCancel",BuyCancel2 );}

if ( LastValue(Cover) AND LastValue(STgtBuy) AND ShortCancel1 != ShortCancel2)
{plus = CreateStaticObject("Nest.PlusApi");
plus.SetObjectName(NClient);
plus.CancelOrder(ShortSLRefNo);
StaticVarSetText(StrFormat(Name())+"-ShortCancel",ShortCancel2 );}

if ( LastValue(Cover) AND LastValue(SSLBuy) AND ShortCancel1 != ShortCancel2)
{plus = CreateStaticObject("Nest.PlusApi");
plus.SetObjectName(NClient);
plus.CancelOrder(ShortTgtRefNo);
StaticVarSetText(StrFormat(Name())+"-ShortCancel",ShortCancel2 );}

PlotShapes( Buy * shapeUpArrow + Sell * shapeDownArrow + Short * shapeDownArrow +  Cover * shapeUpArrow , IIf( Buy, colorGreen,IIf(Short,colorRed,IIf(Sell,colorOrange, IIf(Cover,colorPaleGreen,colorBlack))) ),0,IIf(Buy OR Cover ,L,IIf(Sell OR Short ,H,C)), Offset=-30 );

_SECTION_END();
3 Likes

Hi,
If U want it in AFL(amibroker) and subscribed to Plus trading Plugin,then it is possible.U can place a target order and Stloss order when buy is executed and cancel one order if others gets executed.

Choks Rocks Always !!!

Hi Chokkalingam,
Thanks for your reply. Can you give me more details to achieve this. I want to implement this.

Hi Thangamani,
I 'll Post Tomorrow…

Hi,
I have few questions.
1.What is the difference between executing this AFL and Bracket order in Zerodha?
2.It seems automated trading if yes Do I need to get approval from Exchange and clear NCFM certification?
3.Can we also enter SL-M order which is not possible in Bracket Order?
4.Please let me know How much does it cost for getting Ambi Broker and Plus Trading plugin and anyother data feed charges for executing this AFL?

Hi,
1.This code is for those whose use amibroker to generate signal based on technical analysis and to place order in Nest Trader.
2.It is semi automatic trading i.e. U have to confirm the order in nest Trader.For Semi Auto Trading Exchange Approval not Required and NCFM Cert not required.
3.Yes
4.For Amibroker It cost u appx Rs20,000 and Plus Trading Plugin Rs300/PerMonth and DataFeed Appx 1200 Pe rmonth
U Can also Read This …
https://drive.google.com/uc?export=download&id=0B-AWKNHhYByLY29QOWlhdjQzcUk

I have Nest Also logged into Nestplus started Amibroker pasted your code in formula applied it
There is no error shown But no Orders are being placed in Nest Please guide me
Sachin