Complete Grid System, Apply Refactor on Signalnfo and Change it To TradeHandler ...

This commit is contained in:
2024-06-15 08:06:40 +03:30
parent 87e3cb2661
commit df1cfae022
6 changed files with 1698 additions and 1125 deletions
+154 -82
View File
@@ -284,15 +284,7 @@ public:
virtual void OnTick()
{
//
double mEquity = mTrader.mAccount.GetEquity();
if (mEquity > mMaxDrawdown)
{
mMaxDrawdown = mEquity;
}
//
mDrawdownPercent = (mMaxDrawdown - mEquity) / (mStaticBalanceForCalculateDrawdown > 0 ? mStaticBalanceForCalculateDrawdown : mMaxDrawdown) * 100;
mDrawdownPercent = NormalizeDouble(mDrawdownPercent, 3);
CalculateMaxDrawdown();
//
// Draw ...
@@ -341,11 +333,26 @@ public:
HandleSignalsExecution(signals);
}
//
// These are Some Event Handler ...
//
// Calls whenever a signal Executed ...
virtual void OnSignalExecuted(const XSignal &signal) {}
//
// Cals whenever a Signal Execution Failed ...
virtual void OnSignalExecutionFailed(
const XSignal &signal,
const ENUM_X_SIGNAL_EXECUTION_RESULT reason //
)
{
}
//
// Calls when a Position Modified ...
virtual void OnPositionModified(
const ulong ticket,
const double profit,
const XPosition &position,
const string comment //
)
{
@@ -354,7 +361,6 @@ public:
//
// Calls when a Position Closed ...
virtual void OnPositionClosed(
const ulong ticket,
const XPosition &position,
const string comment //
)
@@ -364,8 +370,7 @@ public:
//
// Calls when a Position Partially Closed ...
virtual void OnPositionPartialyClosed(
const ulong ticket,
const double profit,
const XPosition &position,
const string comment //
)
{
@@ -535,15 +540,6 @@ public:
return 0;
}
//
// When Some Errors happens in Executing Signals ...
virtual void ManageUnExecutedSignals(
ENUM_X_SIGNAL_EXECUTION_RESULT &states[],
XSignal &unExecutedSignals[] //
)
{
}
//
// Execute Provided Signals ...
virtual void HandleSignalsExecution(XSignal &signals[])
@@ -566,66 +562,16 @@ public:
// Notify Signal Event Handlers for Reciving New Signal ...
for (int i = 0; i < managedSignalsCount; i++)
{
//
NotifyOnSignalEventHandlers(signals[i]);
}
//
// Try to Execute Signals ...
XSignal notExecuteds[];
ENUM_X_SIGNAL_EXECUTION_RESULT reasons[];
int executedSignalsCount = mTrader.ExecuteSpecifiedSignals(
int executedSignalsCount = ExecuteSignals(
signals,
notExecuteds, // Not Executed Signals
reasons
//
reasons //
);
//
string message = NULL;
if (executedSignalsCount == 0)
{
//
message = "(" + ToString(managedSignalsCount) + ") Signals Execution Failed ...";
//
for (int j = 0; j < ArraySize(reasons); j++)
{
//
string iReason = ToString(reasons[j]);
//
message += "\n" + ToString(j) + ": " + iReason;
}
}
else if (executedSignalsCount < managedSignalsCount)
{
//
int diff = managedSignalsCount - executedSignalsCount;
//
message = "Failed to Execute (" + ToString(diff) + ") Signals ...";
}
else if (executedSignalsCount == managedSignalsCount)
{
message = "(" + ToString(executedSignalsCount) + ") Signals Executed Successfully ...";
}
//
if (ArraySize(notExecuteds) > 0)
{
//
ManageUnExecutedSignals(
reasons,
notExecuteds //
);
}
//
if (IsValid(message))
{
mAlert.Alert(message);
}
}
//
@@ -648,17 +594,139 @@ protected:
double mDrawdownPercent;
double mStaticBalanceForCalculateDrawdown;
//
// Calculate Max Drawdown and it's Percent ...
void CalculateMaxDrawdown()
{
//
int positionsCount = PositionsTotal();
if (IsValidSize(positionsCount))
{
double mEquity = mTrader.mAccount.GetEquity();
if (mEquity > mMaxDrawdown)
{
mMaxDrawdown = mEquity;
}
//
mDrawdownPercent = (mMaxDrawdown - mEquity) / (mStaticBalanceForCalculateDrawdown > 0 ? mStaticBalanceForCalculateDrawdown : mMaxDrawdown) * 100;
mDrawdownPercent = NormalizeDouble(mDrawdownPercent, 3);
}
else
{
//
mMaxDrawdown = 0.0;
mDrawdownPercent = 0.0;
}
}
//
//
// Tools ...
//
// Since we need to Calls Some Event Handlers on
// - Signal Execution;
// - Modified;
// - Closed;
// - Partial Closed;
// events ...
// we have to implement thouse Methods here and use them ...
//
// Execute Stand alone Signal ...
bool ExecuteSignal(
XSignal &signal, // Signal
ENUM_X_SIGNAL_EXECUTION_RESULT &state, // Result
bool checkPolicies = true, // Check Account Policies
ENUM_ORDER_TYPE_TIME lifetime = ORDER_TIME_GTC, // Order LifeTime
datetime expiration = NULL // order expiration time
)
{
//
bool result = false;
//
result = mTrader.ExecuteSignal(
signal,
state,
lifetime,
expiration,
checkPolicies //
);
if (result)
{
//
OnSignalExecuted(signal);
//
string message = "Signal Executed (" + ToString(signal.positionId) + ") Successfully ...";
//
Alert(message);
}
else
{
//
OnSignalExecutionFailed(signal, state);
//
string message = "Signal Execution Failed: " + ToString(state) + " ...";
//
Alert(message);
}
//
return result;
}
//
// Execute a Collection of Signals ...
int ExecuteSignals(
XSignal &signals[], // Collection of Signal for Execution
ENUM_X_SIGNAL_EXECUTION_RESULT &states[], // Signal Execution Result
bool checkPolicies = true, // Check Account Policies
ENUM_ORDER_TYPE_TIME lifetime = ORDER_TIME_GTC, // Order LifeTime
datetime expiration = NULL // order expiration time
)
{
//
int result = 0;
//
int signalsCount = ArraySize(signals);
if (!IsValidSize(signalsCount))
{
return result;
}
//
Clean(states);
ArrayResize(states, signalsCount, X_SIGNAL_EXECUTION_UNKNOWN);
//
for (int i = 0; i < signalsCount; i++)
{
//
bool isExecuted = ExecuteSignal(
signals[i],
states[i],
checkPolicies,
lifetime,
expiration //
);
if (isExecuted)
{
result++;
}
}
//
return result;
}
//
// Modify ...
bool Modify(
@@ -692,8 +760,7 @@ protected:
{
//
OnPositionModified(
ticket,
position.profit,
position,
comment //
);
@@ -737,7 +804,6 @@ protected:
{
//
OnPositionClosed(
ticket,
position,
comment //
);
@@ -824,10 +890,15 @@ protected:
{
//
OnPositionPartialyClosed(
ticket,
position.profit,
position,
comment //
);
//
string message = "Position " + ToString(ticket) + " Successfully Closed Parital ...";
//
Alert(message);
}
//
@@ -1483,9 +1554,10 @@ protected:
//
ENUM_X_SIGNAL_EXECUTION_RESULT reason = NULL;
bool isExecuted = mTrader.ExecuteSignal(
bool isExecuted = ExecuteSignal(
support,
reason);
reason //
);
//
string comment = NULL;