# Questions from Beginners MQL4 MT4 MetaTrader 4 - page 133

Aliaksandr Kryvanos:

Hi!

Can someone suggest an analogue of replacement of MaxLotCheck() from MT5 for MT4, thanks

There is no function itself. You have to write your own. With all the checks it should fit in 10-20 lines.

[Deleted]

There is no function itself. You have to write your own. With all the checks, it should fit in 10-20 lines.

OK, but the formula itself?

Aliaksandr Kryvanos:

OK, but here's the formula itself?

Let's look at the source:

```//+------------------------------------------------------------------+
//| Access functions OrderCalcMargin(...).                           |
//| INPUT:  name            - symbol name,                           |
//|         price           - price of the opening position,         |
//|         percent         - percent of available margin [1-100%].   |
//+------------------------------------------------------------------+
double CAccountInfo::MaxLotCheck(const string symbol,const ENUM_ORDER_TYPE trade_operation,
const double price,const double percent) const
{
double margin=0.0;
//--- checks
if(symbol=="" || price<=0.0 || percent<1 || percent>100)
{
Print("CAccountInfo::MaxLotCheck invalid parameters");
return(0.0);
}
//--- calculate margin requirements for 1 lot
{
Print("CAccountInfo::MaxLotCheck margin calculation failed");
return(0.0);
}
//---
if(margin==0.0) // for pending orders
return(SymbolInfoDouble(symbol,SYMBOL_VOLUME_MAX));
//--- calculate maximum volume
double volume=NormalizeDouble(FreeMargin()*percent/100.0/margin,2);
//--- normalize and check limits
double stepvol=SymbolInfoDouble(symbol,SYMBOL_VOLUME_STEP);
if(stepvol>0.0)
volume=stepvol*MathFloor(volume/stepvol);
//---
double minvol=SymbolInfoDouble(symbol,SYMBOL_VOLUME_MIN);
if(volume<minvol)
volume=0.0;
//---
double maxvol=SymbolInfoDouble(symbol,SYMBOL_VOLUME_MAX);
if(volume>maxvol)
volume=maxvol;
//--- return volume
return(volume);
}```
ENUM_ORDER_TYPE and OrderCalcMargin need to be replaced here.
[Deleted]
Yum) the source... thank you
[Deleted]

Still not clear....

margin = MarketInfo(Symbol(), MODE_MARGINREQUIRED)

и

double margin;

return different numbers of margin to open 1 lot

Aliaksandr Kryvanos:

Still not clear....

margin = MarketInfo(Symbol(), MODE_MARGINREQUIRED)

и

double margin;

return different numbers of margin for opening 1 lot

You are obviously comparing on different terminals )) Above is MT4, below is MT5. So servers are different, trading conditions are different. The simplest thing is different leverage.

What did you actually get, what were the values?

[Deleted]

Yes, that's right, the leverage is different, on one 1/300, on the other 1/2000

thanks

Hi all!

My EA uses a multi-step search for an entry point, for this it first checks the history for signals from the start, then works in real time. So in the strategy tester, everything works fine with the history, but when I run it in real time, it does not properly scan the history and misses signals, what could be the problem?

Maybe there is an obvious answer, because the code fragment is quite big to post here, so I am not giving the code yet.

ArturR:

Hi all!

My EA uses a multi-step search for entry points, for this it first checks the history for signals from the start, then works in real time. So in the strategy tester, everything works fine with the history, but when I run it in real time, it does not properly scan the history and misses signals, what could be the problem?

Maybe there is an obvious answer, because the code fragment is quite big to post here, so I am not giving the code yet.

Do the debugging piece by piece. Separate the history scan into the script. Output detailed information with Alert(): first the number of bars in the history, the start and end date, ... and then the process of scanning. It may be more convenient to write it all to a file

STARIJ:
Do the debugging step by step. Separate the history scan into a script. Output using Alert() the detailed information: first of all, the number of bars in the history, the start and the end date, ... and then the process of scanning. It may be more convenient to write it all to a file

Thanks for the tip, I will read how to do it.

I have one more question, when I run real-time debugging, Metatrader4 always runs it on CHF, does anybody know what to adjust to make it run on the pair I need?

It was the indicator values for other timeframes which apparently are not considered in the tester. I also found the way to make the right pair. Thanks for your participation )