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

问题描述 投票:1回答:2

我正在尝试在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点

出售:

  1. OSMA直方图小于0
  2. CCI(5)大于100
  3. 上蜡烛芯(阴影)大于20点。

任何帮助将不胜感激。

谢谢

#property copyright "Chinedu Onuoha"
#property link "[email protected]"

// 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);
}
automation mql4
2个回答
0
投票

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


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,在某个范围内声明“内部”(if(){...}-one),一旦代码执行步骤超出有效范围,就不再存在/承担任何值。

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代码执行单元需要这个正式的结构:

公平地说,如果“它有9个警告而0个错误”没有什么致命的,除了阅读警告所涵盖的内容(最重要的是,这样的警告有助于程序员抛光类型转换/类型转换并消除歧义代码)。

随意点击列出的错误/警告,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?
        ..
}
© www.soinside.com 2019 - 2024. All rights reserved.