1198 lines
30 KiB
Plaintext
1198 lines
30 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
|
|
//
|
|
// 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,
|
|
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.provider.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;
|
|
}
|
|
|
|
//
|
|
// Handl Management Actions ...
|
|
// if returns true, it is going to prevent for processing forward ...
|
|
bool HandleStateManagement(XSignal &signals[]) override
|
|
{
|
|
//
|
|
bool result = false;
|
|
|
|
//
|
|
XPosition positions[];
|
|
int positionsCount = mTrader.GetPositions(positions);
|
|
if (positionsCount <= 0)
|
|
{
|
|
return result;
|
|
}
|
|
|
|
//
|
|
// Synch Position Tracker ...
|
|
SyncPositionTracker();
|
|
|
|
//
|
|
HandleEQMState(signals);
|
|
|
|
//
|
|
HandleEQMHedge(positions);
|
|
|
|
//
|
|
HandleTrailManagement(positions);
|
|
|
|
//
|
|
// Close all Positions on 22 ...
|
|
// TODO: Make it Configurable ...
|
|
int closeHour = -1;
|
|
MqlDateTime cTime = GetCurrentTime();
|
|
if (closeHour >= 0 && cTime.hour == closeHour)
|
|
{
|
|
//
|
|
string comment = "EQM Close on Time ...";
|
|
|
|
//
|
|
XPosition positions[];
|
|
int positionsCount = mTrader.GetPositions(positions);
|
|
if (positionsCount <= 0)
|
|
{
|
|
return result;
|
|
}
|
|
|
|
//
|
|
int closed = mTrader.Close(
|
|
positions,
|
|
comment //
|
|
);
|
|
if (closed > 0)
|
|
{
|
|
mAlert.Alert(comment);
|
|
}
|
|
}
|
|
|
|
//
|
|
// Must false ...
|
|
return result;
|
|
}
|
|
|
|
//
|
|
void HandleEQMHedge(XPosition &positions[])
|
|
{
|
|
//
|
|
// TODO:
|
|
// Make them Configurable ...
|
|
int numberOfPositionsForHege = 3;
|
|
|
|
//
|
|
int positionsCount = ArraySize(positions);
|
|
if (positionsCount <= 0)
|
|
{
|
|
return;
|
|
}
|
|
|
|
//
|
|
// Hedge ...
|
|
if (positionsCount >= numberOfPositionsForHege)
|
|
{
|
|
//
|
|
int maxAllowedPositions = GetMaxAllowedPositions();
|
|
|
|
//
|
|
double profit = CalculatePositionsProfit(positions);
|
|
double requiredProfit = CalculateRequiredProfitForHedge(positions);
|
|
|
|
//
|
|
bool isReadyForHedge =
|
|
positionsCount < maxAllowedPositions
|
|
? profit >= requiredProfit
|
|
: positionsCount >= maxAllowedPositions && profit >= (requiredProfit * 0.50)
|
|
? true
|
|
: false;
|
|
if (!isReadyForHedge)
|
|
{
|
|
return;
|
|
}
|
|
|
|
//
|
|
string comment = "EQM Hedge ...";
|
|
int closed = mTrader.Close(
|
|
positions,
|
|
comment);
|
|
if (closed > 0)
|
|
{
|
|
//
|
|
string message = "EQM Hedge Positions on Profit: " + ToString(profit);
|
|
|
|
//
|
|
mAlert.Alert(message);
|
|
|
|
//
|
|
for (int i = 0; i < positionsCount; i++)
|
|
{
|
|
RemoveTrail(positions[i].ticket);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
//
|
|
void HandleEQMState(XSignal &signals[])
|
|
{
|
|
//
|
|
bool equityReached = !mTrader.CheckEquityForTrade();
|
|
bool maxAllowedReached = !mTrader.CheckPositionsForTrade();
|
|
bool enableEQM = equityReached || maxAllowedReached;
|
|
if (!enableEQM)
|
|
{
|
|
//
|
|
return;
|
|
}
|
|
|
|
//
|
|
string eqmMessage = "EQM Enabled: " + "\n";
|
|
if (equityReached)
|
|
{
|
|
eqmMessage += "- Max Equity Reached ..." + "\n";
|
|
}
|
|
if (maxAllowedReached)
|
|
{
|
|
eqmMessage += "- Max Allowed Positions Reached ...";
|
|
}
|
|
// mAlert.Alert(eqmMessage);
|
|
|
|
//
|
|
// Retrieve InDrawdown Positions Count ...
|
|
// Separate based on Directions and Symbols ...
|
|
// Accept Only One Signal based on each symbol Indirect ...
|
|
// for Fittion Positions to HedgeEnable in Long Time Positions ...
|
|
|
|
// //
|
|
// int descriptorsCount = CountDescriptors();
|
|
// if (descriptorsCount <= 0)
|
|
// {
|
|
// return;
|
|
// }
|
|
|
|
// //
|
|
// XSignal supportSignals[];
|
|
// for (int i = 0; i < descriptorsCount; i++)
|
|
// {
|
|
// //
|
|
// X121ProviderDescriptor iDescriptor = mDescriptors[i];
|
|
|
|
// //
|
|
// XSignal iSupportSignals[];
|
|
// int iSupportSignalsCount = iDescriptor
|
|
// .provider
|
|
// .GenerateEQMSupportSignals(
|
|
// iSupportSignals //
|
|
// );
|
|
// if (iSupportSignalsCount <= 0)
|
|
// {
|
|
// continue;
|
|
// }
|
|
|
|
// //
|
|
// Copy(
|
|
// iSupportSignals,
|
|
// supportSignals,
|
|
// false //
|
|
// );
|
|
// }
|
|
|
|
// //
|
|
// int supportSignalsCount = ArraySize(supportSignals);
|
|
// if (supportSignalsCount <= 0)
|
|
// {
|
|
// return;
|
|
// }
|
|
|
|
// //
|
|
// ENUM_X_SIGNAL_EXECUTION_RESULT states[];
|
|
// int executedSupports = mTrader.ExecuteSignals(
|
|
// supportSignals,
|
|
// states,
|
|
// ORDER_TIME_GTC,
|
|
// NULL,
|
|
// false // Ignore Policies ...
|
|
// );
|
|
// if (executedSupports > 0)
|
|
// {
|
|
// //
|
|
// string message = "EQM Execute (" + ToString(executedSupports) + ") Support Signals ...";
|
|
|
|
// //
|
|
// mAlert.Alert(message);
|
|
// }
|
|
}
|
|
|
|
//
|
|
void HandleTrailManagement(XPosition &positions[])
|
|
{
|
|
//
|
|
// TODO:
|
|
// Make them Configurable ...
|
|
int levelsOnClose = 6;
|
|
int requiredAgeForSLTrail = 0;
|
|
int requiredDistanceForSLTrail = 100;
|
|
return;
|
|
|
|
//
|
|
int positionsCount = ArraySize(positions);
|
|
if (positionsCount <= 0)
|
|
{
|
|
return;
|
|
}
|
|
|
|
//
|
|
// Trail Positions Stop ...
|
|
for (int i = 0; i < positionsCount; i++)
|
|
{
|
|
//
|
|
XPosition iPosition = positions[i];
|
|
|
|
//
|
|
bool canManageState =
|
|
//
|
|
true
|
|
//
|
|
;
|
|
|
|
//
|
|
// Position Management for X786 ...
|
|
if (canManageState)
|
|
{
|
|
//
|
|
// Check Position in Profit or not ...
|
|
if (iPosition.profit > 0)
|
|
{
|
|
//
|
|
XTrail trail;
|
|
bool isTrailedBefore = GetSLTrail(
|
|
iPosition.ticket,
|
|
trail //
|
|
);
|
|
|
|
//
|
|
if (!isTrailedBefore)
|
|
{
|
|
//
|
|
// Add First Time ...
|
|
|
|
//
|
|
// Check Position Age ...
|
|
int iPositionAge = iPosition.GetAge(iPosition.period);
|
|
if (iPositionAge < requiredAgeForSLTrail)
|
|
{
|
|
continue;
|
|
}
|
|
|
|
//
|
|
// Check Position Profit Distance ...
|
|
double priceDistanceInPoint = PriceToPoint(iPosition.profit / iPosition.volume, iPosition.symbol);
|
|
double priceDistance = PointToPrice(priceDistanceInPoint, iPosition.symbol);
|
|
if (priceDistance < requiredDistanceForSLTrail)
|
|
{
|
|
continue;
|
|
}
|
|
|
|
//
|
|
double slDiff = PointToPrice(priceDistance, iPosition.symbol);
|
|
double appliedSL = slDiff / 2;
|
|
|
|
// //
|
|
// double sl =
|
|
// IsLong(iPosition.type)
|
|
// ? iPosition.sl + slDiff
|
|
// : iPosition.sl - slDiff;
|
|
|
|
//
|
|
double sl =
|
|
IsLong(iPosition.type)
|
|
? iPosition.entry + appliedSL
|
|
: iPosition.entry - appliedSL;
|
|
|
|
//
|
|
bool isInit = trail.Init(
|
|
iPosition.ticket,
|
|
iPosition.type,
|
|
iPosition.profit,
|
|
iPosition.sl,
|
|
sl //
|
|
);
|
|
if (!isInit)
|
|
{
|
|
continue;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
//
|
|
// Update Exists ...
|
|
|
|
//
|
|
// Check Profit ...
|
|
bool isProfitPassed = iPosition.profit > trail.profit;
|
|
if (!isProfitPassed)
|
|
{
|
|
continue;
|
|
}
|
|
|
|
//
|
|
// Check Age Distance from Last ...
|
|
int age = iBarShift(
|
|
iPosition.symbol,
|
|
iPosition.period,
|
|
trail.time,
|
|
false //
|
|
);
|
|
if (age < requiredAgeForSLTrail)
|
|
{
|
|
continue;
|
|
}
|
|
|
|
//
|
|
double priceDistanceInPoint = PriceToPoint((iPosition.profit - trail.profit) / iPosition.volume, iPosition.symbol);
|
|
double priceDistance = PointToPrice(priceDistanceInPoint, iPosition.symbol);
|
|
if (priceDistance < (requiredDistanceForSLTrail / 2))
|
|
{
|
|
// //
|
|
// // int positionAge = iPosition.GetAge(iPosition.period);
|
|
// if (trail.level >= levelsOnClose)
|
|
// {
|
|
// //
|
|
// string comment = "EQM Close In Profit ...";
|
|
// bool isClosed = mTrader.ClosePositionPack(
|
|
// iPosition.ticket,
|
|
// iPosition.symbol,
|
|
// iPosition.period,
|
|
// comment,
|
|
// true,
|
|
// GetMinProfitPerTrade(),
|
|
// GetMinProfitPerVolumeFactor() //
|
|
// );
|
|
// if (isClosed)
|
|
// {
|
|
// //
|
|
// RemoveTrail(iPosition.ticket);
|
|
|
|
// //
|
|
// string message = "EQM Close (" + ToString(iPosition.ticket) + ") In Profit ...";
|
|
// mAlert.Alert(message);
|
|
// }
|
|
// }
|
|
continue;
|
|
}
|
|
|
|
//
|
|
bool isLong = IsLong(iPosition.type);
|
|
|
|
//
|
|
double slDiff = PointToPrice(priceDistance, iPosition.symbol);
|
|
double appliedSL = slDiff / 2;
|
|
|
|
//
|
|
double selectedValue =
|
|
//
|
|
trail.level == 1 || (isLong
|
|
? iPosition.sl < iPosition.entry
|
|
: iPosition.sl > iPosition.entry)
|
|
? iPosition.entry
|
|
: iPosition.sl
|
|
//
|
|
;
|
|
|
|
//
|
|
double sl =
|
|
isLong
|
|
? selectedValue + appliedSL
|
|
: selectedValue - appliedSL;
|
|
|
|
//
|
|
// Update Trail Values ...
|
|
bool isUpdated = trail.Update(
|
|
sl,
|
|
iPosition.profit //
|
|
);
|
|
if (!isUpdated)
|
|
{
|
|
continue;
|
|
}
|
|
}
|
|
|
|
//
|
|
if (trail.IsValid())
|
|
{
|
|
//
|
|
string comment = trail.GenerateTag();
|
|
bool isModified = mTrader.Modify(
|
|
trail.ticket,
|
|
trail.after,
|
|
iPosition.tp,
|
|
comment //
|
|
);
|
|
if (isModified)
|
|
{
|
|
//
|
|
RemoveTrail(trail.ticket);
|
|
|
|
//
|
|
AddSLTrail(trail);
|
|
|
|
//
|
|
string message = "Position (" + ToString(trail.ticket) + ") Trailed SL on Level: " + ToString(trail.level);
|
|
mAlert.Alert(message);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
//
|
|
// Here we Can Filter Signals before Sending them to Execution ...
|
|
// based on Providers and etc ...
|
|
// for Volumes and Benefits ...
|
|
int HandleSignalManagement(XSignal &signals[]) override
|
|
{
|
|
//
|
|
int result = 0;
|
|
|
|
//
|
|
if (ArraySize(signals) <= 0)
|
|
{
|
|
return result;
|
|
}
|
|
|
|
//
|
|
// Create temp Array for Filtering ...
|
|
XSignal tmps[];
|
|
Copy(
|
|
signals,
|
|
tmps //
|
|
);
|
|
int tmpsCount = ArraySize(tmps);
|
|
|
|
//
|
|
// Cleaning Source ...
|
|
Clean(signals);
|
|
|
|
//
|
|
|
|
//
|
|
// Loop through Signals for Management ...
|
|
for (int i = 0; i < tmpsCount; i++)
|
|
{
|
|
//
|
|
XSignal iSignal = tmps[i];
|
|
|
|
//
|
|
string provider = iSignal.provider;
|
|
if (!IsValid(provider))
|
|
{
|
|
//
|
|
AddRef(
|
|
iSignal,
|
|
signals //
|
|
);
|
|
|
|
//
|
|
continue;
|
|
}
|
|
|
|
//
|
|
XPosition positions[];
|
|
int positionsCount = mTrader.GetPositions(
|
|
positions,
|
|
iSignal.symbol,
|
|
iSignal.provider,
|
|
NULL, // All Periods ...
|
|
iSignal.type //
|
|
);
|
|
if (positionsCount <= 0)
|
|
{
|
|
//
|
|
// First Signal in a While of Specific Provider ...
|
|
|
|
//
|
|
iSignal.volume *= 2;
|
|
|
|
//
|
|
AddRef(
|
|
iSignal,
|
|
signals //
|
|
);
|
|
|
|
//
|
|
continue;
|
|
}
|
|
else if (positionsCount < 2)
|
|
{
|
|
//
|
|
// First Signal in a While of Specific Provider ...
|
|
|
|
//
|
|
AddRef(
|
|
iSignal,
|
|
signals //
|
|
);
|
|
|
|
//
|
|
continue;
|
|
}
|
|
}
|
|
|
|
//
|
|
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;
|
|
}
|
|
|
|
//
|
|
// Handle Position Changed Event ...
|
|
void OnPositionsChangedHandler(int count)
|
|
{
|
|
//
|
|
// Synchronize Tracker ...
|
|
SyncPositionTracker();
|
|
}
|
|
|
|
//
|
|
// Tools ...
|
|
|
|
//
|
|
// Position Tracker ...
|
|
|
|
//
|
|
// Synchronize Positions and Position Tracker ...
|
|
void SyncPositionTracker()
|
|
{
|
|
//
|
|
static datetime lastSyncPositionTracker = NULL;
|
|
datetime cTime = TimeCurrent();
|
|
|
|
//
|
|
int syncInterval = PeriodSeconds(PERIOD_M5);
|
|
int lastDiff = (int)cTime - (int)lastSyncPositionTracker;
|
|
|
|
//
|
|
bool canSync =
|
|
lastSyncPositionTracker == NULL
|
|
? true
|
|
: lastDiff >= syncInterval;
|
|
if (!canSync)
|
|
{
|
|
return;
|
|
}
|
|
|
|
//
|
|
XPositionTrackerState state;
|
|
int synced = mPositionTracker.Sync(
|
|
state,
|
|
mTrader //
|
|
);
|
|
if (synced > 0)
|
|
{
|
|
//
|
|
// Check State and Do What we Want ...
|
|
int onProfitsCount = ArraySize(state.onProfits);
|
|
int onDrawdownCount = ArraySize(state.onDrawdowns);
|
|
|
|
//
|
|
// Do What we want whe a Position Dropped to Prefit ...
|
|
if (onProfitsCount > 0)
|
|
{
|
|
//
|
|
for (int i = 0; i < onProfitsCount; i++)
|
|
{
|
|
//
|
|
XPositionTrack iTrack = state.onProfits[i];
|
|
|
|
//
|
|
string message = "Position (" + ToString(iTrack.ticket) + ", " + iTrack.symbol + ") Dropped in Profit ...";
|
|
mAlert.Alert(message);
|
|
}
|
|
}
|
|
|
|
//
|
|
// Do what we want when a Position Dropped to Drawdown ...
|
|
if (onDrawdownCount > 0)
|
|
{
|
|
//
|
|
for (int i = 0; i < onDrawdownCount; i++)
|
|
{
|
|
//
|
|
XPositionTrack iTrack = state.onDrawdowns[i];
|
|
|
|
//
|
|
string message = "Position (" + ToString(iTrack.ticket) + ", " + iTrack.symbol + ") Dropped in Drawdown ...";
|
|
mAlert.Alert(message);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
//
|
|
// 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);
|
|
}
|
|
}
|
|
|
|
//
|
|
// Private ...
|
|
private:
|
|
//
|
|
// Props ...
|
|
|
|
//
|
|
// Collection of Signal Event Listeners ...
|
|
TX121OnSignal mX121OnSignalEventHandlers[];
|
|
|
|
//
|
|
// a Collection of X5 Provider Descriptors ...
|
|
X121ProviderDescriptor mDescriptors[];
|
|
|
|
//
|
|
int CountDescriptors()
|
|
{
|
|
return ArraySize(mDescriptors);
|
|
}
|
|
|
|
//
|
|
// 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 ...
|
|
|
|
// |