簡體   English   中英

如何使此MQL4 EA正確執行?

[英]How can I get this MQL4 EA to execute properly?

我試圖在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 "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);
}

歡迎使用MQL4! 請顯示EA的代碼(顯示的內容還不夠) OnTick,OnTimer,OnChartEvent必須根據某些事件( OnTick,OnTimer,OnChartEvent )采取一些措施,檢查某些條件(如示例中顯示的條件)並決定關於做什么的事情(什么都不做,發送買入,發送賣出,移動止損/獲利,必要時進行其他修改,關閉憑單),然后發送訂單或對其進行修改或關閉。 查看MQL4 \\ Experts文件夾中提供的MA EA和Macd EA的示例,以了解其外觀

確實發布了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(){...} -此處為double CalcPoint ),一旦代碼執行步驟“超出范圍”,則該不存在/將不具有任何值有效范圍。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM