Download MetaTrader 5

What modify to close the transaction to close out the candle?

To add comments, please log in or register
Start making profit using the Market. Publish your product!
David Araujo Magalhaes
218
David Araujo Magalhaes 2015.09.20 23:17 
friends,

What should I modify this code so that the open operation on the signal , is closed at the close of the candle ?
I do not want to use TP and SL !
I'm still learning to program ! I thank anyone who can help me!
#property copyright ""
#property link      ""


#define MAGIC 30015

//---- input parameters
extern string Sep1 = " Bollinger Bands";
extern int     BBPeriod       = 20;
extern double  BBDev    = 2.0; 

extern int       TimePeriod = 15; // in minutes
extern bool      UseFixedLots = true;
extern int       TamanhoLote = 1.0;


double entry = 0;
//+------------------------------------------------------------------+
//| expert initialization function                                   |
//+------------------------------------------------------------------+
int init()
{
   return(0);
}
  
//+------------------------------------------------------------------+
//| expert deinitialization function                                 |
//+------------------------------------------------------------------+
int deinit()
{
   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()==MAGIC)
        {
         if(OrderType()==OP_BUY)  buys++;
         if(OrderType()==OP_SELL) sells++;
        }
     }
//---- return orders volume
   if(buys>0) return(buys);
   else       return(-sells);
}
  

  
double LotSize()
{
   if (UseFixedLots == true) return (TamanhoLote);

   double size;
   if(size>1000)return(1000);
   if(size<0.01)return(0.01);
   return(size);
}

bool EnterLong() 
{
   double bbdown=iBands(NULL,0,BBPeriod,BBDev,0,PRICE_CLOSE,MODE_LOWER,0);
   
      if (Close[0]<bbdown) {
      return (true);
   }
   else return (false);
}
bool CloseLong()
{
   // Close intentionally if we're profitable
   if (Bid > entry) {
      double ma;
      ma = iMA(NULL,TimePeriod,BBPeriod,0,MODE_EMA,PRICE_TYPICAL,1);
   
      // Close when the price drops below the moving average
      if ((Bid < ma)) {
         return (true);
      }
      else return (false);
   } else return (false);
}
bool EnterShort() 
{
   double bbup=iBands(NULL,0,BBPeriod,BBDev,0,PRICE_CLOSE,MODE_UPPER,0);
   
      if (Close[0]>bbup) {
      return (true);
   }
   else return (false);
}
bool CloseShort()
{
   // Close intentionally if we're profitable
   if (Ask < entry) {
      double ma;
   
      ma = iMA(NULL,TimePeriod,BBPeriod,0,MODE_EMA,PRICE_TYPICAL,1);
   
      // Close when the price is above the moving average
      if ((Ask > ma)) {
         return (true);
      }
      else return (false);
   } else return (false);
}

//+------------------------------------------------------------------+
//| Check for open order conditions                                  |
//+------------------------------------------------------------------+
void CheckForOpen()
  {
   int res;
   
   // long conditions
   if (EnterLong()) {
      res = OrderSend(Symbol(),OP_BUY,LotSize(),Ask,0,0,0,"",MAGIC);
      entry = Ask;
      return;
   }
   
   // short conditions
   else if (EnterShort()) {
      res = OrderSend(Symbol(),OP_SELL,LotSize(),Bid,0,0,0,"",MAGIC);
      entry = Bid;
   }
 
  }
//+------------------------------------------------------------------+
//| Check for close order conditions                                 |
//+------------------------------------------------------------------+
void CheckForClose()
{  
   for (int i=0; i<OrdersTotal(); i++) {
      if (OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false) break;
      if (OrderMagicNumber()!=MAGIC || OrderSymbol()!=Symbol()) continue;
      
      if (OrderType()==OP_BUY) {
         // close long conditions
         if (CloseLong()) {
            OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),0,White);
            if (EnterShort()) {
               OrderSend(Symbol(),OP_SELL,LotSize(),Bid,0,0,0,"",MAGIC);
               entry = Bid;
            }
            break;
         }
      }
      if (OrderType()==OP_SELL) {
         // close short conditions
         if (CloseShort()) {
            OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),0,White);
            if (EnterLong()) {
               OrderSend(Symbol(),OP_BUY,LotSize(),0,0,0,"",MAGIC);
               entry = Ask;
            }
            break;
         }
      }
   }
}

  
  
//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+
int start()
  {
//----

//---- check for history and trading
   if(Bars<100 || IsTradeAllowed()==false) return;
//---- calculate open orders by current symbol
   if(CalculateCurrentOrders(Symbol())==0) CheckForOpen();
   else                                    CheckForClose();
     //----
    //----
   return(0);
  }
//+------------------------------------------------------------------+
To add comments, please log in or register