660 lines
14 KiB
Plaintext
660 lines
14 KiB
Plaintext
///////////////////////////////////////////////////////
|
|
//
|
|
// SaherElm IT Center MQL5 Class Library
|
|
// ----------------------------------------------
|
|
// Name: XSCX121EA
|
|
// Description: provides all X121 EA requirements ...
|
|
// - X5 Provider;
|
|
//
|
|
//
|
|
// Maintainer:
|
|
// ------------
|
|
// Hadi Khazaee Asl (hadi_khazaee_asl@yahoo.com)
|
|
//
|
|
//////////////////////////////////////////////////////
|
|
//
|
|
// Global Properties ...
|
|
#property library
|
|
#property copyright "Copyright 2023, SaherElm IT Center"
|
|
#property link "https://www.saherelm.ir"
|
|
#property version "1.00"
|
|
#property strict
|
|
|
|
//
|
|
// Imports ...
|
|
#include "../Classes/x-saherelm.xea.class.mq5"
|
|
#include "../Classes/x-saherelm.x121.provider.class.mq5"
|
|
|
|
//
|
|
// Define On Signal Event Handler Type Specified for X5 ...
|
|
typedef void (*TX121OnSignal)(X121ProviderDescriptor &descriptor);
|
|
|
|
//
|
|
// Class Definition ...
|
|
|
|
class XSCX121EA : public XSCBaseEA
|
|
{
|
|
//
|
|
// Public ...
|
|
public:
|
|
//
|
|
// Props ...
|
|
|
|
//
|
|
// Constructors ...
|
|
XSCX121EA(
|
|
//
|
|
// XTrade Class Requirements ...
|
|
int slippage, // Specify Slippage
|
|
ulong magicNumber, // Specify Magic Number
|
|
int manageInterval, // Manager Check Intervals Seconds
|
|
double maxAllowedSpread, // Max Allowed Spred for Opening Trades
|
|
int maxAllowedPositions, // Max Allowed Positions
|
|
double maxAllowedDrawdownFactor, // Max Allowed Drawdown Factor
|
|
double staticVolume, // Static Volume for Positions
|
|
//
|
|
// Position Management ...
|
|
bool allowLong = true, // Allow Long Trades
|
|
bool allowShort = true, // Allow Short Trades
|
|
double minProfitPerTrade = 0, // Min Profit Per Trade based On Volume Factor (Hedge)
|
|
double minProfitPerVolumeFactor = 0, // Min Volume Factor for Calculating Profit (Hedge)
|
|
//
|
|
// Event Handlers ...
|
|
TOnStopLoss onStopLossTriggered = NULL, // On StopLoass Triggered Event Handler
|
|
TOnTakeProfit onTakeProfitTriggered = NULL, // On TakeProfit Triggered Event Handler
|
|
TOnDealsChanged onDealsChangedHandler = NULL, // On Deals Changed Event Handler
|
|
TOnOrdersChanged onOrdersChangedHandler = NULL, // On Orders Changed Event Handler
|
|
TOnPositionsChanged onPositionsChangedHandler = NULL, // On Positions Changed Event Handler
|
|
TOnTradeStateChanged onTradeStateChangedHandler = NULL, // On Trade State Changed Event Handler
|
|
//
|
|
TOnSignal onSignalHandler = NULL, // On Signal Event Handler
|
|
//
|
|
// Log Handler ...
|
|
bool enableAlerts = true, // Enable Alerts
|
|
bool logAlerts = true, // Log Alerts
|
|
bool terminalAlerts = false, // Terminal Alerts
|
|
bool mailAlerts = false, // Mail Alerts
|
|
bool pushAlerts = false // Push Alerts
|
|
) : XSCBaseEA(slippage,
|
|
magicNumber,
|
|
manageInterval,
|
|
maxAllowedSpread,
|
|
maxAllowedPositions,
|
|
maxAllowedDrawdownFactor,
|
|
staticVolume,
|
|
allowLong,
|
|
allowShort,
|
|
minProfitPerTrade,
|
|
minProfitPerVolumeFactor,
|
|
onStopLossTriggered,
|
|
onTakeProfitTriggered,
|
|
onDealsChangedHandler,
|
|
onOrdersChangedHandler,
|
|
onPositionsChangedHandler,
|
|
onTradeStateChangedHandler,
|
|
enableAlerts,
|
|
logAlerts,
|
|
terminalAlerts,
|
|
mailAlerts,
|
|
pushAlerts //
|
|
)
|
|
{
|
|
}
|
|
|
|
//
|
|
// Deconstructor ...
|
|
~XSCX121EA() {}
|
|
|
|
//
|
|
// Properties Getter(s) / Setter(s) ...
|
|
|
|
//
|
|
// Add X5 Specified Signal Event Handler ...
|
|
void AddOnSignalEventHandler(TX121OnSignal listener)
|
|
{
|
|
//
|
|
Add(
|
|
listener,
|
|
mX121OnSignalEventHandlers
|
|
//
|
|
);
|
|
}
|
|
|
|
//
|
|
// Add Specified X5 Provider ...
|
|
bool AddProvider(X121ProviderDescriptor &descriptor)
|
|
{
|
|
//
|
|
bool result = false;
|
|
|
|
//
|
|
// Validate Inputs ...
|
|
result = descriptor.Init();
|
|
if (!result)
|
|
{
|
|
return result;
|
|
}
|
|
|
|
//
|
|
AddRef(
|
|
descriptor,
|
|
mDescriptors //
|
|
);
|
|
|
|
//
|
|
return result;
|
|
}
|
|
|
|
//
|
|
// Overrides ...
|
|
|
|
//
|
|
// Customize Token ...
|
|
string GetToken() override
|
|
{
|
|
return GetSpecificToken(this);
|
|
}
|
|
|
|
//
|
|
string GetTag() override
|
|
{
|
|
return this.GetToken();
|
|
}
|
|
|
|
//
|
|
void Draw() override
|
|
{
|
|
//
|
|
int descriptorsCount = CountDescriptors();
|
|
if (descriptorsCount <= 0)
|
|
{
|
|
return;
|
|
}
|
|
|
|
//
|
|
for (int i = 0; i < descriptorsCount; i++)
|
|
{
|
|
//
|
|
X121ProviderDescriptor iDescriptor = mDescriptors[i];
|
|
|
|
//
|
|
iDescriptor.provider.Draw();
|
|
}
|
|
}
|
|
|
|
//
|
|
// Check Provider for any Guards ...
|
|
bool CheckForGuard(XGuard &guards[]) override
|
|
{
|
|
//
|
|
bool result = false;
|
|
|
|
//
|
|
Clean(guards);
|
|
|
|
//
|
|
int descriptorsCount = CountDescriptors();
|
|
result = descriptorsCount > 0;
|
|
if (!result)
|
|
{
|
|
return result;
|
|
}
|
|
|
|
//
|
|
for (int i = 0; i < descriptorsCount; i++)
|
|
{
|
|
//
|
|
X121ProviderDescriptor iDescriptor = mDescriptors[i];
|
|
|
|
//
|
|
XGuard iGuards[];
|
|
bool iHasGuard = iDescriptor.HasGuard(iGuards);
|
|
if (iHasGuard)
|
|
{
|
|
//
|
|
Copy(
|
|
iGuards,
|
|
guards,
|
|
false //
|
|
);
|
|
}
|
|
}
|
|
|
|
//
|
|
result = ArraySize(guards) > 0;
|
|
|
|
//
|
|
return result;
|
|
}
|
|
|
|
//
|
|
// Request Provider to Collect all
|
|
// Potentially Signals and then
|
|
// filters theme here and passed them
|
|
// for Executing ...
|
|
int RequestForSignal(
|
|
XSignal &signals[] // Holds Signals ...
|
|
) override
|
|
{
|
|
//
|
|
bool result = 0;
|
|
|
|
//
|
|
Clean(signals);
|
|
|
|
//
|
|
int descriptorsCount = CountDescriptors();
|
|
if (descriptorsCount <= 0)
|
|
{
|
|
return result;
|
|
}
|
|
|
|
//
|
|
for (int i = 0; i < descriptorsCount; i++)
|
|
{
|
|
//
|
|
X121ProviderDescriptor iDescriptor = mDescriptors[i];
|
|
|
|
//
|
|
bool iHasSignal = iDescriptor.HasSignal();
|
|
if (iHasSignal)
|
|
{
|
|
//
|
|
// Here we Can double check Signals by Conditions
|
|
// for Score Base Filtering ...
|
|
|
|
//
|
|
AddRef(
|
|
iDescriptor.signal,
|
|
signals
|
|
//
|
|
);
|
|
|
|
//
|
|
NotifyX121OnSignalEventHandlers(iDescriptor);
|
|
}
|
|
}
|
|
|
|
//
|
|
result = ArraySize(signals);
|
|
|
|
//
|
|
return result;
|
|
}
|
|
|
|
//
|
|
// Request for Support Signals using Guard ...
|
|
bool RequestForSupport(
|
|
XSignal &support, // Holds Support Signal, if Provided
|
|
string provider, // Positions Provided by Specific Provider
|
|
string symbol, // Trading Symbol
|
|
ENUM_POSITION_TYPE type, // Trading Type
|
|
ENUM_TIMEFRAMES period // Specified Time Frame
|
|
) override
|
|
{
|
|
//
|
|
// TODO: Implement this ...
|
|
|
|
//
|
|
// Support Senario ...
|
|
// Check Support Position Exists or not ...
|
|
// Check Positions for Support based on Types ...
|
|
// Update Untriggered Positions ...
|
|
return false;
|
|
}
|
|
|
|
//
|
|
// Tools ...
|
|
|
|
//
|
|
// Protected ...
|
|
protected:
|
|
//
|
|
// Tools ...
|
|
|
|
//
|
|
void NotifyX121OnSignalEventHandlers(X121ProviderDescriptor &descriptor)
|
|
{
|
|
//
|
|
int listenersCount = ArraySize(mX121OnSignalEventHandlers);
|
|
if (listenersCount <= 0)
|
|
{
|
|
return;
|
|
}
|
|
|
|
//
|
|
for (int i = 0; i < listenersCount; i++)
|
|
{
|
|
//
|
|
TX121OnSignal iListener = mX121OnSignalEventHandlers[i];
|
|
|
|
//
|
|
iListener(descriptor);
|
|
}
|
|
}
|
|
|
|
//
|
|
// Protect Specified Position ...
|
|
void ProtectPosition(XPosition &position)
|
|
{
|
|
//
|
|
// Here i Can Protect Position ...
|
|
// This Protect Include One Position in Market Mode ...
|
|
// If Provided ...
|
|
// the Positions Selection must passed Some conditions ...
|
|
|
|
//
|
|
string message = "For (" + position.symbol + ") Max In Drawdown Position is: (" +
|
|
ToString(position.ticket) + "), by Profit: " + ToString(position.profit);
|
|
|
|
//
|
|
mAlert.Alert(message);
|
|
}
|
|
|
|
//
|
|
// Private ...
|
|
private:
|
|
//
|
|
// Props ...
|
|
|
|
//
|
|
// Collection of Signal Event Listeners ...
|
|
TX121OnSignal mX121OnSignalEventHandlers[];
|
|
|
|
//
|
|
// a Collection of X5 Provider Descriptors ...
|
|
X121ProviderDescriptor mDescriptors[];
|
|
|
|
//
|
|
int CountDescriptors()
|
|
{
|
|
return ArraySize(mDescriptors);
|
|
}
|
|
|
|
//
|
|
// Find Specifc Descriptor ...
|
|
int FindDescriptorIndex(
|
|
string symbol,
|
|
ENUM_TIMEFRAMES period //
|
|
)
|
|
{
|
|
//
|
|
int result = -1;
|
|
|
|
//
|
|
if (!IsValid(symbol) ||
|
|
!IsValid(period))
|
|
{
|
|
return result;
|
|
}
|
|
|
|
//
|
|
int descriptorsCount = CountDescriptors();
|
|
if (!IsValidSize(descriptorsCount))
|
|
{
|
|
return result;
|
|
}
|
|
|
|
//
|
|
for (int i = 0; i < descriptorsCount; i++)
|
|
{
|
|
//
|
|
X121ProviderDescriptor iDescriptor = mDescriptors[i];
|
|
|
|
//
|
|
bool isPassed =
|
|
//
|
|
iDescriptor.symbol == symbol &&
|
|
iDescriptor.period == period
|
|
//
|
|
;
|
|
if (isPassed)
|
|
{
|
|
//
|
|
result = i;
|
|
break;
|
|
}
|
|
}
|
|
|
|
//
|
|
return result;
|
|
}
|
|
|
|
template <typename T>
|
|
int FindDescriptorIndex(
|
|
T &item //
|
|
)
|
|
{
|
|
return FindDescriptorIndex(
|
|
item.symbol,
|
|
item.period //
|
|
);
|
|
}
|
|
|
|
//
|
|
// Trails Holding ...
|
|
|
|
//
|
|
XTrail mSLTrails[];
|
|
XTrail mTPTrails[];
|
|
|
|
//
|
|
int CountSLTrails()
|
|
{
|
|
return ArraySize(mSLTrails);
|
|
}
|
|
int CountTPTrails()
|
|
{
|
|
return ArraySize(mTPTrails);
|
|
}
|
|
|
|
//
|
|
bool RemoveTrail(ulong ticket)
|
|
{
|
|
//
|
|
bool isSLRemoved = RemoveSLTrail(ticket);
|
|
bool isTPRemoved = RemoveTPTrail(ticket);
|
|
|
|
//
|
|
bool result = isSLRemoved || isTPRemoved;
|
|
|
|
//
|
|
return result;
|
|
}
|
|
bool RemoveSLTrail(ulong ticket)
|
|
{
|
|
//
|
|
bool result = false;
|
|
|
|
//
|
|
int idx = FindSLTrailIndex(ticket);
|
|
result = idx >= 0;
|
|
if (!result)
|
|
{
|
|
return result;
|
|
}
|
|
|
|
//
|
|
result = ArrayRemove(
|
|
mSLTrails,
|
|
idx,
|
|
1 //
|
|
);
|
|
|
|
//
|
|
return result;
|
|
}
|
|
bool RemoveTPTrail(ulong ticket)
|
|
{
|
|
//
|
|
bool result = false;
|
|
|
|
//
|
|
int idx = FindTPTrailIndex(ticket);
|
|
result = idx >= 0;
|
|
if (!result)
|
|
{
|
|
return result;
|
|
}
|
|
|
|
//
|
|
result = ArrayRemove(
|
|
mTPTrails,
|
|
idx,
|
|
1 //
|
|
);
|
|
|
|
//
|
|
return result;
|
|
}
|
|
|
|
//
|
|
int FindSLTrailIndex(ulong ticket)
|
|
{
|
|
//
|
|
int result = -1;
|
|
|
|
//
|
|
int trailsCount = CountSLTrails();
|
|
if (ticket <= 0 || trailsCount <= 0)
|
|
{
|
|
return result;
|
|
}
|
|
|
|
//
|
|
for (int i = 0; i < trailsCount; i++)
|
|
{
|
|
//
|
|
XTrail iTrail = mSLTrails[i];
|
|
|
|
//
|
|
if (iTrail.ticket == ticket)
|
|
{
|
|
//
|
|
result = i;
|
|
break;
|
|
}
|
|
}
|
|
|
|
//
|
|
return result;
|
|
}
|
|
int FindTPTrailIndex(ulong ticket)
|
|
{
|
|
//
|
|
int result = -1;
|
|
|
|
//
|
|
int trailsCount = CountTPTrails();
|
|
if (ticket <= 0 || trailsCount <= 0)
|
|
{
|
|
return result;
|
|
}
|
|
|
|
//
|
|
for (int i = 0; i < trailsCount; i++)
|
|
{
|
|
//
|
|
XTrail iTrail = mTPTrails[i];
|
|
|
|
//
|
|
if (iTrail.ticket == ticket)
|
|
{
|
|
//
|
|
result = i;
|
|
break;
|
|
}
|
|
}
|
|
|
|
//
|
|
return result;
|
|
}
|
|
|
|
//
|
|
bool GetSLTrail(
|
|
ulong ticket,
|
|
XTrail &trail //
|
|
)
|
|
{
|
|
//
|
|
bool result = false;
|
|
|
|
//
|
|
int idx = FindSLTrailIndex(ticket);
|
|
result = idx >= 0;
|
|
if (!result)
|
|
{
|
|
return result;
|
|
}
|
|
|
|
//
|
|
trail = mSLTrails[idx];
|
|
|
|
//
|
|
return result;
|
|
}
|
|
bool GetTPTrail(
|
|
ulong ticket,
|
|
XTrail &trail //
|
|
)
|
|
{
|
|
//
|
|
bool result = false;
|
|
|
|
//
|
|
int idx = FindTPTrailIndex(ticket);
|
|
result = idx >= 0;
|
|
if (!result)
|
|
{
|
|
return result;
|
|
}
|
|
|
|
//
|
|
trail = mTPTrails[idx];
|
|
|
|
//
|
|
return result;
|
|
}
|
|
|
|
//
|
|
void AddSLTrail(
|
|
XTrail &trail //
|
|
)
|
|
{
|
|
//
|
|
if (!trail.IsValid())
|
|
{
|
|
return;
|
|
}
|
|
|
|
//
|
|
AddRef(
|
|
trail,
|
|
mSLTrails //
|
|
);
|
|
}
|
|
void AddTPTrail(
|
|
XTrail &trail //
|
|
)
|
|
{
|
|
//
|
|
if (!trail.IsValid())
|
|
{
|
|
return;
|
|
}
|
|
|
|
//
|
|
AddRef(
|
|
trail,
|
|
mTPTrails //
|
|
);
|
|
}
|
|
|
|
//
|
|
// Tools ...
|
|
};
|
|
|
|
//
|
|
// Tools ...
|
|
|
|
// |