//+------------------------------------------------------------------+ //| Time Driver.mq4 | //| Copyright © 2010, Juan Cruz Baudino | //| cacuscacus@gmail.com | //+------------------------------------------------------------------+ #property copyright "Copyright © 2010, Juan Cruz Baudino." #property link "cacuscacus@gmail.com" #include #include extern int OpenAtHour = 2; extern int OpenAtMinute = 0; extern bool BUY = false; extern bool SELL = false; extern int TP = 0; extern int SL = 0; extern double Lots = 0.1; extern int Magic = 7123; extern int SlipPage = 3; double myPoint; //+------------------------------------------------------------------+ //| expert initialization function | //+------------------------------------------------------------------+ int init() { //---- myPoint = SetPoint(Symbol()); //---- return(0); } //+------------------------------------------------------------------+ //| expert deinitialization function | //+------------------------------------------------------------------+ int deinit() { //---- //---- return(0); } //+------------------------------------------------------------------+ //| expert start function | //+------------------------------------------------------------------+ int start() { //---- Comment("Server Time: ", TimeHour(TimeCurrent()),":",TimeMinute(TimeCurrent())); if (TimeHour(TimeCurrent())==OpenAtHour && TimeMinute(TimeCurrent())==OpenAtMinute && OpenOrders()==0) { if (BUY) OpenTrade(OP_BUY, Lots, SL , TP , Symbol() + " " + "Time Driver BUY", Magic); if (SELL) OpenTrade(OP_SELL, Lots, SL , TP , Symbol() + " " + "Time Driver SELL", Magic); } //---- return(0); } //+------------------------------------------------------------------+ double SetPoint(string mySymbol) { double mPoint, myDigits; myDigits = MarketInfo (mySymbol, MODE_DIGITS); if (myDigits < 4) mPoint = 0.01; if (myDigits == 4) mPoint = 0.0001; if (myDigits == 5) mPoint = 0.00001; return(mPoint); } int OpenOrders(){ int OpenOrders_ = 0; for (int i = 0; i < OrdersTotal(); i++) { if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) { if (OrderMagicNumber() == Magic && OrderSymbol()==Symbol()) { if (StringFind(OrderSymbol(), Symbol(), 0) != -1) { OpenOrders_++; } } } } return(OpenOrders_); } int OpenTrade(int signal, double mLots, double StopLoss, double TakeProfit, string msg, int MagicNumber) { int ticket, err; double TPprice,STprice; RefreshRates(); if (signal==OP_BUY) { double Ask1=NormalizeDouble(Ask,Digits); waitForContext(); ticket=OrderSend(Symbol(),OP_BUY,mLots,Ask1,SlipPage,0,0,msg,MagicNumber,0,Green); if (ticket > 0) { if (OrderSelect( ticket,SELECT_BY_TICKET, MODE_TRADES) ) { if (StopLoss != 0 || TakeProfit != 0) { TPprice = 0; if (TakeProfit > 0) { TPprice = Ask + TakeProfit * myPoint; TPprice = ValidTakeProfit(OP_BUY,Ask, TPprice); } STprice = 0; if (StopLoss > 0) { STprice = Ask - StopLoss * myPoint; STprice = ValidStopLoss(OP_BUY,Bid, STprice); } // Normalize stoploss / takeprofit to the proper # of digits. if (Digits > 0) { STprice = NormalizeDouble( STprice, Digits); TPprice = NormalizeDouble( TPprice, Digits); } waitForContext(); OrderModify(ticket, OrderOpenPrice(), STprice, TPprice, 0, LightGreen); } } } } else if (signal==OP_SELL) { double Bid1=NormalizeDouble(Bid,Digits); waitForContext(); ticket=OrderSend(Symbol(),OP_SELL,mLots,Bid1,SlipPage,0,0,msg,MagicNumber,0,Red); if (ticket > 0) { if (OrderSelect( ticket,SELECT_BY_TICKET, MODE_TRADES) ) { if (StopLoss != 0 || TakeProfit != 0) { TPprice = 0; if (TakeProfit > 0) { TPprice=Bid - TakeProfit * myPoint; TPprice = ValidTakeProfit(OP_SELL,Bid, TPprice); } STprice = 0; if (StopLoss > 0) { STprice = Bid + StopLoss * myPoint; STprice = ValidStopLoss(OP_SELL,Ask, STprice); } // Normalize stoploss / takeprofit to the proper # of digits. if (Digits > 0) { STprice = NormalizeDouble( STprice, Digits); TPprice = NormalizeDouble( TPprice, Digits); } waitForContext(); OrderModify(ticket, OrderOpenPrice(), STprice, TPprice, 0, LightGreen); } } } } if(ticket<0) { err = GetLastError(); Print("Error opening SELL order : ",err, " ", ErrorDescription(err)); } return(ticket); } double ValidStopLoss(int type, double price, double SL) { double newSL, temp; if (SL < 0.1) return(SL); temp = MarketInfo(Symbol(), MODE_STOPLEVEL) * myPoint; newSL = SL; if (type == OP_BUY) { if((price - SL) < temp) newSL = price - temp; } if (type == OP_SELL) { if((SL-price) < temp) newSL = price + temp; } newSL = NormalizeDouble(newSL,Digits); return(newSL); } double ValidTakeProfit(int type, double price, double TP) { double newTP, temp; temp = MarketInfo(Symbol(), MODE_STOPLEVEL) * myPoint; newTP = TP; if (type == OP_BUY) { if((TP - price) < temp) newTP = price + temp; } if (type == OP_SELL) { if((price - TP) < temp) newTP = price - temp; } newTP = NormalizeDouble(newTP,Digits); return(newTP); } int waitForContext() { for (int contador = 0; IsTradeContextBusy() && contador < 20; contador++) Sleep(15); if (contador >= 20) Print("El contexto de trading estuvo ocupado por mas de ", DoubleToStr(15 * contador / 1000, 2), " segundos"); else if (contador > 0) Print("El contexto de trading estuvo ocupado por ", DoubleToStr(15 * contador / 1000, 2), " segundos"); return (contador); }