#property copyright "Copyright © 2011" #property link "" #include extern int cSeconds = 14; extern int MinPriceShot = 70; extern int MaxOrdersCount = 1; extern double RiskPercent = 1.0; double Lots = 0.1; extern int TakeProfit = 220; extern int StopLoss = 100; extern int TrailingStop = 50; extern int Slippage = 30; extern int BuyMagic = 11; extern int SellMagic = 12; double mySpread; int TimeLastTicked = 0; double LastClose = -1.0; string EA_Name = "XOX"; double myPoint; int init() { mySpread = Ask - Bid; myPoint = SetPoint(); return (0); } int deinit() { return (0); } int start() { int pipsDif; int secsDiff; int NumOrders; if (!IsOptimization() && !IsTesting() && !IsVisualMode()) { DrawLogo(); DrawStats(); } Lots = LotsOptimized(); int TimeNow = TimeCurrent(); double CurrentClose = Close[0]; if (LastClose != -1.0) { // By how many pips has the price changed since prev bar close? ... pipsDif = (CurrentClose - LastClose) / myPoint; // How many seconds have elapsed since we were last ticked ... secsDiff = TimeNow - TimeLastTicked; NumOrders = OrdersTotalCountByTypeANDMagic(OP_BUY, BuyMagic) + OrdersTotalCountByTypeANDMagic(OP_SELL, SellMagic); if (MathAbs(pipsDif) >= MinPriceShot && secsDiff <= cSeconds) { if (NumOrders < MaxOrdersCount) { if (ND(CurrentClose) > ND(LastClose)) { OpenBuy(Lots); Comment("\nUP = " + DoubleToStr(MathAbs(pipsDif), 0)); } if (ND(CurrentClose) < ND(LastClose)) { OpenSell(Lots); Comment("\nDN = " + DoubleToStr(MathAbs(pipsDif), 0)); } } else Comment("Maximum orders already open!"); LastClose = CurrentClose; TimeLastTicked = TimeNow; } if (secsDiff >= cSeconds) { if (NumOrders < MaxOrdersCount) { if (ND(CurrentClose) > ND(LastClose)) { Comment("Not Open" + "\nUP = " + DoubleToStr(MathAbs(pipsDif), 0)); } if (ND(CurrentClose) < ND(LastClose)) { Comment("Not Open" + "\nDN = " + DoubleToStr(MathAbs(pipsDif), 0)); } } else Comment("Maximum orders already open!"); LastClose = CurrentClose; TimeLastTicked = TimeNow; } } else { LastClose = CurrentClose; TimeLastTicked = TimeNow; } DoTrail(); return (0); } int OrdersTotalCountByTypeANDMagic(int cmd, int magic) { int count = 0; for (int pos = OrdersTotal() - 1; pos >= 0; pos--) { if (!OrderSelect(pos, SELECT_BY_POS, MODE_TRADES)) Print("OrderSelect error : " + ErrorDescription(GetLastError())); else { if (OrderSymbol() == Symbol()) { if (OrderType() == cmd) if (OrderMagicNumber() == magic) count++; } } } return (count); } void OpenBuy(double myLots) { int err, ticket; double TPprice = 0; double STprice = 0; RefreshRates(); while (IsTradeContextBusy()) Sleep(1000); int TimeNow = TimeCurrent(); ticket = OrderSend(Symbol(), OP_BUY, myLots, Ask, Slippage, 0, 0, "", BuyMagic, 0, Blue); if (ticket == -1) { while (ticket == -1 && TimeCurrent() - TimeNow < 60 && !IsTesting()) { err = GetLastError(); Print("Error Opening BUY. (" + err + ")" + ErrorDescription(err)); if (err == 148/* ERR_TRADE_TOO_MANY_ORDERS */) return; Sleep(1000); while (IsTradeContextBusy()) Sleep(1000); Print("Повтор"); RefreshRates(); ticket = OrderSend(Symbol(), OP_BUY, myLots, Ask, Slippage, 0, 0, "", BuyMagic, 0, Blue); } if (ticket == -1) { err = GetLastError(); Print("Error opening BUY. (" + err + ")" + ErrorDescription(err)); } } if (ticket != -1) { Print("Opened BUY for " + Symbol() + " " + Period() + " Lots " + DoubleToStr(myLots, 2) + "!"); if (OrderSelect(ticket,SELECT_BY_TICKET, MODE_TRADES)) { if (StopLoss != 0 || TakeProfit != 0) { if (TakeProfit > 0) TPprice = GetTakeProfit(OP_BUY); if (StopLoss > 0) STprice = Ask - StopLoss * myPoint - mySpread; TPprice = ND(TPprice); STprice = ND(STprice); OrderModify(ticket, OrderOpenPrice(), STprice, TPprice, 0, Green); } } } } void OpenSell(double myLots) { int err, ticket; double TPprice = 0; double STprice = 0; RefreshRates(); while (IsTradeContextBusy()) Sleep(1000); int TimeNow = TimeCurrent(); ticket = OrderSend(Symbol(), OP_SELL, myLots, Bid, Slippage, 0, 0, "", SellMagic, 0, Red); if (ticket == -1) { while (ticket == -1 && TimeCurrent() - TimeNow < 60 && !IsTesting()) { err = GetLastError(); Print("Error opening SELL. (" + err + ")" + ErrorDescription(err)); if (err == 148/* ERR_TRADE_TOO_MANY_ORDERS */) return; Sleep(1000); while (IsTradeContextBusy()) Sleep(1000); Print("Повтор"); RefreshRates(); ticket = OrderSend(Symbol(), OP_SELL, myLots, Bid, Slippage, 0, 0, "", SellMagic, 0, Red); } if (ticket == -1) { err = GetLastError(); Print("Error opening SELL. (" + err + ")" + ErrorDescription(err)); } } if (ticket != -1) { Print("Opened SELL for " + Symbol() + " " + Period() + " Lots " + DoubleToStr(myLots, 2) + "!"); if (OrderSelect(ticket,SELECT_BY_TICKET, MODE_TRADES)) { if (StopLoss != 0 || TakeProfit != 0) { if (TakeProfit > 0) GetTakeProfit(OP_SELL); if (StopLoss > 0) STprice = Bid + StopLoss * myPoint + mySpread; TPprice = ND(TPprice); STprice = ND(STprice); OrderModify(ticket, OrderOpenPrice(), STprice, TPprice, 0, Red); } } } } void DoTrail() { int li_unused_0 = StopLoss; int li_unused_4 = StopLoss; for (int l_pos_8 = 0; l_pos_8 < OrdersTotal(); l_pos_8++) { OrderSelect(l_pos_8, SELECT_BY_POS, MODE_TRADES); if (OrderSymbol() == Symbol()) { if (OrderType() == OP_BUY) { if (OrderMagicNumber() == BuyMagic) { if (Bid - TrailingStop * myPoint > OrderOpenPrice() && OrderOpenPrice() > OrderStopLoss()) { if (!OrderModify(OrderTicket(), OrderOpenPrice(), Bid - TrailingStop * myPoint, OrderTakeProfit(), 0, Pink)) Print("Не получилось модифицировать ордер №" + OrderTicket() + ". Ошибка№" + GetLastError()); } else { if (OrderStopLoss() > OrderOpenPrice() && Bid - OrderStopLoss() >= TrailingStop * myPoint) if (!OrderModify(OrderTicket(), OrderOpenPrice(), Bid - TrailingStop * myPoint, OrderTakeProfit(), 0, Pink)) Print("Не получилось модифицировать ордер №" + OrderTicket() + ". Ошибка№" + GetLastError()); } } } else { if (OrderType() == OP_SELL) { if (OrderMagicNumber() == SellMagic) { if (Ask + TrailingStop * myPoint < OrderOpenPrice() && OrderStopLoss() > OrderOpenPrice() || OrderStopLoss() == 0.0) { if (!OrderModify(OrderTicket(), OrderOpenPrice(), Ask + TrailingStop * myPoint, OrderTakeProfit(), 0, Pink)) Print("Не получилось модифицировать ордер №" + OrderTicket() + ". Ошибка№" + GetLastError()); } else { if (OrderStopLoss() < OrderOpenPrice() && OrderStopLoss() - Ask >= TrailingStop * myPoint) if (!OrderModify(OrderTicket(), OrderOpenPrice(), Ask + TrailingStop * myPoint, OrderTakeProfit(), 0, Pink)) Print("Не получилось модифицировать ордер №" + OrderTicket() + ". Ошибка№" + GetLastError()); } } } } } } } double ND(double ad_0) { return (NormalizeDouble(ad_0, Digits)); } double LotsOptimized() { double lotstep = MarketInfo(Symbol(), MODE_LOTSTEP); double minlot = MarketInfo(Symbol(), MODE_MINLOT); int places = 0; if (lotstep == 0.01) places = 2; else places = 1; double mlot = NormalizeDouble(MathFloor(AccountBalance() * RiskPercent / 100.0) / 1000.0, places); if (mlot < minlot) mlot = minlot; return (mlot); } void DrawLogo() { string l_name_0 = EA_Name + "L_1"; if (ObjectFind(l_name_0) == -1) { ObjectCreate(l_name_0, OBJ_LABEL, 0, 0, 0); ObjectSet(l_name_0, OBJPROP_CORNER, 0); ObjectSet(l_name_0, OBJPROP_XDISTANCE, 390); ObjectSet(l_name_0, OBJPROP_YDISTANCE, 10); } ObjectSetText(l_name_0, "F O R E X", 28, "Arial", DarkTurquoise); l_name_0 = EA_Name + "L_2"; if (ObjectFind(l_name_0) == -1) { ObjectCreate(l_name_0, OBJ_LABEL, 0, 0, 0); ObjectSet(l_name_0, OBJPROP_CORNER, 0); ObjectSet(l_name_0, OBJPROP_XDISTANCE, 382); ObjectSet(l_name_0, OBJPROP_YDISTANCE, 50); } ObjectSetText(l_name_0, "ООО Н А В И Т Е Х", 16, "Arial", Gold); l_name_0 = EA_Name + "L_3"; if (ObjectFind(l_name_0) == -1) { ObjectCreate(l_name_0, OBJ_LABEL, 0, 0, 0); ObjectSet(l_name_0, OBJPROP_CORNER, 0); ObjectSet(l_name_0, OBJPROP_XDISTANCE, 397); ObjectSet(l_name_0, OBJPROP_YDISTANCE, 75); } ObjectSetText(l_name_0, "Роджер Вэлс", 12, "Arial", Gray); l_name_0 = EA_Name + "L_4"; if (ObjectFind(l_name_0) == -1) { ObjectCreate(l_name_0, OBJ_LABEL, 0, 0, 0); ObjectSet(l_name_0, OBJPROP_CORNER, 0); ObjectSet(l_name_0, OBJPROP_XDISTANCE, 382); ObjectSet(l_name_0, OBJPROP_YDISTANCE, 57); } ObjectSetText(l_name_0, "_____________________", 12, "Arial", Gray); l_name_0 = EA_Name + "L_5"; if (ObjectFind(l_name_0) == -1) { ObjectCreate(l_name_0, OBJ_LABEL, 0, 0, 0); ObjectSet(l_name_0, OBJPROP_CORNER, 0); ObjectSet(l_name_0, OBJPROP_XDISTANCE, 382); ObjectSet(l_name_0, OBJPROP_YDISTANCE, 76); } ObjectSetText(l_name_0, "_____________________", 12, "Arial", Gray); } double GetProfitForDay(int ai_0) { double ld_ret_4 = 0; for (int l_pos_12 = 0; l_pos_12 < OrdersHistoryTotal(); l_pos_12++) { if (!(OrderSelect(l_pos_12, SELECT_BY_POS, MODE_HISTORY))) break; if (OrderSymbol() == Symbol() && OrderMagicNumber() == BuyMagic || OrderMagicNumber() == SellMagic) if (OrderCloseTime() >= iTime(Symbol(), PERIOD_D1, ai_0) && OrderCloseTime() < iTime(Symbol(), PERIOD_D1, ai_0) + 86400) ld_ret_4 += OrderProfit(); } return (ld_ret_4); } void DrawStats() { double ld_0 = GetProfitForDay(0); string l_name_8 = EA_Name + "1"; if (ObjectFind(l_name_8) == -1) { ObjectCreate(l_name_8, OBJ_LABEL, 0, 0, 0); ObjectSet(l_name_8, OBJPROP_CORNER, 1); ObjectSet(l_name_8, OBJPROP_XDISTANCE, 10); ObjectSet(l_name_8, OBJPROP_YDISTANCE, 15); } ObjectSetText(l_name_8, "Заработок сегодня: " + DoubleToStr(ld_0, 2), 12, "Courier New", Yellow); ld_0 = GetProfitForDay(1); l_name_8 = EA_Name + "2"; if (ObjectFind(l_name_8) == -1) { ObjectCreate(l_name_8, OBJ_LABEL, 0, 0, 0); ObjectSet(l_name_8, OBJPROP_CORNER, 1); ObjectSet(l_name_8, OBJPROP_XDISTANCE, 10); ObjectSet(l_name_8, OBJPROP_YDISTANCE, 30); } ObjectSetText(l_name_8, "Заработок вчера: " + DoubleToStr(ld_0, 2), 12, "Courier New", Yellow); ld_0 = GetProfitForDay(2); l_name_8 = EA_Name + "3"; if (ObjectFind(l_name_8) == -1) { ObjectCreate(l_name_8, OBJ_LABEL, 0, 0, 0); ObjectSet(l_name_8, OBJPROP_CORNER, 1); ObjectSet(l_name_8, OBJPROP_XDISTANCE, 10); ObjectSet(l_name_8, OBJPROP_YDISTANCE, 45); } ObjectSetText(l_name_8, "Заработок позавчера: " + DoubleToStr(ld_0, 2), 12, "Courier New", Yellow); l_name_8 = EA_Name + "4"; if (ObjectFind(l_name_8) == -1) { ObjectCreate(l_name_8, OBJ_LABEL, 0, 0, 0); ObjectSet(l_name_8, OBJPROP_CORNER, 1); ObjectSet(l_name_8, OBJPROP_XDISTANCE, 10); ObjectSet(l_name_8, OBJPROP_YDISTANCE, 75); } ObjectSetText(l_name_8, "Баланс: " + DoubleToStr(AccountBalance(), 2), 14, "Courier New", Yellow); } double SetPoint() { double mPoint; if (Digits < 4) mPoint = 0.01; else mPoint = 0.0001; return(mPoint); } double GetTakeProfit(int cmd) { double tp; if (cmd == OP_BUY) { tp = Ask + mySpread + TakeProfit * myPoint; if (tp - Ask < (MarketInfo(Symbol(), MODE_STOPLEVEL) + 1.0) * myPoint) { tp = Bid + (MarketInfo(Symbol(), MODE_STOPLEVEL) + 1.0) * myPoint; } } if (cmd == OP_SELL) { tp = Bid - TakeProfit * myPoint - mySpread; if (Bid - tp < (MarketInfo(Symbol(), MODE_STOPLEVEL) + 1.0) * myPoint) { tp = Ask - (MarketInfo(Symbol(), MODE_STOPLEVEL) + 1.0) * myPoint; } } return(tp); }