Files
MQL4Data/Bkp/BTCUSD-15M/Libraries/x-saherelm.signal.lib.mq4
T
2024-01-25 04:05:58 +03:30

1757 lines
45 KiB
Plaintext

///////////////////////////////////////////////////////
//
// SaherElm IT Center MQL4 Signal Draw Global Library
// ---------------------------------------------------
// saherelm useful tools and definitions ...
//
//
// Maintainer:
// ------------
// Hadi Khazaee Asl (hadi_khazaee_asl@yahoo.com)
//
//////////////////////////////////////////////////////
#property library
#property copyright "Copyright 2023, SaherElm IT Center"
#property link "https://www.saherelm.ir"
#property version "1.00"
#property strict
//
// Includes library ...
#include "../Libraries/x-saherelm.lib.mq4"
//
// Includes Indicator library ...
#include "../Libraries/x-saherelm.indicator.lib.mq4"
//
// Includes Models library ...
#include "../Libraries/x-saherelm.models.lib.mq4"
//
// Includes Drawing library ...
#include "../Libraries/x-saherelm.draw.lib.mq4"
//
#include "../Libraries/x-saherelm.x.signal.lib.mq4"
#include "../Libraries/x-saherelm.xr.signal.lib.mq4"
#include "../Libraries/x-saherelm.xtpw.signal.lib.mq4"
//
// START Global Requirement Functions ...
//
//
// END Global Requirement Functions ...
//
//
// START Signal Related Functions ...
//
//
// Retrieve Signal Bar Index ...
int GetSignalBarIndex(XSignal &signal) {
//
// Temp Result ...
int result = -1;
//
// Retriev Signal Bar Index ...
result = iBarShift(
_Symbol,
_Period,
signal.time
);
//
return result;
}
//
string GetSignalProviderIdentifier(
ENUM_X_SIGNAL_PROVIDER provider
) {
//
string result = "";
//
switch (provider) {
//
case X_XXX_PROVIDER:
result = "XXX";
break;
//
case X_XR_PROVIDER:
result = "XR";
break;
//
case X_XTPW_PROVIDER:
result = "XTPW";
break;
//
default:
result = "";
break;
}
//
return result;
}
//
ENUM_X_SIGNAL_PROVIDER GetSignalProvider(string identifier) {
//
ENUM_X_SIGNAL_PROVIDER result = X_UNKNOWN_PROVIDER;
//
if (
StringFind(
identifier,
GetSignalProviderIdentifier(X_XXX_PROVIDER)
) > -1
) {
result = X_XXX_PROVIDER;
} else if (
StringFind(
identifier,
GetSignalProviderIdentifier(X_XR_PROVIDER)
) > -1
) {
result = X_XR_PROVIDER;
} else if (
StringFind(
identifier,
GetSignalProviderIdentifier(X_XTPW_PROVIDER)
) > -1
) {
result = X_XTPW_PROVIDER;
}
//
return result;
}
//
// Retrieve Signal Lots Per Trade ...
double GetSignalLottage(
const XSignal &signal,
const double defLotsPerTrade
) {
//
double result = defLotsPerTrade;
//
bool isLong = signal.type == X_SIGNAL_LONG;
//
switch (signal.provider) {
//
case X_XR_PROVIDER:
//
result = isLong ?
xRLongLotsPerTradePercent :
xRShortLotsPerTradePercent
;
break;
//
case X_XXX_PROVIDER:
//
result = isLong ?
xLongLotsPerTradePercent :
xShortLotsPerTradePercent
;
break;
//
case X_XTPW_PROVIDER:
//
result = isLong ?
xTPWLongLotsPerTradePercent :
xTPWShortLotsPerTradePercent
;
break;
}
//
return result;
}
//
// Draw Specific Range On Chart ...
void DrawRange(
const XRange &range,
const long chartID = 0,
const int subWindowID = 0,
const string tag = "",
const color clr = clrYellow
) {
//
// Loop through Levels Count ...
for (int i = 0; i <= range.levels; i++) {
//
datetime time1 = iTime(
_Symbol,
_Period,
1
);
//
datetime time2 = iTime(
_Symbol,
_Period,
0
);
//
double price = range.lowestLow + (i * range.rate);
//
string lbl = StringConcatenate(
tag,
"_XRL_",
price
);
//
DrawTrendLine(
chartID,
lbl,
subWindowID,
time1,
price,
time2,
price,
clr,
STYLE_DOT
);
}
}
//
// Draw Signal Shapes on Chart ...
bool DrawSignal(
const XSignal &signal,
long chartID = 0,
int subWindowID = 0,
const double positionThresholdFactor = 10,
string longSignalLabel = "X-Long",
color longSignalColor = clrAqua,
string shortSignalLabel = "X-Short",
color shortSignalColor = clrFuchsia
) {
//
// Generate isLongSignal Var ...
bool isLongSignal = signal.type == X_SIGNAL_LONG;
//
// Signal Bar Index ...
int bar_index = iBarShift(
_Symbol,
_Period,
signal.time
);
//
// START Draw Signal Arrow ...
//
//
// Arrow Shape Name ...
string arrowName = StringConcatenate(signal.tag, "_Arrow_", signal.id);
//
// Arrow Price ...
double arrowPrice = 0;
if (isLongSignal) {
//
arrowPrice = iLow(
_Symbol,
_Period,
bar_index + 1
);
//
arrowPrice = arrowPrice - (positionThresholdFactor * _Point);
} else {
//
arrowPrice = iHigh(
_Symbol,
_Period,
bar_index + 1
);
//
arrowPrice = arrowPrice + (positionThresholdFactor * _Point);
}
//
// Arrow Code ...
uchar arrowCode = isLongSignal ? SYMBOL_ARROWUP : SYMBOL_ARROWDOWN;
//
// Arrow Anchor Point ...
ENUM_ARROW_ANCHOR arrowAnchorPoint = isLongSignal ? ANCHOR_BOTTOM : ANCHOR_TOP;
//
// Arrow Color ...
color arrowColor = isLongSignal ? longSignalColor : shortSignalColor;
//
// Draw Signal Arrow Shape ...
bool result = DrawArrow(
chartID,
arrowName,
subWindowID,
signal.time,
arrowPrice,
arrowCode,
arrowAnchorPoint,
arrowColor
);
//
// END Draw Signal Arrow ...
//
//
// START Draw Signal SL/TP ...
//
string slLineName = StringConcatenate(signal.tag, "_SL_", signal.id);
string tpLineName = StringConcatenate(signal.tag, "_TP_", signal.id);
string entryLineName = StringConcatenate(signal.tag, "_ENTRY_", signal.id);
//
color slColor = shortSignalColor; // isLongSignal ? shortSignalColor : longSignalColor;
color tpColor = longSignalColor; // isLongSignal ? shortSignalColor : longSignalColor;
color entryColor = clrGreen;
//
datetime time1 = iTime(
_Symbol,
_Period,
bar_index + 1
);
//
// Draw Enrty Price ...
result = DrawTrendLine(
chartID,
entryLineName,
subWindowID,
time1,
signal.entry,
signal.time,
signal.entry,
entryColor,
STYLE_SOLID,
2
);
//
// Draw Signal SL ...
if (signal.sl > 0) {
//
result = DrawTrendLine(
chartID,
slLineName,
subWindowID,
time1,
signal.sl,
signal.time,
signal.sl,
slColor,
STYLE_SOLID,
2
);
}
//
// Draw Signal TP ...
if (signal.tp > 0) {
//
result = DrawTrendLine(
chartID,
tpLineName,
subWindowID,
time1,
signal.tp,
signal.time,
signal.tp,
tpColor,
STYLE_SOLID,
2
);
}
//
// END Draw Signal SL/TP ...
//
//
// Start Draw Signal Label ...
//
//
string labelName = StringConcatenate(signal.tag, "_Label_", signal.id);
//
double labelPrice = isLongSignal ? arrowPrice - (positionThresholdFactor * _Point) : arrowPrice + (positionThresholdFactor * _Point);
//
string labelText = isLongSignal ? longSignalLabel : shortSignalLabel;
labelText = StringConcatenate(
labelText,
"_",
GetSignalProviderIdentifier(signal.provider)
);
//
result = DrawText(
chartID,
labelName,
subWindowID,
signal.time,
labelPrice,
labelText,
"Arial",
8,
arrowColor,
0,
ANCHOR_CENTER
);
//
// End Draw Signal Label ...
//
//
return result;
}
//
// Trade on Specific Signal ...
bool TradeSignal(
XSignal &signal,
//
double volumePercent,
//
// Long ...
const string longSignalLabel,
const color longSignalColor,
//
// Short ...
const string shortSignalLabel,
const color shortSignalColor
) {
//
// Temp Result ...
bool result = false;
//
if (initialBalance == 0) {
initialBalance = AccountInfoDouble(ACCOUNT_BALANCE);
}
//
double balance = AccountInfoDouble(ACCOUNT_BALANCE);
int openPositions = CountOpenTrades();
double volume = (volumePercent * initialBalance);
//
if (
signal.type != X_SIGNAL_LONG &&
signal.type != X_SIGNAL_SHORT
) {
return result;
}
//
bool hasLongSignal = signal.type == X_SIGNAL_LONG;
//
// Check TP and Entry Price ...
if (
(
hasLongSignal
&& signal.entry > signal.tp
) ||
(
!hasLongSignal
&& signal.entry < signal.tp
)
) {
return result;
}
//
// Order Operation ...
int operation = hasLongSignal ?
OP_BUY :
OP_SELL;
//
// Detect Trade Color ...
color clr = hasLongSignal ?
longSignalColor :
shortSignalColor;
//
// Generate Signal Comment ...
string label = hasLongSignal ?
longSignalLabel :
shortSignalLabel;
//
// Generate Signal Comment ...
string comment = StringConcatenate(
signal.tag,
"_", signal.id,
"_", GetSignalProviderIdentifier(signal.provider),
"_", label
);
//
// Placing Order ...
int orderTicket = OrderSend(
_Symbol,
operation,
volume,
signal.entry,
3,
signal.sl,
signal.tp,
comment,
signal.id,
0,
clr
);
//
// Draw Signal Shape if Order Opened Successfully ...
if (orderTicket >= 0) {
//
signal.ticket = orderTicket;
//
// Here i must to Draw Signal Shapes ...
bool isDrawn = DrawSignal(
signal,
0,
0,
50,
longSignalLabel, // Long Signal Label ...
longSignalColor,
shortSignalLabel, // Short Signal Label ...
shortSignalColor
);
//
LogMessage(
StringConcatenate(
"Order [", orderTicket ,"]: Provided by: ", GetSignalProviderIdentifier(signal.provider)
)
);
//
// TODO: here we have to check if there are another
// open positions which their entry price is bigger
// than current signal entry price, for long trades and
// their tp is high than current tp, ...
// close in profit trades or modify orders tp to current ...
// //
// if (CountOpenTrades() > 0) {
// //
// // Here we have to
// ModifyOpenTradesBasedOnCurrentTrade(signal);
// }
//
result = true;
}
//
return result;
}
//
// END Signal Related Functions ...
//
//
// START Orders Functions ...
//
//
// Convert To XSignal Model ...
XSignal OrderToSignal() {
//
// Temp Result ...
XSignal result = {};
result.type = X_SIGNAL_NONE;
//
// Here we can get some usefull info based on selected order ...
double swap = OrderSwap();
double lots = OrderLots();
int ticket = OrderTicket();
double profit = OrderProfit();
double stopLoss = OrderStopLoss();
double openPrice = OrderOpenPrice();
datetime openTime = OrderOpenTime();
int magicNumber = OrderMagicNumber();
double takeProfit = OrderTakeProfit();
string comment = OrderComment();
//
ENUM_X_SIGNAL_PROVIDER provider = GetSignalProvider(comment);
//
// find order type ...
bool isLongOrder = OrderType() == OP_BUY;
//
// Conver Position to Signal ...
result.sl = stopLoss;
result.tp = takeProfit;
result.time = openTime;
result.symbol = _Symbol;
result.id = magicNumber;
result.comment = comment;
result.entry = openPrice;
result.provider = provider;
result.type = isLongOrder ? X_SIGNAL_LONG : X_SIGNAL_SHORT;
//
return result;
}
//
// START Count Orders ...
//
//
// Count Open Positions ...
int CountOpenTrades() {
//
int result = 0;
//
// Loop through Orders ...
for (int i = 0; i < OrdersTotal(); i++) {
//
// Select indexed Order ...
if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) {
//
// Check Order ...
if (OrderSymbol() == _Symbol) {
result++;
}
}
}
//
// Return Result ...
return result;
}
//
// Count Open Long Positions ...
int CountOpenLongTrades() {
//
int result = 0;
//
// Loop through Orders ...
for (int i = 0; i < OrdersTotal(); i++) {
//
// Select indexed Order ...
if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) {
//
// Check Order ...
if (
OrderSymbol() == _Symbol
&& OrderType() == OP_BUY
) {
result++;
}
}
}
//
// Return Result ...
return result;
}
//
// Count Open Short Positions ...
int CountOpenShortTrades() {
//
int result = 0;
//
// Loop through Orders ...
for (int i = 0; i < OrdersTotal(); i++) {
//
// Select indexed Order ...
if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) {
//
// Check Order ...
if (
OrderSymbol() == _Symbol
&& OrderType() == OP_SELL
) {
result++;
}
}
}
//
// Return Result ...
return result;
}
//
// END Count Orders ...
//
//
// START Close Orders ...
//
//
// Close All Open Trades ...
bool CloseAllTrades() {
//
// Loop through all Positions ...
bool result = false;
for (int i = OrdersTotal(); i >= 0; i--) {
//
// Check Order Selected or not ...
if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) {
//
// Check Order Symbol is same as Current Symbol ...
if (OrderSymbol() == _Symbol) {
//
double lots = OrderLots();
int ticket = OrderTicket();
//
// find order type ...
bool isLongOrder = OrderType() == OP_BUY;
//
// find close price ...
double closePrice = isLongOrder ?
SymbolInfoDouble(_Symbol, SYMBOL_BID) :
SymbolInfoDouble(_Symbol, SYMBOL_ASK);
//
// Since because of equity ...
// we have to ckose allpositions ...
bool isOrderClosed = OrderClose(
ticket,
lots,
closePrice,
clrYellow
);
//
if (isOrderClosed) {
//
LogMessage(
StringConcatenate(
"Order [", ticket, "] Closed because of All Trade Closing issue ..."
)
);
}
//
result =
result
|| isOrderClosed;
}
}
}
//
return result;
}
//
bool CloseAllProviderTrades(
ENUM_X_SIGNAL_PROVIDER provider
) {
//
bool result = false;
//
// Loop through Signals ...
for (int i = OrdersTotal(); i >= 0; i--) {
//
// Check Order Selected or not ...
if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) {
//
// Check Order Symbol is same as Current Symbol ...
if (OrderSymbol() == _Symbol) {
//
XSignal signal = OrderToSignal();
//
if (signal.provider == provider) {
//
double lots = OrderLots();
int ticket = OrderTicket();
//
// find order type ...
bool isLongOrder = OrderType() == OP_BUY;
//
// find close price ...
double closePrice = isLongOrder ?
SymbolInfoDouble(_Symbol, SYMBOL_BID) :
SymbolInfoDouble(_Symbol, SYMBOL_ASK);
//
// Since because of equity ...
// we have to ckose allpositions ...
bool isOrderClosed = OrderClose(
ticket,
lots,
closePrice,
clrYellow
);
//
if (isOrderClosed) {
//
LogMessage(
StringConcatenate(
"Order [", ticket, "] Closed because of All Provider Long Trades Closing issue ..."
)
);
}
//
result =
result
|| isOrderClosed;
}
}
}
}
//
return result;
}
//
bool CloseAllProviderLongTrades(
ENUM_X_SIGNAL_PROVIDER provider
) {
//
bool result = false;
//
// Loop through Signals ...
for (int i = OrdersTotal(); i >= 0; i--) {
//
// Check Order Selected or not ...
if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) {
//
// Check Order Symbol is same as Current Symbol ...
if (OrderSymbol() == _Symbol) {
//
XSignal signal = OrderToSignal();
//
if (
signal.provider == provider
&& signal.type == X_SIGNAL_LONG
) {
//
double lots = OrderLots();
int ticket = OrderTicket();
//
// find order type ...
bool isLongOrder = OrderType() == OP_BUY;
//
// find close price ...
double closePrice = isLongOrder ?
SymbolInfoDouble(_Symbol, SYMBOL_BID) :
SymbolInfoDouble(_Symbol, SYMBOL_ASK);
//
// Since because of equity ...
// we have to ckose allpositions ...
bool isOrderClosed = OrderClose(
ticket,
lots,
closePrice,
clrYellow
);
//
if (isOrderClosed) {
//
LogMessage(
StringConcatenate(
"Order [", ticket, "] Closed because of All Provider Long Trades Closing issue ..."
)
);
}
//
result =
result
|| isOrderClosed;
}
}
}
}
//
return result;
}
//
bool CloseAllProviderShortTrades(
ENUM_X_SIGNAL_PROVIDER provider
) {
//
bool result = false;
//
// Loop through Signals ...
for (int i = OrdersTotal(); i >= 0; i--) {
//
// Check Order Selected or not ...
if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) {
//
// Check Order Symbol is same as Current Symbol ...
if (OrderSymbol() == _Symbol) {
//
XSignal signal = OrderToSignal();
//
if (
signal.provider == provider
&& signal.type == X_SIGNAL_SHORT
) {
//
double lots = OrderLots();
int ticket = OrderTicket();
//
// find order type ...
bool isLongOrder = OrderType() == OP_BUY;
//
// find close price ...
double closePrice = isLongOrder ?
SymbolInfoDouble(_Symbol, SYMBOL_BID) :
SymbolInfoDouble(_Symbol, SYMBOL_ASK);
//
// Since because of equity ...
// we have to ckose allpositions ...
bool isOrderClosed = OrderClose(
ticket,
lots,
closePrice,
clrYellow
);
//
if (isOrderClosed) {
//
LogMessage(
StringConcatenate(
"Order [", ticket, "] Closed because of All Provider Long Trades Closing issue ..."
)
);
}
//
result =
result
|| isOrderClosed;
}
}
}
}
//
return result;
}
//
// Close All Open Long Trades ...
bool CloseAllLongTrades() {
//
// Loop through all Positions ...
bool result = false;
for (int i = OrdersTotal(); i >= 0; i--) {
//
// Check Order Selected or not ...
if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) {
//
// Check Order Symbol is same as Current Symbol ...
if (OrderSymbol() == _Symbol) {
//
double lots = OrderLots();
int ticket = OrderTicket();
//
// find order type ...
bool isLongOrder = OrderType() == OP_BUY;
if (isLongOrder) {
//
// find close price ...
double closePrice = isLongOrder ?
SymbolInfoDouble(_Symbol, SYMBOL_BID) :
SymbolInfoDouble(_Symbol, SYMBOL_ASK);
//
// Since because of equity ...
// we have to ckose allpositions ...
bool isOrderClosed = OrderClose(
ticket,
lots,
closePrice,
clrYellow
);
//
if (isOrderClosed) {
//
LogMessage(
StringConcatenate(
"Order [", ticket, "] Closed because of All Long Trade Closing issue ..."
)
);
}
//
result =
result
|| isOrderClosed;
}
}
}
}
//
return result;
}
//
// Close All Open Short Trades ...
bool CloseAllShortTrades() {
//
// Loop through all Positions ...
bool result = false;
for (int i = OrdersTotal(); i >= 0; i--) {
//
// Check Order Selected or not ...
if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) {
//
// Check Order Symbol is same as Current Symbol ...
if (OrderSymbol() == _Symbol) {
//
double lots = OrderLots();
int ticket = OrderTicket();
//
// find order type ...
bool isLongOrder = OrderType() == OP_BUY;
if (!isLongOrder) {
//
// find close price ...
double closePrice = isLongOrder ?
SymbolInfoDouble(_Symbol, SYMBOL_BID) :
SymbolInfoDouble(_Symbol, SYMBOL_ASK);
//
// Since because of equity ...
// we have to ckose allpositions ...
bool isOrderClosed = OrderClose(
ticket,
lots,
closePrice,
clrYellow
);
//
if (isOrderClosed) {
//
LogMessage(
StringConcatenate(
"Order [", ticket, "] Closed because of All Short Trade Closing issue ..."
)
);
}
//
result =
result
|| isOrderClosed;
}
}
}
}
//
return result;
}
//
// Close All In Profit Trades ...
bool CloseAllInProfitTrades() {
//
// Loop through all Positions ...
bool result = false;
for (int i = OrdersTotal(); i >= 0; i--) {
//
// Check Order Selected or not ...
if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) {
//
// Check Order Symbol is same as Current Symbol ...
if (OrderSymbol() == _Symbol) {
//
double lots = OrderLots();
int ticket = OrderTicket();
double profit = OrderProfit();
//
// find order type ...
bool isLongOrder = OrderType() == OP_BUY;
//
// find close price ...
double closePrice = isLongOrder ?
SymbolInfoDouble(_Symbol, SYMBOL_BID) :
SymbolInfoDouble(_Symbol, SYMBOL_ASK);
//
if (
profit > 0
&& profit > 50 * _Point
) {
//
// Since because of equity ...
// we have to ckose allpositions ...
bool isOrderClosed = OrderClose(
ticket,
lots,
closePrice,
clrYellow
);
//
if (isOrderClosed) {
//
LogMessage(
StringConcatenate(
"Order [", ticket, "] Closed because of InProfit Trade Closing issue ..."
)
);
}
//
result =
result
|| isOrderClosed;
}
}
}
}
//
return result;
}
//
// Close All In DrawDown Trades ...
bool CloseAllInDrawDownTrades() {
//
// Loop through all Positions ...
bool result = false;
for (int i = OrdersTotal(); i >= 0; i--) {
//
// Check Order Selected or not ...
if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) {
//
// Check Order Symbol is same as Current Symbol ...
if (OrderSymbol() == _Symbol) {
//
double lots = OrderLots();
int ticket = OrderTicket();
double profit = OrderProfit();
//
// find order type ...
bool isLongOrder = OrderType() == OP_BUY;
//
// find close price ...
double closePrice = isLongOrder ?
SymbolInfoDouble(_Symbol, SYMBOL_BID) :
SymbolInfoDouble(_Symbol, SYMBOL_ASK);
//
if (profit < 0) {
//
// Since because of equity ...
// we have to ckose allpositions ...
bool isOrderClosed = OrderClose(
ticket,
lots,
closePrice,
clrYellow
);
//
if (isOrderClosed) {
//
LogMessage(
StringConcatenate(
"Order [", ticket, "] Closed because of InDrawDown Trade Closing issue ..."
)
);
}
//
result =
result
|| isOrderClosed;
}
}
}
}
//
return result;
}
//
// Close Biggest In DrawDown Trades ...
bool CloseBiggestDrawDownTrade() {
//
// Loop through all Positions ...
bool result = false;
double drawDown = 0;
//
// Find Biggest In DrawDown ...
for (int i = OrdersTotal(); i >= 0; i--) {
//
// Check Order Selected or not ...
if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) {
//
// Check Order Symbol is same as Current Symbol ...
if (OrderSymbol() == _Symbol) {
//
double lots = OrderLots();
int ticket = OrderTicket();
double profit = OrderProfit();
//
// find order type ...
bool isLongOrder = OrderType() == OP_BUY;
//
// find close price ...
double closePrice = isLongOrder ?
SymbolInfoDouble(_Symbol, SYMBOL_BID) :
SymbolInfoDouble(_Symbol, SYMBOL_ASK);
//
if (
profit < 0
&& MathAbs(profit) > drawDown
) {
drawDown = profit;
}
}
}
}
//
// Close Biggest In DrawDown ...
for (int i = OrdersTotal(); i >= 0; i--) {
//
// Check Order Selected or not ...
if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) {
//
// Check Order Symbol is same as Current Symbol ...
if (OrderSymbol() == _Symbol) {
//
double lots = OrderLots();
int ticket = OrderTicket();
double profit = OrderProfit();
//
// find order type ...
bool isLongOrder = OrderType() == OP_BUY;
//
// find close price ...
double closePrice = isLongOrder ?
SymbolInfoDouble(_Symbol, SYMBOL_BID) :
SymbolInfoDouble(_Symbol, SYMBOL_ASK);
//
if (
profit < 0
&& MathAbs(profit) >= drawDown
) {
//
// Since because of equity ...
// we have to ckose allpositions ...
bool isOrderClosed = OrderClose(
ticket,
lots,
closePrice,
clrYellow
);
//
if (isOrderClosed) {
//
LogMessage(
StringConcatenate(
"Order [", ticket, "] Closed because of Max InDrawDown Trade Closing issue ..."
)
);
}
//
result =
result
|| isOrderClosed;
}
}
}
}
//
return result;
}
//
// Close All Long Time Trades ...
bool CloseAllLongTimeTrades(
const int passedCandles,
const ENUM_X_SIGNAL_PROVIDER provider = X_UNKNOWN_PROVIDER
) {
//
bool result = false;
//
if (passedCandles <= 0) {
return result;
}
//
int currentBarIndex = iBarShift(
_Symbol,
_Period,
TimeCurrent()
);
//
int openTrades = CountOpenTrades();
//
// Loop through all Positions ...
for (int i = OrdersTotal(); i >= 0; i--) {
//
// Check Order Selected or not ...
if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) {
//
// Check Order Symbol is same as Current Symbol ...
if (OrderSymbol() == _Symbol) {
//
double lots = OrderLots();
int ticket = OrderTicket();
double profit = OrderProfit();
datetime openTime = OrderOpenTime();
int openBarIndex = iBarShift(
_Symbol,
_Period,
openTime
);
string comment = OrderComment();
//
ENUM_X_SIGNAL_PROVIDER orderProvider = GetSignalProvider(comment);
bool isProviderVerified =
provider == X_UNKNOWN_PROVIDER ?
true :
orderProvider != X_UNKNOWN_PROVIDER
&& orderProvider == provider
;
//
if (
profit < 0
&& isProviderVerified
&& openBarIndex - currentBarIndex >= passedCandles
// || (
// profit > 0.5
// && openBarIndex - currentBarIndex >= passedCandles - (passedCandles / 3)
// )
) {
//
// find order type ...
bool isLongOrder = OrderType() == OP_BUY;
//
// find close price ...
double closePrice = isLongOrder ?
SymbolInfoDouble(_Symbol, SYMBOL_BID) :
SymbolInfoDouble(_Symbol, SYMBOL_ASK);
//
// Since because of equity ...
// we have to ckose allpositions ...
bool isOrderClosed = OrderClose(
ticket,
lots,
closePrice,
clrYellow
);
//
if (isOrderClosed) {
//
LogMessage(
StringConcatenate(
"Order [", ticket, "] Closed because of Long Time issue ..."
)
);
}
//
result =
result
|| isOrderClosed;
}
// else if (
// openTrades > 0
// && openBarIndex - currentBarIndex >= passedCandles - (passedCandles / 2)
// ) {
// result = CloseAllInProfitTrades()
// && CloseBiggestDrawDownTrade();
// }
}
}
}
//
return result;
}
//
// Close all trades which passed Maximum Drawdown ...
bool CloseAllMaximumDrawDownPassedTrades(
const double maxDrDownPercentPerPTrade,
const ENUM_X_SIGNAL_TYPE type = X_SIGNAL_NONE,
const ENUM_X_SIGNAL_PROVIDER provider = X_UNKNOWN_PROVIDER
) {
//
if (
initialBalance <= 0
|| maxDrDownPercentPerPTrade <= 0
) {
return false;
}
//
// Retrieve Account Balance ...
double balance = AccountInfoDouble(ACCOUNT_BALANCE);
double maxAllowedDrawDown = -1 * (initialBalance * maxDrDownPercentPerPTrade);
//
// Loop through all Positions ...
bool result = false;
for (int i = OrdersTotal(); i >= 0; i--) {
//
// Check Order Selected or not ...
if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) {
//
// Check Order Symbol is same as Current Symbol ...
if (OrderSymbol() == _Symbol) {
//
double lots = OrderLots();
int ticket = OrderTicket();
double profit = OrderProfit();
string comment = OrderComment();
//
// find order type ...
bool isLongOrder = OrderType() == OP_BUY;
bool isTypeVerified =
type == X_SIGNAL_NONE ?
true :
( isLongOrder
&& type == X_SIGNAL_LONG
) || (
!isLongOrder
&& type == X_SIGNAL_SHORT
)
;
//
ENUM_X_SIGNAL_PROVIDER orderProvider = GetSignalProvider(comment);
bool isProviderVerified =
provider == X_UNKNOWN_PROVIDER ?
true :
orderProvider != X_UNKNOWN_PROVIDER
&& orderProvider == provider
;
//
if (
profit < 0
&& isTypeVerified
&& isProviderVerified
&& MathAbs(profit) >= MathAbs(maxAllowedDrawDown)
) {
//
// find close price ...
double closePrice = isLongOrder ?
SymbolInfoDouble(_Symbol, SYMBOL_BID) :
SymbolInfoDouble(_Symbol, SYMBOL_ASK);
//
// Since because of equity ...
// we have to ckose allpositions ...
bool isOrderClosed = OrderClose(
ticket,
lots,
closePrice,
clrYellow
);
//
if (isOrderClosed) {
//
LogMessage(
StringConcatenate(
"Order [", ticket, "] Closed because of Maximum DarwDown issue ..."
)
);
}
//
result =
result
|| isOrderClosed;
}
}
}
}
//
return result;
}
//
// Close All Trades Which Opens before Specified Length ...
bool CloseAllTradesWhichOpenInPastMarketLength(
const int marketLen
) {
//
bool result = false;
//
if (marketLen <= 0) {
return result;
}
//
int currentBarIndex = iBarShift(
_Symbol,
_Period,
TimeCurrent()
);
//
int openTrades = CountOpenTrades();
//
// Loop through all Positions ...
for (int i = OrdersTotal(); i >= 0; i--) {
//
// Check Order Selected or not ...
if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) {
//
// Check Order Symbol is same as Current Symbol ...
if (OrderSymbol() == _Symbol) {
//
double lots = OrderLots();
int ticket = OrderTicket();
double profit = OrderProfit();
datetime openTime = OrderOpenTime();
int openBarIndex = iBarShift(
_Symbol,
_Period,
openTime
);
//
if (openBarIndex <= currentBarIndex + marketLen) {
//
// find order type ...
bool isLongOrder = OrderType() == OP_BUY;
//
// find close price ...
double closePrice = isLongOrder ?
SymbolInfoDouble(_Symbol, SYMBOL_BID) :
SymbolInfoDouble(_Symbol, SYMBOL_ASK);
//
// Since because of equity ...
// we have to ckose allpositions ...
bool isOrderClosed = OrderClose(
ticket,
lots,
closePrice,
clrYellow
);
//
if (isOrderClosed) {
//
LogMessage(
StringConcatenate(
"Order [", ticket, "] Closed because of Market Reverse issue ..."
)
);
}
//
result =
result
|| isOrderClosed;
} else if (openTrades > 0) {
//
// Close all InProfit Trdes and Biggest InDrawDown Trades ...
// result = CloseAllInProfitTrades();
// && CloseBiggestDrawDownTrade();
}
}
}
}
//
return result;
}
//
// END Close Orders ...
//
//
// START Modify Orders ...
//
//
void ModifyAllPriceLesserOpenTrades(
const XSignal &signal
) {
//
// Loop through all Open Trades ...
for (int i = OrdersTotal(); i >= 0; i--) {
//
// Check Order Selected or not ...
if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) {
//
// Check Order Symbol is same as Current Symbol ...
if (OrderSymbol() == _Symbol) {
//
double lots = OrderLots();
int ticket = OrderTicket();
double profit = OrderProfit();
double stopLoss = OrderStopLoss();
double openPrice = OrderOpenPrice();
datetime openTime = OrderOpenTime();
double takeProfit = OrderTakeProfit();
//
// find order type ...
bool isLongOrder = OrderType() == OP_BUY;
//
// find close price ...
double closePrice = isLongOrder ?
SymbolInfoDouble(_Symbol, SYMBOL_BID) :
SymbolInfoDouble(_Symbol, SYMBOL_ASK);
//
// When new Signal Comming with price less than
// currently open trade and tp less than currently
// open trade, this means a market change happens ...
// here we can Close InProfit Trades or Modify Trades TP ...
if (
(
openPrice < signal.entry
|| MathAbs(openPrice - signal.entry) < 300 * _Point
)
&& takeProfit > signal.tp
) {
bool result = OrderModify(
ticket,
openPrice,
signal.sl,
signal.tp,
clrYellow
);
}
}
}
}
}
//
void ModifyOpenTradesBasedOnCurrentTrade(
const XSignal &signal
) {
//
ModifyAllPriceLesserOpenTrades(signal);
}
//
// END Modify Orders ...
//
//
// END Orders Functions ...
//
//
// START Signal Conditions ...
//
//
// Clear Signal Conditions ...
void ClearSignalConditions() {
//
ClearLongSignalConditions();
ClearShortSignalConditions();
}
//
// Clear all Conditions Waiter for Long Signals ...
void ClearLongSignalConditions() {
//
ClearXLongSignalConditions();
ClearXRLongSignalConditions();
ClearXTPWLongSignalConditions();
}
//
// Clear all Conditions Waiter for Short Signals ...
void ClearShortSignalConditions() {
//
ClearXShortSignalConditions();
ClearXRShortSignalConditions();
ClearXTPWShortSignalConditions();
}
//
// END Signal Conditions ...
//