While MQL5 relies on the CTrade class for order execution, MQL4 requires direct use of the OrderSend() function. I have also added RefreshRates() and NormalizeDouble(), which are strictly required in MT4 to ensure you are executing at the most current tick price and to avoid "Invalid Price" errors from unrounded pip values.
Code: Select all
//+------------------------------------------------------------------+
//| FastScalp_Panel.mq4 |
//| One-Click Trading Utility |
//+------------------------------------------------------------------+
#property copyright "Custom MT4 Scalping Tool"
#property version "1.00"
#property strict
#property description "Rapid execution panel mimicking DOM speed for MT4"
input double InpLotSize = 0.1; // Lot Size
input int InpSlippage = 3; // Max Slippage (points)
input int InpStopLoss = 300; // Stop Loss (points) - Risk 3
input int InpTakeProfit = 100; // Take Profit (points) - Reward 1
input int InpMagicNumber = 123456; // Magic Number
//+------------------------------------------------------------------+
//| Expert initialization function |
//+------------------------------------------------------------------+
int OnInit()
{
CreateButton("BtnBuy", "BUY", 50, 50, 100, 40, clrWhite, clrGreen);
CreateButton("BtnSell", "SELL", 160, 50, 100, 40, clrWhite, clrRed);
ChartRedraw();
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Expert deinitialization function |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
ObjectDelete(0, "BtnBuy");
ObjectDelete(0, "BtnSell");
ChartRedraw();
}
//+------------------------------------------------------------------+
//| ChartEvent function |
//+------------------------------------------------------------------+
void OnChartEvent(const int id,
const long &lparam,
const double &dparam,
const string &sparam)
{
if(id == CHARTEVENT_OBJECT_CLICK)
{
// Reset button states immediately for fast repeated clicks
if(sparam == "BtnBuy") ObjectSetInteger(0, "BtnBuy", OBJPROP_STATE, false);
if(sparam == "BtnSell") ObjectSetInteger(0, "BtnSell", OBJPROP_STATE, false);
if(sparam == "BtnBuy")
{
RefreshRates(); // Vital in MT4 to get latest Ask/Bid inside an event
double sl = (InpStopLoss > 0) ? Ask - (InpStopLoss * _Point) : 0;
double tp = (InpTakeProfit > 0) ? Ask + (InpTakeProfit * _Point) : 0;
sl = NormalizeDouble(sl, _Digits);
tp = NormalizeDouble(tp, _Digits);
int ticket = OrderSend(_Symbol, OP_BUY, InpLotSize, Ask, InpSlippage, sl, tp, "Fast DOM Buy", InpMagicNumber, 0, clrGreen);
if(ticket < 0) Print("Buy OrderSend failed with error #", GetLastError());
}
if(sparam == "BtnSell")
{
RefreshRates();
double sl = (InpStopLoss > 0) ? Bid + (InpStopLoss * _Point) : 0;
double tp = (InpTakeProfit > 0) ? Bid - (InpTakeProfit * _Point) : 0;
sl = NormalizeDouble(sl, _Digits);
tp = NormalizeDouble(tp, _Digits);
int ticket = OrderSend(_Symbol, OP_SELL, InpLotSize, Bid, InpSlippage, sl, tp, "Fast DOM Sell", InpMagicNumber, 0, clrRed);
if(ticket < 0) Print("Sell OrderSend failed with error #", GetLastError());
}
}
}
//+------------------------------------------------------------------+
//| Helper to create buttons |
//+------------------------------------------------------------------+
void CreateButton(string name, string text, int x, int y, int width, int height, color textColor, color bgColor)
{
ObjectCreate(0, name, OBJ_BUTTON, 0, 0, 0);
ObjectSetInteger(0, name, OBJPROP_XDISTANCE, x);
ObjectSetInteger(0, name, OBJPROP_YDISTANCE, y);
ObjectSetInteger(0, name, OBJPROP_XSIZE, width);
ObjectSetInteger(0, name, OBJPROP_YSIZE, height);
ObjectSetString(0, name, OBJPROP_TEXT, text);
ObjectSetInteger(0, name, OBJPROP_COLOR, textColor);
ObjectSetInteger(0, name, OBJPROP_BGCOLOR, bgColor);
ObjectSetInteger(0, name, OBJPROP_FONTSIZE, 12);
ObjectSetInteger(0, name, OBJPROP_SELECTABLE, false);
}
//+------------------------------------------------------------------+