简体   繁体   中英

How can I get this MQL4 EA to execute properly?

I am trying to create a fairly simple expert advisor on MQL4 but it does not execute even without any errors after compiling.

The concept is as below:

For a buy order to be executed:

  bool buy_condition_1 = iOsMA(NULL, 0, 12, 26, 9, PRICE_CLOSE, 1)  >  0 ;
  bool buy_condition_2 = iCCI(NULL, 0, 14, PRICE_CLOSE, 1)  <  -100 ;
  bool buy_condition_3 = (MathMin(Open[1],Close[1]) - Low[1] >= 200*Point);

For a sell order to be executed:

  bool sell_condition_1 = iOsMA(NULL, 0, 12, 26, 9, PRICE_CLOSE, 1)  <  0;
  bool sell_condition_2 = iCCI(NULL, 0, 14, PRICE_CLOSE, 1)  > 100 ;
  bool sell_condition_3 = (High[1] - MathMax(Open[1],Close[1]) >=200*Point);

I added these codes but they dont execute any orders.

The Idea is for the EA to Buy when:

  1. OSMA histogram is greater than 0

  2. CC1(5) is Less than -100

  3. the Lower candle Wick(shadow) is greater than 20 pips

For a sell:

  1. OSMA histogram is Less tham 0

  2. CCI(5) is greater than 100

  3. The upper candle wick(shadow) is greater than 20 pips.

Any help will be appreciated.

Thanks

#property copyright "Chinedu Onuoha"
#property link "profedu2001@gmail.com"

// External variables
extern double LotSize = 0.1;
extern double StopLoss = 20;
extern double TakeProfit = 0;
extern double TrailingStopLimit = 0;
extern double TrailingStopStop = 0;
extern int MagicNumber = 23310;


// Global variables
int LongTicket;
int ShortTicket;
double RealPoint;
double open;




// Init function
int init(){
    open = 0;
    RealPoint = RealPipPoint(Symbol());
}

// Start function
int start(){
  if (open  == Open[0]) return 0;
  open = Open[0];

  //long

   OrderSelect(LongTicket,SELECT_BY_TICKET);
   if(OrderCloseTime() != 0 || LongTicket == 0) {

      bool buy_condition_1 = iOsMA(NULL, 0, 12, 26, 9, PRICE_CLOSE, 1)  >  0 ;
      bool buy_condition_2 = iCCI(NULL, 0, 14, PRICE_CLOSE, 1)  <  -100 ;
      bool buy_condition_3 = (MathMin(Open[1],Close[1]) - Low[1] >= 200*Point);


      if( buy_condition_1  &&  buy_condition_2  &&  buy_condition_3 ){

          OrderSelect(ShortTicket,SELECT_BY_TICKET);

          if(OrderCloseTime() == 0 && ShortTicket > 0){
            bool Closed = OrderClose(ShortTicket,OrderLots(),Ask,0,Red);
          }

          LongTicket = OrderSend(Symbol(),OP_BUY,LotSize,Ask,0,0,0,"Buy Order",MagicNumber,0,Green);

          OrderSelect(LongTicket,SELECT_BY_TICKET);
          double OpenPrice = OrderOpenPrice();

          if(StopLoss > 0) double LongStopLoss = OpenPrice - (StopLoss * RealPoint);
          if(TakeProfit > 0) double LongTakeProfit = OpenPrice + (TakeProfit * RealPoint);

            if(LongStopLoss > 0 || LongTakeProfit > 0) {
            bool LongMod = OrderModify(LongTicket,OpenPrice,LongStopLoss, LongTakeProfit,0);
            }
            ShortTicket = 0;
      }
   }


   //Close long
   if (OrdersTotal() > 0){
      bool close_buy_condition_1 = iCCI(NULL, 0, 14, PRICE_CLOSE, 1)  > 100 ;
      bool close_buy_condition_2 = iOsMA(NULL, 0, 12, 26, 9, PRICE_CLOSE, 1)  <  0;
       if( close_buy_condition_1 && close_buy_condition_2){

            OrderSelect(LongTicket,SELECT_BY_TICKET);
            if(OrderCloseTime() == 0 && LongTicket > 0){
                Closed = OrderClose(LongTicket,OrderLots(),Bid,0,Red);
                LongTicket = 0;
            }
        }
    }

   // Short
   OrderSelect(ShortTicket,SELECT_BY_TICKET);
   if (OrderCloseTime() != 0 || ShortTicket == 0) {

      bool sell_condition_1 = iOsMA(NULL, 0, 12, 26, 9, PRICE_CLOSE, 1)  <  0;
      bool sell_condition_2 = iCCI(NULL, 0, 14, PRICE_CLOSE, 1)  > 100 ;
      bool sell_condition_3 = (High[1] - MathMax(Open[1],Close[1]) >= 200*Point);


        if( sell_condition_1  &&  sell_condition_2  &&  sell_condition_3 ){

          OrderSelect(LongTicket,SELECT_BY_TICKET);
          if(OrderCloseTime() == 0 && LongTicket > 0){
            Closed = OrderClose(LongTicket,OrderLots(),Bid,0,Red);
          }
          ShortTicket = OrderSend(Symbol(),OP_SELL,LotSize,Bid,0,0,0,"Sell Order",MagicNumber,0,Red);
          OrderSelect(ShortTicket,SELECT_BY_TICKET);
          OpenPrice = OrderOpenPrice();

          if(StopLoss > 0) double ShortStopLoss = OpenPrice + (StopLoss * RealPoint);
          if(TakeProfit > 0) double ShortTakeProfit = OpenPrice - (TakeProfit * RealPoint);
           if(ShortStopLoss > 0 || ShortTakeProfit > 0) {
                bool ShortMod = OrderModify(ShortTicket,OpenPrice,ShortStopLoss, ShortTakeProfit,0);
           }
          LongTicket = 0;
        }
   }

     //Close Short
   if (OrdersTotal() > 0){
    bool close_sell_condition_1 = iCCI(NULL, 0, 14, PRICE_CLOSE, 1)  <  -100 ;
    bool close_sell_condition_2 = iOsMA(NULL, 0, 12, 26, 9, PRICE_CLOSE, 1)  >  0;

    if ( close_sell_condition_1 && close_sell_condition_2){
        OrderSelect(ShortTicket,SELECT_BY_TICKET);
      if(OrderCloseTime() == 0 && ShortTicket > 0){
        Closed = OrderClose(ShortTicket,OrderLots(),Ask,0,Red);
        ShortTicket = 0;
      }
    }
  }

   return(0);
}


// Pip Point Function
double RealPipPoint(string Currency){
   int CalcDigits = MarketInfo(Currency,MODE_DIGITS);
   if(CalcDigits == 2 || CalcDigits == 3) double CalcPoint = 0.01;
   else if(CalcDigits == 4 || CalcDigits == 5) CalcPoint = 0.0001;
   return(CalcPoint);
}

Welcome to MQL4! Please show the code of the EA (what you showed is not enough)- ea must take some actions based on some events ( OnTick,OnTimer,OnChartEvent ), check some conditions (like those that you've shown in your example) and decide on what to do (nothing, send buy, send sell, move stoploss/takeprofit,other modification if necessary, close ticket) and then send an order or modify it or close. Look at the examples of MA EA and Macd EA that are given in the MQL4\\Experts folder to see how it should look like

Update, after MCVE was indeed posted:

Your code ignores one language rule. It is called a scope-of-validity. Given the code above, the function RealPipPoint() causes in silence a principal problem:

double RealPipPoint( string Currency ){
   int       CalcDigits  = MarketInfo( Currency, MODE_DIGITS );
   if (      CalcDigits == 2
      ||     CalcDigits == 3
        ) double CalcPoint = 0.01;
   else if(  CalcDigits == 4
          || CalcDigits == 5
             )   CalcPoint = 0.0001;
   return( CalcPoint );
}

Here, the return( ) will hardly ever return a proper value. Why? Right because of the scope-of-validity: a double CalcPoint , declared "inside" some scope ( the if(){...} -one here ), cease to exist / bear any value once the code execution steps "outside" of the scope-of-validity.

double RealPipPoint( string Currency ){
   int       CalcDigits  = MarketInfo( Currency, MODE_DIGITS );
   if (      CalcDigits == 2
      ||     CalcDigits == 3
        )                  return( 0.01 );     // WILL WORK FINE
   else if(  CalcDigits == 4
          || CalcDigits == 5
             )             return( 0.0001 );   // WILL WORK FINE
   return( EMPTY );
}

Next,
if you define a function type, it is fair to keep such definition:

// Init function
int init(){                                    // read New-MQL4 int OnInit(){...}
    open      = 0;
    RealPoint = RealPipPoint( _Symbol );
    return( INIT_SUCCEDED );                   // return(); // WAS MISSING AT ALL
}

New - MQL4 code-execution units need about this formal structure:

It is fair to state, that if " it has 9 warnings but 0 errors " there is nothing fatal, except to read what the warnings cover ( most of all, such warnings help programmer to polish type-cast / type-conversions and disambiguate the code ).

Feel free to click on the error/warning listed, and the IDE ought move your cursor to the line, that has triggered the error/warning.

// #####################################################################
// CODE-PREPROCESSOR AND MQL4-LANGUAGE-SPECIFIC DEFINITIONS:
#property show_inputs

// #####################################################################
// HEADER DEFINITIONS:
#define aThingToDEFINE   aValueToHAVE

// #####################################################################
// EXTERNS FOR EA-SETUP + STRATEGY-TESTER OPTIMISATION SCANS:
extern double OsMA_LIMIT_LONG =     0;
extern double iCCA_LIMIT_LONG =  -100;
extern double OC2L_LIMIT_LONG =   200;

extern double OsMA_LIMIT_SHORT =    0;
extern double iCCA_LIMIT_SHORT =  100;
extern double OC2L_LIMIT_SHORT =  200;

// #####################################################################
// INIT HANDLER:
   int  onInit(){

        OC2L_LIMIT_LONG  *= Point;
        H2OC_LIMIT_SHORT *= Point;

        return( INIT_SUCCEEDED );
   }
// #####################################################################
// FX-MARKET QUOTE-FEED EVENT HANDLER:
   void OnTick(){

     // LONG-DIRECTION FLAGS
        bool buy_condition_1 = ( OsMA_LIMIT_LONG <  iOsMA( NULL, 0, 12, 26, 9, PRICE_CLOSE, 1 ) );
        bool buy_condition_2 = ( iCCA_LIMIT_LONG >  iCCI(  NULL, 0, 14, PRICE_CLOSE, 1 ) );
        bool buy_condition_3 = ( OC2L_LIMIT_LONG <= ( MathMin(  Open[1],
                                                               Close[1]
                                                               )
                                                    -            Low[1]
                                                      )
                                 );    
     // SHORT-DIRECTION FLAGS
        bool sell_condition_1 = ( OsMA_LIMIT_SHORT >  iOsMA( NULL, 0, 12, 26, 9, PRICE_CLOSE, 1 ) );
        bool sell_condition_2 = ( iCCI_LIMIT_SHORT <  iCCI(  NULL, 0, 14, PRICE_CLOSE, 1 ) );
        bool sell_condition_3 = ( H2OC_LIMIT_SHORT <= (           High[1]
                                                      - MathMax(  Open[1],
                                                                 Close[1] 
                                                                 )
                                                        )
                                  );
     // DECISIONS:
        if (  buy_condition_1
           && buy_condition_2
           && buy_condition_3
              ){
        // ***********************************
           // ACT:

        }
        if (  sell_condition_1
           && sell_condition_2
           && sell_condition_3
              ){
        // ***********************************   
        // ACT:

        }

}
// #####################################################################
// DEINIT HANDLER:
void OnDeinit( const int aDeinitREASON ){

     // TIDY-UP?
        ..
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM