如何让这个MQL4 EA正确执行?

时间:2017-12-15 09:26:47

标签: automation mql4

我正在尝试在MQL4上创建一个相当简单的专家顾问,但在编译后即使没有任何错误也不会执行。

概念如下:

要执行的买单:

  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);

要执行的卖单:

  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);

我添加了这些代码,但他们没有执行任何订单。

想法是EA在以下时间购买:

  1. OSMA直方图大于0

  2. CC1(5)小于-100

  3. 下蜡烛灯芯(阴影)大于20点

  4. 出售:

    1. OSMA直方图少于0

    2. CCI(5)大于100

    3. 上蜡烛芯(阴影)大于20点。

    4. 任何帮助将不胜感激。

      由于

      #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);
      }
      

2 个答案:

答案 0 :(得分:0)

欢迎使用MQL4!请显示EA的代码(您显示的内容是不够的) - 必须根据某些事件(OnTick,OnTimer,OnChartEvent)采取一些行动,检查一些条件(如您在示例中显示的那些条件)并决定如何做(没有,发送买入,发送卖出,移动止损/止赎,必要时进行其他修改,关闭机票),然后发送订单或修改或关闭。查看MQL4 \ Experts文件夹中给出的MA EA和Macd EA的示例,看看它应该是什么样子

答案 1 :(得分:0)

更新,MCVE确实发布后:

您的代码忽略了一种语言规则。它被称为有效范围。鉴于上面的代码,函数RealPipPoint()会导致一个主要问题:

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 );
}

在这里,return( )几乎不会返回正确的值。为什么?正确的,因为有效范围:double CalcPoint,声明&#34;内部&#34;一些范围(if(){...} - 一个在这里),一旦代码执行步骤&#34;外部&#34;停止存在/承担任何值。有效范围。

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 );
}

接下来,
如果您定义了一个函数类型,那么保持这样的定义是公平的:

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

新的 - MQL4代码执行单元需要这个正式的结构:

公平地说,如果&#34; 它有9个警告但是0个错误 &#34;没有什么是致命的,除了阅读警告所涵盖的内容(最重要的是,此类警告可帮助程序员完善类型转换/类型转换并消除代码歧义)。

随意点击列出的错误/警告,IDE应该将光标移动到触发错误/警告的行。

// #####################################################################
// 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?
        ..
}