Closeorder function is not work Plz help me

To add comments, please log in or register
forexsender
20
forexsender  

I have this codes but not work close function

How can I success it

extern double LotSize = 0.5 ;

int BuyTicket; int SellTicket;
int magic;
double atr_current, atr_past;

//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//---
   SetLotSize();
//---
   return(0);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---
   
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+

       double SetLotSize()
              {

                static double LastAccountBalance;
                   if(LastAccountBalance>AccountBalance())
              {
                  if (LotSize==0.5) 
                  LotSize=1;

                   else if (LotSize==1)
                   LotSize=1.5;

                   else if (LotSize==1.5)
                   LotSize=2;

                   else if (LotSize==2)
                   LotSize=3;
                   
                   
                   else if (LotSize==3)
                   LotSize=3.5;
                   
                   else if (LotSize==3.5)
                   LotSize=4;
                   
                   else if (LotSize==4)
                   LotSize=4.5;
                   

              }

                if(LastAccountBalance < AccountBalance())
                      LotSize=1;

                LastAccountBalance=AccountBalance();
                      return LotSize;
                }
 




          
         
          
         


void OnTick()
  {
//--- 
       double SlowMovingAverage =     iMA(NULL ,0 ,150,0,MODE_EMA,PRICE_CLOSE,0);
       
       double LastSlowMovingAverage = iMA(NULL ,0 ,150,0,MODE_EMA,PRICE_CLOSE,1);
 
       double FastMovingAverage =     iMA(NULL ,0 ,15,0,MODE_EMA,PRICE_CLOSE,0);
       
       double LastFastMovingAverage = iMA(NULL ,0 ,15,0,MODE_EMA,PRICE_CLOSE,1);
       
       
          atr_current = iATR(NULL, 0, 20, 1);    // ATR(20) now
          atr_past    = iATR(NULL, 0, 20, 11);      // ATR(20) 10 periods ago
          
       
       
       if ( OrdersTotal()==0 )
       //buy
       {
               if (atr_current > atr_past)      
                    
                    {
                    if( (LastFastMovingAverage<LastSlowMovingAverage)&& (FastMovingAverage>SlowMovingAverage) )
                           { 
                               BuyTicket = OrderSend( Symbol() ,OP_BUY , SetLotSize(), Ask , 1 ,0 ,0,NULL,magic,0,Green);
  
                            }
                            //Bid-1600*Point,Bid+1000*Point
                    }
     
      }
    }
      
  
  


   void OrdersClose()
    {

       double SlowMovingAverage =     iMA(NULL ,0 ,150,0,MODE_EMA,PRICE_CLOSE,0);
       
       double LastSlowMovingAverage = iMA(NULL ,0 ,150,0,MODE_EMA,PRICE_CLOSE,1);
 
       double FastMovingAverage =     iMA(NULL ,0 ,15,0,MODE_EMA,PRICE_CLOSE,0);
       
       double LastFastMovingAverage = iMA(NULL ,0 ,15,0,MODE_EMA,PRICE_CLOSE,1);
       
   
     if( (LastFastMovingAverage>LastSlowMovingAverage)&& (FastMovingAverage<SlowMovingAverage) )
      
      {
      
         if(OrdersTotal() > 0)
      {
         for(int i = 0; i < OrdersTotal(); i++){
            OrderSelect(i, SELECT_BY_POS);
      
            if(OrderType() == OP_BUY){
               OrderClose(OrderTicket(), OrderLots(), Bid, 3, Blue);
            }
            
         }
       }
       
      
    }
    
  
}
William Roeder
20210
William Roeder  
  1. forexsender: I have this codes but not work close function
    You never call the function. Why do you expect it to "work?"

  2. for(int i = 0; i < OrdersTotal(); i++){
    In the presence of multiple orders (one EA multiple charts, multiple EAs, manual trading,) while you are waiting for the current operation (closing, deleting, modifying) to complete, any number of other operations on other orders could have concurrently happened and changed the position indexing:
    1. For non-FIFO (US brokers,) (or the EA only opens one order per symbol,) you can simply count down in a position loop, and you won't miss orders. Get in the habit of always counting down.
                Loops and Closing or Deleting Orders - MQL4 and MetaTrader 4 - MQL4 programming forum
      For FIFO (US brokers,) and you (potentially) process multiple orders per symbol, you must count up and on a successful operation, reprocess all positions (set index to -1 before continuing.)
    2. and check OrderSelect in case earlier positions were deleted.
                What are Function return values ? How do I use them ? - MQL4 and MetaTrader 4 - MQL4 programming forum
                Common Errors in MQL4 Programs and How to Avoid Them - MQL4 Articles
    3. and if you (potentially) process multiple orders, must call RefreshRates() after server calls if you want to use, on the next order / server call, the Predefined Variables (Bid/Ask) or (be direction independent and use) OrderClosePrice().

  3. if ( OrdersTotal()==0 )
    Using OrdersTotal directly and/or no Magic number filtering on your OrderSelect loop means your code is incompatible with every EA (including itself on other charts and manual trading.)
              Symbol Doesn't equal Ordersymbol when another currency is added to another seperate chart . - MQL4 and MetaTrader 4 - MQL4 programming forum
              MagicNumber: "Magic" Identifier of the Order - MQL4 Articles

To add comments, please log in or register