Position Size Calculation

 

Hello Guys,

I am a beginner in MQL5, I am still in the "testing phase", I did something very simple to start practicing, I have the following problem, I made a calculation for the batch that will be applied to each trade, I am currently testing with EUR / USD , on a demo account of 5,000, 1% risk, so in each trade there is a risk of losing $ 50, however when I take the test and the price hits stopLoss, my loss is $ 200.


//+------------------------------------------------------------------+
//|                                           EMA_RSI_Stochastic.mq5 |
//|                        Copyright 2019, MetaQuotes Software Corp. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2019, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
#include <Trade\Trade.mqh>
CTrade trade;



// Inputs
input int         percentageAccountRiskLimitPerTrade = 1;     // Account Risk Limit Per Trade
input int         pipRiskOnATrade                    = 200;   // Pip Risk on a Trade
input double      lot                                = 0.01;  // Lot Size                                    


// Variables
double            stopLoss;
double            takeProfit;
double            exponentialMovingAverageArray5[];
double            exponentialMovingAverageArray10[];
double            accountRiskMoney;
double            lotSize;

int               exponentialMovingAverage5;
int               exponentialMovingAverage10;


// Strutures
MqlTick           ticks;



int OnInit()
  {
   
   exponentialMovingAverage5 = iMA(_Symbol, _Period, 5, 0, MODE_EMA, PRICE_CLOSE);    
   exponentialMovingAverage10 = iMA(_Symbol, _Period, 10, 0, MODE_EMA, PRICE_CLOSE);
        
   ArraySetAsSeries(exponentialMovingAverageArray5, true);
   ArraySetAsSeries(exponentialMovingAverageArray10, true);
       
   return(INIT_SUCCEEDED);
   
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---,
   
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {            
          
    CopyBuffer(exponentialMovingAverage5, 0, 0, 3, exponentialMovingAverageArray5);
    CopyBuffer(exponentialMovingAverage10, 0, 0, 3, exponentialMovingAverageArray10);
  
  
    SymbolInfoTick(_Symbol, ticks);
           

        
    if(exponentialMovingAverageArray5[0] > exponentialMovingAverageArray10[0] && PositionsTotal() == 0) {
    
       CalculateAccountRiskLimitPerTrade();       
       lotSize        = CalculateLotSizeForATrade();
                 
       stopLoss = NormalizeDouble(ticks.ask - (pipRiskOnATrade * _Point), _Digits);
       takeProfit = NormalizeDouble(ticks.ask + (pipRiskOnATrade * _Point), _Digits);
       
       lotSize = NormalizeDouble(lotSize, 2);
                   
       trade.Buy(lotSize, _Symbol, ticks.ask, stopLoss, takeProfit, NULL); 
     }                      
  }
  
  
void CalculateAccountRiskLimitPerTrade() {

    accountRiskMoney           = ( AccountInfoDouble(ACCOUNT_BALANCE) / 100) * percentageAccountRiskLimitPerTrade;
}  


double CalculateLotSizeForATrade() {

    double positionSize        = accountRiskMoney / (pipRiskOnATrade * 0.10);
    
    return lot * positionSize;
}
//+------------------------------------------------------------------+

Could you give me a help to find the problem and tips on how to better the logic of the code below.

Thanks.

 

Check out these examples:

Money Fixed Margin
Money Fixed Margin
  • www.mql5.com
An example for calculating the lot value with a fixed margin level. That is, if you specify 10%, a position with the margin equal to 10% of free margin will be opened. Input Parameters: Initial value   is set to "warm up" the strategy tester. Then the remainder after devision of  by  (this number was chosen randomly) is calculated. It means...