MT5的自動交易和自動平倉的EA如何編寫?純以最高價,最低價,開盤價,收盤價判斷買進賣出的時機

 
請問自動交易和自動止損的ea怎麼寫?

問題1: 條件a:當根的最高價 大於 前一根的最高價。賦予代碼aaa
條件b:前一根的收盤價 大於 前一根的開盤價。賦予代碼bbb
條件c:其他自己想的條件。賦予代碼ccc



   自動交易碼想法:同時符合條件a和條件b和條件c,自動交易

          例如:(iHigh(0) > iHigh(1)) && (iClose(1) > iOpen(1))
  套用代碼後:(aaa && bbb && ccc)


問題2: 條件d:當根的最低價 小於 前一根的最低價。賦予代碼ddd
條件e:前一根的收盤價 小於 前一根的開盤價。賦予代碼eee
條件f:其他自己想的條件。賦予代碼fff

   自動交易碼想法:同時符合條件d和條件e和條件f,自動平倉或自動止損,並且持有的部位全部平倉

          例如:(iLow(0) < iLow(1)) && (iClose(1) < iOpen(1))

  套用代碼後:(ddd && eee && fff)



以上所有條件,不指定交易貨幣,以打開的圖表貨幣為主


並且圖表上也會出現何時買進和何時平倉的訊號,圖表訊號和自動交易同步



補充說明:以即時的價格達到條件就執行自動交易,不以跳到下一根K棒才自動執行

 
//+----------------------------------------------- -------------------+
//| EA初始化1
//+----------------------------------------------- -------------------+
#include<Trade\Trade.mqh>//包含執行的交易庫
#include<Trade\PositionInfo.mqh>//包含部位的資訊庫

input int UpPeriod=10;
input int DownPeriod=10;

bool   SendNotification;

   int CheckBar;
   int Kcount;
   int iKcount;

input int 已有部位 = 0;
 int iMyPositions;
 int iiMyPositions;

double  UPindex; //存儲句柄
double  Downindex;  //存儲句柄

double Low_buf[];   //存儲每個柱形價格的動態數組
double High_buf[];  //存儲每個柱形價格的動態數組
double Close_buf[]; //存儲每個柱形價格的動態數組

double input 下單量 =0.01;

double hi5;
double low5;

ENUM_TIMEFRAMES my_timeframe; //存儲時間框架的變量

CTrade m_Trade; //執行交易的結構體
CPositionInfo m_Position; //獲取持倉信息的結構體
//+----------------------------------------------- -------------------+
//| EA初始化2
//+----------------------------------------------- -------------------+
int OnInit()
  {
  
   iMyPositions = 已有部位;
   iiMyPositions = 已有部位;
   my_timeframe= PERIOD_CURRENT; //保存圖表的當前時間框架,使EA對其的進一步操作。 

 
    ChartIndicatorAdd ( ChartID (), 0 ,hi5);   //將指標添加到價格圖表中
    ChartIndicatorAdd ( ChartID (), 0 ,low5); //將指標添加到價格圖表中
      
   ArraySetAsSeries (Close_buf, true ); //將 數組的索引設置為時間序列

   return(0); //返回0,初始化結束
  }
//+----------------------------------------------- -------------------+
//| EA去初始化函數|
//+----------------------------------------------- -------------------+
void OnDeinit(const int reason)
  {
   IndicatorRelease (UPindex);   //刪除指標句柄並釋放分配給它的存儲空間
   IndicatorRelease (Downindex); //刪除指標句柄並釋放分配給它的存儲空間
   ArrayFree(Close_buf); //釋放buf的數據
  }
//+----------------------------------------------- -------------------+
//| EA的tick函數|
//+----------------------------------------------- -------------------+
void OnTick()
  {
 
   if(CopyClose(Symbol(),my_timeframe,0,2,Close_buf)!=2) //將價格數據拷貝到動態數Close_buf中,以於進一步處理
     {
      Print("Failed to copy data from the indicator buffer or price chart buffer"); //打印相關錯誤信息到日誌文件
      return; //並退出函數 
     }    
     
    
    UPindex=iHighest(Symbol(),my_timeframe,MODE_HIGH,UpPeriod,2);
   double hi5=iHigh(Symbol(),my_timeframe,UPindex);

   Downindex=iLowest(Symbol(),my_timeframe,MODE_LOW,DownPeriod,2);
   double low5=iLow(Symbol(),my_timeframe,Downindex);


  iKcount = SeriesInfoInteger (Symbol(), Period(), SERIES_BARS_COUNT);
   
   if (Kcount == 0)   Kcount = iKcount;
   if (Kcount == iKcount)  CheckBar = 0 ;
   if (iKcount != Kcount ) CheckBar = 1 ;  



 if (CheckBar == 1 ) //////////////新Bar 超大if
 { 
   // SendNotification( 
   //          "hi5" + "\n    " + hi5 +   "\n " 
   //          "Close_buf[1]" + "\n    " + Close_buf[1] +   "\n " 
   //          "low5" + "\n    " + low5 +   "\n " 
   //  );

 if(Close_buf[1]>hi5) //收盤站上 5周期高点
 {        
     if (iMyPositions<0)
     {
      m_Trade.Buy( 下單量 ,Symbol());
      iMyPositions=0;
      }
      if (iMyPositions == 0 )
      {
       m_Trade.Buy( 下單量 ,Symbol());
       iMyPositions=1;
       }
 
       if( iMyPositions != iiMyPositions)
       SendNotification( 
         Symbol() + "\n" +  "站上 KF"  +"\n"   + "請務必評估自身風險承受度與資金控管,善設停損。"        // 通知文本  
   );
   
 }  
   
     
     

   if(Close_buf[1]<low5) //收盤跌破5周期低点
     {
     if (iMyPositions>0)
     {
      m_Trade.Sell( 下單量 ,Symbol());
      iMyPositions=0;
      }
      if (iMyPositions == 0 )
      {
       m_Trade.Sell( 下單量 ,Symbol());
       iMyPositions=-1;
       }
 
       if( iMyPositions != iiMyPositions)
       SendNotification( 
         Symbol() + "\n" +  "跌破 KF"  +"\n"   + "請務必評估自身風險承受度與資金控管,善設停損。"        // 通知文本  
   );
     }
     
  iiMyPositions = iMyPositions;
  Kcount = iKcount ;
      }//////////////新Bar 超大if
  }
//+----------------------------------------------- -------------------+
原因: