Equity DD watcher added

This commit is contained in:
Nkondog Anselme
2022-03-13 00:00:52 +01:00
parent 6f1689c152
commit d32c204f22
4 changed files with 49 additions and 60 deletions
Binary file not shown.
+10 -3
View File
@@ -7,12 +7,14 @@
#property link "https://www.mql5.com"
#property version "1.00"
#include <Trade\Trade.mqh>
#include <Nkanven\TheChallenger\Parameters.mqh> //EA paramters
#include <Nkanven\TheChallenger\TradingHour.mqh> //Trading hours checks
#include <Nkanven\TheChallenger\Prechecks.mqh> //Trading conditions checks
#include <Nkanven\TheChallenger\ScanPositions.mqh> //Trading conditions checks
#include <Nkanven\TheChallenger\LotSizeCal.mqh> //Lot size calculator
#include <Nkanven\TheChallenger\EntriesManager.mqh> //Lot size calculator
#include <Nkanven\TheChallenger\CloseTransactions.mqh> //Emergency close of transaction
//+------------------------------------------------------------------+
//| Expert initialization function |
//+------------------------------------------------------------------+
@@ -57,8 +59,8 @@ void OnTick()
TimeCurrent(dt);
SymbolInfoTick(_Symbol,last_tick);
//isPinBar();
//isPinBar();
CheckOperationHours();
CheckPreChecks();
@@ -69,7 +71,7 @@ void OnTick()
atr.Refresh(-1);
gAtr = atr.Main(1);
maHT.Refresh(-1);
gHtMa = maHT.Main(1);
@@ -80,6 +82,11 @@ void OnTick()
return;
Print("Good for trading...");
Print("gEmergencyClose ", gEmergencyClose);
drawdownWatcher();
Print("gEmergencyClose ", gEmergencyClose);
CloseTransactions();
getSignal();
ExecuteEntry();
@@ -11,48 +11,12 @@ CTrade trade;
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
void CloseTransactions(ENUM_SIGNAL_EXIT tType)
void CloseTransactions()
{
bool result = true;
int cnt = OrdersTotal();
int cts = PositionsTotal();
if(cnt > 0)
{
for(int i = cnt-1; i>=0; i--)
{
ulong ticket = OrderGetTicket(i);
if(OrderSelect(ticket))
{
if(tType==SIGNAL_EXIT_BUY && (OrderGetInteger(ORDER_TYPE) == ORDER_TYPE_BUY_LIMIT || OrderGetInteger(ORDER_TYPE) == ORDER_TYPE_BUY_STOP) && OrderGetInteger(ORDER_MAGIC)==InpMagicNumber)
{
if(!trade.OrderDelete(ticket))
Print("Error (", GetLastError(), ") while deleting all buy orders");
}
else
{
if(tType==SIGNAL_EXIT_SELL && (OrderGetInteger(ORDER_TYPE) == ORDER_TYPE_SELL_LIMIT || OrderGetInteger(ORDER_TYPE) == ORDER_TYPE_SELL_STOP) && OrderGetInteger(ORDER_MAGIC)==InpMagicNumber)
{
if(!trade.OrderDelete(ticket))
Print("Error (", GetLastError(), ") while deleting all sell orders");
}
else
{
if(tType==SIGNAL_EXIT_ALL)
{
if(!trade.OrderDelete(ticket))
Print("Error (", GetLastError(), ") while deleting all orders");
}
}
}
}
else
{
Print("Error (", GetLastError(), ") while selecting order's ticket");
}
}
}
ulong theTicket;
double positionProfit = 0.0;
if(cts > 0)
{
@@ -61,34 +25,50 @@ void CloseTransactions(ENUM_SIGNAL_EXIT tType)
ulong ticket = PositionGetTicket(i);
if(PositionSelectByTicket(ticket))
{
if(tType==SIGNAL_EXIT_BUY && PositionGetInteger(POSITION_TYPE) == POSITION_TYPE_BUY && PositionGetInteger(POSITION_MAGIC)==InpMagicNumber)
if(PositionGetInteger(POSITION_MAGIC)==InpMagicNumber)
{
if(!trade.PositionClose(ticket))
Print("Error (", GetLastError(), ") while deleting all buy positions");
}
else
{
if(tType==SIGNAL_EXIT_SELL && PositionGetInteger(POSITION_TYPE) == POSITION_TYPE_SELL && PositionGetInteger(POSITION_MAGIC)==InpMagicNumber)
if(positionProfit > PositionGetDouble(POSITION_PROFIT))
{
if(!trade.PositionClose(ticket))
Print("Error (", GetLastError(), ") while deleting all sell positions");
}
else
{
if(tType==SIGNAL_EXIT_ALL)
{
if(!trade.PositionClose(ticket))
Print("Error (", GetLastError(), ") while deleting all positions");
}
positionProfit = PositionGetDouble(POSITION_PROFIT);
theTicket = ticket;
}
}
}
else
{
Print("Error (", GetLastError(), ") while selecting position by ticket");
}
}
if(gEmergencyClose)
{
if(!trade.PositionClose(theTicket))
Print("Error (", GetLastError(), ") while deleting all buy positions");
}
}
}
//+------------------------------------------------------------------+
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
void drawdownWatcher()
{
double amountDiff, equity, balance, percentDiff;
balance=AccountInfoDouble(ACCOUNT_BALANCE);
equity=AccountInfoDouble(ACCOUNT_EQUITY);
gEmergencyClose = false;
if(equity < balance)
{
amountDiff = balance - equity;
percentDiff = (amountDiff*100)/balance;
Print("amountDiff ", amountDiff, " percentDiff ", percentDiff, " InpMaxDrawdown ", InpMaxDrawdown);
if(InpMaxDrawdown < percentDiff)
{
gEmergencyClose = true;
}
}
}
//+------------------------------------------------------------------+
@@ -85,6 +85,7 @@ input ENUM_MODE_SL InpStopLossMode=SL_FIXED; //Stop L
input int InpDefaultStopLoss=200; //Default Stop Loss In Points (0=No Stop Loss)
input int InpMinStopLoss=0; //Minimum Allowed Stop Loss In Points
input int InpMaxStopLoss=5000; //Maximum Allowed Stop Loss In Points
input double InpMaxDrawdown=20.0; //Max DD level
input ENUM_MODE_TP InpTakeProfitMode=TP_FIXED; //Take Profit Mode
input int InpDefaultTakeProfit=60; //Default Take Profit In Points (0=No Take Profit)
input int InpMinTakeProfit=0; //Minimum Allowed Take Profit In Points
@@ -129,6 +130,7 @@ bool gIsPreChecksOk=false; //Indicates if the pre checks are sat
bool gIsSpreadOK=false; //Indicates if the spread is low enough to trade
bool IsSpreadOK=false;
bool IsLosing=false;
bool gEmergencyClose=false; //Urgently close losing trade
double gLotSize=InpDefaultLotSize;