Lot calculation

 
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.

Wait for your help.




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() )
          {
             //for buy order
             if(OrderType()==OP_BUY && OrderProfit()<0)
               {Calcul_Lot=OrderLots()*2;}
             if(OrderType()==OP_SELL && OrderProfit()<0) 
               {Calcul_Lot=OrderLots()*2;}
             if((OrderType()==OP_SELL && OrderProfit()>0)||(OrderType()==OP_BUY && OrderProfit()>0))
               {Calcul_Lot=initial_Lots;}
             Print(Calcul_Lot);
             break; 
          return(Calcul_Lot);
          }
       }
    } 
     
     
     
      //BUY
      if()
         {
         ticket=OrderSend(NULL,OP_BUY,Calcul_Lot,Ask,3,0,0,"ma sample",MagicNo,0,Green);
         if(ticket>0) 
           {
            if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES)) 
            Print("BUY order opened : ",OrderOpenPrice());
           }
         else 
         Print("Error opening BUY order : ",GetLastError()); 
         } 
  
  
  return(0);
  }
//+------------------------------------------------------------------+


 
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.
 
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?

 

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!

 

I made the code different.

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(/*Buy Entry condition*/)
         {
         ticket=OrderSend(NULL,OP_BUY, LotCal(),Ask,3,0,0,"sample",MagicNo,0,Green);
         if(ticket>0) 
           {
            if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES))
            Print("BUY order opened : ",OrderOpenPrice());
            Print("Buy SR : ",Success_Rate); 
           }
         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
          //for buy order
       }
    }
    if(OrderType()==OP_BUY && OrderProfit()<0)  //As Buy In Losses
            {Calcul_Lot=LotsVolume*Martingale;}
    if(OrderType()==OP_SELL && OrderProfit()<0) //As Sell In Losses
            {Calcul_Lot=LotsVolume*Martingale;}
    if((OrderType()==OP_SELL && OrderProfit()>0)||(OrderType()==OP_BUY && OrderProfit()>0))
            {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
}
 
 
Keith Watford:

Could you explain it to me based on the code?

 
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);                          
  }
//+------------------------------------------------------------------+
 
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.

Thank you for your help.
Reason: