This commit is contained in:
2024-06-21 09:18:04 +03:30
parent a5cb716d0b
commit e54a991b6d
4 changed files with 554 additions and 335 deletions
+138 -139
View File
@@ -2810,13 +2810,13 @@ struct X121SignalGenerator
// //
// Generate Support Signals ... // Generate Support Signals ...
if (_useSupport) // if (_useSupport)
{ // {
// // //
GenerateSupportSignals( // GenerateSupportSignals(
signal // // signal //
); // );
} // }
// //
return result; return result;
@@ -2827,11 +2827,52 @@ struct X121SignalGenerator
XSignal &signal // Source Signal for Support Generating ... XSignal &signal // Source Signal for Support Generating ...
) )
{ {
//
double pivots[];
int pivotsCount = GenerateSupportPivots(
signal,
pivots //
);
if (!IsValidSize(pivotsCount))
{
return;
}
//
XSignal supports[];
int indirectionalsCount =
PrepareInDirectionalSupportSignals(
supports,
signal,
pivots //
);
if (IsValidSize(indirectionalsCount))
{
//
Copy(
supports,
signal.supports //
);
//
Clean(supports);
}
}
//
int GenerateSupportPivots(
XSignal &signal, // Source Signal for Support Generating ...
double &pivots[] //
)
{
//
int result = 0;
// //
// Validate Signal ... // Validate Signal ...
if (!signal.IsValid()) if (!signal.IsValid())
{ {
return; return result;
} }
// //
@@ -2875,6 +2916,7 @@ struct X121SignalGenerator
); );
// //
Clean(pivots);
double tmpPivots[]; double tmpPivots[];
// //
@@ -2999,27 +3041,44 @@ struct X121SignalGenerator
} }
// //
XSignal supports[]; int tmpPivotsCount = ArraySize(tmpPivots);
int indirectionalsCount = if (!IsValidSize(tmpPivotsCount))
PrepareInDirectionalSupportSignals(
supports,
signal,
tmpPivots //
);
if (IsValidSize(indirectionalsCount))
{ {
// return result;
Copy(
supports,
signal.supports //
);
//
Clean(supports);
} }
//
for (int i = 0; i < tmpPivotsCount; i++)
{
//
double iPivot = tmpPivots[i];
//
bool isValidPivot =
isLong
? signal.entry > iPivot
: signal.entry < iPivot;
if (isValidPivot)
{
//
Add(
iPivot,
pivots //
);
}
}
//
ArrayFree(tmpPivots);
// //
RemoveDraws(entryLineName); RemoveDraws(entryLineName);
//
result = ArraySize(pivots);
//
return result;
} }
// //
@@ -3043,77 +3102,71 @@ struct X121SignalGenerator
} }
// //
bool isDirectional = support.provider == XEQMSupportToken + XDirectionalToken; mResult = support.provider == XEQMSupportToken + XINDirectionalToken;
if (!mResult)
{
return mResult;
}
// //
if (!isDirectional) // Indirectional Supports Updated Using Directional ...
//
double entryPrice = GetEntry(
parent.symbol,
parent.type //
);
//
ENUM_X_ORDER_MODES rMode = X_ORDER_MODE_NOTHING;
bool isParentLong = IsLong(parent.type);
if (isParentLong)
{ {
// //
// Indirectional Supports Updated Using Directional ... if (entryPrice < parent.entry)
//
double entryPrice = GetEntry(
parent.symbol,
parent.type //
);
//
ENUM_X_ORDER_MODES rMode = X_ORDER_MODE_NOTHING;
bool isParentLong = IsLong(parent.type);
if (isParentLong)
{ {
// rMode = X_ORDER_MODE_STOP;
if (entryPrice < parent.entry)
{
rMode = X_ORDER_MODE_STOP;
}
else if (entryPrice > parent.entry)
{
rMode = X_ORDER_MODE_LIMIT;
}
}
else
{
//
if (entryPrice < parent.entry)
{
rMode = X_ORDER_MODE_LIMIT;
}
else if (entryPrice > parent.entry)
{
rMode = X_ORDER_MODE_STOP;
}
}
//
if (rMode != X_ORDER_MODE_NOTHING)
{
//
mResult = result.Prepare(
parent.symbol,
XEQMSupportToken + XDirectionalToken,
parent.period,
parent.type,
rMode,
parent.entry,
parent.volume,
0,
0 //
);
}
//
if (mResult)
{
//
string comment = GenerateSupportTag(parent.ticket);
result.comment = comment;
} }
} }
else else
{ {
// //
// TODO: Implement ... if (entryPrice > parent.entry)
{
rMode = X_ORDER_MODE_STOP;
}
}
//
mResult = rMode != X_ORDER_MODE_NOTHING;
if (!mResult)
{
return mResult;
}
//
mResult = result.Prepare(
parent.symbol,
XEQMSupportToken + XDirectionalToken,
parent.period,
parent.type,
rMode,
parent.entry,
parent.volume,
0,
0 //
);
//
if (mResult)
{
//
string comment = GenerateSupportTag(parent.ticket);
result.comment = comment;
}
else
{
result.Clean();
} }
// //
@@ -3232,60 +3285,6 @@ struct X121SignalGenerator
rTP = 0; rTP = 0;
rEntry = 0; rEntry = 0;
isSignalPrepared = false; isSignalPrepared = false;
//
// Here we Create Related Directional Order ...
//
// Directional ...
if (isLong)
{
//
rType = POSITION_TYPE_BUY;
rMode = X_ORDER_MODE_LIMIT;
}
else
{
//
rType = POSITION_TYPE_SELL;
rMode = X_ORDER_MODE_LIMIT;
}
//
rEntry = source.entry;
rVolume = source.volume;
//
isSignalPrepared = rSignal.Prepare(
source.symbol,
XEQMSupportToken + XDirectionalToken,
source.period,
rType,
rMode,
rEntry,
rVolume,
rSL,
rTP //
);
//
if (isSignalPrepared)
{
//
AddRef(
rSignal,
supports //
);
//
rSignal.Clean();
//
rSL = 0;
rTP = 0;
rEntry = 0;
isSignalPrepared = false;
}
} }
} }
+393 -173
View File
@@ -116,6 +116,7 @@ struct XTradeInfo
// //
ResetGrid(); ResetGrid();
ResetSupport();
ResetRecovery(); ResetRecovery();
} }
@@ -181,6 +182,15 @@ struct XTradeInfo
bullishScore = _bullishScore; bullishScore = _bullishScore;
bearishScore = _bearishScore; bearishScore = _bearishScore;
//
X121SignalGenerator generator;
XSignal tSignal = _signal;
generator.GenerateSupportPivots(
tSignal,
pivots //
);
tSignal.Clean();
// //
return result; return result;
} }
@@ -427,6 +437,183 @@ struct XTradeInfo
// These are Some Helpers for Trading Controls and do not relate to Main // These are Some Helpers for Trading Controls and do not relate to Main
// purpose of Trade Handler ... // purpose of Trade Handler ...
//
// Supporting ...
//
double pivots[];
//
void ResetSupport()
{
Clean(pivots);
}
//
// Retrieve Support Signal ...
bool HasSupport(
XSignal &support //
)
{
//
bool result = false;
//
result = IsValid();
if (!result)
{
return result;
}
//
int pivotsCount = ArraySize(pivots);
result = IsValidSize(pivotsCount);
if (!result)
{
return result;
}
//
support.Clean();
//
ENUM_POSITION_TYPE sType =
type == X_POSITION_TYPE_LONG
? POSITION_TYPE_BUY
: POSITION_TYPE_SELL;
//
bool isLong = IsLong(type);
//
ENUM_POSITION_TYPE cType = GetOpposit(sType);
double cEntry = GetEntry(
symbol,
cType //
);
//
double pivot = 0;
int pivotIndex = -1;
for (int i = 0; i < pivotsCount; i++)
{
//
double iPivot = pivots[i];
if (iPivot <= 0)
{
continue;
}
//
result =
isLong
? cEntry < entry && cEntry <= iPivot
: cEntry > entry && cEntry >= iPivot;
if (result)
{
//
pivot = iPivot;
pivotIndex = i;
break;
}
}
//
if (result)
{
//
result = support.Prepare(
symbol,
XEQMSupportToken + XINDirectionalToken,
period,
cType,
X_ORDER_MODE_MARKET,
cEntry,
volume * 2,
0,
0 //
);
if (result)
{
//
string comment = GenerateSupportTag(ticket);
support.comment = comment;
//
// Prepare Directional Support ...
XSignal mSup;
result = mSup.Prepare(
symbol,
XEQMSupportToken + XDirectionalToken,
period,
sType,
X_ORDER_MODE_STOP,
entry,
volume,
0,
0 //
);
if (result)
{
//
string comment = GenerateSupportTag(ticket);
mSup.comment = comment;
//
AddRef(
mSup,
support.supports //
);
//
result = ArrayRemove(
pivots,
pivotIndex,
1 //
);
}
}
}
//
if (!result)
{
support.Clean();
}
//
return result;
}
//
bool RemovePivot(
double pivot,
int index //
)
{
//
bool result = false;
//
result = IsValid() &&
IsValidIndex(index) &&
ArraySize(pivots) > index;
if (!result)
{
return result;
}
//
result = ArrayRemove(
pivots,
index,
1 //
);
//
return result;
}
// //
// Grid System Calculations ... // Grid System Calculations ...
@@ -1860,8 +2047,7 @@ public:
} }
// //
// Supporting ... if (AllowSupport())
if (EnableSupport())
{ {
// //
XSignal supports[]; XSignal supports[];
@@ -2148,10 +2334,6 @@ protected:
// //
int result = 0; int result = 0;
//
// TODO: Complete this Later ...
return result;
// //
Clean(signals); Clean(signals);
@@ -2165,104 +2347,137 @@ protected:
Sync(); Sync();
// //
// Validate Setups Count ... int count = Count();
int setupsCount = ArraySize(setups); if (!IsValidSize(count))
if (!IsValidSize(setupsCount))
{ {
return result; return result;
} }
// //
// Implement Supporting Mechanism ... for (int i = 0; i < count; i++)
//
// Extracting In Drawdown Positions ...
XPosition positions[];
int positionsCount = mTrader.GetInDrawdownPositions(
positions //
);
if (!IsValidSize(positionsCount))
{
return result;
}
//
double supportDistance = SupportDistance();
int maxNUmberOfSupports = MaxNumberOfSupports();
double maxAllowedVolume = MaxAllowedSupportVolume();
double volumeMultiplier = SupportVolumeMultiplier();
//
X121SignalGenerator signalGenerator;
//
for (int i = 0; i < positionsCount; i++)
{ {
// //
XPosition iPosition = positions[i]; XSignal iSupport;
double iPivot = -1;
// int iPivotIDX = -1;
// Check Position is not Support Position ... bool hasSupport = data[i].HasSupport(
bool isSupport = IsSupport(iPosition.comment); iSupport //
if (isSupport)
{
continue;
}
//
// Find Related Setp ...
int idx = FindSetupIndex(
iPosition.symbol,
setups //
); );
if (!IsValidIndex(idx)) if (hasSupport)
{ {
continue; //
AddRef(
iSupport,
signals //
);
} }
//
int index = FindIndex(iPosition.ticket);
if (!IsValidIndex(index))
{
continue;
}
//
// Check Position must atleast 20 Point in drawdown ...
//
double onePointPrice = PointToPrice(
1,
iPosition.symbol //
);
//
double iExitPrice = GetExit(
iPosition.symbol,
iPosition.type //
);
//
double priceDiff = MathAbs(iPosition.entry - iExitPrice);
double priceDiffPoints = priceDiff / onePointPrice;
//
bool isFarEnough = priceDiffPoints >= (supportDistance / 2);
if (!isFarEnough)
{
continue;
}
//
// Check Support COunts and etc ...
// implementing supporting mechanis ...
//
} }
// //
result = ArraySize(signals); result = ArraySize(signals);
//
// TODO: Complete this Later ...
return result;
//
// Validate Setups Count ...
// int setupsCount = ArraySize(setups);
// if (!IsValidSize(setupsCount))
// {
// return result;
// }
// //
// // Implement Supporting Mechanism ...
// //
// // Extracting In Drawdown Positions ...
// XPosition positions[];
// int positionsCount = mTrader.GetInDrawdownPositions(
// positions //
// );
// if (!IsValidSize(positionsCount))
// {
// return result;
// }
// //
// double supportDistance = SupportDistance();
// int maxNUmberOfSupports = MaxNumberOfSupports();
// double maxAllowedVolume = MaxAllowedSupportVolume();
// double volumeMultiplier = SupportVolumeMultiplier();
// //
// X121SignalGenerator signalGenerator;
// //
// for (int i = 0; i < positionsCount; i++)
// {
// //
// XPosition iPosition = positions[i];
// //
// // Check Position is not Support Position ...
// bool isSupport = IsSupport(iPosition.comment);
// if (isSupport)
// {
// continue;
// }
// //
// // Find Related Setp ...
// int idx = FindSetupIndex(
// iPosition.symbol,
// setups //
// );
// if (!IsValidIndex(idx))
// {
// continue;
// }
// //
// int index = FindIndex(iPosition.ticket);
// if (!IsValidIndex(index))
// {
// continue;
// }
// //
// // Check Position must atleast 20 Point in drawdown ...
// //
// double onePointPrice = PointToPrice(
// 1,
// iPosition.symbol //
// );
// //
// double iExitPrice = GetExit(
// iPosition.symbol,
// iPosition.type //
// );
// //
// double priceDiff = MathAbs(iPosition.entry - iExitPrice);
// double priceDiffPoints = priceDiff / onePointPrice;
// //
// bool isFarEnough = priceDiffPoints >= (supportDistance / 2);
// if (!isFarEnough)
// {
// continue;
// }
// //
// // Check Support COunts and etc ...
// // implementing supporting mechanis ...
// //
// }
// //
// result = ArraySize(signals);
// //
return result; return result;
} }
@@ -4029,10 +4244,10 @@ public:
} }
// //
// Handle Support of Supports here ... // Orders Changed ...
void OnPositionsChangedHandler(int count) override void OnOrdersChangedHandler(int count) override
{ {
// EQMHandleSupportProtection(count); EQMHandleSupportProtection(count);
} }
// //
@@ -4756,98 +4971,103 @@ private:
} }
// //
void EQMHandleSupportProtection(int positionsCount) void EQMHandleSupportProtection(int count)
{ {
// // //
if (!IsValidSize(positionsCount)) // if (IsValidSize(count))
{ // {
return; // return;
} // }
// // //
ulong ticket = mTrader.GetLastOpenPositionTicket(); // // This means on order Executed ...
XPosition position; // ulong ticket = mTrader.GetLastOpenPositionTicket();
bool hasPosition = mTrader.GetPosition(
ticket,
position //
);
if (!hasPosition)
{
return;
}
// // //
bool isSupport = IsSupport(position.comment); // XPosition position;
if (!isSupport) // bool hasPosition = mTrader.GetPosition(
{ // ticket,
return; // position //
} // );
// if (!hasPosition)
// {
// return;
// }
// // //
bool isINDIRSupport = position.provider == XEQMSupportToken + XINDirectionalToken; // bool isSupport = IsSupport(position.comment);
if (!isINDIRSupport) // if (!isSupport)
{ // {
return; // return;
} // }
// // //
ulong parentTicket = ExtractParentTicket(position.comment); // bool isInDirectional = position.provider == XEQMSupportToken + XINDirectionalToken;
XPosition parent; // if (!isInDirectional)
bool hasParent = mTrader.GetPosition( // {
parentTicket, // return;
parent // // }
);
if (!hasParent)
{
return;
}
// // //
// Remove Parent TP SL ... // XPosition parent;
if (parent.tp > 0 || parent.sl > 0) // ulong parentTicket = ExtractParentTicket(position.comment);
{ // bool hasParent = mTrader.GetPosition(
// // parentTicket,
string comment = XEQMSupportToken + "Remoove TPSL due Protect ..."; // parent //
bool isModeified = mTrader.Modify( // );
parentTicket,
0,
0,
comment //
);
if (isModeified)
{
//
string message = XEQMSupportToken + " Removes Position (" + ToString(parentTicket) + ")' TP/SL Due Start Supporting ...";
// // //
Alert(message); // if (!hasParent)
} // {
} // return;
// }
// // //
XSignal signal; // // Remove Parent TP SL ...
X121SignalGenerator signalGenerator; // if (parent.tp > 0 || parent.sl > 0)
bool hasSupport = signalGenerator.PrepareDirectionalSupportSignals( // {
position, // //
parent, // string comment = XEQMSupportToken + " Remoove TP/SL due Protect ...";
signal // // bool isModeified = mTrader.Modify(
); // parentTicket,
if (!hasSupport) // 0,
{ // 0,
return; // comment //
} // );
// if (isModeified)
// {
// //
// string message = XEQMSupportToken + " Removes Position (" + ToString(parentTicket) + ")' TP/SL Due Start Supporting ...";
// // //
XSignal signals[]; // Alert(message);
// }
// }
// // //
AddRef( // XSignal signal;
signal, // X121SignalGenerator signalGenerator;
signals // // bool hasSupport = signalGenerator.PrepareDirectionalSupportSignals(
); // position,
// parent,
// signal //
// );
// if (!hasSupport)
// {
// return;
// }
// // //
EQMExecuteSignals(signals); // XSignal signals[];
// //
// AddRef(
// signal,
// signals //
// );
// //
// EQMExecuteSignals(signals);
} }
// //
+22 -22
View File
@@ -497,11 +497,11 @@ public:
// this Method Calls For Retrieve Support Signal ... // this Method Calls For Retrieve Support Signal ...
// if it's Provided, Execute it ... // if it's Provided, Execute it ...
virtual bool RequestForSupport( virtual bool RequestForSupport(
XSignal &support, // Holds Support Signal, if Provided XSignal &support, // Holds Support Signal, if Provided
string provider, // Positions Provided by Specific Provider string provider, // Positions Provided by Specific Provider
string symbol, // Trading Symbol string symbol, // Trading Symbol
ENUM_X_POSITION_TYPES type, // Trading Type ENUM_X_POSITION_TYPES type, // Trading Type
ENUM_TIMEFRAMES period // Specified Time Frame ENUM_TIMEFRAMES period // Specified Time Frame
) )
{ {
return false; return false;
@@ -1091,7 +1091,7 @@ protected:
// //
XPosition positions[]; XPosition positions[];
int positionsCount = mTrader.GetPositions(positions); int positionsCount = mTrader.GetPositions(positions);
result = IsValidSize(positions); result = positionsCount > 1;
if (!result) if (!result)
{ {
return result; return result;
@@ -1264,7 +1264,7 @@ protected:
void HandleGuardHedgeAction( void HandleGuardHedgeAction(
string provider, // Positions Provided by Specific Provider string provider, // Positions Provided by Specific Provider
string symbol, // Trading Symbol string symbol, // Trading Symbol
ENUM_X_POSITION_TYPES type, // Trading Type ENUM_X_POSITION_TYPES type, // Trading Type
ENUM_TIMEFRAMES period, // Specified Time Frame ENUM_TIMEFRAMES period, // Specified Time Frame
double minProfitForHedging = 1, // Specified Profit for Hedge double minProfitForHedging = 1, // Specified Profit for Hedge
double minProfitForHedgingVolumeFactor = 0.01 // Specified Volume Factor for Hedge double minProfitForHedgingVolumeFactor = 0.01 // Specified Volume Factor for Hedge
@@ -1323,7 +1323,7 @@ protected:
void HandleGuardCloseInProfitAction( void HandleGuardCloseInProfitAction(
string provider, // Positions Provided by Specific Provider string provider, // Positions Provided by Specific Provider
string symbol, // Trading Symbol string symbol, // Trading Symbol
ENUM_X_POSITION_TYPES type, // Trading Type ENUM_X_POSITION_TYPES type, // Trading Type
ENUM_TIMEFRAMES period, // Specified Time Frame ENUM_TIMEFRAMES period, // Specified Time Frame
ENUM_X_POSITION_SELECT_METHODS method = NULL // Select Method ENUM_X_POSITION_SELECT_METHODS method = NULL // Select Method
) )
@@ -1365,7 +1365,7 @@ protected:
void HandleGuardCloseInDrawdownAction( void HandleGuardCloseInDrawdownAction(
string provider, // Positions Provided by Specific Provider string provider, // Positions Provided by Specific Provider
string symbol, // Trading Symbol string symbol, // Trading Symbol
ENUM_X_POSITION_TYPES type, // Trading Type ENUM_X_POSITION_TYPES type, // Trading Type
ENUM_TIMEFRAMES period, // Specified Time Frame ENUM_TIMEFRAMES period, // Specified Time Frame
ENUM_X_POSITION_SELECT_METHODS method = NULL // Select Method ENUM_X_POSITION_SELECT_METHODS method = NULL // Select Method
) )
@@ -1405,11 +1405,11 @@ protected:
// //
// Force Close Specified Set Of Position ... // Force Close Specified Set Of Position ...
void HandleGuardForceCloseAction( void HandleGuardForceCloseAction(
string provider, // Positions Provided by Specific Provider string provider, // Positions Provided by Specific Provider
string symbol, // Trading Symbol string symbol, // Trading Symbol
ENUM_X_POSITION_TYPES type, // Trading Type ENUM_X_POSITION_TYPES type, // Trading Type
ENUM_TIMEFRAMES period, // Specified Time Frame ENUM_TIMEFRAMES period, // Specified Time Frame
ulong ticket = 0 // Specific Ticket ulong ticket = 0 // Specific Ticket
) )
{ {
// //
@@ -1482,11 +1482,11 @@ protected:
// //
// Trailing Specified Set Of Positions Stop Losses ... // Trailing Specified Set Of Positions Stop Losses ...
void HandleGuardTrailingStopAction( void HandleGuardTrailingStopAction(
string provider, // Positions Provided by Specific Provider string provider, // Positions Provided by Specific Provider
string symbol, // Trading Symbol string symbol, // Trading Symbol
ENUM_X_POSITION_TYPES type, // Trading Type ENUM_X_POSITION_TYPES type, // Trading Type
ENUM_TIMEFRAMES period, // Specified Time Frame ENUM_TIMEFRAMES period, // Specified Time Frame
double sl // Desired SL Value ... double sl // Desired SL Value ...
) )
{ {
// //
@@ -1560,10 +1560,10 @@ protected:
// //
// Add Support Signal for Specified Positions ... // Add Support Signal for Specified Positions ...
void HandleGuardAddSupportPositionAction( void HandleGuardAddSupportPositionAction(
string provider, // Positions Provided by Specific Provider string provider, // Positions Provided by Specific Provider
string symbol, // Trading Symbol string symbol, // Trading Symbol
ENUM_X_POSITION_TYPES type, // Trading Type ENUM_X_POSITION_TYPES type, // Trading Type
ENUM_TIMEFRAMES period // Specified Time Frame ENUM_TIMEFRAMES period // Specified Time Frame
) )
{ {
// //
@@ -1614,10 +1614,10 @@ protected:
// //
// Cancel all Placed Orders ... // Cancel all Placed Orders ...
void HandleGuardCancelPendingOrdersAction( void HandleGuardCancelPendingOrdersAction(
string provider, // Positions Provided by Specific Provider string provider, // Positions Provided by Specific Provider
string symbol, // Trading Symbol string symbol, // Trading Symbol
ENUM_X_POSITION_TYPES type, // Trading Type ENUM_X_POSITION_TYPES type, // Trading Type
ENUM_TIMEFRAMES period // Specified Time Frame ENUM_TIMEFRAMES period // Specified Time Frame
) )
{ {
// //
+1 -1
View File
@@ -202,7 +202,7 @@ bool IsLong(ENUM_X_POSITION_TYPES value)
} }
// //
bool ToPositinType( bool ToPositionType(
ENUM_POSITION_TYPE &dest, ENUM_POSITION_TYPE &dest,
ENUM_X_POSITION_TYPES source // ENUM_X_POSITION_TYPES source //
) )