can open a order and can not close a order

 

I write a cross trendline ea, it can open a order but can not close it, I wondered what's wrong in it. please help me about.

//+------------------------------------------------------------------+
//|                                               Moving Average.mq4 |
//|                      Copyright ?2005, MetaQuotes Software Corp. |
//|                                       http://www.metaquotes.net/ |
//+------------------------------------------------------------------+
#define MAGICMA  20090610

extern double Lots               = 0.1;
extern double MaximumRisk        = 0.005;
extern double DecreaseFactor     = 3;
extern double MovingPeriod       = 12;
extern double MovingShift        = 6;
extern double TakeProfit         =1000;
extern double StopLoss           =500;



//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
int init()
  {
//----
  start(); 
//----
return(0);
  }
//+------------------------------------------------------------------+
//| expert deinitialization function                                 |
//+------------------------------------------------------------------+
int deinit()
  {
//----
ObjectDelete("qsx");
ObjectDelete("qsxText");  
//----
   return(0);
  }


//+------------------------------------------------------------------+
//| Calculate open positions                                         |
//+------------------------------------------------------------------+
int CalculateCurrentOrders(string symbol)
  {
   int buys=0,sells=0;
//----
   for(int i=0;i<OrdersTotal();i++)
     {
      if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false) break;
      if(OrderSymbol()==Symbol() && OrderMagicNumber()==MAGICMA)
        {
         if(OrderType()==OP_BUY)  buys++;
         if(OrderType()==OP_SELL) sells++;
        }
     }
//---- return orders volume
   if(buys>0) return(buys);
   else       return(-sells);
  }
//+------------------------------------------------------------------+
//| Calculate optimal lot size                                       |
//+------------------------------------------------------------------+
double LotsOptimized()
  {
   double lot=Lots;
   int    orders=HistoryTotal();     // history orders total
   int    losses=0;                  // number of losses orders without a break
//---- select lot size
   lot=NormalizeDouble(AccountFreeMargin()*MaximumRisk/1000.0,1);
//---- calcuulate number of losses orders without a break
   if(DecreaseFactor>0)
     {
      for(int i=orders-1;i>=0;i--)
        {
         if(OrderSelect(i,SELECT_BY_POS,MODE_HISTORY)==false) { Print("Error in history!"); break; }
         if(OrderSymbol()!=Symbol() || OrderType()>OP_SELL) continue;
         //----
         if(OrderProfit()>0) break;
         if(OrderProfit()<0) losses++;
        }
      if(losses>1) lot=NormalizeDouble(lot-lot*losses/DecreaseFactor,1);
     }
//---- return lot size
   if(lot<0.1) lot=0.1;
   return(lot);
  }
//+------------------------------------------------------------------+
//| Check for open order conditions                                  |
//+------------------------------------------------------------------+
void CheckForOpen()
  {
   double ma;
   int ticket;
//---- sell conditions

  if(Close[0] < ObjectGetValueByShift("qsx", 0)&& Close[1] > ObjectGetValueByShift("qsx", 0))
        {
         ticket=OrderSend(Symbol(),OP_SELL,LotsOptimized(),Bid,3,Bid-StopLoss*Point,Bid-TakeProfit*Point,"趋势线",16384,0,Red);
         if(ticket>0)
           {
            if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES)) 
            Print("SELL order opened : ",OrderOpenPrice());
            if (ObjectGetValueByShift("qsx", 0) > 0.0) 
             ObjectDelete("qsx");
            if (ObjectGet("qsxText", OBJPROP_PRICE1) > 0.0) 
             ObjectDelete("qsxText");

            
           }
         else Print("Error opening SELL order : ",GetLastError()); 
         return(0); 
        }
  
//---- buy conditions
  if (Close[0] > ObjectGetValueByShift("qsx", 0)&& Close[1]<ObjectGetValueByShift("qsx", 0)) 
        {
         ticket=OrderSend(Symbol(),OP_BUY,LotsOptimized(),Ask,3,Ask-StopLoss*Point*Point,Ask+TakeProfit*Point,"趋势线",16384,0,Green);
         if(ticket>0)
           {
            if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES))
             Print("BUY order opened : ",OrderOpenPrice());
            if (ObjectGetValueByShift("qsx", 0) > 0.0) 
             ObjectDelete("qsx");
            if (ObjectGet("qsxText", OBJPROP_PRICE1) > 0.0) 
             ObjectDelete("qsxText");
           }
         else Print("Error opening BUY order : ",GetLastError()); 
         return(0); 
        }
  }
//+------------------------------------------------------------------+
//| Check for close order conditions                                 |
//+------------------------------------------------------------------+
void CheckForClose()
  {
   double ma;
//---- go trading only for first tiks of new bar
//  if(Volume[0]>1) return;
//---- get Moving Average 
  // ma=iMA(NULL,0,MovingPeriod,MovingShift,MODE_SMA,PRICE_CLOSE,0);
//----
   for(int i=0;i<OrdersTotal();i++)
     {
      if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false)        break;
      if(OrderMagicNumber()!=MAGICMA || OrderSymbol()!=Symbol()) continue;
      //---- check order type 
      if(OrderType()==OP_BUY)
        {
         if(Close[0] <ObjectGetValueByShift("qsx", 0)&& Close[1] >ObjectGetValueByShift("qsx", 0)) 
         OrderClose(OrderTicket(),OrderLots(),Bid,3,White);
         break;
        }
      if(OrderType()==OP_SELL)
        {
         if(Close[0] > ObjectGetValueByShift("qsx", 0)&& Close[1] < ObjectGetValueByShift("qsx", 0)) 
         OrderClose(OrderTicket(),OrderLots(),Ask,3,White);
         break;
        }
     }
//----
  }
//+------------------------------------------------------------------+
//| Start function                                                   |
//+------------------------------------------------------------------+
void start()
  {
//---- check for history and trading
   //if(Bars<100 || IsTradeAllowed()==false) return;
//---- calculate open orders by current symbol


   
   if (ObjectGetValueByShift("qsx", 0) > 0.0) 
   {
      if (ObjectGet("qsx", OBJPROP_COLOR) != 32768.0) ObjectSet("qsx", OBJPROP_COLOR, Yellow);
      if (ObjectGet("qsx", OBJPROP_RAY) == 0.0) ObjectSet("qsx", OBJPROP_RAY, TRUE);
      ObjectCreate("qsxText", OBJ_TEXT, 0, 0,0);
      ObjectSet("qsxText", OBJPROP_TIME1, Time[20]);
      ObjectSet("qsxText", OBJPROP_PRICE1, ObjectGetValueByShift("qsx", 0));
      ObjectSetText("qsxText", "MyLine", 10, "Book Antiqua", Yellow);
    }

   if(CalculateCurrentOrders(Symbol())==0) CheckForOpen(); 
   else                                   CheckForClose();
//----
  }
//+------------------------------------------------------------------+ 

 

use some print() or alert() to c where the problem is

 
qjol:

use some print() or alert() to c where the problem is


Thank you very much,
 
extern double TakeProfit         =1000;
extern double StopLoss           =500;

      OrderClose(OrderTicket(),OrderLots(),Bid,3,White);
// or OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),3,White);

These lead me to believe you're on a 5 digit broker, therefor TP, SL, and Slippage must be adjusted:
//++++ These are adjusted for 5 digit brokers.
double  pips2points,    // slippage  3 pips    3=points    30=points
        pips2dbl;       // Stoploss 15 pips    0.0015      0.00150
int     Digits.pips;    // DoubleToStr(dbl/pips2dbl, Digits.pips)
int init(){
    if (Digits == 5 || Digits == 3){    // Adjust for five (5) digit brokers.
                pips2dbl    = Point*10; pips2points = 10;   Digits.pips = 1;
    } else {    pips2dbl    = Point;    pips2points =  1;   Digits.pips = 0; }
    // OrderSend(... Slippage.Pips * pips2points, Bid - StopLossPips * pips2dbl
 
WHRoeder:

These lead me to believe you're on a 5 digit broker, therefor TP, SL, and Slippage must be adjusted:


Thank you very much,Sir, I'm on a 5 digit broker,I'll study your code,and I have not find out where is wrong yet.

Reason: