- Expert Class; - Class Based EA; - Signaller(s); - Multi Symbol; - Multi Session; - ...
1168 lines
33 KiB
Plaintext
1168 lines
33 KiB
Plaintext
//
|
|
// Guards ...
|
|
|
|
//
|
|
// Do All Provided Guards ...
|
|
void DoGuards(XGuard &guards[])
|
|
{
|
|
//
|
|
int guardsCount = ArraySize(guards);
|
|
if (guardsCount <= 0)
|
|
{
|
|
return;
|
|
}
|
|
|
|
//
|
|
for (int i = 0; i < guardsCount; i++)
|
|
{
|
|
//
|
|
XGuard iGuard = guards[i];
|
|
|
|
//
|
|
switch (iGuard.action)
|
|
{
|
|
//
|
|
// Hedge In Profit Specified Positions ...
|
|
case X_GUARD_ACTION_HEDGE:
|
|
HandleGuardHedgeAction(
|
|
iGuard.provider,
|
|
iGuard.symbol,
|
|
iGuard.type,
|
|
iGuard.period);
|
|
break;
|
|
|
|
//
|
|
// Trail Stop Loss in Specified Positions ...
|
|
case X_GUARD_ACTION_TRAIL_STOP:
|
|
HandleGuardTrailingStopAction(
|
|
iGuard.provider,
|
|
iGuard.symbol,
|
|
iGuard.type,
|
|
iGuard.period,
|
|
iGuard.dblPayLoad);
|
|
break;
|
|
|
|
//
|
|
// Force Close Specified Positions ...
|
|
case X_GUARD_ACTION_FORCE_CLOSE:
|
|
HandleGuardForceCloseAction(
|
|
iGuard.provider,
|
|
iGuard.symbol,
|
|
iGuard.type,
|
|
iGuard.period);
|
|
break;
|
|
|
|
//
|
|
// Generate and Place Support Signals ...
|
|
case X_GUARD_ACTION_ADD_SUPPORT_SIGNAL:
|
|
HandleGuardAddSupportPositionAction(
|
|
iGuard.provider,
|
|
iGuard.symbol,
|
|
iGuard.type,
|
|
iGuard.period);
|
|
break;
|
|
|
|
//
|
|
// Delete All Placed Pending Orders ...
|
|
case X_GUARD_ACTION_CANCEL_PENDING_ORDERS:
|
|
HandleGuardCancelPendingOrdersAction(
|
|
iGuard.provider,
|
|
iGuard.symbol,
|
|
iGuard.type,
|
|
iGuard.period);
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
//
|
|
// Close All Specified Provider's Positions in Profit Summary ...
|
|
void HandleGuardHedgeAction(
|
|
string provider, // Positions Provided by Specific Provider
|
|
string symbol, // Trading Symbol
|
|
ENUM_POSITION_TYPE type, // Trading Type
|
|
ENUM_TIMEFRAMES period, // Specified Time Frame
|
|
double minProfitForHedging = 1 // Specified Profit for Hedge
|
|
)
|
|
{
|
|
//
|
|
XPosition positions[];
|
|
int positionsCount = mTrader
|
|
.GetPositions(
|
|
positions,
|
|
symbol,
|
|
provider,
|
|
period,
|
|
type
|
|
//
|
|
);
|
|
if (positionsCount <= 1)
|
|
{
|
|
return;
|
|
}
|
|
|
|
//
|
|
double profit = CalculatePositionsProfit(positions);
|
|
double requiredProfit = CalculateRequiredProfitForHedge(positions);
|
|
|
|
//
|
|
bool isReadyForHedge = profit >= requiredProfit;
|
|
if (!isReadyForHedge)
|
|
{
|
|
return;
|
|
}
|
|
|
|
//
|
|
string comment = "Guard Hedge " + provider;
|
|
int closed = mTrader.Close(
|
|
positions,
|
|
comment
|
|
//
|
|
);
|
|
if (closed > 0)
|
|
{
|
|
//
|
|
string message = "Guard Hedge " + provider + "'s Positions on Profit: " + ToString(profit);
|
|
|
|
//
|
|
mAlert.Alert(message);
|
|
}
|
|
}
|
|
|
|
//
|
|
// Close Max In Profit Trade Guard Action ...
|
|
void HandleGuardCloseInProfitAction(
|
|
string provider, // Positions Provided by Specific Provider
|
|
string symbol, // Trading Symbol
|
|
ENUM_POSITION_TYPE type, // Trading Type
|
|
ENUM_TIMEFRAMES period, // Specified Time Frame
|
|
ENUM_X_POSITION_SELECT_METHODS method = NULL // Select Method
|
|
)
|
|
{
|
|
//
|
|
XPosition positions[];
|
|
int positionsCount = mTrader
|
|
.GetInProfitPositions(
|
|
positions,
|
|
symbol,
|
|
provider,
|
|
period,
|
|
type,
|
|
method
|
|
//
|
|
);
|
|
if (positionsCount <= 0)
|
|
{
|
|
return;
|
|
}
|
|
|
|
//
|
|
string comment = "Close InProfit ...";
|
|
mTrader.Close(
|
|
positions,
|
|
comment
|
|
//
|
|
);
|
|
|
|
//
|
|
string message = "Guard Close (" + ToString(positionsCount) + ") Postion(s) In Profit ...";
|
|
|
|
//
|
|
mAlert.Alert(message);
|
|
}
|
|
|
|
//
|
|
// Close Max In Drawdown Trade Guard Action ...
|
|
void HandleGuardCloseInDrawdownAction(
|
|
string provider, // Positions Provided by Specific Provider
|
|
string symbol, // Trading Symbol
|
|
ENUM_POSITION_TYPE type, // Trading Type
|
|
ENUM_TIMEFRAMES period, // Specified Time Frame
|
|
ENUM_X_POSITION_SELECT_METHODS method = NULL // Select Method
|
|
)
|
|
{
|
|
//
|
|
XPosition positions[];
|
|
int positionsCount = mTrader
|
|
.GetInDrawdownPositions(
|
|
positions,
|
|
symbol,
|
|
provider,
|
|
period,
|
|
type,
|
|
method
|
|
//
|
|
);
|
|
if (positionsCount <= 0)
|
|
{
|
|
return;
|
|
}
|
|
|
|
//
|
|
string comment = "Close InDrawdown ...";
|
|
mTrader.Close(
|
|
positions,
|
|
comment
|
|
//
|
|
);
|
|
|
|
//
|
|
string message = "Guard Close (" + ToString(positionsCount) + ") Position(s) In Drawdown ...";
|
|
|
|
//
|
|
mAlert.Alert(message);
|
|
}
|
|
|
|
//
|
|
// Force Close Specified Set Of Position ...
|
|
void HandleGuardForceCloseAction(
|
|
string provider, // Positions Provided by Specific Provider
|
|
string symbol, // Trading Symbol
|
|
ENUM_POSITION_TYPE type, // Trading Type
|
|
ENUM_TIMEFRAMES period, // Specified Time Frame
|
|
ulong ticket = 0 // Specific Ticket
|
|
)
|
|
{
|
|
//
|
|
string comment = "";
|
|
|
|
//
|
|
// Close All Positions ...
|
|
if (ticket == 0)
|
|
{
|
|
//
|
|
XPosition positions[];
|
|
int positionsCount = mTrader.GetPositions(
|
|
positions,
|
|
symbol,
|
|
provider,
|
|
period,
|
|
type
|
|
//
|
|
);
|
|
if (positionsCount <= 0)
|
|
{
|
|
return;
|
|
}
|
|
|
|
//
|
|
comment = provider + " Force Close Guard ...";
|
|
|
|
//
|
|
mTrader.Close(
|
|
positions,
|
|
comment
|
|
//
|
|
);
|
|
}
|
|
//
|
|
// Close Specific Position ...
|
|
else
|
|
{
|
|
//
|
|
XPosition position;
|
|
bool hasPosition = mTrader
|
|
.GetPosition(
|
|
ticket,
|
|
position
|
|
//
|
|
);
|
|
|
|
//
|
|
if (hasPosition)
|
|
{
|
|
//
|
|
comment = provider + "(" + ToString(ticket) + ")" + " Force Close Guard ...";
|
|
|
|
//
|
|
mTrader.Close(
|
|
ticket,
|
|
comment
|
|
//
|
|
);
|
|
}
|
|
}
|
|
|
|
//
|
|
if (IsValid(comment))
|
|
{
|
|
mAlert.Alert(comment);
|
|
}
|
|
}
|
|
|
|
//
|
|
// Trailing Specified Set Of Positions Stop Losses ...
|
|
void HandleGuardTrailingStopAction(
|
|
string provider, // Positions Provided by Specific Provider
|
|
string symbol, // Trading Symbol
|
|
ENUM_POSITION_TYPE type, // Trading Type
|
|
ENUM_TIMEFRAMES period, // Specified Time Frame
|
|
double sl // Desired SL Value ...
|
|
)
|
|
{
|
|
//
|
|
if (sl <= 0)
|
|
{
|
|
return;
|
|
}
|
|
|
|
//
|
|
NormalizePrice(
|
|
sl,
|
|
symbol
|
|
//
|
|
);
|
|
|
|
//
|
|
XPosition positions[];
|
|
int positionsCount = mTrader
|
|
.GetPositions(
|
|
positions,
|
|
symbol,
|
|
provider,
|
|
period,
|
|
type
|
|
//
|
|
);
|
|
if (positionsCount <= 0)
|
|
{
|
|
return;
|
|
}
|
|
|
|
//
|
|
int modified = 0;
|
|
for (int i = 0; i < positionsCount; i++)
|
|
{
|
|
//
|
|
XPosition iPosition = positions[i];
|
|
|
|
//
|
|
string comment = PrepareSLTrailTag(iPosition.comment);
|
|
|
|
//
|
|
if (iPosition.sl == sl)
|
|
{
|
|
continue;
|
|
}
|
|
|
|
//
|
|
bool isModified = mTrader.Modify(
|
|
iPosition.ticket,
|
|
sl,
|
|
iPosition.tp,
|
|
comment);
|
|
if (isModified)
|
|
{
|
|
modified++;
|
|
}
|
|
}
|
|
|
|
//
|
|
if (modified > 0)
|
|
{
|
|
//
|
|
string message = "(" + ToString(modified) + ") Position's Stop Trailed Successfully ...";
|
|
|
|
//
|
|
mAlert.Alert(message);
|
|
}
|
|
}
|
|
|
|
//
|
|
// Add Support Signal for Specified Positions ...
|
|
void HandleGuardAddSupportPositionAction(
|
|
string provider, // Positions Provided by Specific Provider
|
|
string symbol, // Trading Symbol
|
|
ENUM_POSITION_TYPE type, // Trading Type
|
|
ENUM_TIMEFRAMES period // Specified Time Frame
|
|
)
|
|
{
|
|
//
|
|
XSignal support;
|
|
bool hasSupport = RequestForSupport(
|
|
support,
|
|
provider,
|
|
symbol,
|
|
type,
|
|
period
|
|
//
|
|
);
|
|
|
|
//
|
|
if (!hasSupport || !support.IsValid())
|
|
{
|
|
return;
|
|
}
|
|
|
|
//
|
|
// TODO: May be need to add Support Tag to comments ...
|
|
|
|
//
|
|
ENUM_X_SIGNAL_EXECUTION_RESULT reason = NULL;
|
|
bool isExecuted = mTrader.ExecuteSignal(
|
|
support,
|
|
reason);
|
|
|
|
//
|
|
string comment = NULL;
|
|
if (!isExecuted)
|
|
{
|
|
comment = "Support Execution Failed doue " + ToString(reason);
|
|
}
|
|
else
|
|
{
|
|
comment = "Guard Support Signal Executed Successfully ...";
|
|
}
|
|
|
|
//
|
|
if (IsValid(comment))
|
|
{
|
|
mAlert.Alert(comment);
|
|
}
|
|
}
|
|
|
|
//
|
|
// Cancel all Placed Orders ...
|
|
void HandleGuardCancelPendingOrdersAction(
|
|
string provider, // Positions Provided by Specific Provider
|
|
string symbol, // Trading Symbol
|
|
ENUM_POSITION_TYPE type, // Trading Type
|
|
ENUM_TIMEFRAMES period // Specified Time Frame
|
|
)
|
|
{
|
|
//
|
|
XOrder orders[];
|
|
int ordersCount = mTrader.GetOrders(
|
|
orders,
|
|
symbol,
|
|
provider,
|
|
period,
|
|
ToOrderType(type),
|
|
ORDER_STATE_PLACED,
|
|
true // Filter by Magic ...
|
|
//
|
|
);
|
|
if (ordersCount <= 0)
|
|
{
|
|
return;
|
|
}
|
|
|
|
//
|
|
int canceledOrdersCount = mTrader.CancelOrders(orders);
|
|
|
|
//
|
|
if (canceledOrdersCount > 0)
|
|
{
|
|
//
|
|
string message = "Guard Cancel (" + ToString(canceledOrdersCount) + ") Pending Orders ...";
|
|
|
|
//
|
|
mAlert.Alert(message);
|
|
}
|
|
}
|
|
|
|
//////////////////////////////////////////////////////////////////
|
|
|
|
/**
|
|
* Handle Guard Actions ...
|
|
*
|
|
* @param guards: X121SMCGuard instance Collection ...
|
|
*/
|
|
void HandleGuards(X121SMCGuard &guards[])
|
|
{
|
|
//
|
|
int count = ArraySize(guards);
|
|
if (!IsValidSize(guards))
|
|
{
|
|
return;
|
|
}
|
|
|
|
//
|
|
for (int i = 0; i < count; i++)
|
|
{
|
|
//
|
|
X121SMCGuard iGuard = guards[i];
|
|
|
|
//
|
|
if (!iGuard.IsValid())
|
|
{
|
|
continue;
|
|
}
|
|
|
|
//
|
|
// Here we Have to Handle Guard Actions ...
|
|
|
|
//
|
|
// Close ...
|
|
bool canClose =
|
|
NotEmpty(iGuard.ticket) &&
|
|
iGuard.action == X_121_SMC_GUARD_ACTION_CLOSE;
|
|
|
|
//
|
|
// Close All ...
|
|
bool canCloseAll = iGuard.action == X_121_SMC_GUARD_ACTION_CLOSE_ALL;
|
|
|
|
//
|
|
// Close Longs ...
|
|
bool canCloseLongs = iGuard.action == X_121_SMC_GUARD_ACTION_CLOSE_LONGS;
|
|
|
|
//
|
|
// Close Shorts ...
|
|
bool canCloseShorts = iGuard.action == X_121_SMC_GUARD_ACTION_CLOSE_SHORTS;
|
|
|
|
//
|
|
// Partial Close ...
|
|
bool canPartialClose =
|
|
NotEmpty(iGuard.ticket) &&
|
|
iGuard.volumeMultiplier > 0 &&
|
|
iGuard.action == X_121_SMC_GUARD_ACTION_PARTIAL_CLOSE;
|
|
|
|
//
|
|
// Trail Stop ...
|
|
bool canTrailStop =
|
|
iGuard.sl > 0 &&
|
|
NotEmpty(iGuard.ticket) &&
|
|
iGuard.action == X_121_SMC_GUARD_ACTION_TRAIL_STOP;
|
|
|
|
//
|
|
// Trail Target ...
|
|
bool canTrailTarget =
|
|
iGuard.tp > 0 &&
|
|
NotEmpty(iGuard.ticket) &&
|
|
iGuard.action == X_121_SMC_GUARD_ACTION_TRAIL_TARGET;
|
|
|
|
//
|
|
// Hedge ...
|
|
bool canHedge = iGuard.action == X_121_SMC_GUARD_ACTION_HEDGE;
|
|
|
|
//
|
|
// Check is Guard Valid ...
|
|
bool isValidGuardAction =
|
|
canHedge ||
|
|
canClose ||
|
|
canCloseAll ||
|
|
canTrailStop ||
|
|
canCloseLongs ||
|
|
canTrailTarget ||
|
|
canCloseShorts ||
|
|
canPartialClose;
|
|
if (!isValidGuardAction)
|
|
{
|
|
continue;
|
|
}
|
|
|
|
//
|
|
// Now we Sure to Have a Valid Guard ...
|
|
// Start to o Guard Actions ...
|
|
|
|
//
|
|
// Close All ...
|
|
if (canCloseAll)
|
|
{
|
|
//
|
|
XPosition positions[];
|
|
int positionsCount =
|
|
mTrader
|
|
.GetPositions(
|
|
positions, // Result ...
|
|
iGuard.symbol,
|
|
iGuard.provider,
|
|
NULL, // Period ...
|
|
X_POSITION_TYPE_ALL, // All Types ...
|
|
true // Filter by Magic ...
|
|
);
|
|
if (IsValidSize(positionsCount))
|
|
{
|
|
//
|
|
string comment = "Guard Close All ...";
|
|
string message = "Guard Close All (" + ToString(positionsCount) + ") Positions Successfully ...";
|
|
int forceClosed = mTradeHandler.ForceClose(
|
|
positions,
|
|
comment,
|
|
message //
|
|
);
|
|
|
|
//
|
|
if (forceClosed)
|
|
{
|
|
RestEA(120 * 60);
|
|
}
|
|
}
|
|
}
|
|
|
|
//
|
|
// Close Longs ...
|
|
if (canCloseLongs)
|
|
{
|
|
//
|
|
XPosition longPositions[];
|
|
XPosition shortPositions[];
|
|
mTrader
|
|
.GetPositions(
|
|
longPositions, // Result ...
|
|
shortPositions, // Result ...
|
|
iGuard.symbol,
|
|
iGuard.provider,
|
|
NULL, // Period ...
|
|
true // Filter by Magic ...
|
|
);
|
|
|
|
//
|
|
int longPositionsCount = ArraySize(longPositions);
|
|
bool hasLongPositions = IsValidSize(longPositionsCount);
|
|
|
|
//
|
|
int shortPositionsCount = ArraySize(shortPositions);
|
|
bool hasShortPositions = IsValidSize(shortPositionsCount);
|
|
|
|
//
|
|
if (hasLongPositions)
|
|
{
|
|
//
|
|
string comment = "Guard Close Longs ...";
|
|
string message = "Guard Close (" + ToString(longPositionsCount) + ") Long Positions Successfully ...";
|
|
bool isClosed = mTradeHandler.ForceClose(
|
|
longPositions,
|
|
comment,
|
|
message //
|
|
);
|
|
|
|
//
|
|
if (isClosed)
|
|
{
|
|
RestEA(120 * 60);
|
|
}
|
|
}
|
|
}
|
|
|
|
//
|
|
// Close Shorts ...
|
|
if (canCloseShorts)
|
|
{
|
|
//
|
|
XPosition longPositions[];
|
|
XPosition shortPositions[];
|
|
mTrader
|
|
.GetPositions(
|
|
longPositions, // Result ...
|
|
shortPositions, // Result ...
|
|
iGuard.symbol,
|
|
iGuard.provider,
|
|
NULL, // Period ...
|
|
true // Filter by Magic ...
|
|
);
|
|
|
|
//
|
|
int longPositionsCount = ArraySize(longPositions);
|
|
bool hasLongPositions = IsValidSize(longPositionsCount);
|
|
|
|
//
|
|
int shortPositionsCount = ArraySize(shortPositions);
|
|
bool hasShortPositions = IsValidSize(shortPositionsCount);
|
|
|
|
//
|
|
if (hasShortPositions)
|
|
{
|
|
//
|
|
string comment = "Guard Close Shorts ...";
|
|
string message = "Guard Close (" + ToString(shortPositionsCount) + ") Short Positions Successfully ...";
|
|
bool isClosed = mTradeHandler.ForceClose(
|
|
shortPositions,
|
|
comment,
|
|
message //
|
|
);
|
|
|
|
//
|
|
if (isClosed)
|
|
{
|
|
RestEA(120 * 60);
|
|
}
|
|
}
|
|
}
|
|
|
|
//
|
|
// Close ...
|
|
if (canClose)
|
|
{
|
|
//
|
|
XPosition iPosition;
|
|
bool hasPosition = mTrader.GetPosition(
|
|
iGuard.ticket,
|
|
iPosition //
|
|
);
|
|
|
|
//
|
|
bool isLong = IsLong(iPosition.type);
|
|
|
|
//
|
|
if (hasPosition)
|
|
{
|
|
//
|
|
XPosition positions[];
|
|
AddRef(
|
|
iPosition,
|
|
positions //
|
|
);
|
|
|
|
//
|
|
string comment = "Guard Close ...";
|
|
string message = "Guard Close " + (isLong ? "Long" : "Short") + " Position: (" + ToString(iGuard.ticket) + ") Successfully ...";
|
|
bool isClosed = mTradeHandler.ForceClose(
|
|
positions,
|
|
comment,
|
|
message //
|
|
);
|
|
|
|
//
|
|
if (isClosed)
|
|
{
|
|
RestEA(120 * 60);
|
|
}
|
|
}
|
|
}
|
|
|
|
//
|
|
// Partial Close ...
|
|
if (canPartialClose)
|
|
{
|
|
//
|
|
XPosition iPosition;
|
|
bool hasPosition = mTrader.GetPosition(
|
|
iGuard.ticket,
|
|
iPosition //
|
|
);
|
|
|
|
//
|
|
bool isLong = IsLong(iPosition.type);
|
|
|
|
//
|
|
if (hasPosition)
|
|
{
|
|
//
|
|
// Normalize Volume Multiplier ...
|
|
double vMult = iGuard.volumeMultiplier;
|
|
if (vMult > 0.5)
|
|
{
|
|
vMult = 0.5;
|
|
}
|
|
if (vMult < 0)
|
|
{
|
|
vMult = 0.5;
|
|
}
|
|
|
|
//
|
|
// Calculate and Normalize Volume ...
|
|
double volume = iPosition.volume * iGuard.volumeMultiplier;
|
|
volume = NormalizeVolume(
|
|
volume,
|
|
iPosition.symbol //
|
|
);
|
|
|
|
//
|
|
string comment = "Guard Partially Closed: " + ToString(volume) + " ...";
|
|
string message = "Guard Partially Closed " + (isLong ? "Long" : "Short") + ": " + ToString(volume) + " Successfully ...";
|
|
bool isModified = mTrader.ClosePartial(
|
|
iPosition.ticket,
|
|
volume,
|
|
comment //
|
|
);
|
|
if (isModified)
|
|
{
|
|
//
|
|
mTradeHandler.Alert(message);
|
|
|
|
//
|
|
if (volume == iPosition.volume)
|
|
{
|
|
//
|
|
mTradeHandler.Finish(
|
|
iPosition.ticket,
|
|
iPosition,
|
|
comment //
|
|
);
|
|
|
|
//
|
|
RestEA(120 * 60);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
//
|
|
// Hedge ...
|
|
if (canHedge)
|
|
{
|
|
//
|
|
XPosition positions[];
|
|
int positionsCount =
|
|
mTrader
|
|
.GetPositions(
|
|
positions, // Result ...
|
|
iGuard.symbol,
|
|
iGuard.provider,
|
|
NULL, // Period ...
|
|
X_POSITION_TYPE_ALL, // All Types ...
|
|
true // Filter by Magic ...
|
|
);
|
|
if (IsValidSize(positionsCount))
|
|
{
|
|
//
|
|
int longs = 0;
|
|
double longProfits = 0;
|
|
double longVolumes = 0;
|
|
|
|
//
|
|
int shorts = 0;
|
|
double shortProfits = 0;
|
|
double shortVolumes = 0;
|
|
CountPositions(
|
|
positions,
|
|
longs,
|
|
longProfits,
|
|
longVolumes,
|
|
shorts,
|
|
shortProfits,
|
|
shortVolumes //
|
|
);
|
|
|
|
//
|
|
int count = longs + shorts;
|
|
double profits = longProfits + shortProfits;
|
|
double volumes = longVolumes + shortVolumes;
|
|
|
|
//
|
|
bool canDoHedge =
|
|
count > 0 &&
|
|
profits > 0 &&
|
|
volumes > 0;
|
|
if (canDoHedge)
|
|
{
|
|
//
|
|
string comment = "Guard Hedge ...";
|
|
string message = "Guard Hedge (" + ToString(count) + ") Positions by: (" + ToString(profits) + ") Successfully ...";
|
|
bool isClosed = mTradeHandler.ForceClose(
|
|
positions,
|
|
comment,
|
|
message //
|
|
);
|
|
|
|
//
|
|
if (isClosed)
|
|
{
|
|
RestEA(120 * 60);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
//
|
|
// Trail Stop ...
|
|
if (canTrailStop)
|
|
{
|
|
//
|
|
XPosition iPosition;
|
|
bool hasPosition = mTrader.GetPosition(
|
|
iGuard.ticket,
|
|
iPosition //
|
|
);
|
|
|
|
//
|
|
bool isLong = IsLong(iPosition.type);
|
|
|
|
//
|
|
bool canDoTrail =
|
|
hasPosition &&
|
|
iPosition.profit > 0 &&
|
|
isLong
|
|
? iPosition.price > iGuard.sl &&
|
|
(iPosition.sl == 0 ||
|
|
iPosition.sl < iGuard.sl)
|
|
: iPosition.price < iGuard.sl &&
|
|
(iPosition.sl == 0 ||
|
|
iPosition.sl > iGuard.sl);
|
|
if (canDoTrail)
|
|
{
|
|
//
|
|
string comment = "Guard Trail Stop ...";
|
|
string message = "Guard Trail " + (isLong ? "Long" : "Short") + " Position: (" + ToString(iGuard.ticket) + ") Stop Successfully ...";
|
|
bool isModified = mTrader.Modify(
|
|
iPosition.ticket,
|
|
iGuard.sl,
|
|
iPosition.tp,
|
|
comment //
|
|
);
|
|
if (isModified)
|
|
{
|
|
mTradeHandler.Alert(message);
|
|
}
|
|
}
|
|
}
|
|
|
|
//
|
|
// Trail Target ...
|
|
if (canTrailTarget)
|
|
{
|
|
//
|
|
XPosition iPosition;
|
|
bool hasPosition = mTrader.GetPosition(
|
|
iGuard.ticket,
|
|
iPosition //
|
|
);
|
|
|
|
//
|
|
bool isLong = IsLong(iPosition.type);
|
|
|
|
//
|
|
bool canDoTrail =
|
|
hasPosition &&
|
|
iGuard.tp != iPosition.tp &&
|
|
isLong
|
|
? iGuard.tp > iPosition.price
|
|
: iGuard.tp < iPosition.price;
|
|
if (canDoTrail)
|
|
{
|
|
//
|
|
string comment = "Guard Trail Target ...";
|
|
string message = "Guard Trail " + (isLong ? "Long" : "Short") + " Position: (" + ToString(iGuard.ticket) + ") Target Successfully ...";
|
|
bool isModified = mTrader.Modify(
|
|
iPosition.ticket,
|
|
iPosition.sl,
|
|
iGuard.tp,
|
|
comment //
|
|
);
|
|
if (isModified)
|
|
{
|
|
mTradeHandler.Alert(message);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
//
|
|
Clean(guards);
|
|
ArrayFree(guards);
|
|
}
|
|
|
|
//////////////////////////////////////////////////////////////////
|
|
|
|
|
|
// //
|
|
// eaTB.Clean();
|
|
// eaSignal.Clean();
|
|
// eaConditions.Clean();
|
|
// eaCatbConditions.Clean();
|
|
|
|
// //
|
|
// bool has = false;
|
|
// int barIndex = 0;
|
|
// int cBarIndex = 0;
|
|
|
|
// //
|
|
// // Check Bar Processing ...
|
|
// if (!eaBarTraker.CanProcessBar())
|
|
// {
|
|
// return;
|
|
// }
|
|
|
|
// //
|
|
// eaHelper.Check(barIndex);
|
|
|
|
// //
|
|
// // Reading Required Bars ...
|
|
// XOHCL zBar;
|
|
// XOHCL cBar;
|
|
// has = zBar.Init(
|
|
// _Symbol,
|
|
// _Period,
|
|
// barIndex //
|
|
// );
|
|
// has =
|
|
// has &&
|
|
// zBar.GetPreviousBar(cBar);
|
|
// if (!has)
|
|
// {
|
|
// //
|
|
// zBar.Clean();
|
|
// cBar.Clean();
|
|
|
|
// //
|
|
// return;
|
|
// }
|
|
|
|
// //
|
|
// cBarIndex = barIndex + 1;
|
|
|
|
// //
|
|
// // Update All Required Buffers ...
|
|
// eaHelper.Update(cBarIndex);
|
|
|
|
// //
|
|
// // Check Trigger Block Detected or Not ...
|
|
// if (tradeUsingTriggerBlock)
|
|
// {
|
|
// //
|
|
// // Trigger Block Detection ...
|
|
// has = DetectTriggerBlock1(
|
|
// eaHelper,
|
|
// eaBarAnalyser,
|
|
// eaTB,
|
|
// eaCatbConditions,
|
|
// eaValidations,
|
|
// eaFilters,
|
|
// barIndex,
|
|
// 10 // Loopback ...
|
|
// );
|
|
|
|
// //
|
|
// if (has)
|
|
// {
|
|
// //
|
|
// // Wait for Next Bar for anything ...
|
|
// eaBarTraker.Waits();
|
|
|
|
// //
|
|
// // Prepare Target ...
|
|
// double targets[];
|
|
// for (int i = 1; i <= eaSignalR2R; i++)
|
|
// {
|
|
// //
|
|
// Add(
|
|
// (double)i,
|
|
// targets //
|
|
// );
|
|
// }
|
|
|
|
// //
|
|
// // Calculate SL Additions ...
|
|
// double iATR = 0;
|
|
// if (barIndex > 0 &&
|
|
// barIndex < ArraySize(eaHelper.atrBuffer))
|
|
// {
|
|
// iATR = eaHelper.atrBuffer[barIndex];
|
|
// }
|
|
// double slAdditional = eaSLAtrMultiplier * iATR;
|
|
|
|
// //
|
|
// // Preparing Signal ...
|
|
// has = ToSignal(
|
|
// eaTB.trigger,
|
|
// eaTB.signal,
|
|
// targets,
|
|
// slAdditional //
|
|
// );
|
|
|
|
// //
|
|
// // Draw Trigger Block ...
|
|
// DrawTriggerBlock(
|
|
// eaTB,
|
|
// eaDrawer,
|
|
// eaObjects,
|
|
// NULL //
|
|
// );
|
|
|
|
// //
|
|
// // Attach Signal ...
|
|
// eaSignal = eaTB.signal;
|
|
|
|
// //
|
|
// // Check Signal Type ...
|
|
// bool isLong = IsLong(eaSignal.type);
|
|
|
|
// //
|
|
// // Update Entry ...
|
|
// eaSignal.entry = GetEntry(
|
|
// eaSignal.symbol,
|
|
// eaSignal.type //
|
|
// );
|
|
|
|
// //
|
|
// // Update Signal Targets ...
|
|
// UpdateSignalTargets(eaSignal);
|
|
|
|
// //
|
|
// // Handle Volume Management ...
|
|
// double iVolume = eaVolume.CalculateVolume(eaSignal);
|
|
// if (iVolume > X_MIN_VOLUME &&
|
|
// iVolume != eaSignal.volume)
|
|
// {
|
|
// eaSignal.volume = iVolume;
|
|
// }
|
|
|
|
// //
|
|
// eaConditions.sl = eaSignal.sl;
|
|
// eaConditions.tp = eaSignal.tp;
|
|
// eaConditions.pivot = eaSignal.sl;
|
|
// eaConditions.time = eaSignal.time;
|
|
// eaConditions.reward = eaSignalR2R;
|
|
// eaConditions.point = eaSignal.entry;
|
|
// eaConditions.entry = eaSignal.entry;
|
|
// eaConditions.symbol = eaSignal.symbol;
|
|
// eaConditions.period = eaSignal.period;
|
|
// eaConditions.setupTime = eaSignal.time;
|
|
// eaConditions.triggerTime = eaSignal.time;
|
|
// eaConditions.decisionZone = eaTB.trigger;
|
|
// eaConditions.provider = eaSignal.provider;
|
|
// eaConditions.conditions = eaCatbConditions;
|
|
// eaConditions.type = ToPositionType(eaSignal.type);
|
|
// eaConditions.dir =
|
|
// isLong
|
|
// ? X_DIRECTION_BULLISH
|
|
// : X_DIRECTION_BEARISH;
|
|
// Copy(
|
|
// eaSignal.targets,
|
|
// eaConditions.targets //
|
|
// );
|
|
// eaSignal.conditions =
|
|
// eaCatbConditions
|
|
// .GenerateSummary(
|
|
// false, // Commons ...
|
|
// true, // Conditions ...
|
|
// true, // Scores ...
|
|
// false // Ignore Falses ...
|
|
// );
|
|
|
|
// //
|
|
// // Execute Signal ...
|
|
// has = eaTradeManager.Execute(
|
|
// eaSignal,
|
|
// eaConditions //
|
|
// );
|
|
// }
|
|
|
|
// //
|
|
// eaTB.Clean();
|
|
// }
|
|
|
|
// //
|
|
// // Trade Using Market Conditions ...
|
|
// if (tradeUsingMarketConditions1)
|
|
// {
|
|
// //
|
|
// XBoxZone signalZone;
|
|
// X121XCatbConditions conditions;
|
|
// has = DetectSignalZone(
|
|
// eaDrawer,
|
|
// eaHelper,
|
|
// eaBarAnalyser,
|
|
// signalZone,
|
|
// conditions,
|
|
// barIndex //
|
|
// );
|
|
// if (has)
|
|
// {
|
|
// //
|
|
// // Convert Box to Signal ...
|
|
// }
|
|
// }
|
|
|
|
// //
|
|
// // Cleanup Resources ...
|
|
|
|
// //
|
|
// zBar.Clean();
|
|
// cBar.Clean();
|
|
// eaTB.Clean();
|
|
// eaSignal.Clean();
|
|
// eaConditions.Clean();
|
|
// eaCatbConditions.Clean();
|