// Developer: Rajandran R (Founder - Marketcalls / Co-Founder - Algomojo) // Date: 23-Oct-2022 // Website: algomojo.com / marketcalls.in _SECTION_BEGIN("Nest Broker - Broker Controls"); RequestTimedRefresh(1, False); // Send orders even if Amibroker is minimized or Chart is not active broker = Paramlist("Broker","ab|zb",0); //ab - aliceblue, zb - zebu ver = ParamStr("API Version ","1.0"); clnt_id = ParamStr("Client ID","xxxxxx"); //Enter your trading client ID user_apikey = ParamStr("user_apikey","xxxxxx"); //Enter your API key here api_secretkey = ParamStr("api_secretkey","xxxxxx"); //Enter your API secret key here _SECTION_END(); _SECTION_BEGIN("Nest Broker - Order Controls"); strg_name = ParamStr("Strategy Name","Test Strategy Chart"); // Strategy Name s_prdt_ali = "BO:BO|CNC:CNC|CO:CO|MIS:MIS|NRML:NRML"; //Product Alias Tsym = ParamStr("Trading Symbol","RELIANCE-EQ"); //Symbol Name exch = ParamList("Exchange","NFO|NSE|BSE|CDS|MCX|NCDEX|BFO|MCXSXFO|MCXSX",1); //Exchange Ret = "DAY"; //Retention prctyp = ParamList("prctyp","MKT|L|SL|SL-M",0); // Pricetype Pcode = ParamList("Product code","NRML|BO|CNC|CO|MIS",4); // Product Code qty = Param("Quantity",1,0,100000,1); // Quantity AMO = "NO"; //AMO Order 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 = "B"; iSell = "S"; 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() { algomojo=CreateObject("AMAMIBRIDGE.Main"); api_data ="{\"uid\":\""+clnt_id+"\",\"actid\":\""+clnt_id+"\",\"type\":\""+"NET"+"\",\"s_prdt_ali\":\""+s_prdt_ali+"\"}"; resp=algomojo.AMDispatcher(user_apikey, api_secretkey,"PositionBook",api_data,broker,ver); _TRACE("Position Book Response : " +resp); return resp; } function NetOpenPositions() { flag = 0; resp = GetPositionsBook(); posNetqty =0; for( item = 0; ( sym = StrExtract( resp, item,'{' )) != ""; item++ ) { if(Strfind(sym,Tsym) AND StrFind(sym,Pcode)) //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,"Tsym")) { posdetails = StrExtract(posdetails,1,':'); possym = StrTrim(posdetails,"\""); _TRACE("\nSymbol : "+possym); } if(Strfind(posdetails,"Netqty")) { 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; }//end of Net Open Positions function PlaceOrder(TransType,orderqty) { //Creating the Trading Bridge algomojo=CreateObject("AMAMIBRIDGE.Main"); //Preparing the API data api_data ="{\"strg_name\":\""+strg_name+"\",\"s_prdt_ali\":\""+s_prdt_ali+"\",\"Tsym\":\""+Tsym+"\",\"exch\":\""+exch+"\",\"Ttranstype\":\""+""+TransType+""+"\",\"Ret\":\""+Ret+"\",\"prctyp\":\""+"MKT"+"\",\"qty\":\""+orderqty+"\",\"discqty\":\""+"0"+"\",\"MktPro\":\""+"NA"+"\",\"Price\":\""+"0"+"\",\"TrigPrice\":\""+"0"+"\",\"Pcode\":\""+Pcode+"\",\"AMO\":\""+AMO+"\"}"; _TRACE("Broker 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 : "+ Tsym +"Trading Interval : "+Interval(2)+" Chard Id : "+ GetChartID()); Say( "Order Placed" ); return resp; } _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();