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
@@ -30,35 +30,6 @@
// //
// Definitions ... // Definitions ...
//
class X121SetupCycleScoreTracker : public XSCBase
{
//
// Public ...
public:
//
// Define Bullish and Bearish Score ...
CArrayObj bullishScore;
CArrayObj bearishScore;
//
// Tools ...
void AddScores(
double bullScore,
double bearScore //
) {
//
// bullishScore
}
//
// Protected ...
protected:
//
// Private ...
private:
};
// //
// Inputs ... // Inputs ...
struct X121SetupCycleInputs struct X121SetupCycleInputs
File diff suppressed because it is too large Load Diff
+154 -82
View File
@@ -284,15 +284,7 @@ public:
virtual void OnTick() virtual void OnTick()
{ {
// //
double mEquity = mTrader.mAccount.GetEquity(); CalculateMaxDrawdown();
if (mEquity > mMaxDrawdown)
{
mMaxDrawdown = mEquity;
}
//
mDrawdownPercent = (mMaxDrawdown - mEquity) / (mStaticBalanceForCalculateDrawdown > 0 ? mStaticBalanceForCalculateDrawdown : mMaxDrawdown) * 100;
mDrawdownPercent = NormalizeDouble(mDrawdownPercent, 3);
// //
// Draw ... // Draw ...
@@ -341,11 +333,26 @@ public:
HandleSignalsExecution(signals); 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 ... // Calls when a Position Modified ...
virtual void OnPositionModified( virtual void OnPositionModified(
const ulong ticket, const XPosition &position,
const double profit,
const string comment // const string comment //
) )
{ {
@@ -354,7 +361,6 @@ public:
// //
// Calls when a Position Closed ... // Calls when a Position Closed ...
virtual void OnPositionClosed( virtual void OnPositionClosed(
const ulong ticket,
const XPosition &position, const XPosition &position,
const string comment // const string comment //
) )
@@ -364,8 +370,7 @@ public:
// //
// Calls when a Position Partially Closed ... // Calls when a Position Partially Closed ...
virtual void OnPositionPartialyClosed( virtual void OnPositionPartialyClosed(
const ulong ticket, const XPosition &position,
const double profit,
const string comment // const string comment //
) )
{ {
@@ -535,15 +540,6 @@ public:
return 0; return 0;
} }
//
// When Some Errors happens in Executing Signals ...
virtual void ManageUnExecutedSignals(
ENUM_X_SIGNAL_EXECUTION_RESULT &states[],
XSignal &unExecutedSignals[] //
)
{
}
// //
// Execute Provided Signals ... // Execute Provided Signals ...
virtual void HandleSignalsExecution(XSignal &signals[]) virtual void HandleSignalsExecution(XSignal &signals[])
@@ -566,66 +562,16 @@ public:
// Notify Signal Event Handlers for Reciving New Signal ... // Notify Signal Event Handlers for Reciving New Signal ...
for (int i = 0; i < managedSignalsCount; i++) for (int i = 0; i < managedSignalsCount; i++)
{ {
//
NotifyOnSignalEventHandlers(signals[i]); NotifyOnSignalEventHandlers(signals[i]);
} }
// //
// Try to Execute Signals ... // Try to Execute Signals ...
XSignal notExecuteds[];
ENUM_X_SIGNAL_EXECUTION_RESULT reasons[]; ENUM_X_SIGNAL_EXECUTION_RESULT reasons[];
int executedSignalsCount = mTrader.ExecuteSpecifiedSignals( int executedSignalsCount = ExecuteSignals(
signals, 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 mDrawdownPercent;
double mStaticBalanceForCalculateDrawdown; 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 ... // Tools ...
// //
// Since we need to Calls Some Event Handlers on // Since we need to Calls Some Event Handlers on
// - Signal Execution;
// - Modified; // - Modified;
// - Closed; // - Closed;
// - Partial Closed; // - Partial Closed;
// events ... // events ...
// we have to implement thouse Methods here and use them ... // 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 ... // Modify ...
bool Modify( bool Modify(
@@ -692,8 +760,7 @@ protected:
{ {
// //
OnPositionModified( OnPositionModified(
ticket, position,
position.profit,
comment // comment //
); );
@@ -737,7 +804,6 @@ protected:
{ {
// //
OnPositionClosed( OnPositionClosed(
ticket,
position, position,
comment // comment //
); );
@@ -824,10 +890,15 @@ protected:
{ {
// //
OnPositionPartialyClosed( OnPositionPartialyClosed(
ticket, position,
position.profit,
comment // comment //
); );
//
string message = "Position " + ToString(ticket) + " Successfully Closed Parital ...";
//
Alert(message);
} }
// //
@@ -1483,9 +1554,10 @@ protected:
// //
ENUM_X_SIGNAL_EXECUTION_RESULT reason = NULL; ENUM_X_SIGNAL_EXECUTION_RESULT reason = NULL;
bool isExecuted = mTrader.ExecuteSignal( bool isExecuted = ExecuteSignal(
support, support,
reason); reason //
);
// //
string comment = NULL; string comment = NULL;
+423
View File
@@ -0,0 +1,423 @@
//
// Hind Signals ...
if (hSignal.IsValid())
{
//
bool isLong = IsLong(hSignal.type);
if (isLong)
{
mBullishSigns += hPusher;
}
else
{
mBearishSigns += hPusher;
}
//
if (hPusher >= mReuiredSignalVerifications)
{
//
X121SetupConditions otherConditions[];
AddRef(
cConditions,
otherConditions //
);
AddRef(
sConditions,
otherConditions //
);
AddRef(
mConditions,
otherConditions //
);
AddRef(
lConditions,
otherConditions //
);
//
// Check Final Validations ...
bool isValid =
//
(
//
cHasSignal
//
||
//
sHasSignal
//
||
//
mHasSignal
//
||
//
lHasSignal
//
) //
||
//
(
//
!mGetVerificationFromOtherTimeFrames
? true
: isLong
? signalGenerator.HasLongVerifications(otherConditions)
: signalGenerator.HasShortVerifications(otherConditions)
//
)
//
;
//
if (isValid)
{
//
AddRef(
hSignal,
signals //
);
}
}
}
else
//
// Long Signals ...
if (lSignal.IsValid())
{
//
bool isLong = IsLong(lSignal.type);
if (isLong)
{
mBullishSigns += lPusher;
}
else
{
mBearishSigns += lPusher;
}
//
if (lPusher >= mReuiredSignalVerifications)
{
//
X121SetupConditions otherConditions[];
AddRef(
cConditions,
otherConditions //
);
AddRef(
sConditions,
otherConditions //
);
AddRef(
mConditions,
otherConditions //
);
AddRef(
hConditions,
otherConditions //
);
//
// Check Final Validations ...
bool isValid =
//
(
//
cHasSignal
//
||
//
sHasSignal
//
||
//
mHasSignal
//
||
//
hHasSignal
//
)
//
||
//
(
//
!mGetVerificationFromOtherTimeFrames
? true
: isLong
? signalGenerator.HasLongVerifications(otherConditions)
: signalGenerator.HasShortVerifications(otherConditions)
//
)
//
;
//
if (isValid)
{
//
AddRef(
lSignal,
signals //
);
}
}
}
else
//
// Medium Signals ...
if (mSignal.IsValid())
{
//
bool isLong = IsLong(mSignal.type);
if (isLong)
{
mBullishSigns += mPusher;
}
else
{
mBearishSigns += mPusher;
}
//
if (mPusher >= mReuiredSignalVerifications)
{
//
X121SetupConditions otherConditions[];
AddRef(
cConditions,
otherConditions //
);
AddRef(
sConditions,
otherConditions //
);
AddRef(
lConditions,
otherConditions //
);
AddRef(
hConditions,
otherConditions //
);
//
// Check Final Validations ...
bool isValid =
//
(
//
cHasSignal
//
||
//
sHasSignal
//
||
//
lHasSignal
//
||
//
hHasSignal
//
)
//
||
//
(
//
!mGetVerificationFromOtherTimeFrames
? true
: isLong
? signalGenerator.HasLongVerifications(otherConditions)
: signalGenerator.HasShortVerifications(otherConditions)
//
)
//
;
//
if (isValid)
{
//
AddRef(
mSignal,
signals //
);
}
}
}
else
//
// Short Signals ...
if (sSignal.IsValid())
{
//
bool isLong = IsLong(sSignal.type);
if (isLong)
{
mBullishSigns += sPusher;
}
else
{
mBearishSigns += sPusher;
}
//
if (sPusher >= mReuiredSignalVerifications)
{
//
X121SetupConditions otherConditions[];
AddRef(
cConditions,
otherConditions //
);
AddRef(
mConditions,
otherConditions //
);
AddRef(
lConditions,
otherConditions //
);
AddRef(
hConditions,
otherConditions //
);
//
// Check Final Validations ...
bool isValid =
//
(
//
cHasSignal
//
||
//
mHasSignal
//
||
//
lHasSignal
//
||
//
hHasSignal
//
)
//
||
//
(
//
!mGetVerificationFromOtherTimeFrames
? true
: isLong
? signalGenerator.HasLongVerifications(otherConditions)
: signalGenerator.HasShortVerifications(otherConditions)
//
)
//
;
//
if (isValid)
{
//
AddRef(
sSignal,
signals //
);
}
}
}
else
//
// Current Signals ...
if (cSignal.IsValid())
{
//
bool isLong = IsLong(cSignal.type);
if (isLong)
{
mBullishSigns += cPusher;
}
else
{
mBearishSigns += cPusher;
}
//
if (cPusher >= mReuiredSignalVerifications)
{
//
X121SetupConditions otherConditions[];
AddRef(
sConditions,
otherConditions //
);
AddRef(
mConditions,
otherConditions //
);
AddRef(
lConditions,
otherConditions //
);
AddRef(
hConditions,
otherConditions //
);
//
// Check Final Validations ...
bool isValid =
//
(
//
sHasSignal
//
||
//
mHasSignal
//
||
//
lHasSignal
//
||
//
hHasSignal
//
)
//
||
//
(
//
!mGetVerificationFromOtherTimeFrames
? true
: isLong
? signalGenerator.HasLongVerifications(otherConditions)
: signalGenerator.HasShortVerifications(otherConditions)
//
)
//
;
//
if (isValid)
{
//
AddRef(
cSignal,
signals //
);
}
}
}
+20 -17
View File
@@ -42,27 +42,27 @@ input group "Signals";
input string x121EASymbols = "EURUSDb"; // Symbols input string x121EASymbols = "EURUSDb"; // Symbols
input bool x121EAAllowLong = true; // Allow Long Trades input bool x121EAAllowLong = true; // Allow Long Trades
input bool x121EAAllowShort = true; // Allow Short Trades input bool x121EAAllowShort = true; // Allow Short Trades
input int x121EAReuiredSignalVerifications = 3; // Required Verifications for Signals input int x121EAReuiredSignalVerifications = 4; // Required Verifications for Signals
input bool x121EAGetVerificationFromOtherTimeFrames = true; // Get Verifications from Other Time Frames input bool x121EAGetVerificationFromOtherTimeFrames = true; // Get Verifications from Other Time Frames
// //
// Risk Management ... // Risk Management ...
input group "Risk Management"; input group "Risk Management";
input double x121EAVolume = 0.01; // Static Volume input double x121EAVolume = 0.01; // Static Volume
input double x121EATPPoint = 60; // TP Point input double x121EATPPoint = 60; // TP Point
input double x121EASLPoint = 300; // SL Point input double x121EASLPoint = 300; // SL Point
input int x121EAMaxAllowedTrades = 1; // Max Allowed Positions input int x121EAMaxAllowedTrades = 1; // Max Allowed Positions
input bool x121EAIgnoreSL = true; // Ignore Calculated SL input bool x121EAIgnoreSL = true; // Ignore Calculated SL
input bool x121EAIgnoreTP = false; // Ignore Calculated TP input bool x121EAIgnoreTP = false; // Ignore Calculated TP
input bool x121EAUseSupport = false; // Use Support Signals input bool x121EAUseSupport = false; // Use Support Signals
input bool x121EAUseGrid = true; // Use Grid Signals input bool x121EAAllowGrid = true; // Allow Grid Signals
input double x121EAGridDistance = 50; // Grid Distance input double x121EAGridDistance = 100; // Grid Distance
input double x121EAGridVolumeMultiplier = 2; // Grid Volume Multiplier input double x121EAGridVolumeMultiplier = 2; // Grid Volume Multiplier
input double x121EAMinRequiredProfitPerTrade = 1; // Minimum Required Profit for Hedging input double x121EAMinRequiredProfitPerTrade = 0.5; // Minimum Required Profit for Hedging
input double x121EAMinRequiredProfitPerVolumeFactor = 0.1; // Minimum Required Profit for Hedging Per Volume input double x121EAMinRequiredProfitPerVolumeFactor = 0.01; // Minimum Required Profit for Hedging Per Volume
input int x121EARestingAfterHedge = 0; // Resting Seconds After Hedge input int x121EARestingAfterHedge = 300; // Resting Seconds After Hedge
input int x121EACloseOnSpecificTime = -1; // Close All Trades in Specific Time input int x121EACloseOnSpecificTime = -1; // Close All Trades in Specific Time
input bool x121EACloseOnOpposit = false; // Close all Positions on Opposit input bool x121EACloseOnOpposit = false; // Close all Positions on Opposit
// //
// Alert ... // Alert ...
@@ -187,8 +187,11 @@ bool InitialEA()
x121EA.GetVerificationFromOtherTimeFrames(x121EAGetVerificationFromOtherTimeFrames); x121EA.GetVerificationFromOtherTimeFrames(x121EAGetVerificationFromOtherTimeFrames);
// //
x121EA.UseGrid(x121EAUseGrid);
x121EA.UseSupport(x121EAUseSupport); x121EA.UseSupport(x121EAUseSupport);
//
// Grid System ...
x121EA.AllowGrid(x121EAAllowGrid);
x121EA.GridDistance(x121EAGridDistance); x121EA.GridDistance(x121EAGridDistance);
x121EA.GridVolumeMultiplier(x121EAGridVolumeMultiplier); x121EA.GridVolumeMultiplier(x121EAGridVolumeMultiplier);
+57 -5
View File
@@ -154,6 +154,46 @@ enum ENUM_X_POSITION_TYPE
X_POSITION_TYPE_SHORT = 3, X_POSITION_TYPE_SHORT = 3,
}; };
//
string ToString(ENUM_X_POSITION_TYPE value)
{
//
string result = NULL;
//
switch (value)
{
//
case X_POSITION_TYPE_ALL:
result = "All";
break;
//
case X_POSITION_TYPE_NONE:
result = "NONE";
break;
//
case X_POSITION_TYPE_SHORT:
result = "Short";
break;
//
case X_POSITION_TYPE_LONG:
result = "Long";
break;
}
//
return result;
}
//
bool IsLong(ENUM_X_POSITION_TYPE value)
{
return value == X_POSITION_TYPE_LONG;
}
// //
bool ToPositinType( bool ToPositinType(
ENUM_POSITION_TYPE &dest, ENUM_POSITION_TYPE &dest,
@@ -182,7 +222,15 @@ bool ToPositinType(
} }
// //
ENUM_X_POSITION_TYPE GetOppositXPositionType(ENUM_POSITION_TYPE type) ENUM_X_POSITION_TYPE ToPositionType(ENUM_POSITION_TYPE value)
{
return IsLong(value)
? X_POSITION_TYPE_LONG
: X_POSITION_TYPE_SHORT;
}
//
ENUM_X_POSITION_TYPE GetOppositPositionType(ENUM_POSITION_TYPE type)
{ {
// //
ENUM_X_POSITION_TYPE result = X_POSITION_TYPE_NONE; ENUM_X_POSITION_TYPE result = X_POSITION_TYPE_NONE;
@@ -4040,7 +4088,8 @@ ulong ExtractSupportedTicket(string value)
int ExtractSupportPositions( int ExtractSupportPositions(
XPosition &source[], XPosition &source[],
XPosition &dest[] // Result ... XPosition &dest[] // Result ...
) { )
{
// //
int result = 0; int result = 0;
@@ -4049,18 +4098,21 @@ int ExtractSupportPositions(
// //
int sourceCount = ArraySize(source); int sourceCount = ArraySize(source);
if (!IsValidSize(sourceCount)) { if (!IsValidSize(sourceCount))
{
return result; return result;
} }
// //
for (int i = 0; i < sourceCount; i++) { for (int i = 0; i < sourceCount; i++)
{
// //
XPosition iSource = source[i]; XPosition iSource = source[i];
// //
ulong iParentTicket = ExtractSupportedTicket(iSource.comment); ulong iParentTicket = ExtractSupportedTicket(iSource.comment);
if (iParentTicket > 0) { if (iParentTicket > 0)
{
// //
AddRef( AddRef(
iSource, iSource,