# pls help me to Calculate Position Size with Balance and ATR.

Hello Guys , i want to  Create a personal panel for trading ,Everything is fine

Except that the calculation of lot size according to ATR does not work correctly.

Lot size calculations should be according to account balance and atr...

//+------------------------------------------------------------------+
//|                        Copyright 2019, MetaQuotes Software Corp. |

//+------------------------------------------------------------------+
#property version   "1.00"
#property strict

#include <stderror.mqh>
#include <stdlib.mqh>

#include "Include/Components.mqh"

int PointToPip=1;
double pips;

extern double Risk_Percent = 1;       // per cent of free margin of a single offer

int bar_offset=1;//bar offset to acquire size
double risk=1;//risk %

//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+

double CalculateLotSize(double atr, double risk)
{
double lot = 0.01; // مقدار اولیه lot

double tickValue = MarketInfo(Symbol(), MODE_TICKVALUE);
double minLot = MarketInfo(Symbol(), MODE_MINLOT); //
double maxLot = MarketInfo(Symbol(), MODE_MAXLOT); //

double tickSize = MarketInfo(Symbol(), MODE_TICKSIZE); //

double atrValue = atr * tickSize; //

double totalRisk = tickValue * atrValue * risk; //

if (totalRisk != 0)
{
lot = NormalizeDouble(totalRisk / AccountEquity() * 100, 2); //
}

if (lot > maxLot)
{
lot = maxLot; //
}
else if (lot < minLot)
{
lot = minLot; //
}

return lot;
}

int OnInit()
{

// If the digits are 3 or 5, we normalize multiplying by 10.
if ((Digits == 3) || (Digits == 5)){
pips =_Point* 10;
PointToPip = PointToPip * 10;
}
else
pips=_Point;
//---
MyPanel();
//---
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnTimer(){

}

void OnDeinit(const int reason)
{
//---
}
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
{
//---

double PointToPip = MarketInfo(Symbol(), MODE_TICKVALUE);
// If the digits are 3 or 5, we normalize multiplying by 10.
if ((Digits == 3) || (Digits == 5)){
PointToPip = PointToPip * 10;
}

//double lot=get_lot_by_bar_size_and_risk(bar_offset,risk);
//double stoplosspips = NormalizeDouble( stoplosspips.Text(lot),_Digits );
//double lotsizestop = NormalizeDouble( lotsizestop.Text(lot),2);
double riskedit=StrToDouble( lotedit.Text() );
int Atrper=StrToInteger( atredit.Text() );
int stopedit=StrToInteger( stoplossedit.Text() );
double Atrvalue=iATR(_Symbol,_Period,Atrper,0);
double showatr=NormalizeDouble(rightatr.Text(Atrvalue),_Digits);
double showpips=NormalizeDouble(rightpips.Text(Atrvalue*stopedit),_Digits);
showatr=NormalizeDouble(showatr,2);
double riskbalance=(AccountBalance()/100.0)*riskedit;
int showriskbalance=NormalizeDouble(rightrisk.Text(riskbalance),_Digits);

}
//+------------------------------------------------------------------+
//| ChartEvent function                                              |
//+------------------------------------------------------------------+
void OnChartEvent(const int id,
const long &lparam,
const double &dparam,
const string &sparam)
{
//---

//--- Place Noticable for Buy Position
{
int ans = MessageBox("Are you sure to place a BUY order?","Place Order",MB_YESNO | MB_ICONQUESTION);

if(ans == IDYES)
}

//--- Place Noticable for Sell Position
if(id==CHARTEVENT_OBJECT_CLICK && sparam=="Sell Button")
{
int ans = MessageBox("Are you sure to place a SELL order?","Place Order",MB_YESNO | MB_ICONQUESTION);

if(ans == IDYES)
PlaceOrder(OP_SELL);
}

//--- Place Noticable for Buy Position
{
int ans = MessageBox("Are you sure to place a BUY order?","Place Order",MB_YESNO | MB_ICONQUESTION);

if(ans == IDYES)
}

//--- Place Noticable for Sell Position
if(id==CHARTEVENT_OBJECT_CLICK && sparam=="Sell limit Button")
{
int ans = MessageBox("Are you sure to place a SELL order?","Place Order",MB_YESNO | MB_ICONQUESTION);

if(ans == IDYES)
PlaceOrder(OP_SELLLIMIT);
}

}
//+------------------------------------------------------------------+
//|  Place Order  Market                                                   |
//+------------------------------------------------------------------+
void PlaceOrder(int type)
{

//--- Initialize Variables (openprice, stoploss, takeprofit)
double lotsize=StrToDouble( lotedit.Text() );
int Atrper=StrToInteger( atredit.Text() );
int stopedit=StrToInteger( stoplossedit.Text() );
double tpedit=StrToDouble( Rewardedit.Text() );
double Atrvalue=iATR(_Symbol,_Period,Atrper,0);
double openprice  = 0  ;
double stoplosss   = Atrvalue*stopedit  ;
double takeprofit = 0 ;

//      double lotsize = StrToDouble( lotedit.Text() );
//     //double Candleback = StrToDouble( Candleback.Text() );
//   double Rewardprofit = StrToDouble( Rewardprofit.Text() );
//
//   double StopLoss=StrToDouble( Candleback.Text() );

double lot= CalculateLotSize(Atrvalue,lotsize);
double stoploss=0;

{
//---openprice, stoploss, takeprofit for Buy Position
stoploss    = NormalizeDouble(openprice - stoplosss,_Digits);
takeprofit  = NormalizeDouble(openprice + (stoplosss*tpedit),_Digits);

}

//---SELL Position Checking...
if(type == OP_SELL)
{
//---openprice, stoploss, takeprofit for Sell Position
openprice = Bid;
stoploss    = NormalizeDouble(openprice + stoplosss,_Digits);
takeprofit  = NormalizeDouble(openprice - (stoplosss*tpedit),_Digits);
}

if( OrderSend(_Symbol,type,lot,openprice,30,stoploss,takeprofit) == -1)
Print("unable to place order due to \"",ErrorDescription(GetLastError()),"\"");

}

//double LotSizeCalc()
//{
//
//
//   int Atrper=StrToInteger( atredit.Text() );
//   int stopedit=StrToInteger( stoplossedit.Text() );
//   double tpedit=StrToDouble( Rewardedit.Text() );
//   double Atrvalue=iATR(_Symbol,_Period,Atrper,0);
//   double stoplosss   = NormalizeDouble((Atrvalue*stopedit)*pips,_Digits);
//
//Risk_Percent=StrToDouble( lotedit.Text());
//double lotsize=0;
//double Tick_Value=MarketInfo(_Symbol,MODE_TICKVALUE)*PointToPip;
//double Account_Size=AccountBalance();
//
//double Risk=Account_Size*Risk_Percent*0.01; //1000*1*0.01=10\$;
//
//lotsize=(Risk/(stoplosss*1.0))/Tick_Value;
//lotsize=NormalizeDouble(lotsize,2);
//
//double minlot=MarketInfo(_Symbol,MODE_MINLOT);
//double maxlot=MarketInfo(_Symbol,MODE_MAXLOT);
//if(lotsize<=0)
//   lotsize=minlot;
//   else if(lotsize>maxlot)
//   lotsize=maxlot;
//
//   return lotsize;
//
//   }

Here is an example: https://www.mql5.com/en/code/23886

It's for mq4 but just study the calculation.

Position Size Calculator - Based on VP Money Management rules
• www.mql5.com
This indicator is based on the Money Management approach used by VP from nononsenseforex.com It uses ATR to calculate Stop Loss, Take Profit and Volume you should enter into a trade based on the percentage of your Equity you want to risk.

aliasgari524:

Hello Guys , i want to  Create a personal panel for trading ,Everything is fine

Except that the calculation of lot size according to ATR does not work correctly.

Lot size calculations should be according to account balance and atr...

Hello , why are you multiplying by the tick size here ?

double atrValue = atr * tickSize; //

You are looking for "how many points the atr is" there right ?

If yes then divide by tickSize.