UTE/Strategy/TradeControl.mqh
super.admin bd7e405a90 convert
2025-05-30 16:34:43 +02:00

149 lines
16 KiB
MQL5

//+------------------------------------------------------------------+
//| TradeControl.mqh |
//| Copyright 2016, Vasiliy Sokolov, St-Petersburg, Russia |
//| https://www.mql5.com/ru/users/c-4 |
//+------------------------------------------------------------------+
#property copyright "Copyright 2016, Vasiliy Sokolov."
#property link "https://www.mql5.com/ru/users/c-4"
#include <Object.mqh>
#include "Dictionary.mqh"
#include "TradeCustom.mqh"
#include "Message.mqh"
#include "Logs.mqh"
/*
Данный класс интегрирует дополнительный модуль контроля в стандартный торговый модуль CTradeCustom. Дополнения:
1.Если обращение к одному из торговых методов происходит раньше, чем через определеное количество
миллисекунд, установленное методом TradeDelayMsc, выводится предупреждающее сообщение,
о том, что торговые действия происходят слишком часто и торговое действие не совершается.
2. Если при покупке или продаже запрашиваемого объема объем совокупной нетто-позиции
превысит предельно допустимый, устанавливаемый методом AddLimit, объем будет скорректирован
на новый либо торговое действие будет отклонено вовсе.
*/
//+------------------------------------------------------------------+
//| Торговый класс, основанный на CTradeCustom и включающий в себя |
//| дополнительные модули контроля риска. |
//+------------------------------------------------------------------+
class CTradeControl : public CTradeCustom
{
private:
uint m_last_trade_action; // Время последнего совершенного торгового действия.
uint m_trade_delay; // Время задержки в мсек, раньше которого не может поступить повторный приказ на совершение сделки.
CLog* Log; // Логирование
public:
CTradeCustom Trade; // Базовый торговый модуль. Венесен в паблик для доступа к информации о статусе сделки.
CTradeControl(void);
/* Конфигурирование свойств CTradeCustom */
void TradeDelayMsc(uint msec);
void AsynchMode(bool asynch);
/* Переопределенные торговые методы CTradeCustom */
virtual bool Buy(const double volume,const string symbol,const string comment="");
virtual bool Sell(const double volume,const string symbol,const string comment="");
virtual bool BuyLimit(const double volume,const double price,const string symbol,
const ENUM_ORDER_TYPE_TIME type_time=ORDER_TIME_GTC,const datetime expiration=0,const string comment="");
virtual bool BuyStop(const double volume,const double price,const string symbol,
const ENUM_ORDER_TYPE_TIME type_time=ORDER_TIME_GTC,const datetime expiration=0,const string comment="");
virtual bool SellLimit(const double volume,const double price,const string symbol,
const ENUM_ORDER_TYPE_TIME type_time=ORDER_TIME_GTC,const datetime expiration=0,const string comment="");
virtual bool SellStop(const double volume,const double price,const string symbol,
const ENUM_ORDER_TYPE_TIME type_time=ORDER_TIME_GTC,const datetime expiration=0,const string comment="");
};
//+------------------------------------------------------------------+
//| Конструктор. |
//+------------------------------------------------------------------+
CTradeControl::CTradeControl(void) : m_trade_delay(0),
m_last_trade_action(0)
{
Log=CLog::GetLog();
}
//+------------------------------------------------------------------+
//| Устанавливает флаг исполнения приказа в асинхронном режиме. |
//+------------------------------------------------------------------+
void CTradeControl::AsynchMode(bool asynch)
{
Trade.SetAsyncMode(asynch);
}
//+------------------------------------------------------------------+
//| Покупаем по рынку. |
//+------------------------------------------------------------------+
bool CTradeControl::Buy(const double volume,const string symbol,const string comment="")
{
bool res=CTradeCustom::Buy(volume,symbol,comment);
if(res)
m_last_trade_action=GetTickCount();
return res;
}
//+------------------------------------------------------------------+
//| Покупаем по рынку. |
//+------------------------------------------------------------------+
bool CTradeControl::Sell(const double volume,const string symbol,const string comment="")
{
bool res=CTradeCustom::Sell(volume,symbol,comment);
if(res)
m_last_trade_action=GetTickCount();
return res;
}
//+------------------------------------------------------------------+
//| Выставляем buy-stop. |
//+------------------------------------------------------------------+
bool CTradeControl::BuyStop(const double volume,
const double price,
const string symbol,
const ENUM_ORDER_TYPE_TIME type_time=0,
const datetime expiration=0,
const string comment="")
{
bool res=CTradeCustom::BuyStop(volume,price,symbol,type_time,expiration,comment);
if(res)
m_last_trade_action=GetTickCount();
return res;
}
//+------------------------------------------------------------------+
//| Выставляем sell stop ордер. |
//+------------------------------------------------------------------+
bool CTradeControl::SellStop(const double volume,
const double price,
const string symbol,
const ENUM_ORDER_TYPE_TIME type_time=0,
const datetime expiration=0,
const string comment="")
{
bool res=CTradeCustom::SellStop(volume,price,symbol,type_time,expiration,comment);
if(res)
m_last_trade_action=GetTickCount();
return res;
}
//+------------------------------------------------------------------+
//| Выставляем buy limit ордер. |
//+------------------------------------------------------------------+
bool CTradeControl::BuyLimit(const double volume,
const double price,
const string symbol,
const ENUM_ORDER_TYPE_TIME type_time=0,
const datetime expiration=0,
const string comment="")
{
bool res=CTradeCustom::BuyLimit(volume,price,symbol,type_time,expiration,comment);
if(res)
m_last_trade_action=GetTickCount();
return res;
}
//+------------------------------------------------------------------+
//| Выставляем buy limit ордер. |
//+------------------------------------------------------------------+
bool CTradeControl::SellLimit(const double volume,
const double price,
const string symbol,
const ENUM_ORDER_TYPE_TIME type_time=0,
const datetime expiration=0,
const string comment="")
{
bool res=CTradeCustom::SellLimit(volume,price,symbol,type_time,expiration,comment);
if(res)
m_last_trade_action=GetTickCount();
return res;
}
//+------------------------------------------------------------------+