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
@@ -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;