apply some EQM Force State Handling ...
This commit is contained in:
@@ -153,16 +153,6 @@ struct XTradeInfo
|
||||
lConditions = _lConditions;
|
||||
hConditions = _hConditions;
|
||||
|
||||
//
|
||||
X121SignalGenerator generator;
|
||||
XSignal tSignal = _signal;
|
||||
generator.GenerateSupportPivots(
|
||||
tSignal,
|
||||
pivots //
|
||||
);
|
||||
tSignal.Clean();
|
||||
int pivotsCount = ArraySize(pivots);
|
||||
|
||||
//
|
||||
allowGrid = _allowGrid;
|
||||
allowRecover = _allowRecover;
|
||||
@@ -412,14 +402,11 @@ struct XTradeInfo
|
||||
// Supporting ...
|
||||
|
||||
//
|
||||
double pivots[];
|
||||
bool allowSupport; // Allow Support
|
||||
|
||||
//
|
||||
void ResetSupport()
|
||||
{
|
||||
//
|
||||
Clean(pivots);
|
||||
allowSupport = false;
|
||||
}
|
||||
|
||||
@@ -432,158 +419,6 @@ struct XTradeInfo
|
||||
//
|
||||
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;
|
||||
}
|
||||
@@ -4675,24 +4510,12 @@ private:
|
||||
// EQM Functions ...
|
||||
|
||||
//
|
||||
bool DoEQMForceClose(
|
||||
double requiredProfit = 0 // Required Profit For EQM Force Hedge ...
|
||||
)
|
||||
// Force Close All Positions Due EQM Hedge ...
|
||||
bool DoEQMForceClose()
|
||||
{
|
||||
//
|
||||
bool result = false;
|
||||
|
||||
//
|
||||
double hedgingProfit =
|
||||
requiredProfit == 0
|
||||
? mForceCloseProfit
|
||||
: requiredProfit;
|
||||
bool canDo = hedgingProfit > 0;
|
||||
if (!canDo)
|
||||
{
|
||||
return result;
|
||||
}
|
||||
|
||||
//
|
||||
XPosition positions[];
|
||||
int positionsCount = mTrader.GetPositions(positions);
|
||||
@@ -4706,12 +4529,6 @@ private:
|
||||
double profit = SpecifiedCalculatePositionsProfit(
|
||||
positions //
|
||||
);
|
||||
result =
|
||||
profit > hedgingProfit;
|
||||
if (!result)
|
||||
{
|
||||
return result;
|
||||
}
|
||||
|
||||
//
|
||||
string comment = XEQMSupportToken + " Hedge ...";
|
||||
@@ -4722,6 +4539,12 @@ private:
|
||||
|
||||
//
|
||||
result = IsValidSize(closed);
|
||||
if (result)
|
||||
{
|
||||
//
|
||||
mTradeHandler.Sync();
|
||||
EQMClearAllForceStates();
|
||||
}
|
||||
|
||||
//
|
||||
// Handle Sleeping ...
|
||||
@@ -4799,24 +4622,7 @@ private:
|
||||
if (!IsValidSize(positionsCount))
|
||||
{
|
||||
//
|
||||
int setupsCount = CountSetups();
|
||||
if (IsValidSize(setupsCount))
|
||||
{
|
||||
//
|
||||
for (int i = 0; i < setupsCount; i++)
|
||||
{
|
||||
mSetups[i].ForceState(false);
|
||||
}
|
||||
}
|
||||
|
||||
//
|
||||
// Disable Force State if there isn't any open Positions
|
||||
// and it's Enabled ...
|
||||
// this means Hedged positions ...
|
||||
// if (ForceState())
|
||||
// {
|
||||
// ForceState(false);
|
||||
// }
|
||||
EQMClearAllForceStates();
|
||||
|
||||
//
|
||||
return;
|
||||
@@ -4910,30 +4716,156 @@ private:
|
||||
}
|
||||
|
||||
//
|
||||
// bool isForce = forceCount > 1;
|
||||
// if (!ForceState() && isForce)
|
||||
// {
|
||||
// //
|
||||
// ForceState(isForce);
|
||||
// return;
|
||||
// }
|
||||
// else if (ForceState() && !isForce)
|
||||
// {
|
||||
// //
|
||||
// ForceState(isForce);
|
||||
// return;
|
||||
// }
|
||||
|
||||
// //
|
||||
// // Handle Force State ...
|
||||
// if (!ForceState())
|
||||
// {
|
||||
// return;
|
||||
// }
|
||||
bool isForce = forceCount >= 1;
|
||||
if (!isForce || !IsValidSize(setupsCount))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
//
|
||||
// TODO ...
|
||||
// Here i Can Check what to do In Force State ...
|
||||
|
||||
//
|
||||
// Handle Hedging Based On Positions ...
|
||||
double drawdown = GetDrawdownPercent();
|
||||
double forceDrawdown = MaxDrawdownForce();
|
||||
int positionsForce = MaxPositionsForce();
|
||||
double forceCloseProfit = ForceCloseProfit();
|
||||
double minRequiredProfitPerTrade = MinRequiredProfitPerTrade();
|
||||
double minRequiredProfitPerVolumeFactor = MinRequiredProfitPerVolumeFactor();
|
||||
|
||||
//
|
||||
// Try To Force Hedging ...
|
||||
bool canForceHedge =
|
||||
(positionsForce <= 0
|
||||
? true
|
||||
: positionsCount > positionsForce)
|
||||
//
|
||||
||
|
||||
//
|
||||
(forceDrawdown > 0 &&
|
||||
drawdown >= forceDrawdown);
|
||||
if (canForceHedge)
|
||||
{
|
||||
//
|
||||
// Check Required Profit ...
|
||||
|
||||
//
|
||||
double profit = SpecifiedCalculatePositionsProfit(positions);
|
||||
|
||||
//
|
||||
// Check Force Close Profit ...
|
||||
if (forceCloseProfit > 0)
|
||||
{
|
||||
//
|
||||
canForceHedge = profit >= forceCloseProfit;
|
||||
if (canForceHedge)
|
||||
{
|
||||
//
|
||||
bool isHedged = DoEQMForceClose();
|
||||
if (isHedged)
|
||||
{
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//
|
||||
// Calculate Required Profit ...
|
||||
double requiredProfit = SpecifiedCalculateRequiredProfitForHedge(
|
||||
positions,
|
||||
minRequiredProfitPerTrade,
|
||||
minRequiredProfitPerVolumeFactor //
|
||||
);
|
||||
|
||||
//
|
||||
canForceHedge =
|
||||
requiredProfit <= 0
|
||||
? profit > 0
|
||||
: profit >= (requiredProfit / 2);
|
||||
if (canForceHedge)
|
||||
{
|
||||
//
|
||||
bool isHedged = DoEQMForceClose();
|
||||
if (isHedged)
|
||||
{
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
//
|
||||
// At Least Hedgeing ...
|
||||
// Minimal Hedge ...
|
||||
|
||||
//
|
||||
// Check Symbol Hedging ...
|
||||
for (int i = 0; i < setupsCount; i++)
|
||||
{
|
||||
//
|
||||
string iSymbol = mSetups[i].GetSymbol();
|
||||
|
||||
//
|
||||
XPosition iSymbolPositions[];
|
||||
int iSymbolPositionsCount = mTrader.GetPositions(
|
||||
iSymbolPositions,
|
||||
//
|
||||
iSymbol,
|
||||
NULL, // All Providers ...
|
||||
NULL, // All Periods ...
|
||||
NULL, // All Types ...
|
||||
true // Filter By Magic ...
|
||||
);
|
||||
if (!IsValidSize(iSymbolPositionsCount))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
//
|
||||
double profit = SpecifiedCalculatePositionsProfit(iSymbolPositions);
|
||||
|
||||
//
|
||||
double requiredProfit = SpecifiedCalculateRequiredProfitForHedge(
|
||||
iSymbolPositions,
|
||||
minRequiredProfitPerTrade,
|
||||
minRequiredProfitPerVolumeFactor //
|
||||
);
|
||||
|
||||
//
|
||||
bool canHedge =
|
||||
requiredProfit <= 0
|
||||
? profit > 0
|
||||
: profit >= requiredProfit;
|
||||
if (canHedge)
|
||||
{
|
||||
//
|
||||
string comment = XEQMSupportToken + " Hedge Symbol ...";
|
||||
|
||||
//
|
||||
int closed = mTrader.Close(
|
||||
iSymbolPositions,
|
||||
comment //
|
||||
);
|
||||
|
||||
//
|
||||
if (closed == iSymbolPositionsCount)
|
||||
{
|
||||
//
|
||||
mSetups[i].ForceState(false);
|
||||
|
||||
//
|
||||
string message = XEQMSupportToken + " Hedge Symbol (" + iSymbol + "): " + ToString(profit);
|
||||
Alert(message);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
//
|
||||
// Implement Other Senarios ...
|
||||
|
||||
//
|
||||
}
|
||||
|
||||
//
|
||||
@@ -4954,13 +4886,15 @@ private:
|
||||
|
||||
//
|
||||
int setupIDX = FindSetupIndex(signal.symbol);
|
||||
if (!IsValidIndex(setupIDX)) {
|
||||
if (!IsValidIndex(setupIDX))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
//
|
||||
bool isForce = mSetups[setupIDX].ForceState();
|
||||
if (!isForce) {
|
||||
if (!isForce)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -5120,6 +5054,23 @@ private:
|
||||
}
|
||||
}
|
||||
|
||||
//
|
||||
void EQMClearAllForceStates()
|
||||
{
|
||||
//
|
||||
int setupsCount = CountSetups();
|
||||
if (!IsValidSize(setupsCount))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
//
|
||||
for (int i = 0; i < setupsCount; i++)
|
||||
{
|
||||
mSetups[i].ForceState(false);
|
||||
}
|
||||
}
|
||||
|
||||
//
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user