Here is my EA for MT4 and it works fine. But I wan...
Erstellt am: 23. März 2025
Erstellt am: 23. März 2025
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.
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:
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); } }
Modify the OnChartEvent
Function:
You will need to capture the button clicks and handle them by opening either a "LONG" or "SHORT" trade.
mql4void 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(); } } }
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.).
mql4void 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()); } } }
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 }
Initialization:
Call the CreateTradeButtons()
function inside the init()
function to create the buttons when the EA is initialized.
mql4int init() { CreateTradeButtons(); // Create the buttons return (INIT_SUCCEEDED); }
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:
Let me know if you need further clarification or adjustments!