int lotsDigits=MathLog10(MarketInfo("EURUSD",MODE_MINLOT)); // I deleted " (int)- "
The purpose is to get "valid" volume size number, e.g. 0.02 , etc.
MarketInfo("EURUSD",MODE_MAXLOT) MarketInfo("EURUSD",MODE_MINLOT)
Get the maximum/minumym volume limit of the pair, e.g. "EURUSD"
maximumRisk (normally in percentage of e.g. Free Margin) newlots = AccountFreeMargin() * maximumRisk / (500000.0/AccountLeverage()) lots=NormalizeDouble(newlots)
Why the number 500000.0 ?
No idea, probably an upper limit / a fix balance ?
You might want to simplify the
formula to
maximumRisk = 2.5 newlots = AccountFreeMargin() * maximumRisk / 100.0 lots = NormalizeDouble(newlots)
Good luck.
PS: Try to format your post according to forum's standard, especially regarding SOURCE CODE formatting.
It
makes reading your source code easier.
You can refer to https://docs.mql4.com/function_indices and https://www.mql5.com/en/docs/function_indices to
understand what those MT4/MT5 functions do.
The condition "if ( lots < MinLots ) lots = MinLots" is incorrect because it leads to an incorrectly accepted risk
For example
Let "maximumRisk" be the maximum risk for the calculated operation
Let "lots = 0.02" and "MinLots = 0.10"
Then "lots = MinLots" increases the risk by 5 times from the permissible
newlots = AccountFreeMargin() * maximumRisk / (500000.0/AccountLeverage())
The 500K is somebody's BOGUS attempt to relate account balance to lot size.
Never risk more than a small percentage of your account, certainly less than 2% per trade, 6% total to the account.
Risk depends on your initial stop loss, lot size, and the value of the pair. It does not depend on margin and leverage. No SL means you have infinite risk.
- 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.
- AccountBalance * percent/100 = RISK = OrderLots * (|OrderOpenPrice - OrderStopLoss| * DeltaPerLot + CommissionPerLot) (Note OOP-OSL includes the spread, and DeltaPerLot is usually around $10/pip but it takes account of the exchange rates of the pair vs. your account currency.)
- Do NOT use TickValue by itself - DeltaPerLot
and verify that MODE_TICKVALUE is returning a value in your deposit currency, as promised by the documentation, or whether it
is returning a value in the instrument's base currency.
MODE_TICKVALUE is not reliable on non-fx instruments with many brokers - MQL4 programming forum 2017.10.10
Is there an universal solution for Tick value? - Currency Pairs - General - MQL5 programming forum 2018.02.11
Lot value calculation off by a factor of 100 - MQL5 programming forum 2019.07.19 - You must normalize lots properly and check against min and max.
- You must also check FreeMargin to avoid stop out
Most pairs are worth about $10 per PIP. A $5 risk with a (very small) 5 PIP SL is $5/$10/5 or 0.1 Lots maximum.
The purpose is to get "valid" volume size number, e.g. 0.02 , etc.
Get the maximum/minumym volume limit of the pair, e.g. "EURUSD"
Why the number 500000.0 ?
No idea, probably an upper limit / a fix balance ?
You might want to simplify
the formula to
Good luck.
PS: Try to format your post according to forum's standard, especially regarding SOURCE CODE formatting.
It
makes reading your source code easier.
You can refer to https://docs.mql4.com/function_indices and https://www.mql5.com/en/docs/function_indices to
understand what those MT4/MT5 functions do.
The 500K is somebody's BOGUS attempt to relate account balance to lot size.
Never risk more than a small percentage of your account, certainly less than 2% per trade, 6% total to the account.
Risk depends on your initial stop loss, lot size, and the value of the pair. It does not depend on margin and leverage. No SL means you have infinite risk.
- 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.
- AccountBalance * percent/100 = RISK = OrderLots * (|OrderOpenPrice - OrderStopLoss| * DeltaPerLot + CommissionPerLot) (Note OOP-OSL includes the spread, and DeltaPerLot is usually around $10/pip but it takes account of the exchange rates of the pair vs. your account currency.)
- Do NOT use TickValue by itself - DeltaPerLot
and verify that MODE_TICKVALUE is returning a value in your deposit currency, as promised by the documentation, or
whether it is returning a value in the instrument's base currency.
MODE_TICKVALUE is not reliable on non-fx instruments with many brokers - MQL4 programming forum 2017.10.10
Is there an universal solution for Tick value? - Currency Pairs - General - MQL5 programming forum 2018.02.11
Lot value calculation off by a factor of 100 - MQL5 programming forum 2019.07.19 - You must normalize lots properly and check against min and max.
- You must also check FreeMargin to avoid stop out
Most pairs are worth about $10 per PIP. A $5 risk with a (very small) 5 PIP SL is $5/$10/5 or 0.1 Lots maximum.
Thank you William, sorry for replying so late.
I need time to read your message, however the code I posted suppose put no SL as it is a hedge involved method trading.
I myself couldn't figure out how to really code for this purpose. I have been thinking just put an fixed lot to make it simple.
Anyway, I shall study your comments and see how I can understand and what I can get from it. thank you again.
The condition "if ( lots < MinLots ) lots = MinLots" is incorrect because it leads to an incorrectly accepted risk
For example
Let "maximumRisk" be the maximum risk for the calculated operation
Let "lots = 0.02" and "MinLots = 0.10"
Then "lots = MinLots" increases the risk by 5 times from the permissible
Thanks Airat, but the server/broker's system had decided how much is the MinLots right ? so it should be reasonable , isn't it ?
Regards
Sean
As I said "No SL means you have infinite risk." What happens if your machine dies, or your network? And stays that way for days. You potentially risk your entire account. Reality ½ due to a margin call.
Hedging requires two spreads. A SL looses you one. And if the market stops going adverse, you can reenter at a better price. Cut your losses quickly;
let your winners run.
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
I couldn't get in touch with the author , therefore I decided to ask in the forum to help me understand some function :
Thank you in advance, please bare in mind that I am not professional IT or trader. so I am just an amateur, but I like to study more in Mql4 and EA, so please help me understand some code. I DO have studied some from books/youtubes/etc, but still can not figure out some, such as this one :
-qte-
//+------------------------------------------------------------------+
//| Calculate position volume |
//+------------------------------------------------------------------+
double Lots()
{
double maximumRisk=0.1;
int lotsDigits=MathLog10(MarketInfo("EURUSD",MODE_MINLOT)); // I deleted " (int)- "
double lots=NormalizeDouble(AccountFreeMargin()*maximumRisk/(500000.0/AccountLeverage()),lotsDigits);
if(lots>MarketInfo("EURUSD",MODE_MAXLOT))
lots=MarketInfo("EURUSD",MODE_MAXLOT);
if(lots<MarketInfo("EURUSD",MODE_MINLOT))
lots=MarketInfo("EURUSD",MODE_MINLOT);
return(lots);
}
- unqte -
can explain line by line whats meaning ? and especially why there is this '500000.0' ?
Thanks you
Regards
Seanlu21