There is SYMBOL_VOLUME_MAX
as well
OrderCalcMargin ().
But when checking SYMBOL_VOLUME_MAX with this function, with a Deposit of $10,000, it turns out that it is impossible to enter the market with this volume
. How do I find out the maximum possible volume to enter the market with a current Deposit?
cyberdev
Experimented with free margin:
double volumeChecked;
double cutVoluneByFreeMargin(string _symbol, double _volume, int _stopLoss, ENUM_ORDER_TYPE _type) {
volumeChecked = false;
double lotValue = SymbolInfoDouble(_symbol, SYMBOL_TRADE_TICK_VALUE);
double freeMargin = AccountInfoDouble(ACCOUNT_MARGIN_FREE);
double maxLot_ = (freeMargin * 0.3 / _stopLoss) / lotValue; / / only works starting from 0.3 from freeMargin
double maxLot = SymbolInfoDouble(_symbol, SYMBOL_VOLUME_MAX);
if(maxLot_ > maxLot)
maxLot_ = maxLot;
double lot = (_volume <= maxLot_) ? _volume : maxLot_;
/ / checking for sufficient funds to enter the market
MqlTick mqltick;
SymbolInfoTick(_symbol, mqltick);
double price;
switch(_type) {
case ORDER_TYPE_BUY:
price = mqltick.ask;
break;
case ORDER_TYPE_SELL:
price = mqltick.bid;
break;
default:
Print(__FUNCTION__," Error! Line:", __LINE__);
return 0;
}
double margin;
if(!OrderCalcMargin(_type, _symbol, lot, price, margin)) {
/ / - - - something went wrong
Print("Error in ",__FUNCTION__," code: ",GetLastError());
return 0;
}
/ / - - - if there are not enough funds for the operation
if(margin > freeMargin) {
/ / - - - report an error and return false
Print("Not enough money for ",EnumToString(_type)," ", lot," ",_symbol," Error code: ",GetLastError());
Print("margin: ", margin, " freeMargin: ", freeMargin, " lot: ", lot);
return 0;
}
volumeChecked = true;
return lot;
}
But the function skips the volume calculated with a value of no more than 0.3 * freeMargin
double maxLot_ = (freeMargin * 0.3 / _stopLoss) / lotValue;
Why did you get exactly this pattern? Maybe
maybe I’m calculating the maximum possible lot incorrectly?
barabashkakvn
Mihail Matkovskij:
I checked the code on the demo account.
It turns out that maxLot passes the test on the demo, but in
in the expert Advisor, on the tester, the message crashes ”
Not
enough money to make a deal!” and the position with the maxLot volume does not open, though, the Deposit size, as on
on the tester and on the demo 10000. On the demo, even less is a bit. How can I calculate the maximum possible lot if
OrderCalcMargin gives a fake?
In addition to checking
if(!OrderCalcMargin
you need to check at least
if(margin>0.0
and even better (at least I do tai) – so that margin is enough for another such position.
Code example: OnInit prints the maximum lot; if the Start parameter is set to “true“,
it will give a signal to open a position with the specified lot (
Money management: Lot OR Risk is set to Constant lot, and The value for “Money management” is set
to 500.0)
cyberdev
Set checkmoneyfortrade (style= ” background-color:rgb(251, 249, 245); color:rgb(0, 0, 0);”>)>,
which I do not allow the operation of the OrderSend function (if the data is not checked) gives a message about the lack of funds on the
Deposit:
2019.10.06 17:15:02.028 Core 1 2012.05.03 14:53:00 Not enough money for ORDER_TYPE_BUY 500.0 EURUSD Error code: 0
But theDeposit, again, has grown to
47730.00, with 10000 and here the
terminal does not miss the volume of 500.0, which corresponds to SYMBOL_VOLUME_MAX.
There are no errors
– “Error
code: 0″, but there are not enough funds on the Deposit. How can this be?
barabashkakvn
Article What
checks should the trading robot pass before publishing in the Market read?
cyberdev
I checked the code on the demo account.
//+------------------------------------------------------------------+
//| Script program start function |
//+------------------------------------------------------------------+
void OnStart() {
string symbol = Symbol();
double maxLot = SymbolInfoDouble(symbol, SYMBOL_VOLUME_MAX);
double margin;
if(!OrderCalcMargin(ORDER_TYPE_BUY, symbol, maxLot, SymbolInfoDouble(symbol, SYMBOL_ASK), margin)) {
Print("Not enough money to make a deal!");
// Recalculation of the maximum lot for the current Deposit
// ...
}
}
//+------------------------------------------------------------------+
It turns out that on the demo maxLot passes the test, but in the expert
Advisor, on the tester, the message crashes ”
Not
enough money to make a deal!” and the position with the maxLot volume does not open, although the Deposit size, both on the tester
and on the demo, is 10000. On the demo, even less is a bit. How can I calculate the maximum possible lot if
OrderCalcMargin gives a fake?