381 lines
6.9 KiB
Plaintext
381 lines
6.9 KiB
Plaintext
|
|
//
|
|
// Generate SL Support Signal ...
|
|
bool GenerateSLSupportSignal(
|
|
XSignal &signal, // Holds Result ....
|
|
ulong ticket, // Specific Position Ticket ...
|
|
double slGapPoint = 10, // Which Distance to SL for Enabling Signal ...
|
|
double volumeMultiplier = 2 // Support Signals Volume Multiplier ...
|
|
)
|
|
{
|
|
//
|
|
bool result = false;
|
|
|
|
//
|
|
int count = CountPositions();
|
|
if (count <= 0)
|
|
{
|
|
return result;
|
|
}
|
|
|
|
//
|
|
bool hasSupport = HasSupport(ticket);
|
|
if (hasSupport)
|
|
{
|
|
return result;
|
|
}
|
|
|
|
//
|
|
XPosition position;
|
|
result = GetPosition(
|
|
position,
|
|
ticket);
|
|
if (!result)
|
|
{
|
|
return result;
|
|
}
|
|
|
|
//
|
|
bool isLong =
|
|
IsLong(position.type);
|
|
|
|
//
|
|
string symbol = position.symbol;
|
|
ENUM_TIMEFRAMES period = ExtractPositionPeriod(position.comment);
|
|
double volume = position.volume * volumeMultiplier;
|
|
volume = NormalizeVolume(
|
|
volume,
|
|
symbol);
|
|
|
|
ENUM_POSITION_TYPE type = isLong
|
|
? POSITION_TYPE_SELL
|
|
: POSITION_TYPE_BUY;
|
|
ENUM_X_ORDER_MODES mode = X_ORDER_MODE_STOP;
|
|
|
|
//
|
|
double slGapPrice = PointToPrice(
|
|
symbol,
|
|
slGapPoint);
|
|
double riskPrice = MathAbs(position.entry - position.sl);
|
|
|
|
//
|
|
double entry =
|
|
isLong
|
|
? position.sl + slGapPrice
|
|
: position.sl - slGapPrice;
|
|
entry = NormalizePrice(entry, symbol);
|
|
|
|
//
|
|
double sl =
|
|
isLong
|
|
? entry + riskPrice
|
|
: entry - riskPrice;
|
|
sl = NormalizePrice(sl, symbol);
|
|
|
|
//
|
|
double tp =
|
|
isLong
|
|
? entry - riskPrice
|
|
: entry + riskPrice;
|
|
tp = NormalizePrice(tp, symbol);
|
|
|
|
//
|
|
signal.sl = sl;
|
|
signal.tp = tp;
|
|
signal.type = type;
|
|
signal.mode = mode;
|
|
signal.entry = entry;
|
|
signal.volume = volume;
|
|
signal.symbol = symbol;
|
|
signal.period = period;
|
|
|
|
//
|
|
return result;
|
|
}
|
|
|
|
//
|
|
// Find an Specific Position Has Support Signal Or not ...
|
|
bool HasSupport(ulong ticket)
|
|
{
|
|
//
|
|
bool result = false;
|
|
|
|
//
|
|
int count = CountPositions();
|
|
if (count <= 1)
|
|
{
|
|
return result;
|
|
}
|
|
|
|
//
|
|
XPosition position;
|
|
result = GetPosition(
|
|
position,
|
|
ticket);
|
|
if (!result)
|
|
{
|
|
return result;
|
|
}
|
|
|
|
//
|
|
XPosition positions[];
|
|
GetPositions(positions);
|
|
for (int i = 0; i < count; i++)
|
|
{
|
|
//
|
|
XPosition iPosition = positions[i];
|
|
|
|
//
|
|
string supportTicketStr = ExtractParentTicketOfSupport(iPosition.comment);
|
|
if (StringLen(supportTicketStr) == 0)
|
|
{
|
|
continue;
|
|
}
|
|
|
|
//
|
|
ulong parentTicket = (ulong)supportTicketStr;
|
|
|
|
//
|
|
result =
|
|
parentTicket == ticket &&
|
|
iPosition.ticket != parentTicket;
|
|
if (result)
|
|
{
|
|
break;
|
|
}
|
|
}
|
|
|
|
//
|
|
return result;
|
|
}
|
|
|
|
//
|
|
// Get All Support Positions for Specific Position ...
|
|
bool GetSupportPositions(
|
|
ulong ticket, // Parent Position Ticket
|
|
XPosition &supports[] // Holds Result ...
|
|
)
|
|
{
|
|
//
|
|
bool result = false;
|
|
|
|
//
|
|
Clean(supports);
|
|
|
|
//
|
|
int count = CountPositions();
|
|
if (count <= 0)
|
|
{
|
|
return result;
|
|
}
|
|
|
|
//
|
|
XPosition parentPosition;
|
|
bool positionExist = GetPosition(
|
|
parentPosition,
|
|
ticket);
|
|
if (!positionExist)
|
|
{
|
|
return result;
|
|
}
|
|
|
|
//
|
|
bool hasSupport = HasSupport(ticket);
|
|
if (!hasSupport)
|
|
{
|
|
return result;
|
|
}
|
|
|
|
//
|
|
XPosition positions[];
|
|
GetPositions(positions);
|
|
for (int i = 0; i < count; i++)
|
|
{
|
|
//
|
|
XPosition iPosition = positions[i];
|
|
|
|
//
|
|
if (iPosition.ticket == ticket)
|
|
{
|
|
continue;
|
|
}
|
|
|
|
//
|
|
string pTicketStr = ExtractParentTicketOfSupport(iPosition.comment);
|
|
if (StringLen(pTicketStr) <= 0)
|
|
{
|
|
continue;
|
|
}
|
|
|
|
//
|
|
ulong pTicket = (ulong)pTicketStr;
|
|
if (pTicket != ticket)
|
|
{
|
|
continue;
|
|
}
|
|
|
|
//
|
|
Add(
|
|
iPosition,
|
|
supports);
|
|
}
|
|
|
|
//
|
|
result = ArraySize(supports) > 0;
|
|
|
|
//
|
|
return result;
|
|
}
|
|
|
|
//
|
|
bool GetSupportOrders(
|
|
ulong ticket, // Parent Position Ticket
|
|
XOrder &supports[], // Holds Result ...
|
|
string symbol, // Trading Symbol ...
|
|
ENUM_TIMEFRAMES period, // Trading TimeFrame ...
|
|
ENUM_ORDER_STATE state = NULL // Order State ...
|
|
)
|
|
{
|
|
//
|
|
bool result = false;
|
|
|
|
//
|
|
Clean(supports);
|
|
|
|
//
|
|
XOrder orders[];
|
|
GetOrders(
|
|
orders,
|
|
symbol,
|
|
period,
|
|
state);
|
|
int ordersCount = ArraySize(orders);
|
|
result = ordersCount > 0;
|
|
if (!result)
|
|
{
|
|
return result;
|
|
}
|
|
|
|
//
|
|
for (int i = 0; i < ordersCount; i++)
|
|
{
|
|
//
|
|
XOrder iOrder = orders[i];
|
|
|
|
//
|
|
string pTicketStr = ExtractParentTicketOfSupport(iOrder.comment);
|
|
if (StringLen(pTicketStr) == 0)
|
|
{
|
|
continue;
|
|
}
|
|
|
|
//
|
|
ulong pTicket = (ulong)pTicketStr;
|
|
if (pTicket != ticket)
|
|
{
|
|
continue;
|
|
}
|
|
|
|
//
|
|
Add(
|
|
iOrder,
|
|
supports);
|
|
}
|
|
|
|
//
|
|
result = ArraySize(supports) > 0;
|
|
|
|
//
|
|
return result;
|
|
}
|
|
|
|
//
|
|
// Close a Position and it's Supports on a Profit ...
|
|
bool ClosePositionAndSupports(
|
|
ulong ticket, // Position Ticket
|
|
double minProfit = 4 // Minimum Profit For Closing
|
|
)
|
|
{
|
|
//
|
|
bool result = false;
|
|
|
|
//
|
|
if (minProfit <= 0)
|
|
{
|
|
return result;
|
|
}
|
|
|
|
//
|
|
int count = CountPositions();
|
|
if (count <= 0)
|
|
{
|
|
return result;
|
|
}
|
|
|
|
//
|
|
XPosition position;
|
|
bool isExist = GetPosition(
|
|
position,
|
|
ticket);
|
|
if (!isExist)
|
|
{
|
|
return result;
|
|
}
|
|
|
|
//
|
|
bool hasSupport = HasSupport(ticket);
|
|
if (!hasSupport)
|
|
{
|
|
return result;
|
|
}
|
|
|
|
//
|
|
XPosition supports[];
|
|
hasSupport = GetSupportPositions(
|
|
ticket,
|
|
supports);
|
|
int supportsCount = ArraySize(supports);
|
|
if (!hasSupport || supportsCount <= 0)
|
|
{
|
|
return result;
|
|
}
|
|
|
|
//
|
|
double profitSummary = 0;
|
|
for (int i = 0; i < supportsCount; i++)
|
|
{
|
|
//
|
|
XPosition iSupport = supports[i];
|
|
|
|
//
|
|
profitSummary += iSupport.profit;
|
|
}
|
|
|
|
//
|
|
result = profitSummary > 0 && profitSummary >= minProfit;
|
|
if (!result)
|
|
{
|
|
return result;
|
|
}
|
|
|
|
//
|
|
Add(
|
|
position,
|
|
supports);
|
|
|
|
//
|
|
string comment = "Close Position (" + (string)ticket + ") and it's Supports in Profit: " + (string)profitSummary + " ...";
|
|
ClosePositions(
|
|
supports,
|
|
comment);
|
|
|
|
//
|
|
if (result)
|
|
{
|
|
LogMessage(comment);
|
|
}
|
|
|
|
//
|
|
return result;
|
|
}
|