Calculating Lot Sizes (volume) from MT4 Symbol Properties

 

Hi,

I really have a question for technical support, but as MetaQuotes don't  deal directly with clients and link them to this forum I am posting here. I am hoping someone here can help shed some light on this as my broker is unable to help with technical issues relating to MT4. And to pre-empt anyone asking whether I have searched the forums for an answer before posting ... yes I have.

Please take a look at the FXCM Risk Managment Indicator for Trading Station (i.e. not MT4): https://www.fxcmapps.com/apps/risk-management-indicator/

I have been trying to create something similar on MT4 to aid in calculating my lot size for given stoploss and risk (1% of equity). Up until now I have been using an online tool that works with my broker, but is not provided by them, to calculate lot sizes across different instruments (forex, commodities, stocks, ETFs, CFDs, etc.), but I don't want to rely on an online tool and would like to fully automate my trade calculations using MT4 without any spreadsheet hacks and info required from my broker.

Initially, I would like to run an MQL script to do all the calculations for me and enter a trade without any user requirement for manual calculation and hacks as input. You'd think this should be straight forward as MT4 has Points, TickSize and TickValue properties (no pips) that I should be able to use to perform the required lot size calculation for a given risk (1%) and stoploss (based on average true range indicator), but it isn't so easy, or even possible from what I can tell.

I have created two methods for calculating the lot size (my account is in GBP)...

 

1)      Using Points

 

[Lots from Points] = ([1% Risk in Account Currency GBP] * [GBP to Quote Rate]) / [StopSize in Points] / [Point] / [LotSize]

 

where [StopSize in Points] = [StopSize 2ATR(15)] / [Point]

 

 

2)      Using Tick Size / Tick Value

 

Lots from Ticks = [1% Risk in Account Currency GBP] / ([StopSize in Ticks] * [Tick Value GBP])

 

where [StopSize in Ticks] = [StopSize 2ATR(15)] / [TickSize]

*[Tick Value GBP] is per LOT

 

Both lot size calculations give matching answers for all Forex and a handful of CFDs, but fail for most commodities and most stocks. I can upload a spreadsheet if this helps, but to cut a long story short I am wondering whether the issue is due to my calculations, or whether it is due to MT4 symbol properties not being set up for the instruments that give me the wrong result. I compared lot size to the online calculated value to check my results. One stock I tried was ARM.L, which is a London stock exchange traded stock and is quoted in pence rather than GBP, so my calculation was off by a factor of 100, resulting in £10K risk instead of £100 (1% on £10K account). A prime example of why calculating position size is critical.

So, what am I doing wrong? Is this issue due to my broker not setting up the MT4 symbol properties for these instruments? Is this MetaQuotes responsibility? Is there a solution. Why is this so difficult?

I am guessing that the reason why MT4 Forex symbols do give correct lot size results is because the calculations for Forex are the same for all brokers, so MetaQuotes has preconfigured these on the platform.

For stocks, commodities, ETFs, etc. there are so many, and these vary from broker to broker that these are left with basic settings and are often incorrect (e.g. TickValue = 0). I can post some detailed examples if required. Now, the system does calculate profit/loss on open trades, so that tells me that it should be possible to calculate a lot size from the available parameters otherwise how would the open profit/loss calculation come up with a value?

How do trading robots do this? Trading fixed lot sizes that result in varying unknown risk is not a good solution. Proper risk management requires lot sizes based on percentage account risk.

I feel having a tool like the FXCM Risk Management Indicator would be one of the most useful features to have built in to MT4, but how would you do the calculations? MetaQuotes, are you listening? Anyone please feel free to comment if you can help. Thank you. 

 

 

Well it is not that complicated and it must be the same for all!

1) You know how much money you want to risk: equity (or balance or free margin) * x%-risk = riskMoney.

2) If you know the actual price and your stop (=diff in points).

3) Calculate the amount of this diff in the currency of riskMoney for one lot: cDiff.

4) Now the lot size should simply spoken cDiff*lots =riskMoney or lots = riskMoney/cDiff.

 

An update...

After finding another method to manually calculate lot size I spotted where some of the issues with using TickValue for the calculation. It appears that TickValue is NOT in the account currency (GBP) for all instruments, in fact this only appears to apply to Forex! Is this by design, or a bug?

Most other instruments (energies, soft commodities, equities, etc.), but not all, will give me the correct lot size if I first convert the TickValue from the quote currency to GBP. This doesn't work for all examples as I found one (SAN.MC) which is quoted in euro, but the TickValue appears to be in GBP.

Another problem is with london stock exchange instruments (e.g. ARM.L), which is quoted in pence instead of pounds, hence my lt size calculation needs to convert the TickValue to pounds by multiplying by 100.

There are some other examples that I am as yet unable to figure out. This is using TickValue, the issues with using Point I am still trying to work out where the problem lies. This is a real pain ... it shouldn't be this difficult to calculate lot sizes :=( 

 
@gooly: thanks for your reply ... I will take a look at this tomorrow.
 
gooly:

Well it is not that complicated and it must be the same for all!

1) You know how much money you want to risk: equity (or balance or free margin) * x%-risk = riskMoney.

2) If you know the actual price and your stop (=diff in points).

3) Calculate the amount of this diff in the currency of riskMoney for one lot: cDiff.

4) Now the lot size should simply spoken cDiff*lots =riskMoney or lots = riskMoney/cDiff.

  • You place the stop where it needs to be - where the reason for the trade is no longer valid. E.g. trading a support bounce the stop goes below the support.
  • Account Balance * percent = RISK = |OrderOpenPrice - OrderStopLoss| * OrderLots * DeltaPerlot (Note OOP-OSL includes the SPREAD)
  • Do NOT use TickValue by itself - DeltaPerlot
  • You must normalize lots properly and check against min and max.
  • You must also check FreeMargin to avoid stop out
 

Does anyone know where the Points, TickSize and TickValue settings come from?

Does the broker set these up?

If they are wrong, who is responsible for correcting these?

 
nbrus:

Does anyone know where the Points, TickSize and TickValue settings come from?

Does the broker set these up?

If they are wrong, who is responsible for correcting these?

The broker is responsible for these values.
 

@zirkoner: Thank you :=). 

@gooly/WHRoeder: Well, I have tried calculating LotSize as suggested and I get the same answers as with my calculation using Points (above) ... Forex not a problem, but for Metals, some stocks, commodities (e.g Cocoa), etc. the results are incorrect! I believe this is likely because my broker (PipIndex) has not set these up properly as they tend to use spreadsheets with manually input values to calculate lot sizes. They are not MQL programmers.

This is absolutely no use for those wishing to calculate lot sizes using MQL and automate everything, or if using EAs purchased from the marketplace. If this was so easy, then please build this function into MQL so we can simply call it with risk amount and stoploss!

Trading is hard enough without having to deal with impossibly challenging lot size calculations that cannot be relied upon due to bad data on the platform. Surely, the fact that Profit/Loss is correctly calculated on the broker's MT4 server, then it must be possible to automatically calculate suitable parameters for the data feed to MT4 clients so that accurate lot size can be calculated for any instrument.

Most traders are not interested in lots so why force them to use lots leaving them exposed to huge losses because it is impossible to know the risk amount from lot size without having done the calculations. This needs to be easier ... forget the bells and whistles on the next version of MT4 ... please make trading easier and solve this serious risk management issue ... get rid of manual/impossible lot size calculation!!!!! :=)

Thanks for listening.

Best Regards

Nic 

 
nbrus: I believe this is likely because my broker (PipIndex) has not set these up properly as they tend to use spreadsheets with manually input values to calculate lot sizes. They are not MQL programmers.
  1. If they set up improperly, tell them to fix their server.
  2. They offer MT4, they better know how to set up their side
  3. If they won't or can't, why use that broker?
 
WHRoeder:
nbrus: I believe this is likely because my broker (PipIndex) has not set these up properly as they tend to use spreadsheets with manually input values to calculate lot sizes. They are not MQL programmers.
  1. If they set up improperly, tell them to fix their server.
  2. They offer MT4, they better know how to set up their side
  3. If they won't or can't, why use that broker?

You're right! I should change broker ... except ...

I tried a demo account on another broker (fxpro) and got different results. They had different values for Cocoa (for example), but still had issues calculating lots...

 

An example using Cocoa Futures at two brokers:

PipIndex: TickSize = 0.0001, TickValue = 0.01 USD, Point = 0.01, BID = 2785.00

   -> [stoploss = 180.53 USD, Risk = 100 GBP]

   -> Results: Lots from Points = 0.80, Lots from Ticks = 0.01 -> Expected Lots should be 0.76!!! 

FxPro: TickSize = 1, TickValue = 10 USD, Point = 1, BID = 2787

   -> [stoploss = 187 USD, Risk = 100 GBP]

   -> Results: Lots from Points = 0.08, Lots from Ticks = 0.08 -> Expected Lots should be 0.76!!! 

 

As you can see I get different results when using Points/Tick and depending on broker and each broker has set up the symbol properties differently! Which broker is correct, if any?

I spoke to my broker to ask them about this and they said they would not be touching their server. They said calculating lots is easy and gave me some examples on a spreadsheet!

Have I got my calculations wrong? I don't believe so. I can post my scripts if required, or please try this using your own methods/scripts.

 

The above makes it clear to me that...

1. I can't trust the settings any broker has set up for non-forex instruments.

2. Brokers don't have the expertise to be able to properly configure there MT4 servers, or they simply don't care because they don't use these settings ... they do manual calculations on spreadsheets!

3. Changing broker is a fruitless task as there will be issues with configuration on certain instruments regardless of broker.

4. It is completely unreliable/impossible to fully automate lot size calculation due to incorrect symbol properties when using MT4! 

 

I would be happy if proved wrong, so please feel free to correct me :=).

If you have complete scripts to calculate lot size for me as mine do not work, then please post the complete code here ... thank you!

 

*Take Away from all this is that the process of setting up instruments on MT4 server needs to be a lot easier for brokers so that they will find it simple to do, and easy to test. Automate as much as possible. Build lot size calculation into MT4 client, so it will be visible and then brokers will be forced to fix any issues.

 

 UPDATE: My code...

*Note: I use 2*ATR(15) for dynamic volatility based stoploss, and 1% Risk = 100 GBP.  Also, I have used some currency conversion functions I created  to simplify things.

 

//Method 1 - Points 

double LotsFromPoints(double stoploss_in_quote_currency, double risk_percent)

{

   //Risk (percent of account equity in quote currency)

   double risk_in_account_currency = AccountEquity() * risk_percent / 100;

   double risk_in_quote_currency = AccountCurrencyToQuoteCurrency(risk_in_account_currency);

   

   //Stoploss (points in quote currency)

   double stoploss_points = stoploss_in_quote_currency / _Point;

   

   //PointValue in quote currency

   double point_risk_in_quote_currency = risk_in_quote_currency / stoploss_points;


   //Notional Trade Size (barrels, bushels, stockprice, etc.)

   double notional_trade_size = point_risk_in_quote_currency / _Point;

   

   // *** Lots Required ***

   double lots = notional_trade_size / MarketInfo(_Symbol, MODE_LOTSIZE);


   return lots;

}


//Method 2 - Ticks 

double LotsFromTicks(double stoploss_in_quote_currency, double risk_percent)

{

   //Risk (percent of account equity in quote currency)

   double risk_in_account_currency = AccountEquity() * risk_percent / 100;

   double risk_in_quote_currency = AccountCurrencyToQuoteCurrency(risk_in_account_currency);

   

   //Stoploss (points in quote currency)

   double stoploss_ticks = stoploss_in_quote_currency / MarketInfo(_Symbol, MODE_TICKSIZE);

   //TickValue per LOT is stated in Account Currency for Forex and in Quote Currency for everything else!

   double tickvalue_per_lot_in_account_currency = MarketInfo(_Symbol, MODE_TICKVALUE) * (IsForex()? 1 : QuoteCurrencyToAccountCurrency(1));

   double risk_in_account_currency_per_lot = stoploss_ticks * tickvalue_per_lot_in_account_currency;

   

   // *** Lots Required ***

   double lots = risk_in_account_currency / risk_in_account_currency_per_lot;


   return lots;

}

 

//Method 3 - Price 

double LotsFromPrice(double stoploss_in_quote_currency, double risk_percent)

{

   //Risk (percent of account equity in quote currency)

   double risk_in_account_currency = AccountEquity() * risk_percent / 100;

   double risk_in_quote_currency = AccountCurrencyToQuoteCurrency(risk_in_account_currency);

   

   //Notional Trade Size (barrels, bushels, stockprice, etc.)

   double notional_trade_size = risk_in_quote_currency / stoploss_in_quote_currency;

   

   // *** Lots Required ***

   double lots = notional_trade_size / MarketInfo(_Symbol, MODE_LOTSIZE);


   return lots;

}

 

******************************************************************************************************************************************************

Note: 3 methods provided above and none are reliable with CFDs due to broker data issues (i.e. incorrect Point, TickSize, TickValue, and/or LotSize).

******************************************************************************************************************************************************

 

I don't think it is good time, 

the main role in calculating are  (different for any of the non-forex, cocoa, barrels, bushels etc.) :

1.) Default Lot Size (all have different)

2.) Leverage (different for any type, broker, account - often hidden, must be get from broker additionally,)

3.) Margin percent required (some require different % from the account - some fixed portion) 

The rest are common like point and price, for calculations. Often 1 / Price can give near value, if You do need exact.

Reason: