Here is my EA for MT4 and it works fine. But I wan...

Erstellt am: 23. März 2025

Beantwortet mit O1 von Chat01

Frage

KraftFiboAutoEA.txt

Here is my EA for MT4 and it works fine. But I want to replace the CYCLE functionality completely and add instead of the cycle button 2 buttons "LONG" and "SHORT" . Now I want that when I press for instance "LONG" button EA open immediately a buy position with a stop distance that is defined by stop loss buttons and position size that was defined by position size buttons.

Gedanke

Let's think through the user's request for replacing the CYCLE functionality in their EA for MT4. I'll explore ways to implement this change.

Wondering about the user's request to completely replace the CYCLE functionality in their EA for MT4. Considering the best ways to approach this, focusing on effective solutions.

Considering how to respond to the user's request to replace the CYCLE functionality in their EA for MT4, focusing on addressing their needs and offering effective solutions.

Wondering how to respond to the user's request to replace the CYCLE functionality in their EA for MT4, focusing on understanding their needs and providing effective solutions.

Wondering how to respond to the user's request to replace the CYCLE functionality in their EA for MT4, with a focus on understanding their needs and providing effective solutions.

markdown
I'm gathering info on how to replace the CYCLE functionality in the user's EA for MT4, focusing on understanding their needs and providing effective solutions.

Reasoned for 5 seconds

Gedanke

Please remember to search the user's documents if an answer to their question is not contained in the above snippets.

undefined

Antwort

To modify your existing Expert Advisor (EA) for MetaTrader 4 (MT4) to replace the cycle functionality with two new buttons for "LONG" and "SHORT" trades, you can follow these general steps:

Key Changes:

  1. Remove Cycle Logic: We will remove any existing logic for the "Cycle" button and replace it with logic for the "LONG" and "SHORT" buttons.
  2. Add New Buttons: Create the buttons for "LONG" and "SHORT", similar to the existing button creation logic.
  3. Implement Trade Execution: When the "LONG" or "SHORT" button is pressed, the EA should immediately open a position with the desired stop loss and position size as defined by other buttons for position size and stop loss.

Steps to Implement:

  1. Create Buttons for "LONG" and "SHORT": You'll need to add two new buttons on the chart: one for buying ("LONG") and one for selling ("SHORT"). These buttons will trigger the opening of trades when clicked.

    mql4
    // Create "LONG" and "SHORT" buttons void CreateTradeButtons() { int buttonXDistance = 255; int buttonYDistance = 1; // Create "LONG" button if(ObjectFind("LongButton") == -1) { ObjectCreate("LongButton", OBJ_BUTTON, 0, 0, 0); ObjectSetText("LongButton", "LONG", 12, "Arial", clrGreen); ObjectSetInteger(0, "LongButton", OBJPROP_XSIZE, 100); ObjectSetInteger(0, "LongButton", OBJPROP_YSIZE, 30); ObjectSetInteger(0, "LongButton", OBJPROP_XDISTANCE, buttonXDistance); ObjectSetInteger(0, "LongButton", OBJPROP_YDISTANCE, buttonYDistance); ObjectSetInteger(0, "LongButton", OBJPROP_COLOR, clrGreen); ObjectSetInteger(0, "LongButton", OBJPROP_ZORDER, 0); } // Create "SHORT" button if(ObjectFind("ShortButton") == -1) { ObjectCreate("ShortButton", OBJ_BUTTON, 0, 0, 0); ObjectSetText("ShortButton", "SHORT", 12, "Arial", clrRed); ObjectSetInteger(0, "ShortButton", OBJPROP_XSIZE, 100); ObjectSetInteger(0, "ShortButton", OBJPROP_YSIZE, 30); ObjectSetInteger(0, "ShortButton", OBJPROP_XDISTANCE, buttonXDistance); ObjectSetInteger(0, "ShortButton", OBJPROP_YDISTANCE, buttonYDistance + 40); ObjectSetInteger(0, "ShortButton", OBJPROP_COLOR, clrRed); ObjectSetInteger(0, "ShortButton", OBJPROP_ZORDER, 0); } }
  2. Modify the OnChartEvent Function: You will need to capture the button clicks and handle them by opening either a "LONG" or "SHORT" trade.

    mql4
    void OnChartEvent(const int id, const long &lparam, const double &dparam, const string &sparam) { if(id == CHARTEVENT_OBJECT_CLICK) { // LONG button pressed if(sparam == "LongButton") { OpenLongTrade(); } // SHORT button pressed else if(sparam == "ShortButton") { OpenShortTrade(); } } }
  3. Open "LONG" and "SHORT" Trades: When the user presses the "LONG" or "SHORT" button, open the respective trade. You need to define the stop loss based on the buttons for stop loss percentage (like SLButton_1_3, SLButton_1_2, etc.).

    mql4
    void OpenLongTrade() { double entryPrice = Ask; double stopLossPrice = entryPrice - (SLLevel1 * Point); // Example for 1:3 SL level double lotSize = CalculateLotSize(OP_BUY, entryPrice, stopLossPrice, StandardRISK, MarketInfo(Symbol(), MODE_TICKVALUE), MarketInfo(Symbol(), MODE_SPREAD) * Point, CommissionPerLot, MarketInfo(Symbol(), MODE_MINLOT), MarketInfo(Symbol(), MODE_LOTSTEP), MarketInfo(Symbol(), MODE_MAXLOT)); if(lotSize > 0) { int ticket = OrderSend(Symbol(), OP_BUY, lotSize, entryPrice, Slippage, stopLossPrice, 0, "", 0, 0, clrBlue); if(ticket < 0) { Print("Error opening LONG trade: ", GetLastError()); } } } void OpenShortTrade() { double entryPrice = Bid; double stopLossPrice = entryPrice + (SLLevel1 * Point); // Example for 1:3 SL level double lotSize = CalculateLotSize(OP_SELL, entryPrice, stopLossPrice, StandardRISK, MarketInfo(Symbol(), MODE_TICKVALUE), MarketInfo(Symbol(), MODE_SPREAD) * Point, CommissionPerLot, MarketInfo(Symbol(), MODE_MINLOT), MarketInfo(Symbol(), MODE_LOTSTEP), MarketInfo(Symbol(), MODE_MAXLOT)); if(lotSize > 0) { int ticket = OrderSend(Symbol(), OP_SELL, lotSize, entryPrice, Slippage, stopLossPrice, 0, "", 0, 0, clrRed); if(ticket < 0) { Print("Error opening SHORT trade: ", GetLastError()); } } }
  4. Handling Stop Loss Selection: Ensure that when the user clicks on one of the stop loss buttons (e.g., SLButton_1_3, SLButton_1_2), the stop loss is set correctly before opening the trade.

    Example of setting the stop loss based on selected buttons:

    mql4
    // Assuming SLLevel1, SLLevel2, SLLevel3 are set based on the button click if(ObjectGetInteger(0, "SLButton_1_3", OBJPROP_STATE) == 1) { SLLevel1 = 620.0; // For 1:3 Stop Loss } else if(ObjectGetInteger(0, "SLButton_1_2", OBJPROP_STATE) == 1) { SLLevel1 = 525.0; // For 1:2 Stop Loss } else { SLLevel1 = 490.0; // Default stop loss }
  5. Initialization: Call the CreateTradeButtons() function inside the init() function to create the buttons when the EA is initialized.

    mql4
    int init() { CreateTradeButtons(); // Create the buttons return (INIT_SUCCEEDED); }

Summary:

This modification replaces the "CYCLE" mode with "LONG" and "SHORT" buttons that allow the user to open positions directly based on the selected stop loss and position size. The key tasks are:

  • Creating the buttons for "LONG" and "SHORT".
  • Handling button clicks to open the trades.
  • Dynamically setting the stop loss and position size.

Let me know if you need further clarification or adjustments!

Diesen Q&A teilen