# Lot calculation

188

It's a pleasant weekend.

I'm trying to make the Martin Gail lot calculation.

If I lose, I'll double up in the front door twice and return to the initial value if I get profits.

However, this will result in error 4051.

And I don't know if it's okay to solve 4051 or something else.

```extern double initial_Lots = 1.0;
extern int MagicNo = 1234;
extern double TakeProfit = 50;
extern double StopLoss = 50;

extern bool EnterOpenBar = true;

int CurrentTime;
int Past = 2;
int BarsCount = 0;
int i;

double Calcul_Lot;

//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
{
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
}
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
int start()
{
int cnt, ticket, total;
bool ticketM;
total=OrdersTotal();

for(i=OrdersHistoryTotal()-1;i>=0;i--)
{
if(OrderSelect(i, SELECT_BY_POS,MODE_HISTORY))
{
if(OrderSymbol()==Symbol() )
{
{Calcul_Lot=OrderLots()*2;}
if(OrderType()==OP_SELL && OrderProfit()<0)
{Calcul_Lot=OrderLots()*2;}
{Calcul_Lot=initial_Lots;}
Print(Calcul_Lot);
break;
return(Calcul_Lot);
}
}
}

if()
{
if(ticket>0)
{
}
else
Print("Error opening BUY order : ",GetLastError());
}

return(0);
}
//+------------------------------------------------------------------+
```

16572

cape1354: However, this will result in error 4051.
`ticket=OrderSend(NULL,OP_BUY,Calcul_Lot,Ask,3,0,0,"ma sample",MagicNo,0,Green);`
• You can use NULL in place of _Symbol only in those calls that the documentation specially says you can. iHigh does, MarketInfo does not. OrderSend does not.
• Don't use NULL (except for pointers where you explicitly check for it.) Use _Symbol and _Period, that is minimalist as possible and more efficient.
• Zero is the same as PERIOD_CURRENT which means _Period.
• No need for a function call with iHigh(NULL,0,s) just use the predefined arrays, i.e. High[].
1. You assume history is ordered by date, it's not.
188

whroeder1:
• You can use NULL in place of _Symbol only in those calls that the documentation specially says you can. iHigh does, MarketInfo does not. OrderSend does not.
• Don't use NULL (except for pointers where you explicitly check for it.) Use _Symbol and _Period, that is minimalist as possible and more efficient.
• Zero is the same as PERIOD_CURRENT which means _Period.
• No need for a function call with iHigh(NULL,0,s) just use the predefined arrays, i.e. High[].
1. You assume history is ordered by date, it's not.

I tried to like this

` ticket=OrderSend("EURUSD",OP_SELL,Calcul_Lot,Bid,3,0,0,"ma sample",MagicNo,0,Red);`

but problem still exist  and

```2017.08.05 00:17:05.456 2016.08.09 09:00:00  Test 17.08.04-1 EURUSD,M30: invalid lots amount for OrderSend function
```

These problems occur as well.

Isn't there a problem with lot calculation?

Moderator
10541

I have deleted your other topic.

Please do not open new topics that are basically a duplication of this one. You will not get different answers by duplicating your posts. In fact you are more likely to annoy posters and they may elect to not help you at all!

188

However, errors 4051 and " Initial Requirement for Functional Analysis " still occur.

Which is the problem?

```extern double initial_Lots = 1.0;
extern int MagicNo = 1234;
extern double Martingale = 2.0;

int start()
{

if(NewBar() )
{
{
if(ticket>0)
{
}
else
Print("Error opening BUY order : ",GetLastError());
}
}

//+------------------------------------------------------------------+
//|  Accompany only when new bar appears                |
//+------------------------------------------------------------------+
bool NewBar()
{
if(PreviousBar<Time[0])
{
PreviousBar=Time[0];
return(true);
}
return(false);
}
//+------------------------------------------------------------------+
//|   LOT calculation                                                                                |
//+------------------------------------------------------------------+
double LotCal()
{
double Calcul_Lot;
double LotsVolume=0;
for(i=OrdersHistoryTotal()-1;i>=0;i--)    //Find Last Order
{
if(OrderSelect(i, SELECT_BY_POS,MODE_HISTORY))
{
LotsVolume=OrderLots();             // Save the last order to LotsVolume
}
}
{Calcul_Lot=LotsVolume*Martingale;}
if(OrderType()==OP_SELL && OrderProfit()<0) //As Sell In Losses
{Calcul_Lot=LotsVolume*Martingale;}
{Calcul_Lot=initial_Lots;}          //As Buy In profit,as Sell In profit

return(Calcul_Lot);                            // Finally, obtain a double value of CalCul_local in the LotCal () function
}```
Moderator
10541

whroeder1:
1. You assume history is ordered by date, it's not.
188

Keith Watford:

Could you explain it to me based on the code?

Moderator
10541

cape1354:

Could you explain it to me based on the code?

You must find the last closed order as in the code below. (Not compiled or tested)

```//+------------------------------------------------------------------+
//|   LOT calculation                                                                                |
//+------------------------------------------------------------------+
double LotCal()
{
double Calcul_Lot=initial_Lots;
datetime order_close_time=0;
for(int i=OrdersHistoryTotal()-1;i>=0;i--) //Find Last Order
{
if(OrderSelect(i,SELECT_BY_POS,MODE_HISTORY) && OrderSymbol()==_Symbol && OrderMagicNumber()==MagicNumber && (OrderType()==OP_BUY || OrderType()==OP_SELL))
{
if(OrderCloseTime()>order_close_time)
{
order_close_time=OrderCloseTime();
if(OrderProfit()<0)
Calcul_Lot=LotsVolume*Martingale;
else
Calcul_Lot=initial_Lots;
}
}
}

return(Calcul_Lot);
}
//+------------------------------------------------------------------+```
188

Keith Watford:
You must find the last closed order as in the code below. (Not compiled or tested)

I solved the problem with your help.