Calculating the cost of 1 lot, for any pair / digits / decimals / broker

 

How to calculate the cost in dollars of entering with 1 lot at given price ( EntryPrice ) with given StopLoss price ( SLprice ) ?

double RiskPoints = MathAbs(SLprice - EntryPrice) + MathAbs((Ask - Bid));//The number of points we risk to loose

double RiskDollars = RiskPoints * MarketInfo(Symbol(),MODE_TICKVALUE);            //The number of dollars we risk to loose 

The (more or less) above seems to work ok for eg. EURUSD, but not for the more special pairs like Silver... for Silver I get a cost that is approx 1/5 of what it should be... so I enter with 5 times the intended lots !

It obviously has to do with PointSize, TickSize, TickValue, Points, and Digits .

There must be a "fully automatic" way to get this right for any digits, decimals, and 4 or 5 digit brokers. But I can't get my head around this.

Any suggestions ?

Here are MarketInfos that I extracted for some pairs:

------- Alpari (UK) Ltd.     MarketInfo for EURUSD at 2011.02.25 13:49 -------
Point size in the quote currency: ------------------------------  0.00001
Count of digits after decimal point in the symbol prices: ------  5
Spread value in points: ----------------------------------------  4
Stop level in points: ------------------------------------------  20
Lot size in the base currency: ---------------------------------  100000
Tick value in the deposit currency: ----------------------------  1.00000000
Tick size in the quote currency: -------------------------------  0.00001000

------- Alpari (UK) Ltd.     MarketInfo for USDJPY at 2011.02.25 13:52 -------
Point size in the quote currency: ------------------------------  0.001
Count of digits after decimal point in the symbol prices: ------  3
Spread value in points: ----------------------------------------  10
Stop level in points: ------------------------------------------  30
Lot size in the base currency: ---------------------------------  100000
Tick value in the deposit currency: ----------------------------  1.22234446
Tick size in the quote currency: -------------------------------  0.00100000

------- Alpari (UK) Ltd.     MarketInfo for XAGUSD at 2011.02.25 13:52 -------
Point size in the quote currency: ------------------------------  0.01
Count of digits after decimal point in the symbol prices: ------  2
Spread value in points: ----------------------------------------  4
Stop level in points: ------------------------------------------  10
Lot size in the base currency: ---------------------------------  5000
Tick value in the deposit currency: ----------------------------  50.00000000
Tick size in the quote currency: -------------------------------  0.01000000

------- Alpari (UK) Ltd.     MarketInfo for XAUUSD at 2011.02.25 13:52 -------
Point size in the quote currency: ------------------------------  0.01
Count of digits after decimal point in the symbol prices: ------  2
Spread value in points: ----------------------------------------  50
Stop level in points: ------------------------------------------  100
Lot size in the base currency: ---------------------------------  100
Tick value in the deposit currency: ----------------------------  5.00000000
Tick size in the quote currency: -------------------------------  0.05000000
 

I posted up the fully-automated code for this a while back, see my posts and links throughout the following thread: https://www.mql5.com/en/forum/129553

Works for any currency pair, regardless of 4D/5D, and is broker agnostic, etc. Haven't found a broker that it does not work for but I'm sure there are some.

Reason: