orderclose not working. Can somebody Point to me whats wrong with my code. correct me.( this readable raptorUk)?

 

Experts plz make this orderclose work thank yu. any help suggestions are welcome

              
  int a=1,b=2, c=1,d=2;   //VALUES ALWAYS TRUE IF NO ORDERS THEN OPEN 2 ORDERS
  
   
  //**************
  //OPEN A HEDGE**
  //**************

  if(a < b)       //openbuy 
   {
   openbuy=true;
   }

  if(openbuy==true) // if openbuy true
     {
      tellbuy=1;    // initiate tellbuy
     }


   if(c<d)  
    {
    opensell=true;  //if open sell true
    } 
             

  if(opensell==true)
    {
     tellsell=2; // initiate tell sell
    }  
                                        
   
   // THE CLOSING ORDERS 
   //$$$$$$$$$$$$$$$$$$$$$$$
   
   double currprice=iClose(NULL,PERIOD_D1,0);                   // gives the currentprice
   
   
   if(tellbuy==1 && currprice>tellbuy && currprice>tellsell)    // if the currentprice is now greater than price of openbuy.  
      {
        closebuy=true;                                          //then close buy
      }


    if( tellsell==1 && currprice<tellbuy && currprice<tellsell)         //if the current is now less than the price of opensell.
       {                                                              //then close sell
        closesell=true;
       }     

   
  
  //CLOSING ORDERS -->
  //****************************************************************************************************************************************************************
  //8888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888 
  //****************************************************************************************************************************************************************
  
 //here is OrderClose functions.
  
  while(true)
     { 
  if(Tip==0 && closebuy==true)    // closing of buy
  {
  Alert("Closing Buy",ticket,".Waiting For Response...");
  RefreshRates()
  Cpos=OrderClose(ticket,lots,Bid,3,Yellow);

  if(Cpos==true)
    {
     Alert("Buy Closed",ticket);
     break;
    }
      if(Fun_Error(GetLastError())==1)
      continue; 
      return;
      }
   
                                               
 if(Tip==1 && closesell==true)   // closing of sell
   {
    Alert("Closing Sell",ticket,".Waiting For Response...");
    RefreshRates();               
    Cpos=OrderClose(ticket,lots,Ask,3);

if(Cpos==true)
 {
  Alert("Sell Closed",ticket);
  break;
 }
       
 if(Fun_Error(GetLastError())==1
 continue;
 return;
    }
  break;
      }
                                                      
                                                       
  //****************************************************************************************************************************************************************
  //8888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888
  //****************************************************************************************************************************************************************
  
 
Files:
greedy_1.mq4  9 kb
 
tafadzwa:

Here is my attempt download the EA, check the source code thank you for yo help.

Remember if current price is above OrderOpenPrice (of both orders) close buy and Close sell when the price is below OrderOpen price (of both Orders)

You really need to have a read of this thread: https://www.mql5.com/en/forum/146570
 
tafadzwa:

Experts plz make this orderclose work thank yu. any help suggestions are welcome


Fix your style . . . I find it hard to read and follow your code . . . make it easy for me to read and I'll read it and help you, make it hard for me and I won't.
 

It's better . . .

In this code, where does the value of ticket come from ? you seem to be using the same value of ticket to try and close a buy and a sell, if you have a Buy and a Sell they will each have unique ticket numbers . . .

   while(true)
     {
      if(Tip==0 && closebuy==true)
        {
         Alert("Closing Buy",ticket,".Waiting For Response...");
         RefreshRates();
         Cpos=OrderClose(ticket,lots,Bid,3,Yellow);

         if(Cpos==true)
           {
            Alert("Buy Closed",ticket);
            break;
           }
         if(Fun_Error(GetLastError())==1)
            continue;
         return;
        }

      if(Tip==1 && closesell==true)
        {
         Alert("Closing Sell",ticket,".Waiting For Response...");
         RefreshRates();
         Cpos=OrderClose(ticket,lots,Ask,3);

         if(Cpos==true)
           {
            Alert("Sell Closed",ticket);
            break;
           }
         if(Fun_Error(GetLastError())==1)
            continue;
         return;
        }
      break;
     }

You should only check GetLastError() when you need to, so if Cpos = false then GetLastError() and report the error and any relevant variables that you will need to diagnose the issue.

 
RaptorUK:

It's better . . .

In this code, where does the value of ticket come from ? you seem to be using the same value of ticket to try and close a buy and a sell, if you have a Buy and a Sell they will each have unique ticket numbers . . .

You should only check GetLastError() when you need to, so if Cpos = false then GetLastError() and report the error and any relevant variables that you will need to diagnose the issue.

THANKS