last works ...

This commit is contained in:
2024-08-01 02:18:31 +03:30
parent 5403a46aa5
commit 49a52f54d4
6 changed files with 1316 additions and 252 deletions
+317 -17
View File
@@ -4156,6 +4156,99 @@ public:
return maxSameTimeTrades;
}
//
bool AllowLong()
{
return mAllowLong;
}
//
void AllowLong(bool value)
{
mAllowLong = value;
}
//
bool AllowShort()
{
return mAllowShort;
}
//
void AllowShort(bool value)
{
mAllowShort = value;
}
//
int MaxAllowedLongs()
{
return mMaxAllowedLongs;
}
//
void MaxAllowedLongs(int value)
{
//
if (value < 0)
{
value = 0;
}
//
mMaxAllowedLongs = value;
}
//
int MaxAllowedShorts()
{
return mMaxAllowedShorts;
}
//
void MaxAllowedShorts(int value)
{
//
if (value < 0)
{
value = 0;
}
//
mMaxAllowedShorts = value;
}
//
bool UseMaxAllowedTradesPerSymbol()
{
return mUseMaxAllowedTradesPerSymbol;
}
//
void UseMaxAllowedTradesPerSymbol(bool value)
{
mUseMaxAllowedTradesPerSymbol = value;
}
//
double MaxDrawdownPercentForOpenTrades()
{
return mMaxDrawdownPercentForOpenTrades;
}
//
void MaxDrawdownPercentForOpenTrades(double value)
{
//
if (value < 0)
{
value = 0;
}
//
mMaxDrawdownPercentForOpenTrades = value;
}
//
// Trailling ...
@@ -4370,18 +4463,6 @@ public:
mAllowHedging = value;
}
//
bool OnlyHedgeSupportedPositions()
{
return mOnlyHedgeSupportedPositions;
}
//
void OnlyHedgeSupportedPositions(bool value)
{
mOnlyHedgeSupportedPositions = value;
}
//
double HedgeVolumeFactor()
{
@@ -4401,6 +4482,25 @@ public:
mHedgeVolumeFactor = value;
}
//
int MinTradesForHedge()
{
return mMinTradesForHedge;
}
//
void MinTradesForHedge(int value)
{
//
if (value < 0)
{
value = 0;
}
//
mMinTradesForHedge = value;
}
//
double HedgeMinProfitPerVolumeFactor()
{
@@ -4423,6 +4523,36 @@ public:
//
// Tools ...
//
// Calculate Max Drawdown and it's Percent ...
double GetMaxDrawdown()
{
//
XPosition positions[];
int positionsCount = mTrader.GetPositions(positions);
if (IsValidSize(positionsCount))
{
double mEquity = mTrader.mAccount.GetEquity();
if (mEquity > maxDrawdown)
{
maxDrawdown = mEquity;
}
//
drawdownPercent = (maxDrawdown - mEquity) / (staticBalanceForCalculateDrawdown > 0 ? staticBalanceForCalculateDrawdown : maxDrawdown) * 100;
drawdownPercent = NormalizeDouble(drawdownPercent, 3);
}
else
{
//
maxDrawdown = 0.0;
drawdownPercent = 0.0;
}
//
return drawdownPercent;
}
//
// First Step of Trade Handling ...
// Since Must Call when a Signal Executed ...
@@ -4662,6 +4792,9 @@ public:
{
maxSameTimeTrades = count;
}
//
GetMaxDrawdown();
}
//
@@ -4681,9 +4814,9 @@ public:
{
//
// Check Values Validation ...
if ((count > 1 ||
(count == 1 &&
mOnlyHedgeSupportedPositions)) &&
if ((mMinTradesForHedge <= 0
? count > 1
: count >= mMinTradesForHedge) &&
mHedgeVolumeFactor > 0 &&
mHedgeMinProfitPerVolumeFactor > 0)
{
@@ -5342,6 +5475,162 @@ public:
return result;
}
//
bool ExecuteSignal(
XSignal &signal, // Signal for Execution
ENUM_X_SIGNAL_EXECUTION_RESULT &state, // Execution State
bool ignorePolicies = false // Ignore Execution Policies
)
{
//
bool result = false;
//
// Check Signal Validation ...
result = signal.IsValid();
if (!result)
{
return result;
}
//
// Filter Signals if Necessary ...
result = CanExecute(signal);
if (!result)
{
//
state = X_SIGNAL_EXECUTION_FAILED_INVALID_PARAMS;
return result;
}
//
// Check Policies ...
if (!ignorePolicies)
{
//
bool isLong = IsLong(signal.type);
//
// Check Allow Trade Type ...
result =
isLong
? mAllowLong
: mAllowShort;
if (!result)
{
//
state = X_SIGNAL_EXECUTION_FAILED_NOT_ALLOWED;
return result;
}
//
// Check Trades Count ...
if (mMaxAllowedLongs > 0 || mMaxAllowedShorts > 0)
{
//
XPosition longs[];
XPosition shorts[];
//
// Counting Positions ...
if (mUseMaxAllowedTradesPerSymbol)
{
//
mTrader.GetPositions(
longs,
shorts,
signal.symbol //
);
}
else
{
//
mTrader.GetPositions(
longs,
shorts,
NULL // All Symbols ...
);
}
//
int longsCount = ArraySize(longs);
int shortsCount = ArraySize(shorts);
//
result =
//
// Long ...
isLong
? mMaxAllowedLongs <= 0
? true
: longsCount < mMaxAllowedLongs
//
// Short ...
: mMaxAllowedShorts <= 0
? true
: shortsCount < mMaxAllowedShorts
//
;
if (!result)
{
//
state = X_SIGNAL_EXECUTION_FAILED_MAX_REACHED;
return result;
}
}
//
// Check Drawdown ...
if (mMaxDrawdownPercentForOpenTrades > 0)
{
//
drawdownPercent = GetMaxDrawdown();
//
result =
drawdownPercent <= 0
? true
: drawdownPercent < mMaxDrawdownPercentForOpenTrades;
if (!result)
{
//
state = X_SIGNAL_EXECUTION_FAILED_NO_EQUITY;
return result;
}
}
}
//
// Execute Signal ...
result = mTrader.ExecuteSignal(
signal,
state //
);
if (result)
{
//
double commission = mTrader.GetPositionCommission(signal.positionId);
//
// Add Signal to Trade Handler ...
AddData(
signal,
commission //
);
//
string msg = (signal.type == POSITION_TYPE_BUY ? "Long" : "Short") +
" Signal Provided by: " + signal.provider +
" on: " + signal.symbol +
" in: " + ToString(signal.period) +
" Executed Successfully ...";
Alert(msg);
}
//
return result;
}
//
// Protected ...
protected:
@@ -5352,6 +5641,14 @@ protected:
bool mSaveSignals; // Save Signals
bool mSaveTrades; // Save Trades
//
bool mAllowLong; // Allow Long/Buy Trade Type
bool mAllowShort; // Allow Short/Sell Trade Type
int mMaxAllowedLongs; // Max Allowe Long/Buy Trades
int mMaxAllowedShorts; // Max Allowe Short/Sell Trades
bool mUseMaxAllowedTradesPerSymbol; // Use Max Allowed Trades Per Symbol
double mMaxDrawdownPercentForOpenTrades; // Max Drawdown Percent for Open Trades
//
XSCTrade *mTrader; // Instance of Trader Class
XTradeCollector *mCollector; // Instance of Trade Collector Class
@@ -5377,7 +5674,7 @@ protected:
//
// Hedging ...
bool mAllowHedging; // Hedge Trades
bool mOnlyHedgeSupportedPositions; // Only Hedge Supported Positions
int mMinTradesForHedge; // Minimum Open Trades for Do Hedge
double mHedgeVolumeFactor; // Hedging Volume Factor
double mHedgeMinProfitPerVolumeFactor; // Hedging Min Required Profit Per Volume Factor
@@ -5389,7 +5686,10 @@ protected:
private:
//
// Props ...
int maxSameTimeTrades; // Max Same Time Trades
int maxSameTimeTrades; // Max Same Time Trades
double maxDrawdown; // Max Drawdown
double drawdownPercent; // Drawdown Percent
double staticBalanceForCalculateDrawdown; // Static Balance for Max Drawdown Calculation
//
// Count Data ...