Implement two conditions to enter a trade_ MA and CCI. (EA) - page 2

 
OTMT Howard #:

Hei Alexander Martinez,


I appreciate your patience in answering my questions. Before entering a trade, I want to confirm two conditions by looking for both the MA and the CCI to give a confirmed signal on either bullish momentum or bearish conditions.


With this in mind, I'd like to be able to set these in my EA's Settings as "true" or "false."


I hope my idea is clear for your support with this plan


The code attached is what I'm trying to implement in my EA

No problem. I edited my previous post since I didn't see your post afterwards.

By the way, with the code you provided, you're now re-declaring x1 and x2 here:

int signal_cci() { // CCI crossing 100 level - buy; crossing -100 level - sell enter market
   double x1,x2;
   
   x1 = iCCI(Symbol(),_Period,50,PRICE_TYPICAL,0);
   x2 = iCCI(Symbol(),_Period,50,PRICE_TYPICAL,1);
  
  
  if(x1>-100 && x2<=-100) return _Sell;
  if(x1<100 && x2>=100) return _Buy;
   return 0;   
}

Also, in the future, it's recommended that you post your entire code instead of big parts that are missing other parts.

Lastly, since you're still learning, I recommend that you write mini-programs of your code to make sure it works exactly how you want, then integrate it into your main program. This way, you can be absolutely certain about what it is you're doing and that you're getting the desired result. Otherwise, your main program becomes a lot tougher to debug and also tougher for others to help you out.

For example, if you had written this in a separate program, you would realize that you're missing a semi-colon at the end of the bool declaration, and that that you have a type mismatch (i.e., you're declaring a bool variable, but you're actually assigning a double value to it). You would also see that you're not getting the desired result and you can debug that pretty quickly.

bool MAFilterCheck() {
  const bool maVal = iMA(Symbol(),0, MA_Period,MA_Shift,MA_Method,MA_Price,1) //--- enter your MA condition

  if (Close[0] > maVal && Close [0] <= maVal)
    return true;

  return false;
}
 

I'm thinking that my task might be a bit of a mission to complete, so I've decided to go off with CCI and 2MA conditions integrated into my system:


I'd appreciate it if you could walk me through the following two errors:

The void OnTick(){ presents an '{' - unbalanced parentheses followed with '}' - unexpected end of program

extern int     StartHour      = 9;
extern int     MinPipLimit    = 0;
extern int     TakeProfit     = 40;
extern int     StopLoss       = 40;
extern bool    TrailingStop   = false; //Trailing Stop
extern int     TrailingStart  = 15;    //Trailing Start (pips)
extern int     TrailingStep   = 5;     //Trailing Step (pips)
input int      Slippage       = 0;
extern double  Lots           = 1;
extern int     Magic          = 50262802;
input string   TradeComment   = "CCI2MA";
//===================================================================================================================================================//
input string   Fast_MA_Parameters  = "||========== Fast MA Parameters ==========||";
extern int     FastMA_Period      = 14;
input ENUM_MA_METHOD FastMA_Method      = MODE_EMA;
input int      FastMA_Shift             = 0;
input ENUM_APPLIED_PRICE FastMA_Price   = PRICE_CLOSE;
//===================================================================================================================================================//
input string   MA_Parameters  = "||========== Slow MA Parameters ==========||";
extern int     SlowMA_Period      = 25;
input ENUM_MA_METHOD SlowMA_Method      = MODE_EMA;
input int      SlowMA_Shift             = 0;
input ENUM_APPLIED_PRICE SlowMA_Price   = PRICE_CLOSE;
//===================================================================================================================================================//
input string   CCI_Inputs     = "||========== CCI Parameters ==========||"; 
input int                  CCIPeriod      =  14;            // CCI Period
input ENUM_APPLIED_PRICE   CCIPrice       =  PRICE_CLOSE;   // CCI Applied price

double vPoint;
int vSlippage;
double GetPipPoint(string pair);

int OnInit()
  {
// Detect 3/5 digit brokers for Point and Slippage

   if(Point == 0.00001)
     { vPoint = 0.0001; vSlippage = Slippage *10;}
   else
     {
      if(Point == 0.001)
        { vPoint = 0.01; vSlippage = Slippage *10;}
      else
         vPoint = Point;
      vSlippage = Slippage;

      if(TrailingStop && TrailingStart <= 0)
        {
         Alert("Parameters incorrect");
         return(INIT_PARAMETERS_INCORRECT);
        }
      double myPoint = Point()*10;
      myPoint = GetPipPoint(Symbol());
     }
     {
      Print("Free margin for 1 lot=",MarketInfo(Symbol(), MODE_MARGINREQUIRED));
      Print("Freezing Distance[pts]=",MarketInfo(Symbol(), MODE_FREEZELEVEL));
      Print("Min Allowed Distance[pts]=",MarketInfo(Symbol(), MODE_STOPLEVEL));
      Print("MODE_LOTSIZE = ", MarketInfo(Symbol(), MODE_LOTSIZE));
      Print("MODE_MINLOT = ", MarketInfo(Symbol(), MODE_MINLOT));
      Print("MODE_LOTSTEP = ", MarketInfo(Symbol(), MODE_LOTSTEP));
      Print("MODE_MAXLOT = ", MarketInfo(Symbol(), MODE_MAXLOT));
      }
   return(INIT_SUCCEEDED);
  }

void OnTick(){
   
   if (!(bool)TerminalInfoInteger(TERMINAL_TRADE_ALLOWED)) return;
    
   static int  maCross  =  0;
   static int  cciCross =  0;
   static int  ticket = 0;
   string comment;
   
   bool  newBar   =  NewBar();
   if (newBar) {
      double   cci1     =  iCCI(Symbol(), Period(), CCIPeriod, CCIPrice, 1);
      double   cci2     =  iCCI(Symbol(), Period(), CCIPeriod, CCIPrice, 2);
       
      if (cci1>0 && cci2<=0)   cciCross =  1;
      if (cci1<0 && cci2>=0)   cciCross =  -1;
      
      double   fastMa1  =  iMA(Symbol(), Period(), FastMA_Period, 0, FastMA_Method, FastMA_Price, 1);
      double   fastMa2  =  iMA(Symbol(), Period(), FastMA_Period, 0, FastMA_Method, FastMA_Price, 2);
      double   slowMa1  =  iMA(Symbol(), Period(), SlowMA_Period, 0, SlowMA_Method, SlowMA_Price, 1);
      double   slowMa2  =  iMA(Symbol(), Period(), SlowMA_Period, 0, SlowMA_Method, SlowMA_Price, 2);
      
      if(Hour() == StartHour)
      {
      comment = GetDateAndTime();
      if(newBar)
      {
         NewBar = false;
      
         //FindTicket makes sure that the EA will pick up its orders
         //even if it is relaunched
         ticket = FindTicket(Magic);         
            
         bool res;
         res = OrderSelect(ticket, SELECT_BY_TICKET);
         if(res == true)
         {
            if(OrderCloseTime() == 0)
            {
               bool res2;
               res2 = OrderClose(ticket, OrderLots(), OrderClosePrice(), 10);
               
               if(res2 == false)
               {
                  Alert("Error Closing Order #", ticket);
               }
            } 
         }
      
      if (fastMa1>slowMa1 && fastMa2<=slowMa2) {
         maCross  =  1;
         CloseTrades(ORDER_TYPE_SELL);
      }
       
      if (fastMa1<slowMa1 && fastMa2>=slowMa2) {
         maCross  =  -1;
         CloseTrades(ORDER_TYPE_BUY);
      }
      if (maCross!=0 && maCross==cciCross) {
         if (OpenTrade(maCross)>0) {
            maCross  =  0;
            cciCross =  0;
         }
      }
   }
}

bool  CloseTrades(ENUM_ORDER_TYPE type) {

   bool     result         =  true;
   double   closingPrice   =  (type==ORDER_TYPE_BUY) ? SymbolBid() : SymbolAsk();
    
   int   cnt               =  OrdersTotal();
   for(int i = cnt - 1; i >= 0; i--) {
   
      if (!OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) {
         result   =  false;
         continue;
      }
      if (OrderSymbol()!=Symbol() || OrderMagicNumber()!=Magic)   continue;
      if ( OrderType()==type) {
         result   &=   OrderClose(OrderTicket(), OrderLots(), closingPrice, 0);
      }
   }
    
   return(result);
    
}
int   OpenTrade(int maCross) {
         if(Open[0] < Open[StartHour] - MinPipLimit*vPoint) //v3.0
           {
            if (Close[0] > maCross )) {
               //here we are assuming that the TakeProfit and StopLoss are entered in Pips
               ticket = OrderSend(Symbol(), OP_BUY, Lots, Ask, vSlippage, Bid-StopLoss*vPoint, Bid+TakeProfit*vPoint, "Set by Process Expert Advisor", Magic,0,Blue);
               if(ticket < 0)
                 {
                  comment = comment + " " + "Error Sending BUY Order";
                 }
               else
                 {
                  comment = comment + " " + "BUY Order Executed Succesfuly";
           }
   else
       if(Open[0] > Open[StartHour] + MinPipLimit*vPoint) //v3.0
   { 
   if(Close[1] < maCross)) {
                  ticket = OrderSend(Symbol(), OP_SELL, Lots, Bid, vSlippage, Ask+StopLoss*vPoint, Ask-TakeProfit*vPoint, "Set by Process Expert Advisor", Magic,0,Blue);
                  if(ticket < 0)
                    {
                     comment = comment + " " + "Error Sending SELL Order";
                    }
                  else
                    {
                     comment = comment + " " + "SELL Order Executed Succesfuly";
                 }
              }
            else
              {
               comment = comment + " " + "Reason Order Not Opened: MinPipLimit Filter Not Passed";
              }

         Comment(comment);
         Print(comment);
        }
     }
   else
     {
      NewBar = true;
     }

     {

bool  NewBar() {
   static datetime   current  =  0;
   datetime          now      =  iTime(Symbol(), Period(), 0);
   if (now == current)  return(false);
   current  =  now;
   return(true);
}
double   SymbolAsk(){  return(SymbolInfoDouble(Symbol(), SYMBOL_ASK)); }
double   SymbolBid(){  return(SymbolInfoDouble(Symbol(), SYMBOL_BID)); }

int FindTicket(int M)
  {
   int ret = 0;
   int total = OrdersTotal();
   for(int i = 0 ; i < total ; i++)
     {
      bool res;
      res = OrderSelect(i, SELECT_BY_POS,MODE_TRADES);
      if(res == true)
        {
         if(OrderMagicNumber() == M)
           {
            ret = OrderTicket();
            break;
           }
        }
     }

   return(ret);
  }

//+------------------------------------------------------------------+
//|                       Time Function                              |
//+------------------------------------------------------------------+
string GetDateAndTime()
  {
   return(string(Year())+"-"+StringFormat("%02d",Month())+"-"+StringFormat("%02d",Day())+" "+StringFormat("%02d",Hour())+":"+StringFormat("%02d",Minute()));
  }
  //+------------------------------------------------------------------+
//|    MQL4 Slippage & Point Value And Broker Pip Points                                           |
//+------------------------------------------------------------------+

// Fungsi GetPipPoint
double GetPipPoint(string pair)
  {
   double point= 0.0;
   int digits = (int) MarketInfo(pair, MODE_DIGITS);
   if(digits == 2 || digits== 3)
      point= 0.01;
   else
      if(digits== 4 || digits== 5)
         point= 0.0001;
   return(point);
  }
//+------------------------------------------------------------------+
 
OTMT Howard #:

I'm thinking that my task might be a bit of a mission to complete, so I've decided to go off with CCI and 2MA conditions integrated into my system:


I'd appreciate it if you could walk me through the following two errors:

The void OnTick(){ presents an '{' - unbalanced parentheses followed with '}' - unexpected end of program

Your program has a lot of missing / extra parenthesis and curly braces. The following is one example from "int   OpenTrade(int maCross)"; here you have an extra parenthesis at the the start of the if-statement.
if(Close[0] > maCross))

I made a quick attempt at fixing all the syntax errors, but there's just so many extra / missing curly braces and parenthesis.

I also think this is a good opportunity for you to learn to write organized, structured code, and make it consistent throughout your program.

On top of better styling, your functions and if-statements shouldn't be huge; break some of them down into smaller blocks of code by creating functions.

I'm going to leave you a link to the Google C++ Style Guide. The code blocks in red are bad practices, the code blocks in white are good practices.

Believe me:

1. Create a new EA

2. Take a block of code from start of your current EA and paste it into the new EA.

3. Fix the style so it complies with Google's C++ Style Guide

4. Hit "Compile" and see what errors come up - then fix those errors.

5. If the block of code you copied is huge, break it down into functions, replace the block with a call to those functions, then hit "Compile" and check for errors - fix if any errors that come up.

6. Repeat steps 2-5.

Once you've done that, your program should compile, and then we can check for any logical errors rather than having to deal with syntax errors.

Reason: