//+------------------------------------------------------------------+ //| HiddenTP EMA.mq4 | //| Expert Advisor for hidden take profit management | //+------------------------------------------------------------------+ #property strict // Input Parameters input int EMAPeriod = 20; // EMA period double CloseVariation = 1.0; // Close variation in pips double PipsLot = 0.01; // Default lot size input double EMABuffer = 5.0; // EMA Buffer in pips input color TPLineColor = clrGreen; // Take Profit line color input int ButtonX = 65; // Default X position for buttons input int ButtonY = 25; // Default Y position for buttons // Global Variables bool isEnabled = true; // TP system active or not string toggleButtonName = "ToggleButton"; // Button name string emaLineName = "TPLine"; // EMA-based TP line name // Names for buttons and display string closeVarButton1 = "0.5_Button"; // Button for CloseVariation = 0.5 string closeVarButton2 = "1.0_Button"; // Button for CloseVariation = 1.0 string closeVarButton3 = "2.0_Button"; // Button for CloseVariation = 2.0 string displayBoxName = "CloseVarDisplay"; // Display box for CloseVariation string buyButtonName = "BuyButton"; // Button for Buy string sellButtonName = "SellButton"; // Button for Sell string lotButton1 = "Lot_0.01"; // Lot size button 0.01 string lotButton2 = "Lot_0.02"; // Lot size button 0.02 string lotButton3 = "Lot_0.05"; // Lot size button 0.05 string lotButton4 = "Lot_0.10"; // Lot size button 0.10 string lotDisplayBox = "LotDisplay"; // Display box for PipsLot // Normalize Pips Function double NormalizePip(double value) { double pips = MarketInfo(Symbol(), MODE_POINT) * 10; return NormalizeDouble(value * pips, 0) / pips; } // Create Button on Chart void CreateButton(string name, int xOffset, int yOffset, string text, color textColor, color backgroundColor, int width = 60, int height = 20) { if (ObjectFind(0, name) >= 0) ObjectDelete(0, name); // Delete existing object if it exists if (!ObjectCreate(0, name, OBJ_BUTTON, 0, 0, 0)) { Print("Error creating button ", name, ": ", GetLastError()); return; } ObjectSetInteger(0, name, OBJPROP_XDISTANCE, ButtonX + xOffset); // X Position ObjectSetInteger(0, name, OBJPROP_YDISTANCE, ButtonY + yOffset); // Y Position ObjectSetInteger(0, name, OBJPROP_CORNER, 3); // Corner ObjectSetInteger(0, name, OBJPROP_WIDTH, width); // Width ObjectSetString(0, name, OBJPROP_TEXT, text); // Button Text ObjectSetInteger(0, name, OBJPROP_FONTSIZE, 12); // Font Size ObjectSetInteger(0, name, OBJPROP_COLOR, textColor); // Text Color ObjectSetInteger(0, name, OBJPROP_BACK, true); // Background Active ObjectSetInteger(0, name, OBJPROP_BGCOLOR, backgroundColor); // Background Color } // Create Display Box for Value void CreateDisplayBox(string name, int xOffset, int yOffset, string initialValue, int width = 60) { if (ObjectFind(0, name) >= 0) ObjectDelete(0, name); // Delete existing object if it exists if (!ObjectCreate(0, name, OBJ_LABEL, 0, 0, 0)) { Print("Error creating display box ", name, ": ", GetLastError()); return; } ObjectSetInteger(0, name, OBJPROP_XDISTANCE, ButtonX + xOffset); // X Position ObjectSetInteger(0, name, OBJPROP_YDISTANCE, ButtonY + yOffset); // Y Position ObjectSetInteger(0, name, OBJPROP_CORNER, 3); // Corner ObjectSetString(0, name, OBJPROP_TEXT, initialValue); // Initial Value ObjectSetInteger(0, name, OBJPROP_FONTSIZE, 12); // Font Size ObjectSetInteger(0, name, OBJPROP_COLOR, clrWhite); // Text Color ObjectSetInteger(0, name, OBJPROP_BACK, false); // No Background } // Update Display Box Text void UpdateDisplayBox(string name, string value) { ObjectSetString(0, name, OBJPROP_TEXT, value); } // Open Trade Function void OpenTrade(int tradeType, double lotSize) { int ticket = -1; // Ini?ializare pentru a evita warning-ul if (tradeType == OP_BUY) { ticket = OrderSend(Symbol(), OP_BUY, lotSize, Ask, 3, 0, 0, "Buy Order", 0, 0, clrGreen); } else if (tradeType == OP_SELL) { ticket = OrderSend(Symbol(), OP_SELL, lotSize, Bid, 3, 0, 0, "Sell Order", 0, 0, clrRed); } if (ticket < 0) { Print("Error opening order: ", GetLastError()); } else { Print("Order opened successfully: Ticket ", ticket); } } // OnInit Function int OnInit() { // Create EMA TP line if (ObjectFind(0, emaLineName) >= 0) ObjectDelete(0, emaLineName); // Delete if it exists if (!ObjectCreate(0, emaLineName, OBJ_HLINE, 0, 0, 0)) { Print("Error creating EMA line: ", GetLastError()); return(INIT_FAILED); } ObjectSetInteger(0, emaLineName, OBJPROP_COLOR, TPLineColor); ObjectSetInteger(0, emaLineName, OBJPROP_STYLE, STYLE_DASH); ObjectSetInteger(0, emaLineName, OBJPROP_WIDTH, 2); // Row 1 - CloseVariation Buttons and Display CreateButton(toggleButtonName, 0, 0, "ON", clrBlack, clrGray); CreateButton(closeVarButton1, 60, 0, "0.5", clrBlack, clrWhite); CreateButton(closeVarButton2, 120, 0, "1.0", clrBlack, clrWhite); CreateButton(closeVarButton3, 180, 0, "2.0", clrBlack, clrWhite); CreateDisplayBox(displayBoxName, 240, 0, DoubleToString(CloseVariation, 1)); // Row 2 - Buy/Sell Buttons and Lot Size Selection CreateButton(buyButtonName, 0, 30, "BUY", clrWhite, clrGreen, 150, 50); // Lățime: 150, Înălțime: 50 CreateButton(sellButtonName, 90, 30, "SELL", clrWhite, clrRed, 150, 50); // Lățime: 150, Înălțime: 50 CreateButton(lotButton1, 180, 30, "0.01", clrBlack, clrWhite); CreateButton(lotButton2, 240, 30, "0.02", clrBlack, clrWhite); CreateButton(lotButton3, 300, 30, "0.05", clrBlack, clrWhite); CreateButton(lotButton4, 360, 30, "0.10", clrBlack, clrWhite); CreateDisplayBox(lotDisplayBox, 420, 30, DoubleToString(PipsLot, 2)); Print("EA initialized successfully."); return(INIT_SUCCEEDED); } // OnChartEvent Function void OnChartEvent(const int id, const long& lparam, const double& dparam, const string& sparam) { if (id == CHARTEVENT_OBJECT_CLICK) { if (sparam == toggleButtonName) { isEnabled = !isEnabled; UpdateDisplayBox(displayBoxName, isEnabled ? "ON" : "OFF"); } else if (sparam == closeVarButton1) { CloseVariation = 0.5; UpdateDisplayBox(displayBoxName, DoubleToString(CloseVariation, 1)); } else if (sparam == closeVarButton2) { CloseVariation = 1.0; UpdateDisplayBox(displayBoxName, DoubleToString(CloseVariation, 1)); } else if (sparam == closeVarButton3) { CloseVariation = 2.0; UpdateDisplayBox(displayBoxName, DoubleToString(CloseVariation, 1)); } else if (sparam == buyButtonName) { OpenTrade(OP_BUY, PipsLot); } else if (sparam == sellButtonName) { OpenTrade(OP_SELL, PipsLot); } else if (sparam == lotButton1) { PipsLot = 0.01; UpdateDisplayBox(lotDisplayBox, DoubleToString(PipsLot, 2)); } else if (sparam == lotButton2) { PipsLot = 0.02; UpdateDisplayBox(lotDisplayBox, DoubleToString(PipsLot, 2)); } else if (sparam == lotButton3) { PipsLot = 0.05; UpdateDisplayBox(lotDisplayBox, DoubleToString(PipsLot, 2)); } else if (sparam == lotButton4) { PipsLot = 0.10; UpdateDisplayBox(lotDisplayBox, DoubleToString(PipsLot, 2)); } } } // OnTick Function void OnTick() { double emaValue = iMA(Symbol(), PERIOD_CURRENT, EMAPeriod, 0, MODE_EMA, PRICE_CLOSE, 0); if (emaValue <= 0) { Print("Error calculating EMA: ", GetLastError()); return; } // Calcul?m pre?ul liniei EMA cu buffer double emaWithBuffer = Close[0] > emaValue ? emaValue + (EMABuffer * Point) : emaValue - (EMABuffer * Point); // Actualizeaz? pozi?ia liniei EMA pe grafic if (isEnabled) { if (!ObjectSetDouble(0, emaLineName, OBJPROP_PRICE1, emaWithBuffer)) { Print("Error updating EMA line position: ", GetLastError()); } } // Verific? toate pozi?iile deschise for (int i = OrdersTotal() - 1; i >= 0; i--) { if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) { // Verific? dac? pozi?ia este pe simbolul curent if (OrderSymbol() == Symbol()) { // Verific? dac? pozi?ia este BUY ?i pre?ul Bid dep??e?te TP if (OrderType() == OP_BUY && Bid >= emaWithBuffer) { // Închide pozi?ia BUY if (OrderClose(OrderTicket(), OrderLots(), Bid, 3, clrBlue)) { Print("BUY order closed at TP level: ", emaWithBuffer); } else { Print("Error closing BUY order: ", GetLastError()); } } // Verific? dac? pozi?ia este SELL ?i pre?ul Ask scade sub TP else if (OrderType() == OP_SELL && Ask <= emaWithBuffer) { // Închide pozi?ia SELL if (OrderClose(OrderTicket(), OrderLots(), Ask, 3, clrBlue)) { Print("SELL order closed at TP level: ", emaWithBuffer); } else { Print("Error closing SELL order: ", GetLastError()); } } } } else { Print("Error selecting order: ", GetLastError()); } } }