2026-04-27 22:49:28 -05:00
|
|
|
//+------------------------------------------------------------------+
|
|
|
|
|
//| Trade.mqh |
|
|
|
|
|
//| Copyright 2026, Niquel Mendoza. |
|
|
|
|
|
//| https://www.mql5.com/es/users/nique_372 |
|
|
|
|
|
//+------------------------------------------------------------------+
|
|
|
|
|
#property copyright "Copyright 2026, Niquel Mendoza."
|
|
|
|
|
#property link "https://www.mql5.com/es/users/nique_372"
|
|
|
|
|
#property strict
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#ifndef FULLMT5MCPBYLEO_SRC_TRADE_TRADE_MQH
|
|
|
|
|
#define FULLMT5MCPBYLEO_SRC_TRADE_TRADE_MQH
|
|
|
|
|
|
|
|
|
|
//+------------------------------------------------------------------+
|
|
|
|
|
//| |
|
|
|
|
|
//+------------------------------------------------------------------+
|
|
|
|
|
#include "..\\Def\\Def.mqh"
|
|
|
|
|
|
|
|
|
|
//+------------------------------------------------------------------+
|
|
|
|
|
//| |
|
|
|
|
|
//+------------------------------------------------------------------+
|
|
|
|
|
class CMcpFuncTradeTrade : public CMcpFunction
|
|
|
|
|
{
|
|
|
|
|
protected:
|
|
|
|
|
CTrade* m_trade;
|
|
|
|
|
public:
|
|
|
|
|
CMcpFuncTradeTrade(CTrade* tr)
|
|
|
|
|
: CMcpFunction(0, false, "open_trade"),
|
|
|
|
|
m_trade(tr)
|
|
|
|
|
{}
|
|
|
|
|
~CMcpFuncTradeTrade(void) {}
|
|
|
|
|
|
|
|
|
|
//---
|
|
|
|
|
void Run(CJsonNode& param, string& res) override final;
|
|
|
|
|
};
|
|
|
|
|
//+------------------------------------------------------------------+
|
|
|
|
|
void CMcpFuncTradeTrade::Run(CJsonNode& param, string& res)
|
|
|
|
|
{
|
|
|
|
|
::ResetLastError();
|
|
|
|
|
res = "";
|
|
|
|
|
m_trade.SetExpertMagicNumber(ulong(param["magic"].ToInt(0)));
|
2026-04-28 08:52:21 -05:00
|
|
|
const bool result = (param["type"].ToString("") == "buy")
|
2026-04-27 22:49:28 -05:00
|
|
|
? m_trade.Buy(param["lot_size"].ToDouble(0.00), param["symbol"].ToString(NULL), param["price"].ToDouble(0.000), param["sl"].ToDouble(0.000), param["tp"].ToDouble(0.0000), param["comment"].ToString(""))
|
|
|
|
|
: m_trade.Sell(param["lot_size"].ToDouble(0.00), param["symbol"].ToString(NULL), param["price"].ToDouble(0.000), param["sl"].ToDouble(0.000), param["tp"].ToDouble(0.0000), param["comment"].ToString(""));
|
|
|
|
|
if(!result)
|
|
|
|
|
res = StringFormat("{\"ok\":false,\"error\":\"trade_failed, last mt5 error = %d\"}", ::GetLastError());
|
|
|
|
|
else
|
|
|
|
|
res = StringFormat("{\"ok\":true,\"result\":%lu}", m_trade.ResultOrder());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//+------------------------------------------------------------------+
|
|
|
|
|
//| |
|
|
|
|
|
//+------------------------------------------------------------------+
|
|
|
|
|
class CMcpFuncTradeOpenLimit : public CMcpFunction
|
|
|
|
|
{
|
|
|
|
|
protected:
|
|
|
|
|
CTrade* m_trade;
|
|
|
|
|
public:
|
|
|
|
|
CMcpFuncTradeOpenLimit(CTrade* tr)
|
|
|
|
|
: CMcpFunction(0, false, "open_limit"),
|
|
|
|
|
m_trade(tr)
|
|
|
|
|
{}
|
|
|
|
|
~CMcpFuncTradeOpenLimit(void) {}
|
|
|
|
|
|
|
|
|
|
//---
|
|
|
|
|
void Run(CJsonNode& param, string& res) override final;
|
|
|
|
|
};
|
|
|
|
|
//+------------------------------------------------------------------+
|
|
|
|
|
void CMcpFuncTradeOpenLimit::Run(CJsonNode& param, string& res)
|
|
|
|
|
{
|
|
|
|
|
::ResetLastError();
|
|
|
|
|
res = "";
|
|
|
|
|
m_trade.SetExpertMagicNumber(ulong(param["magic"].ToInt(0)));
|
|
|
|
|
const ENUM_ORDER_TYPE_TIME type_time = CEnumReg::GetValueNoRef<ENUM_ORDER_TYPE_TIME>(param["type_time"].ToString(), ORDER_TIME_GTC);
|
|
|
|
|
const datetime expiration = StringToTime(param["time_expiration"].ToString("0"));
|
2026-04-28 08:52:21 -05:00
|
|
|
const bool result = (param["type"].ToString("") == "buy")
|
2026-04-27 22:49:28 -05:00
|
|
|
? m_trade.BuyLimit(param["lot_size"].ToDouble(0.00), param["price"].ToDouble(0.000), param["symbol"].ToString(NULL), param["sl"].ToDouble(0.000), param["tp"].ToDouble(0.0000), type_time, expiration, param["comment"].ToString(""))
|
|
|
|
|
: m_trade.SellLimit(param["lot_size"].ToDouble(0.00), param["price"].ToDouble(0.000), param["symbol"].ToString(NULL), param["sl"].ToDouble(0.000), param["tp"].ToDouble(0.0000), type_time, expiration, param["comment"].ToString(""));
|
|
|
|
|
if(!result)
|
|
|
|
|
res = StringFormat("{\"ok\":false,\"error\":\"limit_order_failed, last mt5 error = %d\"}", ::GetLastError());
|
|
|
|
|
else
|
|
|
|
|
res = StringFormat("{\"ok\":true,\"result\":%lu}", m_trade.ResultOrder());
|
|
|
|
|
}
|
2026-04-28 08:52:21 -05:00
|
|
|
|
2026-04-27 22:49:28 -05:00
|
|
|
//+------------------------------------------------------------------+
|
|
|
|
|
//| open_stop |
|
|
|
|
|
//+------------------------------------------------------------------+
|
|
|
|
|
class CMcpFuncTradeOpenStop : public CMcpFunction
|
|
|
|
|
{
|
|
|
|
|
protected:
|
|
|
|
|
CTrade* m_trade;
|
|
|
|
|
public:
|
|
|
|
|
CMcpFuncTradeOpenStop(CTrade* tr)
|
|
|
|
|
: CMcpFunction(0, false, "open_stop"),
|
|
|
|
|
m_trade(tr)
|
|
|
|
|
{}
|
|
|
|
|
~CMcpFuncTradeOpenStop(void) {}
|
|
|
|
|
|
|
|
|
|
void Run(CJsonNode& param, string& res) override final;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
//+------------------------------------------------------------------+
|
|
|
|
|
//| |
|
|
|
|
|
//+------------------------------------------------------------------+
|
|
|
|
|
void CMcpFuncTradeOpenStop::Run(CJsonNode& param, string& res)
|
|
|
|
|
{
|
|
|
|
|
::ResetLastError();
|
|
|
|
|
res = "";
|
|
|
|
|
m_trade.SetExpertMagicNumber(ulong(param["magic"].ToInt(0)));
|
|
|
|
|
const ENUM_ORDER_TYPE_TIME type_time = CEnumReg::GetValueNoRef<ENUM_ORDER_TYPE_TIME>(param["type_time"].ToString(), ORDER_TIME_GTC);
|
|
|
|
|
const datetime expiration = StringToTime(param["time_expiration"].ToString("0"));
|
2026-04-28 08:52:21 -05:00
|
|
|
const bool result = (param["type"].ToString("") == "buy")
|
2026-04-27 22:49:28 -05:00
|
|
|
? m_trade.BuyStop(param["lot_size"].ToDouble(0.00), param["price"].ToDouble(0.000), param["symbol"].ToString(NULL), param["sl"].ToDouble(0.000), param["tp"].ToDouble(0.0000), type_time, expiration, param["comment"].ToString(""))
|
|
|
|
|
: m_trade.SellStop(param["lot_size"].ToDouble(0.00), param["price"].ToDouble(0.000), param["symbol"].ToString(NULL), param["sl"].ToDouble(0.000), param["tp"].ToDouble(0.0000), type_time, expiration, param["comment"].ToString(""));
|
|
|
|
|
if(!result)
|
|
|
|
|
res = StringFormat("{\"ok\":false,\"error\":\"stop_order_failed, last mt5 error = %d\"}", ::GetLastError());
|
|
|
|
|
else
|
|
|
|
|
res = StringFormat("{\"ok\":true,\"result\":%lu}", m_trade.ResultOrder());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//+------------------------------------------------------------------+
|
|
|
|
|
#endif // FULLMT5MCPBYLEO_SRC_TRADE_TRADE_MQH
|