diff --git a/Experts/Nkanven/TheChallenger.ex5 b/Experts/Nkanven/TheChallenger.ex5 index e073faa..dec76b8 100644 Binary files a/Experts/Nkanven/TheChallenger.ex5 and b/Experts/Nkanven/TheChallenger.ex5 differ diff --git a/Experts/Nkanven/TheChallenger.mq5 b/Experts/Nkanven/TheChallenger.mq5 index dfcf95a..c67b857 100644 --- a/Experts/Nkanven/TheChallenger.mq5 +++ b/Experts/Nkanven/TheChallenger.mq5 @@ -7,12 +7,14 @@ #property link "https://www.mql5.com" #property version "1.00" +#include #include //EA paramters #include //Trading hours checks #include //Trading conditions checks #include //Trading conditions checks #include //Lot size calculator #include //Lot size calculator +#include //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(); diff --git a/Include/Nkanven/TheChallenger/CloseTransactions.mqh b/Include/Nkanven/TheChallenger/CloseTransactions.mqh index ed3bd1b..ab1575d 100644 --- a/Include/Nkanven/TheChallenger/CloseTransactions.mqh +++ b/Include/Nkanven/TheChallenger/CloseTransactions.mqh @@ -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; + } } } //+------------------------------------------------------------------+ diff --git a/Include/Nkanven/TheChallenger/Parameters.mqh b/Include/Nkanven/TheChallenger/Parameters.mqh index d925249..08473a5 100644 --- a/Include/Nkanven/TheChallenger/Parameters.mqh +++ b/Include/Nkanven/TheChallenger/Parameters.mqh @@ -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;