Suivre
1
0
Forker
Vous avez déjà forké Adwizard
0
forké depuis antekov/Adwizard
Adwizard/Virtual/VirtualOrder.mqh
637 lignes
49 Kio
MQL5
BrutLien permanentVue normaleHistorique
Yuriy Bykov
2025-04-11 13:28:40 +03:00
<EFBFBD><EFBFBD>//+------------------------------------------------------------------+
//| VirtualOrder.mqh |
//| Copyright 2019-2025, Yuriy Bykov |
//| https://www.mql5.com/ru/users/antekov |
//+------------------------------------------------------------------+
#property copyright "Copyright 2019-2025, Yuriy Bykov"
#property link "https://www.mql5.com/ru/users/antekov"
#property version "1.09"
#include <Trade\SymbolInfo.mqh>
//+------------------------------------------------------------------+
//| @5420@8B5;L=K5 >?@545;5=8O :;0AA>2 |
//+------------------------------------------------------------------+
class CVirtualOrder;
class CVirtualReceiver;
class CVirtualStrategy;
#include "../Utils/SymbolsMonitor.mqh"
#include "VirtualReceiver.mqh"
#include "VirtualStrategy.mqh"
// !B@C:BC@0 4;O GB5=8O/70?8A8 87 
// >A=>2=KE A2>9AB2 28@BC0;L=>3> >@45@0/?>78F88
struct VirtualOrderStruct {
string strategyHash;
int strategyIndex;
ulong ticket;
string symbol;
double lot;
ENUM_ORDER_TYPE type;
datetime openTime;
double openPrice;
double stopLoss;
double takeProfit;
datetime closeTime;
double closePrice;
datetime expiration;
string comment;
double point;
};
//+------------------------------------------------------------------+
//| ;0AA 28@BC0;L=KE >@45@>2 8 ?>78F89 |
//+------------------------------------------------------------------+
class CVirtualOrder {
private:
//--- !B0B8G5A:85 ?>;O
static ulong s_count; // !GQBG8: 2A5E A>740==KE >1J5:B>2 CVirtualOrder
static ulong s_ticket;
CSymbolInfo *m_symbolInfo; // 1J5:B 4;O ?>;CG5=8O A2>9AB2 A8<2>;>2
//--- !2O70==K5 >1J5:BK ?>;CG0B5;O 8 AB@0B5388
CSymbolsMonitor *m_symbols;
CVirtualReceiver *m_receiver;
CVirtualStrategy *m_strategy;
//--- !2>9AB20 >@45@0 (?>78F88)
ulong m_id; // ID
ulong m_ticket; // "8:5B
string m_symbol; // !8<2>;
double m_lot; // 1J5<
ENUM_ORDER_TYPE m_type; // "8?
double m_openPrice; // &5=0 >B:@KB8O
double m_stopLoss; // #@>25=L StopLoss
double m_takeProfit; // #@>25=L TakeProfit
string m_comment; // ><<5=B0@89
datetime m_expiration; // @5<O 8AB5G5=8O
datetime m_openTime; // @5<O >B:@KB8O
//--- !2>9AB20 70:@KB>3> >@45@0 (?>78F88)
double m_closePrice; // &5=0 70:@KB8O
datetime m_closeTime; // @5<O 70:@KB8O
string m_closeReason; // @8G8=0 70:@KB8O
double m_point; // 5;8G8=0 ?C=:B0
bool m_isStopLoss; // @87=0: A@010BK20=8O StopLoss
bool m_isTakeProfit; // @87=0: A@010BK20=8O TakeProfit
bool m_isExpired; // @87=0: 8AB5G5=8O 2@5<5=8
//--- '0AB=K5 <5B>4K
bool CheckClose(); // @>25@:0 CA;>289 70:@KB8O
bool CheckTrigger(); // @>25@:0 A@010BK20=8O >B;>65==>3> >@45@0
public:
CVirtualOrder(
CVirtualStrategy *p_strategy
); // >=AB@C:B>@
~CVirtualOrder() {
if(!!m_symbolInfo) delete m_symbolInfo;
}
//--- 5B>4K ?@>25@:8 A>AB>O=8O ?>78F88 (>@45@0)
bool IsOpen() { // @45@ >B:@KB?
return(this.m_openTime > 0 && this.m_closeTime == 0);
};
bool IsClosed() { // @45@ 70:@KB?
return(this.m_openTime > 0 && this.m_closeTime > 0);
};
bool IsMarketOrder() { // -B> @K=>G=0O ?>78F8O?
return IsOpen() && (m_type == ORDER_TYPE_BUY || m_type == ORDER_TYPE_SELL);
}
bool IsPendingOrder() {// -B> >B;>65==K9 >@45@?
return IsOpen() && (m_type == ORDER_TYPE_BUY_LIMIT
|| m_type == ORDER_TYPE_BUY_STOP
|| m_type == ORDER_TYPE_SELL_LIMIT
|| m_type == ORDER_TYPE_SELL_STOP);
}
bool IsBuyOrder() { // -B> >B:@KB0O ?>78F8O BUY?
return IsOpen() && (m_type == ORDER_TYPE_BUY
|| m_type == ORDER_TYPE_BUY_LIMIT
|| m_type == ORDER_TYPE_BUY_STOP);
}
bool IsSellOrder() { // -B> >B:@KB0O ?>78F8O SELL?
return IsOpen() && (m_type == ORDER_TYPE_SELL
|| m_type == ORDER_TYPE_SELL_LIMIT
|| m_type == ORDER_TYPE_SELL_STOP);
}
bool IsStopOrder() { // -B> >B;>65==K9 STOP->@45@?
return IsOpen() && (m_type == ORDER_TYPE_BUY_STOP || m_type == ORDER_TYPE_SELL_STOP);
}
bool IsLimitOrder() { // -B> >B;>65==K9 LIMIT->@45@?
return IsOpen() && (m_type == ORDER_TYPE_BUY_LIMIT || m_type == ORDER_TYPE_SELL_LIMIT);
}
//--- 5B>4K ?>;CG5=8O A2>9AB2 ?>78F88 (>@45@0)
ulong Id() { // ID
return m_id;
}
ulong Ticket() { // "8:5B
return m_ticket;
}
CStrategy *Strategy() {
return m_strategy;
}
ENUM_ORDER_TYPE Type() {
return m_type;
}
string TypeName() {
string s = StringSubstr(EnumToString(m_type), 11);
StringReplace(s, "_", " ");
return s;
}
double Lot() {
return m_lot;
}
double Volume() { // 1J5< A =0?@02;5=85<
return IsBuyOrder() ? m_lot : (IsSellOrder() ? -m_lot : 0);
}
double Profit(); // "5:CI0O ?@81K;L
double ClosedProfit();
string Symbol() { // !8<2>;
return m_symbol;
}
double OpenPrice() {
return m_openPrice;
}
datetime OpenTime() {
return m_openTime;
}
double ClosePrice() {
return m_closePrice;
}
datetime CloseTime() {
return m_closeTime;
}
double FittedBalance() {
return m_strategy.FittedBalance();
}
double StopLoss() {
return m_stopLoss;
}
void StopLoss(double value) {
m_stopLoss = value;
}
double TakeProfit() {
return m_takeProfit;
}
void TakeProfit(double value) {
m_takeProfit = value;
}
//--- 5B>4K >1@01>B:8 ?>78F89 (>@45@>2)
bool CVirtualOrder::Open(string symbol,
ENUM_ORDER_TYPE type,
double lot,
double price,
double sl = 0,
double tp = 0,
string comment = "",
datetime expiration = 0,
bool inPoints = false); // B:@KB85 ?>78F88 (>@45@0)
void Expiration(datetime p_expiration) {
if(IsOpen()) {
m_expiration = p_expiration;
}
}
void Tick(); // 1@01>B:0 B8:0 4;O ?>78F88 (>@45@0)
void Close(); // 0:@KB85 ?>78F88 (>@45@0)
void Clear() {
m_lot = 0;
}
virtual void Load(const VirtualOrderStruct &o); // 03@C7:0 A>AB>O=8O
virtual void Save(VirtualOrderStruct &o); // !>E@0=5=85 A>AB>O=8O
static void Reset() {
s_count = 0;
}
// ABL ;8 >B:@KBK5 @K=>G=K5 28@BC0;L=K5 ?>78F88?
static bool HasMarket(CVirtualOrder* &p_orders[]);
string operator~(); // @5>1@07>20=85 >1J5:B0 2 AB@>:C
};
// =8F80;870F8O AB0B8G5A:8E ?>;59 :;0AA0
ulong CVirtualOrder::s_count = 0;
ulong CVirtualOrder::s_ticket = 0;
//+------------------------------------------------------------------+
//| >=AB@C:B>@ |
//+------------------------------------------------------------------+
CVirtualOrder::CVirtualOrder(CVirtualStrategy *p_strategy) :
// !?8A>: 8=8F80;870F88
m_id(++s_count), // >2K9 845=B8D8:0B>@ = AGQBG8: >1J5:B>2 + 1
m_ticket(0),
m_receiver(CVirtualReceiver::Instance()),
m_strategy(p_strategy),
m_symbol(""),
m_lot(0),
m_type(-1),
m_openPrice(0),
m_stopLoss(0),
m_takeProfit(0),
m_openTime(0),
m_comment(""),
m_expiration(0),
m_closePrice(0),
m_closeTime(0),
m_closeReason(""),
m_point(0) {
PrintFormat(__FUNCTION__ + "#%d | CREATED VirtualOrder", m_id);
m_symbolInfo = NULL;
m_symbols = CSymbolsMonitor::Instance();
}
//+------------------------------------------------------------------+
//| B:@KB85 28@BC0;L=>9 ?>78F88 (>@45@0) |
//+------------------------------------------------------------------+
bool CVirtualOrder::Open(string symbol, // !8<2>;
ENUM_ORDER_TYPE type, // "8? (BUY 8;8 SELL)
double lot, // 1JQ<
double price = 0, // &5=0 >B:@KB8O
double sl = 0, // #@>25=L StopLoss (F5=0 8;8 ?C=:BK)
double tp = 0, // #@>25=L TakeProfit (F5=0 8;8 ?C=:BK)
string comment = "", // ><<5=B0@89
datetime expiration = 0, // @5<O 8AB5G5=8O
bool inPoints = false // #@>2=8 SL 8 TP 7040=K 2 ?C=:B0E?
) {
if(IsOpen()) { // A;8 ?>78F8O C65 >B:@KB0, B> =8G53> =5 45;05<
PrintFormat(__FUNCTION__ "#%d | ERROR: Order is opened already!", m_id);
return false;
}
// >;CG05< >B <>=8B>@0 A8<2>;>2 C:070B5;L =0 8=D>@<0F8>==K9 >1J5:B 4;O =C6=>3> A8<2>;0
m_symbolInfo = m_symbols[symbol];
if(!!m_symbolInfo) {
//m_symbolInfo.RefreshRates(); // 1=>2;O5< 8=D>@<0F8N > B5:CI8E F5=0E
// =8F80;878@C5< A2>9AB20 ?>78F88
m_ticket = ++s_ticket;
m_openPrice = price;
m_symbol = symbol;
m_lot = lot;
m_openTime = TimeCurrent();
m_closeTime = 0;
m_type = type;
m_comment = comment;
m_expiration = expiration;
// B:@K205<0O ?>78F8O (>@45@) =5 O2;O5BAO 70:@KB>9 ?> SL, TP 8;8 8AB5G5=8N
m_isStopLoss = false;
m_isTakeProfit = false;
m_isExpired = false;
m_point = m_symbolInfo.Point();
double bid = m_symbolInfo.Bid();
double ask = m_symbolInfo.Ask();
double spread = ((ask - bid) / m_point);
//m_symbolInfo.Spread();
//  7028A8<>AB8 >B =0?@02;5=8O CAB0=02;8205< F5=C >B:@KB8O 8 C@>2=8 SL 8 TP.
// A;8 SL 8 TP 7040=K 2 ?C=:B0E, B> ?@5420@8B5;L=> 2KG8A;O5< 8E F5=>2K5 C@>2=8
// >B=>A8B5;L=> F5=K >B:@KB8O
if(IsBuyOrder()) {
if(type == ORDER_TYPE_BUY) {
m_openPrice = ask;
}
m_stopLoss = (sl > 0 ? (inPoints ? m_openPrice - sl * m_point - spread * m_point : sl) : 0);
m_takeProfit = (tp > 0 ? (inPoints ? m_openPrice + tp * m_point : tp) : 0);
} else if(IsSellOrder()) {
if(type == ORDER_TYPE_SELL) {
m_openPrice = bid;
}
m_stopLoss = (sl > 0 ? (inPoints ? m_openPrice + sl * m_point : sl) : 0);
m_takeProfit = (tp > 0 ? (inPoints ? m_openPrice - tp * m_point - spread * m_point : tp) : 0);
}
// ?>25I05< ?>;CG0B5;L 8 AB@0B538N, GB> ?>78F8O (>@45@) >B:@KB0