/* Algomojo - Line Trading Module Created on : 06 Dec 2022. Website : www.algomojo.com */ _SECTION_BEGIN("Algomojo - Broker Controls"); // Send orders even if Amibroker is minimized or Chart is not active RequestTimedRefresh(1, False); //Creating Input Controls for Setting Order Related Information broker =Paramlist("Broker","an|ab|fs|fy|fv|fp|gc|pt|sm|tc|up|zb|ze",0); ver = ParamStr("API Version ","v1"); apikey = ParamStr("apikey","xxxxxxxxxxxxxxxxxx"); //Enter your API key here apisecret = ParamStr("secretkey","xxxxxxxxxxxxxxxxxx"); //Enter your API secret key here _SECTION_END(); _SECTION_BEGIN("Algomojo - Order Controls"); strategy = ParamStr("Strategy Name", "Ami Strategy"); exchange = ParamList("Exchange","NSE|NFO|BSE|MCX",0); symbol = ParamStr("Trading Symbol","BHEL-EQ"); product = ParamList("Product","CNC|MIS|NRML",1); pricetype = ParamList("Price Type","MARKET",0); quantity = Param("Quantity",1,1,100000,1); price = 0; disclosed_quantity = 0; trigger_price = 0; amo = "NO"; splitorder = ParamList("To Split Orders","NO|YES",0); split_quantity = Param("Split Quantity",500,1,100000,1); Entrydelay = Param("Entry Delay",0,0,1,1); Exitdelay = Param("Exit Delay",0,0,1,1); EnableButton = ParamList("Button Trading","Disable|Enable",0); VoiceAlert = ParamList("Voice Alert","Disable|Enable",1); EnableAlgo = ParamList("AlgoStatus","Disable|Enable",0); resp = ""; //Static Variables for Order protection static_name_ = Name()+GetChartID()+interval(2)+strategy; static_name_algo = Name()+GetChartID()+interval(2)+strategy+"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 OpenPositions() { //Creating the Trading Bridge algomojo=CreateObject("AMAMIBRIDGE.Main"); api_data = "{ \"broker\": \""+broker+"\",\"symbol\":\""+symbol+"\",\"exchange\":\""+exchange+"\",\"product\":\""+product+"\"}"; _TRACE("OpenPositions API Request"+api_data); //Sending The Broker Request for NetOpenPositions resp=algomojo.AMDispatcher(apikey,apisecret,"AllPositions",api_data,"am",ver); _TRACE("OpenPositions API Response : "+resp); return resp; } function NetOpenPositions() { flag = 0; resp = OpenPositions(); posNetqty =0; sym = StrExtract( resp, 2,'{' ); if( sym != "") { for( jitem = 0; ( posdetails = StrExtract( sym, jitem,',' )) != ""; jitem++ ) { if(Strfind(posdetails,"netqty")) { flag = 1; //turn on the flag posdetails = StrExtract(posdetails,1,':'); posNetqty = StrToNum(StrTrim(posdetails,"\"")); } } //end of for loop } //end of if loop if(flag==0) { _TRACE("\nTrading Symbol Not Found"); posNetqty =0; _TRACE("\nNetQty : "+posNetqty); } return posNetqty; } function PlaceOrder(action,orderqty) { //Creating the Trading Bridge algomojo=CreateObject("AMAMIBRIDGE.Main"); //Preparing the API data api_data = "{ \"broker\": \""+broker+"\",\"strategy\":\""+strategy+"\",\"exchange\":\""+exchange+"\",\"symbol\":\""+symbol+"\",\"action\":\""+action+"\",\"product\":\""+product+"\",\"pricetype\":\""+pricetype+"\",\"quantity\":\""+orderqty+"\",\"price\":\""+price+"\",\"disclosed_quantity\":\""+disclosed_quantity+"\",\"trigger_price\":\""+trigger_price+"\",\"amo\":\""+amo+"\",\"splitorder\":\""+splitorder+"\",\"split_quantity\":\""+split_quantity+"\"}"; _TRACE("Broker API Request"+api_data); resp=algomojo.AMDispatcher(apikey, apisecret,"PlaceOrder",api_data,"am",ver); _TRACE("API Response : "+resp); if(VoiceAlert == "Enable") 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();