Loss Limiter

mitrecyclers

Trader
Apr 13, 2023
13
2
14
44
Hi,

Is there any EA or may be someone can help with MQL4 code, where we can limit the Loss per trade? E.g If I have 1000$ balance and if any one trade loss goes to lets say 50$, it just close that trade. I have seen the equity guardian kind of EA, which closes all when overall drawdown reaches. But I wanted to do on per trade basis. Any help is appreciated.
 

mitrecyclers

Trader
Apr 13, 2023
13
2
14
44
I am not sure why would you need an EA for that. This seems to be easy to achieve with a simple stop-loss.
Thats correct. but plan to do big lots scalping.. and with SL, needed to calculate position sizing manually.. so it is background safety.. I have seen EA, which are specifically designed for Prop firms and they close all trades when certain drawdown hits. but I want to do that very same thing based on per trade base rather than global. May be weird requirement.. but I guess it is good to introduce discipline. I have done a bit of code to achieve something like that.. but Just 2 days into MQL5 programming.. so needed to know a lot more before I can put it on live as currently it closes trades in profit as well. I just wanted to close only Drawdown trades.

MQL5:
for (int i = 0; i < PositionsTotal(); i++)
    {
      //  double totldd = TotalAllowedDrawdown;
        double posnumber= PositionSelectByTicket(PositionGetTicket(i));
 
            double lots = PositionGetDouble(POSITION_VOLUME);
            double posstate = PositionGetDouble(POSITION_PROFIT);
            long posticket = PositionGetInteger(POSITION_TICKET);
            double accbal = AccountInfoDouble(ACCOUNT_BALANCE);
 
               if (posstate<TotalAllowedDrawdown)
                  {
                  Print("Drawdown Exceeded for " + posticket +"  "+ posstate);
                  trade.PositionClosePartial(posticket,lots/2);
                  }   
    }
 
Last edited by a moderator:

Enivid

Administrator
Staff member
Nov 30, 2008
19,153
1,493
144
Odesa
www.earnforex.com
Thats correct. but plan to do big lots scalping.. and with SL, needed to calculate position sizing manually.. so it is background safety.. I have seen EA, which are specifically designed for Prop firms and they close all trades when certain drawdown hits. but I want to do that very same thing based on per trade base rather than global. May be weird requirement.. but I guess it is good to introduce discipline. I have done a bit of code to achieve something like that.. but Just 2 days into MQL5 programming.. so needed to know a lot more before I can put it on live as currently it closes trades in profit as well. I just wanted to close only Drawdown trades.

MQL5:
for (int i = 0; i < PositionsTotal(); i++)
    {
      //  double totldd = TotalAllowedDrawdown;
        double posnumber= PositionSelectByTicket(PositionGetTicket(i));
 
            double lots = PositionGetDouble(POSITION_VOLUME);
            double posstate = PositionGetDouble(POSITION_PROFIT);
            long posticket = PositionGetInteger(POSITION_TICKET);
            double accbal = AccountInfoDouble(ACCOUNT_BALANCE);
 
               if (posstate<TotalAllowedDrawdown)
                  {
                  Print("Drawdown Exceeded for " + posticket +"  "+ posstate);
                  trade.PositionClosePartial(posticket,lots/2);
                  }  
    }

Your TotalAllowedDrawdown should be set to-50, not 50, for this to work on losing trades. Also, your code will only close half the trade, not the entire trade. It will also work incorrectly if you change it to close the entire trade. This is because your cycle goes forward from the Position with index 0 to the position with index Total - 1.

When you are deleting orders or closing positions, you have to go back - from Total - 1 to 0 because if the trade is deleted the remaining trades' indices go down by 1, so you will skip trades and you will attempt to analyze trades with non-existing indices later in the title.
 

mitrecyclers

Trader
Apr 13, 2023
13
2
14
44
Your TotalAllowedDrawdown should be set to-50, not 50, for this to work on losing trades. Also, your code will only close half the trade, not the entire trade. It will also work incorrectly if you change it to close the entire trade. This is because your cycle goes forward from the Position with index 0 to the position with index Total - 1.

When you are deleting orders or closing positions, you have to go back - from Total - 1 to 0 because if the trade is deleted the remaining trades' indices go down by 1, so you will skip trades and you will attempt to analyze trades with non-existing indices later in the title.
Thanks. I will try updating it tomorrow.. yes. I know I closed half the lot. Thanks. for your help..
 
Nov 24, 2023
11
0
7
38
Its a few lines of code, You would need to set up a condition that checks the trade's current loss and closes it if it reaches a specified amount, such as $50 in your example.
 

mitrecyclers

Trader
Apr 13, 2023
13
2
14
44
Its a few lines of code, You would need to set up a condition that checks the trade's current loss and closes it if it reaches a specified amount, such as $50 in your example.
Can you do that? I managed to do it.. but I wanted to close half of it rather full lot and I am unable to do it. It throws up error.