Invalid Ticket for OrderCloseFunction / OrderCloseError4051

 

Hello,


since three days, i sit on this problem and don't find the problem. Maybe someone of you could help me.


//+------------------------------------------------------------------+
//|                                             MovingAverage100.mq4 |
//|                                   Copyright 2012, Maik Steinfeld |
//|                                      http://www.streward-shop.de |
//+------------------------------------------------------------------+
#property copyright "Copyright 2012, Maik Steinfeld"
#property link      "http://www.streward-shop.de"

int ticket;

//+------------------------------------------------------------------+
//| expert initialization function                                   |
//+------------------------------------------------------------------+
int init()
  {
//----
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert deinitialization function                                 |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+
int start()
  {
//----
   bool success=false;
   string type;
   double ma100=iMA(NULL,0,100,0,MODE_SMA,PRICE_CLOSE,1);
   double ma35=iMA(NULL,0,35,0,MODE_SMA,PRICE_CLOSE,1);
   double ma20=iMA(NULL,0,20,0,MODE_SMA,PRICE_CLOSE,1);
   string MagicNumber1 = 1;
   string MagicNumber2 = 2;
   int number;
   
   double priceClose=iClose(NULL,PERIOD_H1,1);
   Print("Start");
   Print (ma100);
   Print (ma20);
   Print (ma35);
   Print (priceClose);
   if (OrdersTotal() == 0){
   Print("OrdersTotal=0");
      if (priceClose > ma100 && ma20 > ma35){
      Print("MA");
         number = OrderSend(Symbol(),OP_BUY,0.01,Ask,20,0,0,"buyOrder",0,0,Blue);
         GlobalVariableSet(ticket,number);
         }
      if (priceClose < ma100 && ma20 < ma35){
      number = OrderSend(Symbol(),OP_SELL,0.01,Bid,20,0,0,"sellOrder",0,0,Green);
      GlobalVariableSet(ticket,number);
      }}
   
   if (OrdersTotal() != 0){
   Print(OrdersTotal());
      OrderSelect(GlobalVariableGet(ticket),SELECT_BY_TICKET); // If the next is available
      int ticket = OrderTicket();
      Print (ticket);
      if(OrderType() == OP_SELL){
         Print("SellOrder Close");
         if((priceClose > ma100) || (ma20 > ma35)){
                
               
               while(OrderCloseTime() == 0){
               RefreshRates();
               OrderClose(OrderTicket(),OrderLots(),Ask,20,Red);
    }}}}    
     if(OrderType() == OP_BUY){      
   if(priceClose < ma100 || (ma20 < ma35)){
         while(OrderCloseTime() == 0){
         
         RefreshRates();
          OrderClose(OrderTicket(),OrderLots(),Bid,20,Red); 
          }}}
      string err=GetLastError();
     Print("error(",err,")");

        
         
//----
   return(0);
  }
//+------------------------------------------------------------------+
 
      GlobalVariableSet(ticket,number);
      }}
   
   if (OrdersTotal() != 0){
   Print(OrdersTotal());
      OrderSelect(GlobalVariableGet(ticket),SELECT_BY_TICKET); // If the next is available
  1. RTFM. What is the first parameter to GVSet? What is it's type? What type is ticket and it's value?
  2. Why are you using global variables? Use them only for communication between EA's. You can not use them for persistent storage.
  3. What are Function return values ? How do I use them ? - MQL4 forum