// Developer: Rajandran R (Founder - Marketcalls / Co-Founder - Algomojo) // Date: 23-Oct-2022 // Website: algomojo.com / marketcalls.in _SECTION_BEGIN("Zerodha - Broker Controls"); RequestTimedRefresh(1, False); broker =ParamStr("Broker","ze"); //Broker Short Code - an- angel broking ver = ParamStr("API Version ","1.0"); clnt_id = ParamStr("Client ID","xxxx"); //Enter your trading client ID user_apikey = ParamStr("apikey","xxxxxxxxxxxxxxxxxxxxx"); //Enter your API key here api_secretkey = ParamStr("secretkey","xxxxxxxxxxxxxxxxxxxxx"); //Enter your API secret key here _SECTION_END(); _SECTION_BEGIN("Zerodha - Order Controls"); strg_name = ParamStr("Strategy Name", "Line Strategy"); variety = ParamList("Variety","regular|amo",0); tradingsymbol = ParamStr("Trading Symbol","RELIANCE"); exchange = ParamList("Exchange","NSE|NFO|BSE|MCX",0); order_type = ParamList("Order Type","MARKET|LIMIT|SL|SL-M",0); product = ParamList("Product Type","CNC|NRML|MIS",2); price = "0"; trigger_price = "0"; quantity = Param("quanity",1,0,10000); disclosed_quantity = "0"; validity = "DAY"; tag = "amorder"; EnableAlgo = ParamList("AlgoStatus","Disable|Enable",0); resp = ""; stgy_name = strg_name; static_name_ = Name()+GetChartID()+interval(2)+stgy_name; static_name_algo = Name()+GetChartID()+interval(2)+stgy_name+"algostatus"; //Mapping of Orders iBuy = "BUY"; iSell = "SELL"; GfxSelectFont( "BOOK ANTIQUA", 14, 100 ); GfxSetBkMode( 1 ); if(EnableAlgo == "Enable") { AlgoStatus = "Algo Enabled"; GfxSetTextColor( colorGreen ); GfxTextOut( "Algostatus : "+AlgoStatus , 20, 40); if(Nz(StaticVarGet(static_name_algo),0)!=1) { _TRACE("Algo Status : Enabled"); StaticVarSet(static_name_algo, 1); } } if(EnableAlgo == "Disable") { AlgoStatus = "Algo Disabled"; GfxSetTextColor( colorRed ); GfxTextOut( "Algostatus : "+AlgoStatus , 20, 40); if(Nz(StaticVarGet(static_name_algo),0)!=0) { _TRACE("Algo Status : Disabled"); StaticVarSet(static_name_algo, 0); } } function GetPositionsBook() { //Creating the Trading Bridge algomojo=CreateObject("AMAMIBRIDGE.Main"); api_data = "{ \"pos_type\":\"NET\" }"; _TRACE("PositionBook API Request"+api_data); //Sending The Broker Request for NetOpenPositions resp=algomojo.AMDispatcher(user_apikey,api_secretkey,"Positions",api_data,broker,ver); _TRACE("PositionBook API Response : "+resp); return resp; } function NetOpenPositions() { flag = 0; resp = GetPositionsBook(); posNetqty =0; netposdata = StrExtract( resp, 1,'[' ); for( item = 1; ( sym = StrExtract( netposdata, item,'{' )) != ""; item++ ) { if(Strfind(sym,tradingsymbol) AND StrFind(sym,product)) //Matches the symbol and //Matches the Order Type { flag = 1; //turn on the flag for( jitem = 0; ( posdetails = StrExtract( sym, jitem,',' )) != ""; jitem++ ) { if(Strfind(posdetails,"tradingsymbol")) { posdetails = StrExtract(posdetails,1,':'); possym = StrTrim(posdetails,"\""); _TRACE("\nTrading Symbol : "+possym); } if(Strfind(posdetails,"quantity") AND !Strfind(posdetails,"disclosed_quantity") AND !Strfind(posdetails,"overnight_quantity") AND !Strfind(posdetails,"buy_quantity") AND !Strfind(posdetails,"sell_quantity") AND !Strfind(posdetails,"day_buy_quantity") AND !Strfind(posdetails,"day_sell_quantity")) { posdetails = StrExtract(posdetails,1,':'); posNetqty = StrToNum(StrTrim(posdetails,"\"")); _TRACE("\nNetQty : "+posNetqty); } } //end of for loop } }//end of for loop if(flag==0) { _TRACE("\nTrading Symbol Not Found"); posNetqty =0; _TRACE("\nNetQty : "+posNetqty); } return posNetqty; } //Buy and Sell Order Functions function Placeorder(TransType,orderqty) { algomojo=CreateObject("AMAMIBRIDGE.Main"); symboltoken = ""; api_data ="{\"strg_name\": \""+strg_name+"\", \"variety\":\""+variety+"\", \"tradingsymbol\":\""+tradingsymbol+"\", \"exchange\":\""+exchange+"\", \"transaction_type\":\""+TransType+"\", \"order_type\":\""+order_type+"\", \"quantity\":\""+orderqty+"\", \"product\":\""+product+"\", \"price\":\""+price+"\", \"trigger_price\":\""+trigger_price+"\", \"disclosed_quantity\":\""+disclosed_quantity+"\", \"validity\":\""+validity+"\", \"tag\":\""+tag+"\" }"; _TRACE("API Request"+api_data); resp=algomojo.AMDispatcher(user_apikey, api_secretkey,"PlaceOrder",api_data,broker,ver); _TRACE("API Response : "+resp); _TRACE("Strategy Name : "+strg_name+" AlgoStatus : "+ EnableAlgo); _TRACE("Chart Symbol : "+ Name() +" Trading Symbol : "+ tradingsymbol +" qty : "+ orderqty +" Signal : Buy Signal TimeFrame : "+ Interval(2)+" ChardId : "+ GetChartID() + " LTP : "+LastValue(C)); Say( "Order Placed" ); } _SECTION_END(); _SECTION_BEGIN("Line Pair Trading Module"); SetBarsRequired(-2,-2); //turning off quick afl 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_END(); //Static Variables will be saved in Amibroker every 60 seconds once SetOption("StaticVarAutoSave",60); // Send orders even if Amibroker is minimized or Chart is not active RequestTimedRefresh(0.1, False); EnableTextOutput(False); IntradayMode = ParamList("Intraday Mode","ON|OFF",0); StartTradeTime = ParamTime("Start Time","09:30"); EndTradeTime = ParamTime("End Time","15:00"); ExitTradeTime = ParamTime("Squareoff Time","15:15"); BuyMode=ParamToggle("Buy Mode","BUY ABOVE|BUY BELOW",0); SellMode=ParamToggle("Sell Mode","SELL ABOVE|SELL BELOW",0); ShortMode=ParamToggle("Short Mode","SHORT ABOVE|SHORT BELOW",1); CoverMode=ParamToggle("Cover Mode","COVER ABOVE|COVER BELOW",1); ExitPos = ParamList("Exit Positions","CURRENT|ALLOPENPOSITIONS"); buyquantity1 = Param("Buy Quantity1",1,0,10000,1); buyquantity2 = Param("Buy Quantity2",1,0,10000,1); buyquantity3 = Param("Buy Quantity3",1,0,10000,1); shortquantity1 = Param("Short Quantity1",1,0,10000,1); shortquantity2 = Param("Short Quantity2",1,0,10000,1); shortquantity3 = Param("Short Quantity3",1,0,10000,1); clear = ParamTrigger("Reset Trades and Signals","Press to Reset"); staticvar = Name()+Interval()+GetChartID(); if(clear) { StaticVarRemove(staticvar+"*"); _TRACE("Static Variables Cleared"); } Plot( C, "Price", colorBlack, styleCandle ); if(BuyMode==0) Buydisplaytext = "Buy Above"; if(BuyMode==1) Buydisplaytext = "Buy Below"; if(SellMode==0) Selldisplaytext = "Sell Above"; if(SellMode==1) Selldisplaytext = "Sell Below"; if(ShortMode==0) Shortdisplaytext = "Short Above"; if(ShortMode==1) Shortdisplaytext = "Short Below"; if(CoverMode==0) Coverdisplaytext = "Cover Above"; if(CoverMode==1) Coverdisplaytext = "Cover Below"; B1 = LastValue(Study("B1", GetChartID() )); B2 = LastValue(Study("B2", GetChartID() )); B3 = LastValue(Study("B3", GetChartID() )); X1 = LastValue(Study("X1", GetChartID() )); X2 = LastValue(Study("X2", GetChartID() )); X3 = LastValue(Study("X3", GetChartID() )); S1 = LastValue(Study("S1", GetChartID() )); S2 = LastValue(Study("S2", GetChartID() )); S3 = LastValue(Study("S3", GetChartID() )); C1 = LastValue(Study("C1", GetChartID() )); C2 = LastValue(Study("C2", GetChartID() )); C3 = LastValue(Study("C3", GetChartID() )); function lineplot(value,text,quantity,displaytext) { textcolor = IIf(text=="B1" OR text=="B2" OR text=="B3", colorGreen, IIf(text=="X1" OR text=="X2" OR text=="X3", colorRed, IIf(text=="S1" OR text=="S2" OR text=="S3", colorBrown, IIf(text=="C1" OR text=="C2" OR text=="C3", colorBlue,colorGrey40)))); x = LastValue( ValueWhen( ExRem( value, 0 ), DateTime() ) ); if( x!= 0 AND (text=="B1" OR text=="B2" OR text=="B3")) PlotText(text+" "+Displaytext+" : "+value+" Qty ="+quantity,BarCount-40,value,colorWhite,textcolor,10); if( x!= 0 AND (text=="S1" OR text=="S2" OR text=="S3")) PlotText(text+" "+Displaytext+" : "+value+" Qty ="+quantity,BarCount-40,value,colorWhite,textcolor,10); if( x!= 0 AND ExitPos=="CURRENT" AND (text=="X1" OR text=="X2" OR text=="X3")) PlotText(text+" "+Displaytext+" : "+value+" Qty ="+quantity,BarCount-40,value,colorWhite,textcolor,10); if( x!= 0 AND ExitPos=="CURRENT" AND (text=="C1" OR text=="C2" OR text=="C3")) PlotText(text+" "+Displaytext+" : "+value+" Qty ="+quantity,BarCount-40,value,colorWhite,textcolor,10); if( x!= 0 AND ExitPos=="ALLOPENPOSITIONS" AND (text=="X1" OR text=="X2" OR text=="X3")) PlotText(text+" "+Displaytext+" : "+value+" Qty = All",BarCount-40,value,colorWhite,textcolor,10); if( x!= 0 AND ExitPos=="ALLOPENPOSITIONS" AND (text=="C1" OR text=="C2" OR text=="C3")) PlotText(text+" "+Displaytext+" : "+value+" Qty = All",BarCount-40,value,colorWhite,textcolor,10); } lineplot(B1,"B1",buyquantity1,Buydisplaytext); lineplot(B2,"B2",buyquantity2,Buydisplaytext); lineplot(B3,"B3",buyquantity3,Buydisplaytext); lineplot(X1,"X1",buyquantity1,Selldisplaytext); lineplot(X2,"X2",buyquantity2,Selldisplaytext); lineplot(X3,"X3",buyquantity3,Selldisplaytext); lineplot(S1,"S1",shortquantity1,Shortdisplaytext); lineplot(S2,"S2",shortquantity2,Shortdisplaytext); lineplot(S3,"S3",shortquantity3,Shortdisplaytext); lineplot(C1,"C1",shortquantity1,Coverdisplaytext); lineplot(C2,"C2",shortquantity2,Coverdisplaytext); lineplot(C3,"C3",shortquantity3,Coverdisplaytext); if(EnableAlgo != "Disable") { lasttime = StrFormat("%0.f",LastValue(BarIndex())); SetChartBkColor(colorDarkGrey); if(IntradayMode=="ON" AND LastValue(TimeNum())== ExitTradeTime AND Nz(StaticVarGet(staticvar+"SquareOff"))==0) { OpenPos = NetOpenPositions(); if(OpenPos>0) { PlaceOrder(iSell,OpenPos); } if(OpenPos<0) { PlaceOrder(iBuy,abs(OpenPos)); } if(OpenPos==0) { _TRACE("No Open Positions Found and Hence Squareoff Order Ignored"); } StaticVarSet(staticvar+"SquareOffIndex",LastValue(BarIndex())); StaticVarSet(staticvar+"SquareOff",1); _TRACE("Squareoff Alert Triggered"); } if(BuyMode==0 AND LastValue(Cross(H,B1)) AND Nz(StaticVarGet(staticvar+"B1"))==0) { if((IntradayMode=="ON" AND LastValue(TimeNum())>= StartTradeTime AND LastValue(TimeNum()) <= EndTradeTime) OR IntradayMode=="OFF") { PlaceOrder(iBuy,buyquantity1); StaticVarSet(staticvar+"B1index",LastValue(BarIndex())); StaticVarSet(staticvar+"B1",1); _TRACE("Buy Above B1 Triggered"); } } if(BuyMode==1 AND LastValue(Cross(B1,L)) AND Nz(StaticVarGet(staticvar+"B1"))==0) { if((IntradayMode=="ON" AND LastValue(TimeNum())>= StartTradeTime AND LastValue(TimeNum()) <= EndTradeTime) OR IntradayMode=="OFF") { PlaceOrder(iBuy,buyquantity1); StaticVarSet(staticvar+"B1index",LastValue(BarIndex())); StaticVarSet(staticvar+"B1",1); _TRACE("Buy Below B1 Triggered"); } } if(BuyMode==0 AND LastValue(Cross(H,B2)) AND Nz(StaticVarGet(staticvar+"B2"))==0) { if((IntradayMode=="ON" AND LastValue(TimeNum())>= StartTradeTime AND LastValue(TimeNum()) <= EndTradeTime) OR IntradayMode=="OFF") { PlaceOrder(iBuy,buyquantity2); StaticVarSet(staticvar+"B2index",LastValue(BarIndex())); StaticVarSet(staticvar+"B2",1); _TRACE("Buy Above B2 Triggered"); } } if(BuyMode==1 AND LastValue(Cross(B2,L)) AND Nz(StaticVarGet(staticvar+"B2"))==0) { if((IntradayMode=="ON" AND LastValue(TimeNum())>= StartTradeTime AND LastValue(TimeNum()) <= EndTradeTime) OR IntradayMode=="OFF") { PlaceOrder(iBuy,buyquantity2); StaticVarSet(staticvar+"B2index",LastValue(BarIndex())); StaticVarSet(staticvar+"B2",1); _TRACE("Buy Below B2 Triggered"); } } if(BuyMode==0 AND LastValue(Cross(H,B3)) AND Nz(StaticVarGet(staticvar+"B3"))==0) { if((IntradayMode=="ON" AND LastValue(TimeNum())>= StartTradeTime AND LastValue(TimeNum()) <= EndTradeTime) OR IntradayMode=="OFF") { PlaceOrder(iBuy,buyquantity3); StaticVarSet(staticvar+"B3index",LastValue(BarIndex())); StaticVarSet(staticvar+"B3",1); _TRACE("Buy Above B3 Triggered"); } } if(BuyMode==1 AND LastValue(Cross(B3,L)) AND Nz(StaticVarGet(staticvar+"B3"))==0) { if((IntradayMode=="ON" AND LastValue(TimeNum())>= StartTradeTime AND LastValue(TimeNum()) <= EndTradeTime) OR IntradayMode=="OFF") { PlaceOrder(iBuy,buyquantity3); StaticVarSet(staticvar+"B3index",LastValue(BarIndex())); StaticVarSet(staticvar+"B3",1); _TRACE("Buy Below B3 Triggered"); } } if(SellMode==0 AND LastValue(Cross(H,X1)) AND Nz(StaticVarGet(staticvar+"X1"))==0) { if((IntradayMode=="ON" AND LastValue(TimeNum())>= StartTradeTime AND LastValue(TimeNum()) <= EndTradeTime) OR IntradayMode=="OFF") { OpenPos = NetOpenPositions(); if(OpenPos > 0 AND ExitPos=="CURRENT") { //If positive open positions are there only then exit the Current Order Quantity PlaceOrder(iSELL,buyquantity1); } if(OpenPos > 0 AND ExitPos=="ALLOPENPOSITIONS") { //If positive open positions are there only then exit the All Open Positions PlaceOrder(iSELL,OpenPos); } if(OpenPos == 0) { _TRACE("No Open Positions. Hence Exit Order is ignored"); } StaticVarSet(staticvar+"X1index",LastValue(BarIndex())); StaticVarSet(staticvar+"X1",1); _TRACE("Sell Above X1 Triggered"); } } if(SellMode==1 AND LastValue(Cross(X1,L)) AND Nz(StaticVarGet(staticvar+"X1"))==0) { if((IntradayMode=="ON" AND LastValue(TimeNum())>= StartTradeTime AND LastValue(TimeNum()) <= EndTradeTime) OR IntradayMode=="OFF") { OpenPos = NetOpenPositions(); if(OpenPos > 0 AND ExitPos=="CURRENT") { //If positive open positions are there only then exit the Current Order Quantity PlaceOrder(iSELL,buyquantity1); } if(OpenPos > 0 AND ExitPos=="ALLOPENPOSITIONS") { //If positive open positions are there only then exit the All Open Positions PlaceOrder(iSELL,OpenPos); } if(OpenPos == 0) { _TRACE("No Open Positions. Hence Exit Order is ignored"); } StaticVarSet(staticvar+"X1index",LastValue(BarIndex())); StaticVarSet(staticvar+"X1",1); _TRACE("Sell Below X1 Triggered"); } } if(SellMode==0 AND LastValue(Cross(H,X2)) AND Nz(StaticVarGet(staticvar+"X2"))==0) { if((IntradayMode=="ON" AND LastValue(TimeNum())>= StartTradeTime AND LastValue(TimeNum()) <= EndTradeTime) OR IntradayMode=="OFF") { OpenPos = NetOpenPositions(); if(OpenPos > 0 AND ExitPos=="CURRENT") { //If positive open positions are there only then exit the Current Order Quantity PlaceOrder(iSELL,buyquantity2); } if(OpenPos > 0 AND ExitPos=="ALLOPENPOSITIONS") { //If positive open positions are there only then exit the All Open Positions PlaceOrder(iSELL,OpenPos); } if(OpenPos == 0) { _TRACE("No Open Positions. Hence Exit Order is ignored"); } StaticVarSet(staticvar+"X2index",LastValue(BarIndex())); StaticVarSet(staticvar+"X2",1); _TRACE("Sell Above X2 Triggered"); } } if(SellMode==1 AND LastValue(Cross(X2,L)) AND Nz(StaticVarGet(staticvar+"X2"))==0) { if((IntradayMode=="ON" AND TimeNum()== ExitTradeTime) OR IntradayMode=="OFF") { OpenPos = NetOpenPositions(); if(OpenPos > 0 AND ExitPos=="CURRENT") { //If positive open positions are there only then exit the Current Order Quantity PlaceOrder(iSELL,buyquantity2); } if(OpenPos > 0 AND ExitPos=="ALLOPENPOSITIONS") { //If positive open positions are there only then exit the All Open Positions PlaceOrder(iSELL,OpenPos); } if(OpenPos == 0) { _TRACE("No Open Positions. Hence Exit Order is ignored"); } StaticVarSet(staticvar+"X2index",LastValue(BarIndex())); StaticVarSet(staticvar+"X2",1); _TRACE("Sell Below X2 Triggered"); } } if(SellMode==0 AND LastValue(Cross(H,X3)) AND Nz(StaticVarGet(staticvar+"X3"))==0) { if((IntradayMode=="ON" AND LastValue(TimeNum())>= StartTradeTime AND LastValue(TimeNum()) <= EndTradeTime) OR IntradayMode=="OFF") { OpenPos = NetOpenPositions(); if(OpenPos > 0 AND ExitPos=="CURRENT") { //If positive open positions are there only then exit the Current Order Quantity PlaceOrder(iSELL,buyquantity3); } if(OpenPos > 0 AND ExitPos=="ALLOPENPOSITIONS") { //If positive open positions are there only then exit the All Open Positions PlaceOrder(iSELL,OpenPos); } if(OpenPos == 0) { _TRACE("No Open Positions. Hence Exit Order is ignored"); } StaticVarSet(staticvar+"X3index",LastValue(BarIndex())); StaticVarSet(staticvar+"X3",1); _TRACE("Sell Above X3 Triggered"); } } if(SellMode==1 AND LastValue(Cross(X3,L)) AND Nz(StaticVarGet(staticvar+"X3"))==0) { if((IntradayMode=="ON" AND LastValue(TimeNum())>= StartTradeTime AND LastValue(TimeNum()) <= EndTradeTime) OR IntradayMode=="OFF") { OpenPos = NetOpenPositions(); if(OpenPos > 0 AND ExitPos=="CURRENT") { //If positive open positions are there only then exit the Current Order Quantity PlaceOrder(iSELL,buyquantity3); } if(OpenPos > 0 AND ExitPos=="ALLOPENPOSITIONS") { //If positive open positions are there only then exit the All Open Positions PlaceOrder(iSELL,OpenPos); } if(OpenPos == 0) { _TRACE("No Open Positions. Hence Exit Order is ignored"); } StaticVarSet(staticvar+"X3index",LastValue(BarIndex())); StaticVarSet(staticvar+"X3",1); _TRACE("Sell Below X3 Triggered"); } } if(ShortMode==0 AND LastValue(Cross(H,S1)) AND Nz(StaticVarGet(staticvar+"S1"))==0) { if((IntradayMode=="ON" AND LastValue(TimeNum())>= StartTradeTime AND LastValue(TimeNum()) <= EndTradeTime) OR IntradayMode=="OFF") { PlaceOrder(iSell,shortquantity1); StaticVarSet(staticvar+"S1index",LastValue(BarIndex())); StaticVarSet(staticvar+"S1",1); _TRACE("Short Above S1 Triggered"); } } if(ShortMode==1 AND LastValue(Cross(S1,L)) AND Nz(StaticVarGet(staticvar+"S1"))==0) { if((IntradayMode=="ON" AND LastValue(TimeNum())>= StartTradeTime AND LastValue(TimeNum()) <= EndTradeTime) OR IntradayMode=="OFF") { PlaceOrder(iSell,shortquantity1); StaticVarSet(staticvar+"S1index",LastValue(BarIndex())); StaticVarSet(staticvar+"S1",1); _TRACE("Short Below S1 Triggered"); } } if(ShortMode==0 AND LastValue(Cross(H,S2)) AND Nz(StaticVarGet(staticvar+"S2"))==0) { if((IntradayMode=="ON" AND LastValue(TimeNum())>= StartTradeTime AND LastValue(TimeNum()) <= EndTradeTime) OR IntradayMode=="OFF") { PlaceOrder(iSell,shortquantity2); StaticVarSet(staticvar+"S2index",LastValue(BarIndex())); StaticVarSet(staticvar+"S2",1); _TRACE("Short Above S2 Triggered"); } } if(ShortMode==1 AND LastValue(Cross(S2,L)) AND Nz(StaticVarGet(staticvar+"S2"))==0) { if((IntradayMode=="ON" AND LastValue(TimeNum())>= StartTradeTime AND LastValue(TimeNum()) <= EndTradeTime) OR IntradayMode=="OFF") { PlaceOrder(iSell,shortquantity2); StaticVarSet(staticvar+"S2index",LastValue(BarIndex())); StaticVarSet(staticvar+"S2",1); _TRACE("Short Below S2 Triggered"); } } if(ShortMode==0 AND LastValue(Cross(H,S3)) AND Nz(StaticVarGet(staticvar+"S3"))==0) { if((IntradayMode=="ON" AND LastValue(TimeNum())>= StartTradeTime AND LastValue(TimeNum()) <= EndTradeTime) OR IntradayMode=="OFF") { PlaceOrder(iSell,shortquantity3); StaticVarSet(staticvar+"S3index",LastValue(BarIndex())); StaticVarSet(staticvar+"S3",1); _TRACE("Short Above S3 Triggered"); } } if(ShortMode==1 AND LastValue(Cross(S3,L)) AND Nz(StaticVarGet(staticvar+"S3"))==0) { if((IntradayMode=="ON" AND LastValue(TimeNum())>= StartTradeTime AND LastValue(TimeNum()) <= EndTradeTime) OR IntradayMode=="OFF") { PlaceOrder(iSell,shortquantity3); StaticVarSet(staticvar+"S3index",LastValue(BarIndex())); StaticVarSet(staticvar+"S3",1); _TRACE("Short Below S3 Triggered"); } } if(CoverMode==0 AND LastValue(Cross(H,C1)) AND Nz(StaticVarGet(staticvar+"C1"))==0) { if((IntradayMode=="ON" AND LastValue(TimeNum())>= StartTradeTime AND LastValue(TimeNum()) <= EndTradeTime) OR IntradayMode=="OFF") { OpenPos = NetOpenPositions(); if(OpenPos < 0 AND ExitPos=="CURRENT") { //If positive open positions are there only then exit the Current Order Quantity PlaceOrder(iBuy,shortquantity1); } if(OpenPos < 0 AND ExitPos=="ALLOPENPOSITIONS") { //If positive open positions are there only then exit the All Open Positions PlaceOrder(iBuy,abs(OpenPos)); } if(OpenPos == 0) { _TRACE("No Open Positions. Hence Exit Order is ignored"); } StaticVarSet(staticvar+"C1index",LastValue(BarIndex())); StaticVarSet(staticvar+"C1",1); _TRACE("Cover Above C1 Triggered"); } } if(CoverMode==1 AND LastValue(Cross(C1,L)) AND Nz(StaticVarGet(staticvar+"C1"))==0) { if((IntradayMode=="ON" AND LastValue(TimeNum())>= StartTradeTime AND LastValue(TimeNum()) <= EndTradeTime) OR IntradayMode=="OFF") { OpenPos = NetOpenPositions(); if(OpenPos < 0 AND ExitPos=="CURRENT") { //If positive open positions are there only then exit the Current Order Quantity PlaceOrder(iBuy,shortquantity1); } if(OpenPos < 0 AND ExitPos=="ALLOPENPOSITIONS") { //If positive open positions are there only then exit the All Open Positions PlaceOrder(iBuy,abs(OpenPos)); } if(OpenPos == 0) { _TRACE("No Open Positions. Hence Exit Order is ignored"); } StaticVarSet(staticvar+"C1index",LastValue(BarIndex())); StaticVarSet(staticvar+"C1",1); _TRACE("Cover Below C1 Triggered"); } } if(CoverMode==0 AND LastValue(Cross(H,C2)) AND Nz(StaticVarGet(staticvar+"C2"))==0) { if((IntradayMode=="ON" AND LastValue(TimeNum())>= StartTradeTime AND LastValue(TimeNum()) <= EndTradeTime) OR IntradayMode=="OFF") { OpenPos = NetOpenPositions(); if(OpenPos < 0 AND ExitPos=="CURRENT") { //If positive open positions are there only then exit the Current Order Quantity PlaceOrder(iBuy,shortquantity2); } if(OpenPos < 0 AND ExitPos=="ALLOPENPOSITIONS") { //If positive open positions are there only then exit the All Open Positions PlaceOrder(iBuy,abs(OpenPos)); } if(OpenPos == 0) { _TRACE("No Open Positions. Hence Exit Order is ignored"); } StaticVarSet(staticvar+"C2index",LastValue(BarIndex())); StaticVarSet(staticvar+"C2",1); _TRACE("Cover Above C2 Triggered"); } } if(CoverMode==1 AND LastValue(Cross(C2,L)) AND Nz(StaticVarGet(staticvar+"C2"))==0) { if((IntradayMode=="ON" AND LastValue(TimeNum())>= StartTradeTime AND LastValue(TimeNum()) <= EndTradeTime) OR IntradayMode=="OFF") { OpenPos = NetOpenPositions(); if(OpenPos < 0 AND ExitPos=="CURRENT") { //If positive open positions are there only then exit the Current Order Quantity PlaceOrder(iBuy,shortquantity2); } if(OpenPos < 0 AND ExitPos=="ALLOPENPOSITIONS") { //If positive open positions are there only then exit the All Open Positions PlaceOrder(iBuy,abs(OpenPos)); } if(OpenPos == 0) { _TRACE("No Open Positions. Hence Exit Order is ignored"); } StaticVarSet(staticvar+"C2index",LastValue(BarIndex())); StaticVarSet(staticvar+"C2",1); _TRACE("Cover Below C2 Triggered"); } } if(CoverMode==0 AND LastValue(Cross(H,C3)) AND Nz(StaticVarGet(staticvar+"C3"))==0) { if((IntradayMode=="ON" AND LastValue(TimeNum())>= StartTradeTime AND LastValue(TimeNum()) <= EndTradeTime) OR IntradayMode=="OFF") { OpenPos = NetOpenPositions(); if(OpenPos < 0 AND ExitPos=="CURRENT") { //If positive open positions are there only then exit the Current Order Quantity PlaceOrder(iBuy,shortquantity3); } if(OpenPos < 0 AND ExitPos=="ALLOPENPOSITIONS") { //If positive open positions are there only then exit the All Open Positions PlaceOrder(iBuy,abs(OpenPos)); } if(OpenPos == 0) { _TRACE("No Open Positions. Hence Exit Order is ignored"); } StaticVarSet(staticvar+"C3index",LastValue(BarIndex())); StaticVarSet(staticvar+"C3",1); _TRACE("Cover Above C3 Triggered"); } } if(CoverMode==1 AND LastValue(Cross(C3,L)) AND Nz(StaticVarGet(staticvar+"C3"))==0) { if((IntradayMode=="ON" AND LastValue(TimeNum())>= StartTradeTime AND LastValue(TimeNum()) <= EndTradeTime) OR IntradayMode=="OFF") { OpenPos = NetOpenPositions(); if(OpenPos < 0 AND ExitPos=="CURRENT") { //If positive open positions are there only then exit the Current Order Quantity PlaceOrder(iBuy,shortquantity3); } if(OpenPos < 0 AND ExitPos=="ALLOPENPOSITIONS") { //If positive open positions are there only then exit the All Open Positions PlaceOrder(iBuy,abs(OpenPos)); } if(OpenPos == 0) { _TRACE("No Open Positions. Hence Exit Order is ignored"); } StaticVarSet(staticvar+"C3index",LastValue(BarIndex())); StaticVarSet(staticvar+"C3",1); _TRACE("Cover Below C3 Triggered"); } } //Plot Signals and Arrows PlotShapes(IIf(BarIndex()==StaticVarGet(staticvar+"B1index"), shapeSquare, shapeNone),colorGreen, 0, L, Offset=-40); PlotShapes(IIf(BarIndex()==StaticVarGet(staticvar+"B1index"), shapeSquare, shapeNone),colorLime, 0,L, Offset=-50); PlotShapes(IIf(BarIndex()==StaticVarGet(staticvar+"B1index"), shapeUpArrow, shapeNone),colorWhite, 0,L, Offset=-45); PlotShapes(IIf(BarIndex()==StaticVarGet(staticvar+"B2index"), shapeSquare, shapeNone),colorGreen, 0, L, Offset=-40); PlotShapes(IIf(BarIndex()==StaticVarGet(staticvar+"B2index"), shapeSquare, shapeNone),colorLime, 0,L, Offset=-50); PlotShapes(IIf(BarIndex()==StaticVarGet(staticvar+"B2index"), shapeUpArrow, shapeNone),colorWhite, 0,L, Offset=-45); PlotShapes(IIf(BarIndex()==StaticVarGet(staticvar+"B3index"), shapeSquare, shapeNone),colorGreen, 0, L, Offset=-40); PlotShapes(IIf(BarIndex()==StaticVarGet(staticvar+"B3index"), shapeSquare, shapeNone),colorLime, 0,L, Offset=-50); PlotShapes(IIf(BarIndex()==StaticVarGet(staticvar+"B3index"), shapeUpArrow, shapeNone),colorWhite, 0,L, Offset=-45); PlotShapes(IIf(BarIndex()==StaticVarGet(staticvar+"S1index"), shapeSquare, shapeNone),colorRed, 0, H, Offset=40); PlotShapes(IIf(BarIndex()==StaticVarGet(staticvar+"S1index"), shapeSquare, shapeNone),colorOrange, 0,H, Offset=50); PlotShapes(IIf(BarIndex()==StaticVarGet(staticvar+"S1index"), shapeDownArrow, shapeNone),colorWhite, 0,H, Offset=-45); PlotShapes(IIf(BarIndex()==StaticVarGet(staticvar+"S2index"), shapeSquare, shapeNone),colorRed, 0, H, Offset=40); PlotShapes(IIf(BarIndex()==StaticVarGet(staticvar+"S2index"), shapeSquare, shapeNone),colorOrange, 0,H, Offset=50); PlotShapes(IIf(BarIndex()==StaticVarGet(staticvar+"S2index"), shapeDownArrow, shapeNone),colorWhite, 0,H, Offset=-45); PlotShapes(IIf(BarIndex()==StaticVarGet(staticvar+"S3index"), shapeSquare, shapeNone),colorRed, 0, H, Offset=40); PlotShapes(IIf(BarIndex()==StaticVarGet(staticvar+"S3index"), shapeSquare, shapeNone),colorOrange, 0,H, Offset=50); PlotShapes(IIf(BarIndex()==StaticVarGet(staticvar+"S3index"), shapeDownArrow, shapeNone),colorWhite, 0,H, Offset=-45); PlotShapes(IIf(BarIndex()==StaticVarGet(staticvar+"X1index"), shapeStar,shapeNone), colorBrightGreen, 0, High, 12); PlotShapes(IIf(BarIndex()==StaticVarGet(staticvar+"X2index"), shapeStar,shapeNone), colorBrightGreen, 0, High, 12); PlotShapes(IIf(BarIndex()==StaticVarGet(staticvar+"X3index"), shapeStar,shapeNone), colorBrightGreen, 0, High, 12); PlotShapes(IIf(BarIndex()==StaticVarGet(staticvar+"C1index"), shapeStar,shapeNone), colorRed, 0, Low, -12); PlotShapes(IIf(BarIndex()==StaticVarGet(staticvar+"C2index"), shapeStar,shapeNone), colorRed, 0, Low, -12); PlotShapes(IIf(BarIndex()==StaticVarGet(staticvar+"C3index"), shapeStar,shapeNone), colorRed, 0, Low, -12); if(IntradayMode=="ON") { PlotShapes(IIf(BarIndex()==StaticVarGet(staticvar+"SquareOffIndex"), shapeStar,shapeNone), colorYellow, 0, Low, -12); } } _SECTION_END();