FullMt5McpByLeo/Src/Data/MarketData.mqh

319 lines
10 KiB
MQL5
Raw Permalink Normal View History

2026-04-28 21:48:06 -05:00
//+------------------------------------------------------------------+
2026-04-27 22:49:28 -05:00
//| MarketData.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_DATA_MARKETDATA_MQH
#define FULLMT5MCPBYLEO_SRC_DATA_MARKETDATA_MQH
//+------------------------------------------------------------------+
//| Include |
//+------------------------------------------------------------------+
#include "..\\Def\\Def.mqh"
2026-04-28 21:48:06 -05:00
2026-04-27 22:49:28 -05:00
//+------------------------------------------------------------------+
2026-04-28 21:48:06 -05:00
//| CMcpFuncCopyTicks |
2026-04-27 22:49:28 -05:00
//+------------------------------------------------------------------+
2026-04-28 21:48:06 -05:00
class CMcpFuncCopyTicks : public CMcpFunction
2026-04-27 22:49:28 -05:00
{
2026-04-28 21:48:06 -05:00
private:
MqlTick m_ticks[];
2026-04-27 22:49:28 -05:00
public:
2026-04-28 21:48:06 -05:00
CMcpFuncCopyTicks(void);
~CMcpFuncCopyTicks(void);
2026-04-27 22:49:28 -05:00
void Run(CJsonNode& param, string& res) override final;
};
2026-04-28 21:48:06 -05:00
2026-04-27 22:49:28 -05:00
//+------------------------------------------------------------------+
2026-04-28 21:48:06 -05:00
//| Constructor |
2026-04-27 22:49:28 -05:00
//+------------------------------------------------------------------+
2026-04-28 21:48:06 -05:00
CMcpFuncCopyTicks::CMcpFuncCopyTicks(void) : CMcpFunction(0, false, "copy_ticks")
2026-04-27 22:49:28 -05:00
{
2026-04-28 21:48:06 -05:00
ArraySetAsSeries(m_ticks, true);
2026-04-27 22:49:28 -05:00
}
//+------------------------------------------------------------------+
2026-04-28 21:48:06 -05:00
//| Destructor |
2026-04-27 22:49:28 -05:00
//+------------------------------------------------------------------+
2026-04-28 21:48:06 -05:00
CMcpFuncCopyTicks::~CMcpFuncCopyTicks(void)
2026-04-27 22:49:28 -05:00
{
2026-04-28 21:48:06 -05:00
}
2026-04-27 22:49:28 -05:00
2026-04-28 21:48:06 -05:00
/*
{
"symbol" : "XAUUSD",
"count" : 10,
"flags" : "COPY_TICKS_ALL",
"from" : 0
}
COPY_TICKS_ALL = 0, // Obtener todos los ticks
COPY_TICKS_BID = 1, // Obtener los ticks donde cambió el precio Bid
COPY_TICKS_ASK = 2, // Obtener los ticks donde cambió el precio Ask
COPY_TICKS_LAST = 4, // Obtener los ticks donde cambió el precio Last
COPY_TICKS_TRADE = 8, // Obtener los ticks con volumen de transacción (Last)
COPY_TICKS_BUY = 16, // Obtener los ticks de compra
COPY_TICKS_SELL = 32 // Obtener los ticks de venta
*/
2026-04-27 22:49:28 -05:00
//+------------------------------------------------------------------+
2026-04-28 21:48:06 -05:00
//| Run - Copia ticks del símbolo especificado |
2026-04-27 22:49:28 -05:00
//+------------------------------------------------------------------+
2026-04-28 21:48:06 -05:00
void CMcpFuncCopyTicks::Run(CJsonNode& param, string& res)
2026-04-27 22:49:28 -05:00
{
::ResetLastError();
2026-04-28 21:48:06 -05:00
//---
2026-04-27 22:49:28 -05:00
const string symbol = param["symbol"].ToString(_Symbol);
2026-04-28 21:48:06 -05:00
const uint count = (uint)param["count"].ToInt(10);
const ulong from = (ulong)param["from"].ToInt((long(TimeCurrent()) * 1000) - (10 * 1000));
const uint flags = uint(param["flags"].ToInt(COPY_TICKS_ALL));
2026-04-27 22:49:28 -05:00
//---
2026-04-28 21:48:06 -05:00
// Validar que se copiaron los ticks correctamente
const int copied = CopyTicks(symbol, m_ticks, flags, from, count);
2026-04-27 22:49:28 -05:00
if(copied != count)
{
2026-04-28 21:48:06 -05:00
res = StringFormat("{\"ok\":false,\"error\":\"CopyTicks failed, last mt5 error = %d\"}", ::GetLastError());
2026-04-27 22:49:28 -05:00
return;
}
//---
res = "{\"ok\":true,\"result\":[";
2026-04-28 21:48:06 -05:00
const int digits = (int)SymbolInfoInteger(symbol, SYMBOL_DIGITS);
//---
2026-04-27 22:49:28 -05:00
for(int i = 0; i < copied; i++)
{
if(i > 0)
res += ",";
2026-04-28 21:48:06 -05:00
// Construir objeto MqlTick en JSON
res += StringFormat(
"{\"time\":\"%s\",\"bid\":%.*f,\"ask\":%.*f,\"last\":%.*f,\"volume\":%I64u,\"time_msc\":%I64d,\"flags\":%u,\"volume_real\":%.*f}",
2026-04-29 21:15:35 -05:00
TimeToString(m_ticks[i].time, TIME_DATE | TIME_SECONDS | TIME_MINUTES),
2026-04-28 21:48:06 -05:00
digits, m_ticks[i].bid,
digits, m_ticks[i].ask,
digits, m_ticks[i].last,
m_ticks[i].volume,
m_ticks[i].time_msc,
m_ticks[i].flags,
digits, m_ticks[i].volume_real
);
2026-04-27 22:49:28 -05:00
}
res += "]}";
}
//+------------------------------------------------------------------+
2026-04-29 21:15:35 -05:00
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
class CMcpFuncBarShift : public CMcpFunction
{
public:
CMcpFuncBarShift(void) : CMcpFunction(0, false, "i_bar_shift") {}
~CMcpFuncBarShift(void) {}
void Run(CJsonNode& param, string& res) override final;
};
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
void CMcpFuncBarShift::Run(CJsonNode &param, string &res)
{
res = StringFormat("{\"ok\":true,\"result\":%d}", iBarShift(param["symbol"].ToString(_Symbol), CEnumReg::GetValueNoRef<ENUM_TIMEFRAMES>(param["timeframe"].ToString(), _Period),
StringToTime(param["time"].ToString("0")), param["exact"].ToBool(false)));
}
2026-04-27 22:49:28 -05:00
//+------------------------------------------------------------------+
2026-04-28 21:48:06 -05:00
//| copy_rates |
2026-04-27 22:49:28 -05:00
//+------------------------------------------------------------------+
2026-04-28 21:48:06 -05:00
class CMcpFuncCopyData : public CMcpFunction
2026-04-27 22:49:28 -05:00
{
protected:
2026-04-28 21:48:06 -05:00
double m_buffer_d[];
long m_buffer_l[];
datetime m_buffer_dt[];
int m_buffer_i[];
2026-04-27 22:49:28 -05:00
public:
2026-04-28 21:48:06 -05:00
CMcpFuncCopyData() : CMcpFunction(0, false, "copy_data")
2026-04-27 22:49:28 -05:00
{
2026-04-28 21:48:06 -05:00
ArraySetAsSeries(m_buffer_d, true);
ArraySetAsSeries(m_buffer_l, true);
ArraySetAsSeries(m_buffer_i, true);
ArraySetAsSeries(m_buffer_dt, true);
2026-04-27 22:49:28 -05:00
}
2026-04-28 21:48:06 -05:00
~CMcpFuncCopyData(void) {}
2026-04-27 22:49:28 -05:00
void Run(CJsonNode& param, string& res) override final;
};
2026-04-28 21:48:06 -05:00
/*
{
"symbol" : "XAUUSD",
"count" : 10,
"timeframe" "PERIOD_M1",
"start" : 0,
"mode" : "COPY_CLOSE"
}
MCPFUNC_COPY_DATA_CLOSE = 0,
MCPFUNC_COPY_DATA_OPEN = 1,
MCPFUNC_COPY_DATA_HIGH = 2,
MCPFUNC_COPY_DATA_LOW = 3,
MCPFUNC_COPY_DATA_TICK_VOLUME = 4,
MCPFUNC_COPY_DATA_REAL_VOLUME = 5,
MCPFUNC_COPY_DATA_TIME = 6,
MCPFUNC_COPY_DATA_SPREAD = 7
*/
2026-04-27 22:49:28 -05:00
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
2026-04-28 21:48:06 -05:00
void CMcpFuncCopyData::Run(CJsonNode& param, string& res)
2026-04-27 22:49:28 -05:00
{
2026-04-28 21:48:06 -05:00
//---
2026-04-27 22:49:28 -05:00
::ResetLastError();
2026-04-28 21:48:06 -05:00
uint8_t t = 0;
2026-04-29 21:15:35 -05:00
const int start = (int)param["start"].ToInt();
const int count = (int)param["count"].ToInt();
2026-04-28 21:48:06 -05:00
const uint8_t mode = uint8_t(CEnumReg::GetValueNoRef<ENUM_MCPFUNC_COPY_DATA>(param["mode"].ToString(), 0));
2026-04-27 22:49:28 -05:00
const string symbol = param["symbol"].ToString(_Symbol);
2026-04-28 21:48:06 -05:00
// t = [0=double,1=long,2=datetime:string]
2026-04-27 22:49:28 -05:00
//---
2026-04-28 21:48:06 -05:00
int copied = -1;
switch(mode)
2026-04-29 21:15:35 -05:00
{
2026-04-28 21:48:06 -05:00
case MCPFUNC_COPY_DATA_CLOSE:
{
2026-04-29 21:15:35 -05:00
copied = CopyClose(symbol, CEnumReg::GetValueNoRef<ENUM_TIMEFRAMES>(param["timeframe"].ToString(), _Period), start, count, m_buffer_d);
2026-04-28 21:48:06 -05:00
break;
}
case MCPFUNC_COPY_DATA_OPEN:
{
2026-04-29 21:15:35 -05:00
copied = CopyOpen(symbol, CEnumReg::GetValueNoRef<ENUM_TIMEFRAMES>(param["timeframe"].ToString(), _Period), start, count, m_buffer_d);
2026-04-28 21:48:06 -05:00
break;
}
case MCPFUNC_COPY_DATA_HIGH:
{
2026-04-29 21:15:35 -05:00
copied = CopyHigh(symbol, CEnumReg::GetValueNoRef<ENUM_TIMEFRAMES>(param["timeframe"].ToString(), _Period), start, count, m_buffer_d);
2026-04-28 21:48:06 -05:00
break;
}
case MCPFUNC_COPY_DATA_LOW:
{
2026-04-29 21:15:35 -05:00
copied = CopyLow(symbol, CEnumReg::GetValueNoRef<ENUM_TIMEFRAMES>(param["timeframe"].ToString(), _Period), start, count, m_buffer_d);
2026-04-28 21:48:06 -05:00
break;
}
case MCPFUNC_COPY_DATA_TICK_VOLUME:
{
t = 1;
2026-04-29 21:15:35 -05:00
copied = CopyTickVolume(symbol, CEnumReg::GetValueNoRef<ENUM_TIMEFRAMES>(param["timeframe"].ToString(), _Period), start, count, m_buffer_l);
2026-04-28 21:48:06 -05:00
break;
}
case MCPFUNC_COPY_DATA_REAL_VOLUME:
{
t = 1;
2026-04-29 21:15:35 -05:00
copied = CopyRealVolume(symbol, CEnumReg::GetValueNoRef<ENUM_TIMEFRAMES>(param["timeframe"].ToString(), _Period), start, count, m_buffer_l);
2026-04-28 21:48:06 -05:00
break;
}
case MCPFUNC_COPY_DATA_TIME:
{
t = 2;
2026-04-29 21:15:35 -05:00
copied = CopyTime(symbol, CEnumReg::GetValueNoRef<ENUM_TIMEFRAMES>(param["timeframe"].ToString(), _Period), start, count, m_buffer_dt);
2026-04-28 21:48:06 -05:00
break;
}
case MCPFUNC_COPY_DATA_SPREAD:
{
t = 3;
2026-04-29 21:15:35 -05:00
copied = CopySpread(symbol, CEnumReg::GetValueNoRef<ENUM_TIMEFRAMES>(param["timeframe"].ToString(), _Period), start, count, m_buffer_i);
2026-04-28 21:48:06 -05:00
break;
}
2026-04-27 22:49:28 -05:00
2026-04-28 21:48:06 -05:00
//---
default:
res = StringFormat("{\"ok\":false,\"error\":\"Invalid mode = %u\"}", mode);
return;
2026-04-27 22:49:28 -05:00
}
//---
if(copied != count)
{
2026-04-29 21:15:35 -05:00
res = StringFormat("{\"ok\":false,\"error\":\"copy_data failed, last mt5 error = %d, [start = %d, count = %d]\"}", ::GetLastError(), start, count);
2026-04-27 22:49:28 -05:00
return;
}
//---
res = "{\"ok\":true,\"result\":[";
2026-04-28 21:48:06 -05:00
switch(t)
2026-04-27 22:49:28 -05:00
{
2026-04-28 21:48:06 -05:00
//---
case 0: // dbl
{
const int digits = (int)SymbolInfoInteger(symbol, SYMBOL_DIGITS);
for(int i = 0; i < copied; i++)
{
if(i > 0)
res += ",";
2026-04-28 22:54:27 -05:00
res += StringFormat("%.*f", digits, m_buffer_d[i]);
2026-04-28 21:48:06 -05:00
}
break;
}
//---
case 1: // long
{
for(int i = 0; i < copied; i++)
{
if(i > 0)
res += ",";
res += string(m_buffer_l[i]);
}
break;
}
//---
case 2: // date
{
for(int i = 0; i < copied; i++)
{
if(i > 0)
res += ",";
2026-04-28 22:54:27 -05:00
res += "\"" + TimeToString(m_buffer_dt[i]) + "\"";
2026-04-28 21:48:06 -05:00
}
break;
}
//---
case 3: // int
{
for(int i = 0; i < copied; i++)
{
if(i > 0)
res += ",";
res += string(m_buffer_i[i]);
}
break;
}
2026-04-27 22:49:28 -05:00
}
res += "]}";
}
//+------------------------------------------------------------------+
#endif // FULLMT5MCPBYLEO_SRC_DATA_MARKETDATA_MQH
2026-04-28 21:48:06 -05:00
//+------------------------------------------------------------------+