Risk percentage lot size calculation

MQL5 Experts

Job finished

Execution time 1 day
Feedback from customer
Great Developer! Code works like a charm and was delivered very quickly with useful complementary information!
Feedback from employee
Excellent customer. Wishing you success in learning MQL.

Specification

Hi All,

I would like to add to an existing and working MQL5 EA the ability to calculate the lot size based on a risk percentage. Or in other words how many percent of the account I'm willing to lose for each deal.

Here are some key elements of my EA setup:

The base currency is EUR

Trading pair is EURJPY

Leverage is 1/100

SL is fixed at 50 pips

Target risk percentage per deal should be a variable set by default on 5%  

 

The below formula seem to be the one needed to achieve my lot size calculation but I'm not sure. 

Lot Size = Amount Risked / Number of Pips x Pip Value 

 

I tried the created the code myself and here is where I stopped: 

Basically I need help to compete the "//Calculating Lot Size based on SL and Risk Percentage" part of the code. 

 

Let me know if you have any questions.  

 

Different Variables for the lot size calculation: 

input int      StopLoss=50;               // Stop Loss
input double   RiskPercentage=5;          // Lots to Trade

int STP, TKP; 
double LotSize;
double Pips;



//--- Let us handle currency pairs with 5 or 3 digit prices instead of 4
   STP = StopLoss;
   TKP = TakeProfit;
   if(_Digits==5 || _Digits==3)
     {
      STP = STP*10;
      TKP = TKP*10;
     }
   return(0);
  }

 My Buying Script

if(Buy_Condition_1 && Buy_Condition_2)
     {
         // any opened position?
         if((Buy_opened)||(Sell_opened))
           {
            //Alert("We already have an open Position. Oportuinity passed.");
            return;    // Don't open a new Buy Position
           }
           
         // Calculating Lot size based on SL and Risk Percentage
         
         MqlTick latest_price;
         SymbolInfoTick(_Symbol,latest_price);
         
         Pips = (latest_price.bid - STP)/_Point;          
         
         double AccountBalance = AccountInfoDouble(ACCOUNT_BALANCE);
         double TickValue = SymbolInfoDouble(_Symbol,SYMBOL_TRADE_TICK_VALUE);
         LotSize = (AccountBalance * (RiskPercentage/100))/(Pips * TickValue);  
         
         Print("Pips ", Pips, " Acc Bal: ", AccountBalance," Tick Val ", TickValue," Lot Size ", LotSize);      
         
         //Executing the Trade 
         ZeroMemory(mrequest);
         mrequest.action = TRADE_ACTION_DEAL;                                    // immediate order execution
         mrequest.price = NormalizeDouble(latest_price.ask,_Digits);             // latest ask price
         mrequest.sl = NormalizeDouble(latest_price.ask - STP*_Point,_Digits);   // Stop Loss
         mrequest.tp = NormalizeDouble(latest_price.ask + TKP*_Point,_Digits);   // Take Profit
         mrequest.symbol = _Symbol;                                              // currency pair
         mrequest.volume = LotSize;                                                  // number of lots to trade
         mrequest.magic = EA_Magic;                                              // Order Magic Number
         mrequest.type = ORDER_TYPE_BUY;                                         // Buy Order
         mrequest.type_filling = ORDER_FILLING_FOK;                              // Order execution type
         mrequest.deviation=100;                                                 // Deviation from current price
         //--- send order
         OrderSend(mrequest,mresult);
         // get the result code
         if(mresult.retcode==10009 || mresult.retcode==10008) //Request is completed or order placed
           {
            Alert("A Buy order has been successfully placed with Ticket#:",mresult.order,"!!");
            SendNotification("BBWP added a new Buy order!");
           }
         else
           {
            Alert("The Buy order request could not be completed -error:",GetLastError());
            ResetLastError();           
            return;
           }
        
     }

 

 

Responded

1
Developer 1
Rating
(182)
Projects
342
42%
Arbitration
118
12% / 73%
Overdue
104
30%
Free
2
Developer 2
Rating
(549)
Projects
826
73%
Arbitration
15
53% / 13%
Overdue
193
23%
Working
Similar orders
Hi man, How are u? I have an EA and I need to check few aspects of the code, if It works fine? I am looking for an expert coder, who can understand mt4 language and help us to solve this out. This job will hardly take about 3-4 hours max for an experienced coder, when explained detail. Also, I would like to give this to someone, who can understand and speak English well. Also, the coder should be able to come via
Hello The EA will work on particular zone choose by the user and can mark it on any TF and with some rules can open trades and mange the trade by some unique rules. the EA need to check the difference by RSI as well and with some extra rules . developer should have good attitude and good communication (englsih) with high performence and knowledge with coding EA. THREE TYPES OF ENTRIES 1: AGGRESSIVE 2: DIVERGENCE 3
Indicator in use: Bollinger Bands Mechanism (See diagrams provided for help) Sells: 1. Trigger candle: When candle low is above the top Bollinger band - accurate to the lowest point scale (e.g. On EURUSD if candle low is 1.07915 and the value of top bollinger is 1.07914 - this is a sell signal; or if on Futures if the increment is .25 or .10 then this is used) 2. Enter sell ONLY on the next candle if price breaks
Indicator in use: Bollinger Bands Mechanism: (See diagrams provided for help) Sells: 1. Trigger candle: When candle low is above the top Bollinger band - accurate to the point scale (e.g. On EURUSD if candle low is 1.07915 and the value of top bollinger is 1.07914 - this is a sell signal) 2. Enter sell ONLY on the next candle if price breaks below the trigger candle LOW (using the e.g. above- if next candle price
hey guys, im looking for an auto mt5 license system through a web app i have already, i simply want it so a unique license key is generated for memebers, they input this onto the EA input and then it checks if its valid. active license per user capped at 5. i already have an mt5 coder and dec team for the app so i dont know if youd prefer to intergrate onto the webapp yourself or simply provide the code and doc so my
I need a simple panel to execute both buy and sell operations with very basic things like stopp loss take profit that functions for both market orders such as buy stop sell stop buy limit sell limit I don't care about colors or design I just want how you can do it what interests me most are the functions
I want to create an EA that can take bids according to information of a logic I have developed to give indication of a BUY or SELL opportunity. The EA will then be able to activate the BUY at the lowest possible position once the indicator clears it for a BUY and take bid upwards or identify the highest point and clears it for a SELL and take bids downwards. As you can see from example of JULY 2024 data to see how
We are seeking a skilled developer who can convert a Tradingview indicator written in Pinescript to a NinjaTrader indicator written in C#. The goal is to create a profitable trading strategy using this indicator. The successful candidate will have expertise in both Pinescript and C# programming languages, as well as a strong understanding of trading indicators and strategies. The main responsibilities will include
I am seeking an experienced MQL5 developer to create a user-friendly manual Grid Trading Expert Advisor (EA) with the following key features: Dynamic Grid Trading: Adjustable Grid Distance: Traders can manually input grid distance in pips via an intuitive, movable table. Take Profit Management: Fixed TP for the initial positions (e.g., first 5 trades). Stop-Loss (Optional): Traders can choose to use a stop-loss with
I want have the possibility to increase lotsize not alone by Lot-multiplier rather I want add a fix-lot increase for excample for 0,05 lot. I want have this for buy / sell and hedge-buy and hedge sell

Project information

Budget
10 - 20 USD
For the developer
9 - 18 USD
Deadline
from 1 to 2 day(s)