Order Close

To add comments, please log in or register
Thomas A.
817
Thomas A.  

Hey guys,

Sorry to bump in here, I'm trying to set up my first robot, woohoo. So far i succeeded in defining inputs for the indicators which do display! Orders are being opened as well but I'm a bit struggling with defining code to close them. At this moment I would just like to close the trades when a buy trade is moving below the middle bollinger and sell trade is moving above. When I set this as below I see that "Some operator is missing".

if (Ask < BollingerMiddle) 

        {

         OrderClose(NULL,NULL,Bid,NULL,CLR_NONE)

         Print ("*Bollinger 1986 Close Buy Order");

        }

 

 

 if (Bid > BollingerMiddle)

        {

         OrderClose(NULL,NULL,Ask,NULL,CLR_NONE)

         Print ("*Bollinger 1986 Close Sell Order");

        }



 } 


 
The full code is below.

Do you have any idea what I'm doing wrong? :(

Full code is below


//+------------------------------------------------------------------+
//|                                                CCI Bollinger.mq4 |
//|                                                      Insearchfor |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
//+------------------------------------------------------------------+
//|                            Testing Checklist: Middle Bank Closer |
//|                   To do: Set Max OpenTrades (per budget)         |
//|                                To do: Set And/Or Functions       |
//|                                To do: Automated lot size         |  
//|                                To do: MonteCarlo Learning        | 
//|                                To do: Martingale                 |   
//+------------------------------------------------------------------+
#property copyright "Insearchfor"
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict
//+------------------------------------------------------------------+
//| Declarations                                  |
//+------------------------------------------------------------------+
bool LongSetup = False;
bool ShortSetup = False;

//+------------------------------------------------------------------+
//| Input                                |
//+------------------------------------------------------------------+

      string                  hint0                          = "===== Magic =====";
      string                  hint1_sep0                     = "== Trading time ==";
      int                     Magic                          = 19121986;

input string                  hint1                          = "===== Time Settings =====";
input string                  hint1_sep1                     = "== Trading time ==";
input string                  TradeOpenTimeFrom1             = "23:00";
input string                  TradeOpenTimeTo1               = "11:30";

input string                  hint1_sep2                       = "== Force all orders closure ==";
input string                  ForceTradeCloseTime              = "22:00";
input bool                    ForceTradeCloseTimeDay_Monday    = false;
input bool                    ForceTradeCloseTimeDay_Tuesday   = false;
input bool                    ForceTradeCloseTimeDay_Wednesday = false;
input bool                    ForceTradeCloseTimeDay_Thursday  = false;
input bool                    ForceTradeCloseTimeDay_Friday    = true;

input string                  hint2                         = "===== Order Settings =====";
input double                  LotSize                       = 0.1;      // Fixed Lotsize
input bool                    AutoLot                       = false;
input double                  Division_Entry                = 1000;
input int                     MaxSpread                     = 15;            
input int                     StopLoss                      = 0;
input int                     TakeProfit                    = 0;
input int                     MaxTradesPerInstrument        = 10;   

input string                  hint3                         = "===== Bollinger Settings =====";
input int                     PeriodB                       = 14;               
input int                     Deviation                     = 1;              
input int                     BollingerBandShift            = 0;                 
input ENUM_APPLIED_PRICE      BollingerAppliedPrice         = PRICE_CLOSE;
input int                     BollingerShift                = 0;  

input string                  hint4                         = "===== CCI Settings =====";
input int                     CCIPeriod                     = 100;
      ENUM_APPLIED_PRICE      CCIAppliedPrice               = PRICE_CLOSE;
      int                     CCIShift                      = 0; 

input string                  hint5                         = "===== Fast Moving Average Settings =====";
input int                     FastMovingAveragePeriod       = 5;
input int                     FastMovingAverageMAShift      = 1;
input ENUM_MA_METHOD          FastMovingAverageMethod       = MODE_SMA;
input ENUM_APPLIED_PRICE      FastMovingAverageAppliedPrice = PRICE_CLOSE;
input int                     FastMovingAverageShift        = 0;

input string                  hint6                         = "===== Slow Moving Average Settings =====";
input int                     SlowMovingAveragePeriod       = 200;
input int                     SlowMovingAverageMAShift      = 1;
input ENUM_MA_METHOD          SlowMovingAverageMethod       = MODE_SMA;
input ENUM_APPLIED_PRICE      SlowMovingAverageAppliedPrice = PRICE_CLOSE;
input int                     SlowMovingAverageShift        = 0;

input string                  hint7                         = "===== MACD =====";
input ENUM_TIMEFRAMES         MACDTimeFrame                 = PERIOD_D1;
input int                     FastEMAPeriod                 = 6;
input int                     SlowEMAPeriod                 = 40;
input int                     SignalPeriod                  = 3;
input ENUM_APPLIED_PRICE      MACDSMAAppliedPrice = PRICE_CLOSE;
input int                     MACDLineIndex                 = 1;
input int                     MACDSHift                     = 0;

//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+

int OnInit()
  {

   return(INIT_SUCCEEDED);
  }
void OnDeinit(const int reason)
  {

   
  }
  
  
  
// Working Hour
bool WorkingHour()
{
   datetime gi_time_01 = StringToTime(TimeToString(TimeCurrent(), TIME_DATE) + " " + TradeOpenTimeFrom1);
   datetime gi_time_02 = StringToTime(TimeToString(TimeCurrent(), TIME_DATE) + " " + TradeOpenTimeTo1);
   datetime datetime_0 = TimeCurrent();
   
   if( gi_time_01 < gi_time_02 && gi_time_01 <= datetime_0 && datetime_0 <= gi_time_02 ) return (true);
   if( gi_time_01 > gi_time_02 && (datetime_0 >= gi_time_01 || datetime_0 <= gi_time_02) ) return (true);
   
   return (false);
}

// Return the total number of open trades
int GetTotalOpenTrades ()
{
   int TotalTrades = 0;
   
   // Loop through open orders and add them to TotalTrades
   for (int t=0; t<OrdersTotal(); t++) 
   {
      if(OrderSelect(t, SELECT_BY_POS, MODE_TRADES))
      {
         if(OrderSymbol() != Symbol()) continue;
         if(OrderMagicNumber() != Magic) continue;
         if(OrderCloseTime() !=0) continue;    
         
         TotalTrades = (TotalTrades +1);
      }

    }  

      return TotalTrades;
}
   
// This runs on every single tick
void OnTick()
  {
      double BollingerUpper = iBands(NULL,NULL,PeriodB,Deviation,BollingerBandShift,BollingerAppliedPrice,MODE_UPPER,BollingerShift);
      double BollingerLower = iBands(NULL,NULL,PeriodB,Deviation,BollingerBandShift,BollingerAppliedPrice,MODE_LOWER,BollingerShift);
      double BollingerMiddle = (BollingerUpper+BollingerLower)/2;  // DOUBLE CHECK WITH TESTING
      double CCI = iCCI(NULL,NULL,CCIPeriod,CCIAppliedPrice,CCIShift);
      double FastMovingAverage = iMA (NULL,NULL,FastMovingAveragePeriod,FastMovingAverageMAShift,FastMovingAverageMethod,FastMovingAverageAppliedPrice,FastMovingAverageShift);
      double SlowMovingAverage = iMA (NULL,NULL,SlowMovingAveragePeriod,FastMovingAverageMAShift,SlowMovingAverageMethod,SlowMovingAverageAppliedPrice,SlowMovingAverageShift);
      double MACD = iMACD(NULL,MACDTimeFrame,FastEMAPeriod,SlowEMAPeriod,SignalPeriod,MACDSMAAppliedPrice,MACDLineIndex,MACDSHift);
 
   //int Trades = GetTotalOpenTrades();
   //Print("*** Our Total number of Trades: ", Trades);
   
   
 
 if ( GetTotalOpenTrades () < MaxTradesPerInstrument )
      
      // Check for long trade setup
      {
      if ( 
         (Ask > BollingerUpper) && 
         (CCI > 100) && 
         (FastMovingAverage > SlowMovingAverage)&& 
         (MACD > 100)
         )
        {
          LongSetup = True;
        }
        
      if (LongSetup == True)
        {
         int OrderResult = OrderSend(Symbol(), OP_BUY, LotSize, Ask, 10, 0, 0,"Bollinger 1986 Buy Order", Magic, 0, clrGreen);
         Print ("*Bollinger 1986 Buy Order");
         LongSetup = False;
        }
      }
      // Check for short trade setup
      {
      if ( 
         (Bid < BollingerLower) && 
         (CCI < 0) && 
         (FastMovingAverage < SlowMovingAverage)&& 
         (MACD < 0)
         )
        {
          ShortSetup = True;
        }
       } 
       if (ShortSetup == True)
        {
         int OrderResult = OrderSend(Symbol(), OP_SELL, LotSize, Bid, 10, 0, 0,"Bollinger 1986 Sell Order", Magic, 0, clrGreen);
         Print ("*Bollinger 1986 Sell Order");
         LongSetup = False;
        }
   
//+-------------------------------
//Close Order
//+-------------------------------

if (Ask < BollingerMiddle) 
        {
         OrderClose(NULL,NULL,Bid,NULL,CLR_NONE)
         Print ("*Bollinger 1986 Close Buy Order");
        }
 
 
 if (Bid > BollingerMiddle)
        {
         OrderClose(NULL,NULL,Ask,NULL,CLR_NONE)
         Print ("*Bollinger 1986 Close Sell Order");
        }

 } 
 
 
William Roeder
20233
William Roeder  
Insearchfor: t "Some operator is missing".
   OrderClose(NULL,NULL,Bid,NULL,CLR_NONE) Print ("*Bollinger 1986 Close Buy Order");
  1. Obviously something is missing there.

  2. Don't use NULL, invalid symbol, invalid size, invalid slippage.
    • You can use NULL in place of _Symbol only in those calls that the documentation specially says you can. iHigh does, MarketInfo does not. OrderSend does not.
    • Don't use NULL (except for pointers where you explicitly check for it.) Use _Symbol and _Period, that is minimalist as possible and more efficient.
    • Zero is the same as PERIOD_CURRENT which means _Period. Don't hard code numbers.
    • MT4: No need for a function call with iHigh(NULL,0,s) just use the predefined arrays, i.e. High[].

  3. Check your return codes for errors, and report them including GLE/LE. Don't look at it unless you have an error. Don't just silence the compiler, it is trying to help you. Don't post code that still has warnings.
              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
    Only those functions that return a value (e.g. iClose, MarketInfo, etc.) must you call ResetLastError before in order to check after.

To add comments, please log in or register