#property copyright "Copyright 2019, liquidentourage" #property link "" #property version "1.00" #property strict #include #include #include extern string basic="==== Basic Settings ===================="; extern double Lots= 0.01; extern int MaxSpread =30; double Stoploss=0; double TakeProfit=0; extern string time_Setting = "---------- Work Schedule"; extern string Start_Time="00:00"; extern string Stop_Time = "23:59"; extern bool TradeOnMondays = true; extern bool TradeOnTuesdays = true; extern bool TradeOnWednesdays = true; extern bool TradeOnThursdays = true; extern bool TradeOnFridays = true; extern bool TradeOnSundays = true; string id="==== Identity Settings ===================="; int ExpertID=999; // Magic number: for identifying an EA's orders string ExpertName="liquidentourage"; // Expert name: for aesthetic purposes int NumberOfTries = 5, //Number of tries to set, close orders; RetryTime = 1; double Balance, accountstatus,maxdd=0; datetime sleepstart; bool Sleeping=false; int candle_signal=0; int lastentry = 0; //+------------------------------------------------------------------+ //| Expert initialization function | //+------------------------------------------------------------------+ int OnInit() { //--- ExpertID = WindowHandle(Symbol(),0); if(Digits==5 || Digits==3){ TakeProfit=TakeProfit*10; Stoploss=Stoploss*10; } if(!IsTesting()) Comment(ExpertName + " is waiting for the next tick to begin."); Balance=AccountBalance(); return(0); //--- return(INIT_SUCCEEDED); } //+------------------------------------------------------------------+ //| Expert deinitialization function | //+------------------------------------------------------------------+ void OnDeinit(const int reason) { //--- } //+------------------------------------------------------------------+ //| Expert tick function | //+------------------------------------------------------------------+ void OnTick() { //--- int buyOrders=0, sellOrders=0, buyPending=0, sellPending=0; checkOrderStatus(buyOrders, sellOrders, buyPending, sellPending); int instantOrders = buyOrders + sellOrders; int pendingOrders = buyPending + sellPending; int allOrders = instantOrders + pendingOrders; //+------------------------------------------------------------------+ //| variable and indicator declaration | //+------------------------------------------------------------------+ int signal = 0; if(Close[2]< Open[2] && Close[1]> Open[1]) signal =1; else if(Close[2]> Open[2] && Close[1]< Open[1]) signal =2; //+------------------------------------------------------------------+ //| close existing orders under apprpriate conditions | //+------------------------------------------------------------------+ if (allOrders>0) { //+------------------------------------------------------------------+ //+------------------------------------------------------------------+ // CODE FOR CLOSING EXISTING ORDERS if(buyOrders>0 && signal==2) { closeBuyOrder(0); Print("Buy trade exit"); } else if(sellOrders>0 && signal==1) { closeSellOrder(0); Print("Sell trade exit"); } //+------------------------------------------------------------------+ } //+------------------------------------------------------------------+ //| create new orders | //+------------------------------------------------------------------+ if (allOrders==0 && TimeOk() && SymbolInfoInteger(Symbol(),SYMBOL_SPREAD)<=MaxSpread) { //+------------------------------------------------------------------+ //+------------------------------------------------------------------+ // CODE FOR CREATING NEW ORDERS if(signal ==1 && lastentry!= Time[0]) { sendBuyOrder(); lastentry = Time[0]; } else if(signal ==2 && lastentry!= Time[0]) { sendSellOrder(); lastentry = Time[0]; } //+------------------------------------------------------------------+ //+------------------------------------------------------------------+ } string cmt=""; string NL = "\n"; //cmt=cmt+"\n--INFO------------------------------------------------------------------"; cmt=cmt+"\n\n--Account/Position Info-------------------------------------------------------"; cmt=cmt+"\n"+"Balance: "+DoubleToStr(AccountBalance(),2); cmt=cmt+"\n"+"Equity: "+DoubleToStr(AccountEquity(),2); cmt=cmt+"\n"+"Margin Used: "+DoubleToStr(AccountMargin(),2); cmt=cmt+"\n"+"Open Positions: "+DoubleToStr(instantOrders,0); cmt=cmt+"\n"+"Profit: "+DoubleToStr(AccountProfit(),2); Comment(cmt); } //+------------------------------------------------------------------+ //+------------------------------------------------------------------+ //| middle-man modules | //+------------------------------------------------------------------+ void sendBuyOrder() { versatileOrderTaker(1, Ask); } //+------------------------------------------------------------------+ void sendSellOrder() { versatileOrderTaker(2, Bid); } //+------------------------------------------------------------------+ void closeSellOrder(double lots) { versatileOrderCloser(2,lots); } //+------------------------------------------------------------------+ void closeBuyOrder(double lots) { versatileOrderCloser(1,lots); } //+------------------------------------------------------------------+ //| modules and functions | //+------------------------------------------------------------------+ bool TimeOk() { int dow = TimeDayOfWeek(TimeCurrent()); string StartTime = TimeToString(TimeCurrent(),TIME_DATE)+ " " + Start_Time; string StopTime = TimeToString(TimeCurrent(),TIME_DATE)+ " " + Stop_Time; if(dow == 0 && TradeOnSundays==false) return false; else if(dow == 1 && TradeOnMondays==false) return false; else if(dow == 2 && TradeOnTuesdays==false) return false; else if(dow == 3 && TradeOnWednesdays==false) return false; else if(dow == 4 && TradeOnThursdays==false) return false; else if(dow == 5 && TradeOnFridays==false) return false; if(TimeCurrent() >= StringToTime(StartTime) && TimeCurrent() <= StringToTime(StopTime)) return true; return false; } //+------------------------------------------------------------------+ void checkOrderStatus(int& buyOrders,int& sellOrders,int& buyPending, int& sellPending) { for(int cnt=OrdersTotal();cnt>=0;cnt--) { bool ret = OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES); if ( orderBelongsToMe() ) { if (OrderType()==OP_BUY) {buyOrders++;} else if (OrderType()==OP_SELL) {sellOrders++;} else if (OrderType()==OP_BUYSTOP || OrderType()==OP_BUYLIMIT) {buyPending++;} else if (OrderType()==OP_SELLSTOP || OrderType()==OP_SELLLIMIT) {sellPending++;} } } } //+------------------------------------------------------------------+ bool orderBelongsToMe() { if (OrderSymbol()==Symbol() && OrderMagicNumber()==ExpertID ) return (true); else return (false); } //+------------------------------------------------------------------+ double lotMaker() { double finalLot=Lots; if (finalLot>100) finalLot=100; return (finalLot); } //+------------------------------------------------------------------+ void versatileOrderTaker(int simpleType, double entryPrice) { // Variable calculations double Stopvalue=0, TPvalue=0; int ticket, oType, slippage=10; string typeName; if(Digits==5 || Digits==3) slippage=slippage*10; switch (simpleType) { case 4: if (entryPriceBid) { oType=OP_SELLLIMIT; typeName="SELLLIMIT"; } if (Stoploss>0) {Stopvalue = entryPrice+Stoploss*Point;} if (TakeProfit>0) {TPvalue = entryPrice-TakeProfit*Point;} break; case 3: if (entryPrice>Ask) { oType=OP_BUYSTOP; typeName="BUYSTOP"; } if (entryPrice0) {Stopvalue = entryPrice-Stoploss*Point;} if (TakeProfit>0) {TPvalue = entryPrice+TakeProfit*Point;} break; case 2: oType=OP_SELL; typeName="SELL"; if (Stoploss>0) {Stopvalue = entryPrice+Stoploss*Point;} if (TakeProfit>0) {TPvalue = entryPrice-TakeProfit*Point;} break; case 1: oType=OP_BUY; typeName="BUY"; if (Stoploss>0) {Stopvalue = entryPrice-Stoploss*Point;} if (TakeProfit>0) {TPvalue = entryPrice+TakeProfit*Point;} break; default: Print ("versatileOrderTaker has been passed an invalid SimpleType parameter: " + simpleType); } // Send Order ticket=OrdSend(Symbol(),oType,lotMaker(),entryPrice,slippage,Stopvalue,TPvalue,ExpertName,ExpertID,0,Green); if (ticket>0) {if (OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES)) Print(ExpertName + " " + typeName + " order at ",OrderOpenPrice()); } else Print("Error opening " + typeName + " order: ",GetLastError()); } int OrdSend(string _symbol, int _cmd, double _volume, double _price, int _slippage, double _stoploss, double _takeprofit, string _comment="", int _magic=0, datetime _expiration=0, color _arrow_color=CLR_NONE) { //Send order with retry capabilities and log int _stoplevel=MarketInfo(_symbol,MODE_STOPLEVEL); double _priceop=0; int ticket,err,tries; tries = 0; _volume=NormalizeDouble(_volume,2); switch (_cmd) { case OP_BUY: if (!IsTradeContextBusy() && IsTradeAllowed()) { while (tries < NumberOfTries) { RefreshRates(); ticket = OrderSend(_symbol,OP_BUY,_volume,Ask,_slippage,0,0,_comment,_magic,_expiration,_arrow_color); if(ticket<=0) { Print("Error Occured : "+ErrorDescription(GetLastError())); Print(Symbol()+" Buy @ "+Ask+" SL @ "+_stoploss+" TP @"+_takeprofit+" ticket ="+ticket); tries++; } else { tries = NumberOfTries; OrderSelect(ticket, SELECT_BY_TICKET, MODE_TRADES); if(_stoploss>0 || _takeprofit>0) OrderModify(ticket,OrderOpenPrice(),NormalizeDouble(_stoploss,Digits),NormalizeDouble(_takeprofit,Digits),_expiration,_arrow_color); Print("Order opened : "+Symbol()+" Buy @ "+Ask+" SL @ "+_stoploss+" TP @"+_takeprofit+" ticket ="+ticket); } Sleep(RetryTime*1000); } } err=ticket; break; case OP_SELL: if (!IsTradeContextBusy() && IsTradeAllowed()) { while (tries < NumberOfTries) { RefreshRates(); ticket = OrderSend(_symbol,OP_SELL,_volume,Bid,_slippage,0,0,_comment,_magic,_expiration,_arrow_color); if(ticket<=0) { Print("Error Occured : "+ErrorDescription(GetLastError())); Print(Symbol()+" Sell @ "+Bid+" SL @ "+_stoploss+" TP @"+_takeprofit+" ticket ="+ticket); tries++; } else { tries = NumberOfTries; OrderSelect(ticket, SELECT_BY_TICKET, MODE_TRADES); if(_stoploss>0 || _takeprofit>0) OrderModify(ticket,OrderOpenPrice(),NormalizeDouble(_stoploss,Digits),NormalizeDouble(_takeprofit,Digits),_expiration,_arrow_color); Print("Order opened : "+Symbol()+" Sell @ "+Bid+" SL @ "+_stoploss+" TP @"+_takeprofit+" ticket ="+ticket); } Sleep(RetryTime*1000); } } err=ticket; break; case OP_BUYSTOP: while (tries < NumberOfTries) { RefreshRates(); if ((_price-Ask)<_stoplevel*Point) _priceop=Ask+_stoplevel*Point; else _priceop=_price; ticket = OrderSend(_symbol,OP_BUYSTOP,_volume,NormalizeDouble(_priceop,Digits),_slippage,NormalizeDouble(_stoploss,Digits),NormalizeDouble(_takeprofit,Digits),_comment,_magic,_expiration,_arrow_color); if(ticket<=0) { Print("Error Occured : "+ErrorDescription(GetLastError())); Print(Symbol()+" Buy Stop @ "+_priceop+" SL @ "+_stoploss+" TP @"+_takeprofit+" ticket ="+ticket); tries++; } else { tries = NumberOfTries; Print("Order opened : "+Symbol()+" Buy Stop@ "+_priceop+" SL @ "+_stoploss+" TP @"+_takeprofit+" ticket ="+ticket); } Sleep(RetryTime*1000); } err=ticket; break; case OP_SELLSTOP: if (!IsTradeContextBusy() && IsTradeAllowed()) { while (tries < NumberOfTries) { RefreshRates(); if ((Bid-_price)<_stoplevel*Point) _priceop=Bid-_stoplevel*Point; else _priceop=_price; ticket = OrderSend(_symbol,OP_SELLSTOP,_volume,NormalizeDouble(_priceop,Digits),_slippage,NormalizeDouble(_stoploss,Digits),NormalizeDouble(_takeprofit,Digits),_comment,_magic,_expiration,_arrow_color); if(ticket<=0) { Print("Error Occured : "+ErrorDescription(GetLastError())); Print(Symbol()+" Sell Stop @ "+_priceop+" SL @ "+_stoploss+" TP @"+_takeprofit+" ticket ="+ticket); tries++; } else { tries = NumberOfTries; Print("Order opened : "+Symbol()+" Sell Stop @ "+_priceop+" SL @ "+_stoploss+" TP @"+_takeprofit+" ticket ="+ticket); } Sleep(RetryTime*1000); } } err=ticket; break; case OP_BUYLIMIT: if (!IsTradeContextBusy() && IsTradeAllowed()) { while (tries < NumberOfTries) { RefreshRates(); if ((Ask-_price)<_stoplevel*Point) _priceop=Ask-_stoplevel*Point; else _priceop=_price; ticket = OrderSend(_symbol,OP_BUYLIMIT,_volume,NormalizeDouble(_priceop,Digits),_slippage,NormalizeDouble(_stoploss,Digits),NormalizeDouble(_takeprofit,Digits),_comment,_magic,_expiration,_arrow_color); if(ticket<=0) { Print("Error Occured : "+ErrorDescription(GetLastError())); Print(Symbol()+" Buy Limit @ "+_priceop+" SL @ "+_stoploss+" TP @"+_takeprofit+" ticket ="+ticket); tries++; } else { tries = NumberOfTries; Print("Order opened : "+Symbol()+" Buy Limit @ "+_priceop+" SL @ "+_stoploss+" TP @"+_takeprofit+" ticket ="+ticket); } Sleep(RetryTime*1000); } } err=ticket; break; case OP_SELLLIMIT: if (!IsTradeContextBusy() && IsTradeAllowed()) { while (tries < NumberOfTries) { RefreshRates(); if ((_price-Bid)<_stoplevel*Point) _priceop=Bid+_stoplevel*Point; else _priceop=_price; ticket = OrderSend(_symbol,OP_SELLLIMIT,_volume,NormalizeDouble(_priceop,Digits),_slippage,NormalizeDouble(_stoploss,Digits),NormalizeDouble(_takeprofit,Digits),_comment,_magic,_expiration,_arrow_color); if(ticket<=0) { Print("Error Occured : "+ErrorDescription(GetLastError())); Print(Symbol()+" Sell Limit @ "+_priceop+" SL @ "+_stoploss+" TP @"+_takeprofit+" ticket ="+ticket); tries++; } else { tries = NumberOfTries; Print("Order opened : "+Symbol()+" Sell Limit @ "+_priceop+" SL @ "+_stoploss+" TP @"+_takeprofit+" ticket ="+ticket); } Sleep(RetryTime*1000); } err=ticket; } break; default: Print("No valid type of order found"); err=-1; break; } return(err); } int OrdClose(int _ticket, int type, double _lots, double _price, int _slippage, color _color=CLR_NONE) { //The function close order with log double _priceop=0; int ticket,err,tries; tries = 0; if (!IsTradeContextBusy() && IsTradeAllowed()) { while (tries < NumberOfTries) { RefreshRates(); if(type==OP_BUY) ticket = OrderClose(_ticket,_lots,Bid,_slippage,_color); if(type==OP_SELL) ticket = OrderClose(_ticket,_lots,Ask,_slippage,_color); if(ticket==0) { Print("Error Occured : "+ErrorDescription(GetLastError())); Print(Symbol()+" Close @ "+_price+" ticket ="+_ticket); tries++; } else { tries = NumberOfTries; Print("Order closed : "+Symbol()+" Close @ "+_price+" ticket ="+_ticket); } Sleep(RetryTime*1000); } } err=ticket; return(err); } //+------------------------------------------------------------------+ int CountTrades(int mode) { int c=0; for (int j=OrdersTotal()-1;j>=0;j--) { OrderSelect(j,SELECT_BY_POS,MODE_TRADES); if (OrderSymbol()==Symbol() && OrderType()==mode && OrderMagicNumber()==ExpertID) c++; } return(c); } void CloseAll() { int cnt, total = OrdersTotal(); Print("Closing out all trades ..."); for(cnt=OrdersTotal()-1;cnt>=0;cnt--) { OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES); if (OrderMagicNumber()==ExpertID) { if (OrderType()>OP_SELL) OrderDelete(OrderTicket()); else if(OrderType()==OP_SELL) OrdClose(OrderTicket(),OrderType(),OrderLots(),Ask,3,CLR_NONE); else if(OrderType()==OP_BUY) OrdClose(OrderTicket(),OrderType(),OrderLots(),Bid,3,CLR_NONE); } } } //+------------------------------------------------------------------+ void versatileOrderCloser(int simpleType,double Lots) { int cnt, total = OrdersTotal(); for(cnt=OrdersTotal()-1;cnt>=0;cnt--) { OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES); if ( orderBelongsToMe() ) { switch (simpleType) { case 4: case 3: if (OrderType()>OP_SELL) OrderDelete(OrderTicket()); break; case 2: if(OrderType()==OP_SELL && Lots==0) OrdClose(OrderTicket(),OrderType(),OrderLots(),Ask,3,CLR_NONE); else if (OrderType()==OP_SELL ) OrdClose(OrderTicket(),OrderType(),Lots,Ask,3,CLR_NONE); break; case 1: if(OrderType()==OP_BUY && Lots==0) OrdClose(OrderTicket(),OrderType(),OrderLots(),Bid,3,CLR_NONE); else if (OrderType()==OP_BUY) OrdClose(OrderTicket(),OrderType(),Lots,Bid,3,CLR_NONE); break; default: Print ("versatileOrderCloser has been passed an invalid SimpleType parameter: " + simpleType); } } } } //+------------------------------------------------------------------+ int GetOrderType(int ticket) { int cnt, total = 0; double OpenLots; for(cnt=0;cnt