This commit is contained in:
2025-05-30 23:37:27 +03:30
parent 1e002ced49
commit 16c58af75c
9 changed files with 5676 additions and 4503 deletions
-814
View File
@@ -141,19 +141,6 @@ enum ENUM_X_SIGNAL_EXECUTION_RESULT
X_SIGNAL_EXECUTION_FAILED_INVALID_PARAMS, // Invalid Params
};
//
// Available Guard Action ...
enum ENUM_X_GUARD_ACTIONS
{
X_GUARD_ACTION_NOTHING, // Nothing
X_GUARD_ACTION_HEDGE, // Hedge
X_GUARD_PARTIAL_CLOSE, // Partial Close
X_GUARD_ACTION_TRAIL_STOP, // Trail StopLoss
X_GUARD_ACTION_FORCE_CLOSE, // Force Close
X_GUARD_ACTION_ADD_SUPPORT_SIGNAL, // Add Support Signal
X_GUARD_ACTION_CANCEL_PENDING_ORDERS, // Cancel Pending Orders
};
//
enum ENUM_X_POSITION_TYPES
{
@@ -2585,320 +2572,6 @@ struct XOnTradeHandlerState
}
};
//
// Model Guard Info ...
struct XGuard
{
//
string symbol; // Trading Symbol ...
string provider; // Signal Provider ...
ENUM_TIMEFRAMES period; // Trading Timeframe ...
ENUM_X_POSITION_TYPES type; // Position Type ...
ENUM_X_GUARD_ACTIONS action; // Which Action to Do ...
//
double dblPayLoad;
string strPayload;
//
// Constructor ...
XGuard()
{
Clean();
}
//
// Initialize ...
//
// Global Initializer ...
bool Init(
string mSymbol, // Trading Symbol ...
ENUM_TIMEFRAMES mPeriod, // Trading Timeframe ...
ENUM_X_GUARD_ACTIONS mAction, // Which Action to Do ...
ENUM_X_POSITION_TYPES mType = NULL, // Position Type ...
string mProvider = NULL, // Signal Provider ...
double mDblPayLoad = NULL,
string mStrPayload = NULL //
)
{
//
type = mType;
symbol = mSymbol;
period = mPeriod;
action = mAction;
provider = mProvider;
dblPayLoad = mDblPayLoad;
strPayload = mStrPayload;
//
bool result = IsValid();
//
return result;
}
//
// Tools ...
//
// Cleanup ...
void Clean()
{
//
symbol = NULL;
provider = NULL;
period = NULL;
action = X_GUARD_ACTION_NOTHING;
//
strPayload = NULL;
dblPayLoad = EMPTY_VALUE;
//
ZeroMemory(this);
}
//
// Validate ...
bool IsValid()
{
//
bool result = false;
//
result =
//
action != X_GUARD_ACTION_NOTHING
//
;
//
return result;
}
};
//
// Model a Position with all of it's Supports ...
struct XPositionPack
{
//
ulong ticket; // Main Position's Ticket
//
XPosition position; // Main Position (if Exists)
//
int supportOrdersCount; // Supported Orders (Untriggered)
int supportPositionsCount; // Supported Positions
//
XOrder supportOrders[]; // Supported Orders (Untriggered)
XPosition supportPositions[]; // Supported Positions
//
// Constructor ...
XPositionPack()
{
Clean();
}
//
// Tools ...
//
// Cleanup ...
void Clean()
{
//
ticket = 0;
//
position.Clean();
//
supportOrdersCount = 0;
supportPositionsCount = 0;
//
Clean(supportOrders);
Clean(supportPositions);
//
ZeroMemory(this);
}
//
// Validate ...
bool IsValid()
{
//
bool result = false;
//
result =
//
NotEmpty(ticket) &&
(
//
supportOrdersCount > 0
? ArraySize(supportOrders) == supportOrdersCount
: true
//
)
//
&&
//
(
//
supportPositionsCount > 0
? ArraySize(supportPositions) == supportPositionsCount
: true
//
)
//
;
//
return result;
}
};
//
// Check Position SL Trails Structure ...
struct XTrail
{
//
ulong ticket; // Position Ticket ...
ENUM_POSITION_TYPE type; // Type ...
datetime time; // Trail Time ...
double before; // SL Value Before Trailing ...
double after; // SL Value After Trailing ...
int level; // Number of Trails SL ...
double profit; // Profit in SL Time ...
//
// Constructor ...
XTrail()
{
Clean();
}
//
// Tools ...
//
bool Init(
ulong mTicket, // Ticket
ENUM_POSITION_TYPE mType, // Type
double mProfit, // Profit
double mBefore, // Before
double mAfter // After
)
{
//
Clean();
//
type = mType;
after = mAfter;
ticket = mTicket;
before = mBefore;
profit = mProfit;
//
level++;
//
time = TimeCurrent();
//
return IsValid();
}
//
bool Update(
double mSl, // new SL ...
double mProfit // Profit
)
{
//
bool result = false;
//
time = TimeCurrent();
//
double cAfter = after;
after = mSl;
before = cAfter;
profit = mProfit;
//
level++;
//
result = IsValid();
//
return result;
}
//
string GenerateTag()
{
//
string result = NULL;
//
string ticketLevelStr = ToString(ticket) + "," + ToString(level);
//
result = Surround(
XSLTrailToken,
ticketLevelStr //
);
//
return result;
}
//
// Cleanup ...
void Clean()
{
//
ticket = 0;
time = 0;
before = 0;
after = 0;
level = 0;
//
ZeroMemory(this);
}
//
// Validate ...
bool IsValid()
{
//
bool result = false;
//
result =
//
ticket > 0 &&
time > 0 &&
level > 0
//
;
//
return result;
}
};
//
struct XSymbolPositions
{
@@ -3508,493 +3181,6 @@ struct XSymbolPositions
//
};
//
struct XSymbolLastPosition
{
//
string symbol;
string provider;
datetime time;
//
XSymbolLastPosition()
{
Clean();
}
//
// Tools ...
void Clean()
{
//
time = NULL;
symbol = NULL;
provider = NULL;
//
ZeroMemory(this);
}
};
//
// an Structure for Holding Positions Data ...
struct XTradeData
{
//
datetime time; // Issue Time (Open Position)
string symbol; // Trading Symbol
double entry; // Entry Price
double volume; // Volume
string provider; // Signaller
ENUM_TIMEFRAMES period; // Trading Timeframe
ENUM_X_POSITION_TYPES type; // Position Type
//
ulong ticket; // Position Ticket
double swap; // Swap
double profit; // Profit on Close
double commission; // Commission
double maxDrawdown; // Max Position Drawdown
string message; // Close Reason
datetime endTime; // End Time
//
XSignal signal; // Signal Object
int pushers; // Signal Pushers
string conditions; // Signal Conditions
//
// Constructor ...
void XTradeInfo()
{
Clean();
}
//
// Filling Trade Handler ...
bool Fill(XSignal &_signal)
{
//
bool result = false;
//
Clean();
//
// Check Signal Validation ...
result = _signal.IsValid();
if (!result)
{
return result;
}
//
signal = _signal;
time = _signal.time;
entry = _signal.entry;
symbol = _signal.symbol;
volume = _signal.volume;
period = _signal.period;
provider = _signal.provider;
conditions = _signal.conditions;
type = ToPositionType(_signal.type);
//
if (_signal.IsExecuted())
{
ticket = _signal.positionId;
}
//
return result;
}
//
// Filling Trade Handler ...
bool Fill(XPosition &_position)
{
//
bool result = false;
//
Clean();
//
// Check Signal Validation ...
result = _position.IsValid();
if (!result)
{
return result;
}
//
time = _position.openAt;
symbol = _position.symbol;
entry = _position.entry;
volume = _position.volume;
provider = _position.provider;
period = _position.period;
type = ToPositionType(_position.type);
//
ticket = _position.ticket;
//
return result;
}
//
// Update Data ...
bool Update(XPosition &_position)
{
//
bool result = false;
//
result = _position.IsValid();
if (!result)
{
return result;
}
//
profit = _position.profit;
swap = _position.swap;
//
if (profit < 0 &&
(maxDrawdown == 0 ||
MathAbs(profit) > MathAbs(maxDrawdown)))
{
maxDrawdown = profit;
}
//
return result;
}
//
// Tools ...
//
// Cleanup ...
void Clean()
{
//
time = NULL;
type = NULL;
symbol = NULL;
period = NULL;
endTime = NULL;
message = NULL;
provider = NULL;
//
swap = 0;
entry = 0;
ticket = 0;
profit = 0;
volume = 0;
commission = 0;
maxDrawdown = 0;
//
signal.Clean();
//
ResetTrail();
ResetRecovery();
//
ZeroMemory(this);
}
//
// Validation ...
bool IsValid()
{
//
bool result = false;
//
result =
//
ticket > 0 &&
IsValid(time) &&
IsValid(symbol) &&
IsValid(period)
//
;
//
return result;
}
//
// Retrieve Trade Data Age ...
int GetAge()
{
//
int result = -1;
//
if (!IsValid(symbol) ||
!IsValid(time) ||
!IsValid(endTime) ||
!IsValid(period))
{
return result;
}
//
int startIndex = iBarShift(
symbol,
period,
time,
false //
);
//
int endIndex = iBarShift(
symbol,
period,
endTime,
false //
);
//
result = MathAbs(startIndex - endIndex);
//
return result;
}
//
// Check Own of Trade Data ...
// based on Ticket ...
bool IsOwn(
ulong _ticket //
)
{
//
bool result = false;
//
result = _ticket == ticket;
//
return result;
}
//
// Check Own of Trade Data ...
// based on Symbol/Provider and Period ...
bool IsOwn(
string _symbol,
string _provider,
ENUM_TIMEFRAMES _period,
ENUM_X_POSITION_TYPES _type //
)
{
//
bool result = false;
//
result =
//
IsValid(_symbol) &&
IsValid(_period) &&
IsValid(_provider) &&
type != X_POSITION_TYPE_ALL &&
type != X_POSITION_TYPE_NONE &&
//
type == _type &&
period == _period &&
symbol == _symbol &&
provider == _provider
//
;
//
return result;
}
//
// Data Collector ...
//
// Get Data File Name ...
string GetFileName()
{
//
string result = NULL;
//
if (!IsValid())
{
return result;
}
//
result =
//
symbol + "\\" +
ToString(type) + "\\" +
(profit >= 0 ? "Profit" : "Loss") + "\\" +
ToString(ticket) + "_" +
ToString(period) + "_" +
ToFormatString(time)
//
;
//
return result;
}
//
// Get Signal File Name ...
string GetSignalFileName()
{
//
string result = NULL;
//
result =
//
symbol + "\\" +
ToString(type) + "\\" +
provider + "_" +
ToFormatString(time)
//
;
//
return result;
}
//
// Convert(s) To String Representation(s) ...
string ToString(
bool onlySignals = false //
)
{
//
string result = NULL;
//
int age = GetAge();
//
result =
//
(onlySignals
? ""
: ToString("Ticket", ticket)) +
ToString("Symbol", symbol) +
ToString("Period", period) +
ToString("Entry", entry) +
ToString("Provider", provider) +
ToString("Type", ToString(type)) +
ToString("Time", time) +
//
// Attach Trade Info ...
(onlySignals
? ""
:
//
"-------------" + "\n" +
ToString("Volume", volume) +
ToString("Profit", profit) +
ToString("Commission", commission) +
ToString("Swap", swap) +
ToString("Max Drawdown", maxDrawdown) +
ToString("End Time", endTime) +
ToString("Age", age) +
ToString("Message", message) +
""
//
) +
//
// Attach Conditions to Signals ...
(!onlySignals
? ""
:
//
"-------------" + "\n" +
ToString("Pushers", pushers) +
"Conditions:" + "\n" +
"-------------" + "\n" +
conditions +
""
//
) +
//
""
//
;
//
return result;
}
//
// There are some Controlling Situations for each Trade
// which implemented here ...
//
// Trailing Stop ....
//
bool allowTrailStop; // Specified Position Can Trail Stop or not
bool ignoreTPOnTrail; // Ignore TP when SL Trailed
bool trailBasedOnProfit; // Do Trail Based on Profit Points
int trailLevel; // Last Trail Level
double trailStartInPoint; // Trail Starts When Profits Growing in Point
double trailStepInPoint; // Profit Must Grows Point to do Trailling
//
// Reset all Trailing Configurations ....
void ResetTrail()
{
//
trailLevel = 0;
trailStepInPoint = 0;
trailStartInPoint = 0;
allowTrailStop = false;
ignoreTPOnTrail = false;
trailBasedOnProfit = false;
}
//
// Recovery ...
//
bool allowRecover; // Allow Recover Specified Position
double recoveryTPPoint; // Recovery TP Point
double recoverySLPoint; // Recovery SL Point
int maxAllowedRecover; // Max Allowed Recovery
int recoveryLevel; // Number of Recovery Signals
double recoveryVolumeMultiplier; // Recovery Volume Multiplier
double recoveryVolume; // Next Recovery Positioin Volume
ENUM_X_POSITION_TYPES recoveryLookingType; // Looking for Next Recovery Type
//
void ResetRecovery()
{
//
allowRecover = false;
recoveryLevel = 0;
recoveryVolume = 0;
recoveryTPPoint = 0;
recoverySLPoint = 0;
recoveryVolumeMultiplier = 0;
recoveryLookingType = X_POSITION_TYPE_NONE;
}
//
};
//
// TypeDefs ...