start implementing XTrade Handler Class ...

This commit is contained in:
2024-07-22 02:04:16 +03:30
parent d194d85082
commit d1e8daa131
5 changed files with 853 additions and 38 deletions
+84 -18
View File
@@ -46,18 +46,19 @@ public:
//
// Constructors ...
void XSCBaseStrategy(
string _symbol, // Trading Symbol
ENUM_TIMEFRAMES _period, // Trading TimeFrame
double _volume, // Voluem
double _r2r, // Risk/Reward Ratio
int _slippage, // Trader Slippage
long _magicNumber, // Trader Magic Number
bool _useTPSLAsPoint = false, // Converts TP and SL by Provided Points
bool _forceMaxTPSLAsPoint = false, // Force Max TP and SL by Provided Points
double _tpPoint = 0, // TP As Point
double _slPoint = 0, // SL As Point
bool _ignoreTP = false, // TP set to 0
bool _ignoreSL = false, // SL set to 0
string _symbol, // Trading Symbol
ENUM_TIMEFRAMES _period, // Trading TimeFrame
double _volume, // Voluem
double _r2r, // Risk/Reward Ratio
int _slippage, // Trader Slippage
long _magicNumber, // Trader Magic Number
bool _ignoreSignalExecution = false, // Ignore Signal Execution
bool _useTPSLAsPoint = false, // Converts TP and SL by Provided Points
bool _forceMaxTPSLAsPoint = false, // Force Max TP and SL by Provided Points
double _tpPoint = 0, // TP As Point
double _slPoint = 0, // SL As Point
bool _ignoreTP = false, // TP set to 0
bool _ignoreSL = false, // SL set to 0
bool _allowLong = true,
bool _allowShort = true,
int _maxAllowedLongs = 0,
@@ -82,6 +83,7 @@ public:
mMaxAllowedLongs = _maxAllowedLongs;
mMaxAllowedShorts = _maxAllowedShorts;
mForceMaxTPSLAsPoint = _forceMaxTPSLAsPoint;
mIgnoreSignalExecution = _ignoreSignalExecution;
//
mForceDisabled = false;
@@ -371,6 +373,29 @@ public:
mIgnoreSL = value;
}
//
bool IgnoreSignalExecution()
{
return mIgnoreSignalExecution;
}
//
void IgnoreSignalExecution(bool value)
{
mIgnoreSignalExecution = value;
}
//
// On Signal Handlers ...
void AddSignalEventHandler(TOnSignal handler)
{
//
Add(
handler,
mOnSignalEventHandlers //
);
}
//
// Virtual Methods ...
@@ -600,6 +625,18 @@ public:
signal.sl = 0;
}
//
// Notify Signal Event Handler(s) ...
NotifyOnSignalEventHandlers(signal);
//
// Check Signal Execution Enabled or not ...
result = !mIgnoreSignalExecution;
if (!result)
{
return result;
}
//
ENUM_X_SIGNAL_EXECUTION_RESULT state;
result = trader.ExecuteSignal(
@@ -652,12 +689,13 @@ protected:
int mMaxAllowedShorts; // Max Allowed Shorts
//
bool mUseTPSLAsPoint; // Converts TP and SL by Provided Points
bool mForceMaxTPSLAsPoint; // Force Max TP and SL by Provided Points
double mTPPoint; // TP As Point
double mSLPoint; // SL As Point
bool mIgnoreTP; // TP set to 0
bool mIgnoreSL; // SL set to 0
bool mUseTPSLAsPoint; // Converts TP and SL by Provided Points
bool mForceMaxTPSLAsPoint; // Force Max TP and SL by Provided Points
double mTPPoint; // TP As Point
double mSLPoint; // SL As Point
bool mIgnoreTP; // TP set to 0
bool mIgnoreSL; // SL set to 0
bool mIgnoreSignalExecution; // Ignore Signal Execution
//
string mSymbol; // Trading Symbol ...
@@ -687,11 +725,39 @@ protected:
barTracker.Clean();
}
//
// Notified Signal Event Handler(s) ...
void NotifyOnSignalEventHandlers(XSignal &signal)
{
//
if (!signal.IsValid())
{
return;
}
//
int count = ArraySize(mOnSignalEventHandlers);
if (!IsValidSize(count))
{
return;
}
//
for (int i = 0; i < count; i++)
{
mOnSignalEventHandlers[i](signal);
}
}
//
// Private ...
private:
//
// Props ...
//
// On Signal Recieved Event Handler(s) ...
TOnSignal mOnSignalEventHandlers[];
};
//
+311
View File
@@ -22,6 +22,7 @@
//
// Imports ...
#include "../Classes/x-saherelm.base.class.mq5"
#include "../Classes/x-saherelm.xalert.class.mq5"
#include "../Libraries/x-saherelm.xtrade.lib.mq5"
#include "../Classes/x-saherelm.xaccount.class.mq5"
@@ -3772,4 +3773,314 @@ private:
}
};
//
// Trade Handler Class ...
class XSCXTradeHandler : public XSCBaseAlert
{
//
// Public ...
public:
//
//
// Constructor(s) ...
void XSCXTradeHandler(XSCTrade *trader)
{
//
mTrader = trader;
}
//
// Deconstructor ...
void ~XSCXTradeHandler()
{
//
delete mTrader;
}
//
// First Step of Trade Handling ...
// Since Must Call when a Signal Executed ...
void AddExecutedSignal(
XSignal &signal, // Executed Signal
double commission = 0 // Commission
)
{
//
// Check Signal Valid ...
if (!signal.IsValid())
{
return;
}
//
XTradeData item;
bool isFilled = item.Fill(signal);
if (!isFilled)
{
return;
}
//
item.commission = commission;
//
Add(item);
}
//
// this Method call's by a Timer,
// or in OnTick for Update Positions,
// Data ...
void UpdateData()
{
//
XPosition positions[];
int count = mTrader.GetPositions(positions);
if (!IsValidSize(count))
{
return;
}
//
for (int i = 0; i < count; i++)
{
//
XPosition iPosition = positions[i];
//
int idx = -1;
bool isExists = HasItem(
iPosition.ticket,
idx //
);
if (!isExists)
{
//
// Add New Data ...
//
// Find Executed Signal Deal History ...
XDeal deals[];
double commission = 0;
int dealsCount = mTrader.GetDeals(
deals,
iPosition.symbol,
iPosition.provider,
iPosition.period,
NULL,
DEAL_ENTRY_IN //
);
if (IsValidSize(dealsCount))
{
//
for (int i = 0; i < dealsCount; i++)
{
//
XDeal iDeal = deals[i];
if (iDeal.positionId == iPosition.ticket)
{
//
commission = iDeal.commission;
break;
}
}
}
//
XTradeData iData;
iData.Fill(
iPosition //
);
//
iData.ticket = iPosition.ticket;
//
Add(iData);
}
else
{
//
// Update Exists ...
mData[idx].Update(iPosition);
}
}
}
//
// Protected ...
protected:
//
XSCTrade *mTrader; // Instance of Trader Class
//
XTradeData mData[]; // Hold Trade Data
//
// Private ...
private:
//
// Props ...
//
// Count Data ...
int Count()
{
return ArraySize(mData);
}
//
// Add Item ...
bool Add(XTradeData &item)
{
//
bool result = false;
//
result = item.IsValid();
if (!result)
{
return result;
}
//
int index = -1;
if (item.ticket > 0)
{
//
result = !HasItem(
item.ticket,
index //
);
}
else
{
//
result = !HasItem(
item.symbol,
item.provider,
item.period,
item.type,
index //
);
}
if (!result || IsValidIndex(index))
{
//
result = false;
return result;
}
//
AddRef(
item,
mData //
);
//
return result;
}
//
// Find Item Index ...
bool HasItem(
ulong ticket,
int &index //
)
{
//
bool result = false;
//
index = -1;
//
int count = Count();
result = IsValidSize(count);
if (!result)
{
return result;
}
//
for (int i = 0; i < count; i++)
{
//
result = mData[i].IsOwn(ticket);
if (result)
{
//
index = i;
break;
}
}
//
return result;
}
//
// Find Item Index ...
bool HasItem(
string symbol,
string provider,
ENUM_TIMEFRAMES period,
ENUM_X_POSITION_TYPES type,
int &index //
)
{
//
bool result = false;
//
index = -1;
//
// Validate ...
result =
//
IsValid(symbol) &&
IsValid(period) &&
IsValid(provider) &&
type != X_POSITION_TYPE_ALL &&
type != X_POSITION_TYPE_NONE
//
;
if (!result)
{
return result;
}
//
int count = Count();
result = IsValidSize(count);
if (!result)
{
return result;
}
//
for (int i = 0; i < count; i++)
{
//
result = mData[i].IsOwn(
symbol,
provider,
period,
type //
);
if (result)
{
//
index = i;
break;
}
}
//
return result;
}
};
//