FullMt5McpByLeo/Src/Trade/Trade.mqh

158 lines
7 KiB
MQL5

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"
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
2026-06-03 20:39:25 -05:00
namespace TSN
{
2026-04-27 22:49:28 -05:00
class CMcpFuncTradeTrade : public CMcpFunction
{
protected:
CTrade* m_trade;
public:
CMcpFuncTradeTrade(CTrade* tr)
: CMcpFunction(0, false, "open_trade"),
m_trade(tr)
{}
~CMcpFuncTradeTrade(void) {}
//---
2026-07-13 21:11:54 -05:00
void Run(CJsonNode& param, CJsonBuilderStr* &out) override final;
2026-04-27 22:49:28 -05:00
};
//+------------------------------------------------------------------+
2026-07-13 21:11:54 -05:00
void CMcpFuncTradeTrade::Run(CJsonNode& param, CJsonBuilderStr* &out)
2026-04-27 22:49:28 -05:00
{
2026-07-13 21:11:54 -05:00
//---
out = m_shared_builder;
2026-04-27 22:49:28 -05:00
::ResetLastError();
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(""));
2026-07-13 21:11:54 -05:00
m_shared_builder.PutChar('"');
m_shared_builder.Obj();
2026-04-27 22:49:28 -05:00
if(!result)
2026-07-13 21:11:54 -05:00
{
m_shared_builder.KeyWV("ok").Val(false);
m_shared_builder.KeyWV("error").ValSWV(StringFormat("trade_failed, last mt5 error = %d", ::GetLastError()));
}
2026-04-27 22:49:28 -05:00
else
2026-07-13 21:11:54 -05:00
{
m_shared_builder.KeyWV("ok").Val(true);
m_shared_builder.KeyWV("result").Val((long)m_trade.ResultOrder());
}
m_shared_builder.EndObj();
m_shared_builder.PutChar('"');
2026-04-27 22:49:28 -05:00
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
class CMcpFuncTradeOpenLimit : public CMcpFunction
{
protected:
CTrade* m_trade;
public:
CMcpFuncTradeOpenLimit(CTrade* tr)
: CMcpFunction(0, false, "open_limit"),
m_trade(tr)
{}
~CMcpFuncTradeOpenLimit(void) {}
//---
2026-07-13 21:11:54 -05:00
void Run(CJsonNode& param, CJsonBuilderStr* &out) override final;
2026-04-27 22:49:28 -05:00
};
//+------------------------------------------------------------------+
2026-07-13 21:11:54 -05:00
void CMcpFuncTradeOpenLimit::Run(CJsonNode& param, CJsonBuilderStr* &out)
2026-04-27 22:49:28 -05:00
{
2026-07-13 21:11:54 -05:00
//---
out = m_shared_builder;
2026-04-27 22:49:28 -05:00
::ResetLastError();
m_trade.SetExpertMagicNumber(ulong(param["magic"].ToInt(0)));
2026-06-28 22:09:10 -05:00
const ENUM_ORDER_TYPE_TIME type_time = CEnumRegBasis::GetValNoRef<ENUM_ORDER_TYPE_TIME>(param["type_time"].ToString(), ORDER_TIME_GTC);
2026-04-27 22:49:28 -05:00
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(""));
2026-07-13 21:11:54 -05:00
m_shared_builder.PutChar('"');
m_shared_builder.Obj();
2026-04-27 22:49:28 -05:00
if(!result)
2026-07-13 21:11:54 -05:00
{
m_shared_builder.KeyWV("ok").Val(false);
m_shared_builder.KeyWV("error").ValSWV(StringFormat("limit_order_failed, last mt5 error = %d", ::GetLastError()));
}
2026-04-27 22:49:28 -05:00
else
2026-07-13 21:11:54 -05:00
{
m_shared_builder.KeyWV("ok").Val(true);
m_shared_builder.KeyWV("result").Val((long)m_trade.ResultOrder());
}
m_shared_builder.EndObj();
m_shared_builder.PutChar('"');
2026-04-27 22:49:28 -05:00
}
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) {}
2026-07-13 21:11:54 -05:00
void Run(CJsonNode& param, CJsonBuilderStr* &out) override final;
2026-04-27 22:49:28 -05:00
};
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
2026-07-13 21:11:54 -05:00
void CMcpFuncTradeOpenStop::Run(CJsonNode& param, CJsonBuilderStr* &out)
2026-04-27 22:49:28 -05:00
{
2026-07-13 21:11:54 -05:00
//---
out = m_shared_builder;
2026-04-27 22:49:28 -05:00
::ResetLastError();
m_trade.SetExpertMagicNumber(ulong(param["magic"].ToInt(0)));
2026-06-28 22:09:10 -05:00
const ENUM_ORDER_TYPE_TIME type_time = CEnumRegBasis::GetValNoRef<ENUM_ORDER_TYPE_TIME>(param["type_time"].ToString(), ORDER_TIME_GTC);
2026-04-27 22:49:28 -05:00
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(""));
2026-07-13 21:11:54 -05:00
m_shared_builder.PutChar('"');
m_shared_builder.Obj();
2026-04-27 22:49:28 -05:00
if(!result)
2026-07-13 21:11:54 -05:00
{
m_shared_builder.KeyWV("ok").Val(false);
m_shared_builder.KeyWV("error").ValSWV(StringFormat("stop_order_failed, last mt5 error = %d", ::GetLastError()));
}
2026-04-27 22:49:28 -05:00
else
2026-07-13 21:11:54 -05:00
{
m_shared_builder.KeyWV("ok").Val(true);
m_shared_builder.KeyWV("result").Val((long)m_trade.ResultOrder());
}
m_shared_builder.EndObj();
m_shared_builder.PutChar('"');
2026-04-27 22:49:28 -05:00
}
//+------------------------------------------------------------------+
2026-06-03 20:39:25 -05:00
} // namespace TSN
2026-04-27 22:49:28 -05:00
#endif // FULLMT5MCPBYLEO_SRC_TRADE_TRADE_MQH