MQL5 Wizard(1): 不需手寫程式碼建立EA

2024/04/21閱讀時間約 41 分鐘
  • 文內如有投資理財相關經驗、知識、資訊等內容,皆為創作者個人分享行為。
  • 有價證券、指數與衍生性商品之數據資料,僅供輔助說明之用,不代表創作者投資決策之推介及建議。
  • 閱讀同時,請審慎思考自身條件及自我決策,並應有為決策負責之事前認知。
  • 方格子希望您能從這些分享內容汲取投資養份,養成獨立思考的能力、判斷、行動,成就最適合您的投資理財模式。

引言

紀錄閱讀MQL5論壇上EA交易工具相關的心得或翻譯

由於後面有一系列文章是使用mql5 wizard這個功能來寫EA

因此先有一個前導系列來研究mql5 wizard這個功能

至於mql5基礎語言由於筆者本身有基礎背景

因此就先不介紹了,後續如果有需要再考慮


文章出處

MQL5 Wizard: Creating Expert Advisors without Programming


介紹

當在建立自動交易系統時

必須要編寫有關"交易訊號""追蹤平倉""資金風險管理"等邏輯

當各個模組編寫完成,最困難的任務就是組裝各個模組並調適算法

而在這個基礎之上還需要有一個讓各個模組互動正常的架構

若架構規劃的不好,則還需要花時間排查架構錯誤,替換模組時還有可能要改寫架構

在MQL5中,使用了物件導向的方法來簡化自動交易系統的編寫和測試流程,

尤其MetaQuotes Software Corp.還內建了基本的交易訊號(20個)、追蹤平倉(4)和資金風險管理模組(5)

透過組合這些模組就可以立即得到一個可用的EA

接下來會介紹如何自動產生一套EA且不需要自己手寫程式碼


使用MQL5 Wizard建立EA

接下來的步驟將在MetaEditor中完成

其中內建的交易模組位於"\<client_terminal_directory>\MQL5\Include\Expert\"路徑的資料夾內

當在開啟MetaEditor後,MQL5 Wizard會到這個路徑解析這些模組並新增到選單中

之後在新增EA時就可以透過選單來選擇要組合哪些模組


  • 接下來到工具>MetaQuotes語言編輯器,或按(F4)來開啟MetaEditor,
  • 接著一樣左上角按新增EA的按鈕(或ctrl+n),並選擇EA交易(生成)
raw-image
  • 輸入名稱等其他資訊
raw-image
  • 增加選擇交易訊號
    • 交易訊號會決定什麼條件下開倉,平倉,反轉部位,圖中是內建的交易訊號
raw-image
    • 之後可以設定交易訊號的相關參數
      參數具有兩種模式,圖中灰色底的(如shift,applied),代表生成EA之後無法在調適EA的階段再次修改參數值,用於加速EA調適的速度,反之則為白色底,可以透過滑鼠左鍵雙擊切換
raw-image
    • 加入之後如下圖
raw-image
  • 增加追蹤平倉模組
raw-image
  • 增加資金風險管理模組
raw-image
  • 之後按下完成就會生成EA
raw-image



解析EA程式碼與架構

  • 生成的程式碼如下
    //+------------------------------------------------------------------+
    //| Test001.mq5 |
    //| Copyright 2024, MetaQuotes Ltd. |
    //| <https://www.mql5.com> |
    //+------------------------------------------------------------------+
    #property copyright "Copyright 2024, MetaQuotes Ltd."
    #property link "<https://www.mql5.com>"
    #property version "1.00"
    //+------------------------------------------------------------------+
    //| Include |
    //+------------------------------------------------------------------+
    #include <Expert\\Expert.mqh>
    //--- available signals
    #include <Expert\\Signal\\SignalMA.mqh>
    //--- available trailing
    #include <Expert\\Trailing\\TrailingFixedPips.mqh>
    //--- available money management
    #include <Expert\\Money\\MoneyFixedLot.mqh>
    //+------------------------------------------------------------------+
    //| Inputs |
    //+------------------------------------------------------------------+
    //--- inputs for expert
    input string Expert_Title ="Test001"; // Document name
    ulong Expert_MagicNumber =13666; //
    bool Expert_EveryTick =false; //
    //--- inputs for main signal
    input int Signal_ThresholdOpen =10; // Signal threshold value to open [0...100]
    input int Signal_ThresholdClose =10; // Signal threshold value to close [0...100]
    input double Signal_PriceLevel =0.0; // Price level to execute a deal
    input double Signal_StopLevel =50.0; // Stop Loss level (in points)
    input double Signal_TakeLevel =50.0; // Take Profit level (in points)
    input int Signal_Expiration =4; // Expiration of pending orders (in bars)
    input int Signal_MA_PeriodMA =12; // Moving Average(12,0,...) Period of averaging
    input int Signal_MA_Shift =0; // Moving Average(12,0,...) Time shift
    input ENUM_MA_METHOD Signal_MA_Method =MODE_SMA; // Moving Average(12,0,...) Method of averaging
    input ENUM_APPLIED_PRICE Signal_MA_Applied =PRICE_CLOSE; // Moving Average(12,0,...) Prices series
    input double Signal_MA_Weight =1.0; // Moving Average(12,0,...) Weight [0...1.0]
    //--- inputs for trailing
    input int Trailing_FixedPips_StopLevel =30; // Stop Loss trailing level (in points)
    input int Trailing_FixedPips_ProfitLevel=50; // Take Profit trailing level (in points)
    //--- inputs for money
    input double Money_FixLot_Percent =10.0; // Percent
    input double Money_FixLot_Lots =0.1; // Fixed volume
    //+------------------------------------------------------------------+
    //| Global expert object |
    //+------------------------------------------------------------------+
    CExpert ExtExpert;
    //+------------------------------------------------------------------+
    //| Initialization function of the expert |
    //+------------------------------------------------------------------+
    int OnInit()
    {
    //--- Initializing expert
    if(!ExtExpert.Init(Symbol(),Period(),Expert_EveryTick,Expert_MagicNumber))
    {
    //--- failed
    printf(__FUNCTION__+": error initializing expert");
    ExtExpert.Deinit();
    return(INIT_FAILED);
    }
    //--- Creating signal
    CExpertSignal *signal=new CExpertSignal;
    if(signal==NULL)
    {
    //--- failed
    printf(__FUNCTION__+": error creating signal");
    ExtExpert.Deinit();
    return(INIT_FAILED);
    }
    //---
    ExtExpert.InitSignal(signal);
    signal.ThresholdOpen(Signal_ThresholdOpen);
    signal.ThresholdClose(Signal_ThresholdClose);
    signal.PriceLevel(Signal_PriceLevel);
    signal.StopLevel(Signal_StopLevel);
    signal.TakeLevel(Signal_TakeLevel);
    signal.Expiration(Signal_Expiration);
    //--- Creating filter CSignalMA
    CSignalMA *filter0=new CSignalMA;
    if(filter0==NULL)
    {
    //--- failed
    printf(__FUNCTION__+": error creating filter0");
    ExtExpert.Deinit();
    return(INIT_FAILED);
    }
    signal.AddFilter(filter0);
    //--- Set filter parameters
    filter0.PeriodMA(Signal_MA_PeriodMA);
    filter0.Shift(Signal_MA_Shift);
    filter0.Method(Signal_MA_Method);
    filter0.Applied(Signal_MA_Applied);
    filter0.Weight(Signal_MA_Weight);
    //--- Creation of trailing object
    CTrailingFixedPips *trailing=new CTrailingFixedPips;
    if(trailing==NULL)
    {
    //--- failed
    printf(__FUNCTION__+": error creating trailing");
    ExtExpert.Deinit();
    return(INIT_FAILED);
    }
    //--- Add trailing to expert (will be deleted automatically))
    if(!ExtExpert.InitTrailing(trailing))
    {
    //--- failed
    printf(__FUNCTION__+": error initializing trailing");
    ExtExpert.Deinit();
    return(INIT_FAILED);
    }
    //--- Set trailing parameters
    trailing.StopLevel(Trailing_FixedPips_StopLevel);
    trailing.ProfitLevel(Trailing_FixedPips_ProfitLevel);
    //--- Creation of money object
    CMoneyFixedLot *money=new CMoneyFixedLot;
    if(money==NULL)
    {
    //--- failed
    printf(__FUNCTION__+": error creating money");
    ExtExpert.Deinit();
    return(INIT_FAILED);
    }
    //--- Add money to expert (will be deleted automatically))
    if(!ExtExpert.InitMoney(money))
    {
    //--- failed
    printf(__FUNCTION__+": error initializing money");
    ExtExpert.Deinit();
    return(INIT_FAILED);
    }
    //--- Set money parameters
    money.Percent(Money_FixLot_Percent);
    money.Lots(Money_FixLot_Lots);
    //--- Check all trading objects parameters
    if(!ExtExpert.ValidationSettings())
    {
    //--- failed
    ExtExpert.Deinit();
    return(INIT_FAILED);
    }
    //--- Tuning of all necessary indicators
    if(!ExtExpert.InitIndicators())
    {
    //--- failed
    printf(__FUNCTION__+": error initializing indicators");
    ExtExpert.Deinit();
    return(INIT_FAILED);
    }
    //--- ok
    return(INIT_SUCCEEDED);
    }
    //+------------------------------------------------------------------+
    //| Deinitialization function of the expert |
    //+------------------------------------------------------------------+
    void OnDeinit(const int reason)
    {
    ExtExpert.Deinit();
    }
    //+------------------------------------------------------------------+
    //| "Tick" event handler function |
    //+------------------------------------------------------------------+
    void OnTick()
    {
    ExtExpert.OnTick();
    }
    //+------------------------------------------------------------------+
    //| "Trade" event handler function |
    //+------------------------------------------------------------------+
    void OnTrade()
    {
    ExtExpert.OnTrade();
    }
    //+------------------------------------------------------------------+
    //| "Timer" event handler function |
    //+------------------------------------------------------------------+
    void OnTimer()
    {
    ExtExpert.OnTimer();
    }
    //+------------------------------------------------------------------+

  • 程式碼的基本資料描述
    //+------------------------------------------------------------------+
    //| Test001.mq5 |
    //| Copyright 2024, MetaQuotes Ltd. |
    //| <https://www.mql5.com> |
    //+------------------------------------------------------------------+
    #property copyright "Copyright 2024, MetaQuotes Ltd."
    #property link "<https://www.mql5.com>"
    #property version "1.00"
  • 引入的參考檔案
    //+------------------------------------------------------------------+
    //| Include |
    //+------------------------------------------------------------------+
    #include <Expert\\Expert.mqh>
    //--- available signals
    #include <Expert\\Signal\\SignalMA.mqh>
    //--- available trailing
    #include <Expert\\Trailing\\TrailingFixedPips.mqh>
    //--- available money management
    #include <Expert\\Money\\MoneyFixedLot.mqh>
    • Expert.mqh:用來組合各個模組
    • 其餘三個檔案是剛剛選擇的各個模組
  • 接來來是之後可以被調適的EA參數
    //+------------------------------------------------------------------+
    //| Inputs |
    //+------------------------------------------------------------------+
    //--- inputs for expert
    input string Expert_Title ="Test001"; // Document name
    ulong Expert_MagicNumber =13666; //
    bool Expert_EveryTick =false; //
    //--- inputs for main signal
    input int Signal_ThresholdOpen =10; // Signal threshold value to open [0...100]
    input int Signal_ThresholdClose =10; // Signal threshold value to close [0...100]
    input double Signal_PriceLevel =0.0; // Price level to execute a deal
    input double Signal_StopLevel =50.0; // Stop Loss level (in points)
    input double Signal_TakeLevel =50.0; // Take Profit level (in points)
    input int Signal_Expiration =4; // Expiration of pending orders (in bars)
    input int Signal_MA_PeriodMA =12; // Moving Average(12,0,...) Period of averaging
    input int Signal_MA_Shift =0; // Moving Average(12,0,...) Time shift
    input ENUM_MA_METHOD Signal_MA_Method =MODE_SMA; // Moving Average(12,0,...) Method of averaging
    input ENUM_APPLIED_PRICE Signal_MA_Applied =PRICE_CLOSE; // Moving Average(12,0,...) Prices series
    input double Signal_MA_Weight =1.0; // Moving Average(12,0,...) Weight [0...1.0]
    //--- inputs for trailing
    input int Trailing_FixedPips_StopLevel =30; // Stop Loss trailing level (in points)
    input int Trailing_FixedPips_ProfitLevel=50; // Take Profit trailing level (in points)
    //--- inputs for money
    input double Money_FixLot_Percent =10.0; // Percent
    input double Money_FixLot_Lots =0.1; // Fixed volume
    • 起頭的三個是基本參數(Expert_Title/Expert_MagicNumber/Expert_EveryTick)
      Expert_Title:EA名稱
      Expert_MagicNumber:用於管理EA的交易訂單
      Expert_EveryTick:是否在每次價格tick跳動時都執行EA邏輯
    • 後面參數則會跟選擇的模組不同而變動
  • 宣告組合模組的EA變數
    //+------------------------------------------------------------------+
    //| Global expert object |
    //+------------------------------------------------------------------+
    CExpert ExtExpert;
  • 設定組合各模組到EA變數內
    //+------------------------------------------------------------------+
    //| Initialization function of the expert |
    //+------------------------------------------------------------------+
    int OnInit()
    {
    ...
    • 在OnInit()中進行初始化,此函數只會在EA開啟時執行一次
  • 接下來設定ExtExpert的初始參數
    //--- Initializing expert
    if(!ExtExpert.Init(Symbol(),Period(),Expert_EveryTick,Expert_MagicNumber))
    {
    //--- failed
    printf(__FUNCTION__+": error initializing expert");
    ExtExpert.Deinit();
    return(INIT_FAILED);
    }
    • 透過ExtExpert.Init(Symbol(),Period(),Expert_EveryTick,Expert_MagicNumber)
      把基本參數配置到ExtExpert, 如果初始化失敗則會回傳-1,然後進入if判斷執行結束EA相關程序
  • 設定交易訊號模組的參數並配置到ExtExpert
    //--- Creating signal
    CExpertSignal *signal=new CExpertSignal;
    if(signal==NULL)
    {
    //--- failed
    printf(__FUNCTION__+": error creating signal");
    ExtExpert.Deinit();
    return(INIT_FAILED);
    }
    //---
    ExtExpert.InitSignal(signal);
    signal.ThresholdOpen(Signal_ThresholdOpen);
    signal.ThresholdClose(Signal_ThresholdClose);
    signal.PriceLevel(Signal_PriceLevel);
    signal.StopLevel(Signal_StopLevel);
    signal.TakeLevel(Signal_TakeLevel);
    signal.Expiration(Signal_Expiration);
    //--- Creating filter CSignalMA
    CSignalMA *filter0=new CSignalMA;
    if(filter0==NULL)
    {
    //--- failed
    printf(__FUNCTION__+": error creating filter0");
    ExtExpert.Deinit();
    return(INIT_FAILED);
    }
    signal.AddFilter(filter0);
    //--- Set filter parameters
    filter0.PeriodMA(Signal_MA_PeriodMA);
    filter0.Shift(Signal_MA_Shift);
    filter0.Method(Signal_MA_Method);
    filter0.Applied(Signal_MA_Applied);
    filter0.Weight(Signal_MA_Weight);
    • 交易訊號分為兩個步驟
    • 建立訊號變數signal,配置到ExtExpert
    • 把各個交易模組當成一層的filter(過濾器),
      透過加入到signal.AddFilter(filter0),
      只有當價格行為通過層層過濾器後,才能發出交易訊號
  • 設定追蹤平倉變數
    //--- Creation of trailing object
    CTrailingFixedPips *trailing=new CTrailingFixedPips;
    if(trailing==NULL)
    {
    //--- failed
    printf(__FUNCTION__+": error creating trailing");
    ExtExpert.Deinit();
    return(INIT_FAILED);
    }
    //--- Add trailing to expert (will be deleted automatically))
    if(!ExtExpert.InitTrailing(trailing))
    {
    //--- failed
    printf(__FUNCTION__+": error initializing trailing");
    ExtExpert.Deinit();
    return(INIT_FAILED);
    }
    //--- Set trailing parameters
    trailing.StopLevel(Trailing_FixedPips_StopLevel);
    trailing.ProfitLevel(Trailing_FixedPips_ProfitLevel);
    • 建立變數trailing,配置到ExtExpert
  • 設定資金風險管理變數
    //--- Creation of money object
    CMoneyFixedLot *money=new CMoneyFixedLot;
    if(money==NULL)
    {
    //--- failed
    printf(__FUNCTION__+": error creating money");
    ExtExpert.Deinit();
    return(INIT_FAILED);
    }
    //--- Add money to expert (will be deleted automatically))
    if(!ExtExpert.InitMoney(money))
    {
    //--- failed
    printf(__FUNCTION__+": error initializing money");
    ExtExpert.Deinit();
    return(INIT_FAILED);
    }
    //--- Set money parameters
    money.Percent(Money_FixLot_Percent);
    money.Lots(Money_FixLot_Lots);
    • 建立變數money,配置到ExtExpert
  • 設定好ExtExpert之後驗證所有參數設置,並把剛剛配置的變數都跑一次初始化
    //--- Check all trading objects parameters
    if(!ExtExpert.ValidationSettings())
    {
    //--- failed
    ExtExpert.Deinit();
    return(INIT_FAILED);
    }
    //--- Tuning of all necessary indicators
    if(!ExtExpert.InitIndicators())
    {
    //--- failed
    printf(__FUNCTION__+": error initializing indicators");
    ExtExpert.Deinit();
    return(INIT_FAILED);
    }
  • 接下來就同一般的EA一樣,執行MQL5基本參數,然後再透過ExtExpert觸發各模組的程式邏輯
    //+------------------------------------------------------------------+
    //| Deinitialization function of the expert |
    //+------------------------------------------------------------------+
    void OnDeinit(const int reason)
    {
    ExtExpert.Deinit();
    }
    //+------------------------------------------------------------------+
    //| "Tick" event handler function |
    //+------------------------------------------------------------------+
    void OnTick()
    {
    ExtExpert.OnTick();
    }
    //+------------------------------------------------------------------+
    //| "Trade" event handler function |
    //+------------------------------------------------------------------+
    void OnTrade()
    {
    ExtExpert.OnTrade();
    }
    //+------------------------------------------------------------------+
    //| "Timer" event handler function |
    //+------------------------------------------------------------------+
    void OnTimer()
    {
    ExtExpert.OnTimer();
    }
  • 編譯EA確定沒有錯誤和警告
raw-image


結語

透過MQL5 Wizard可以簡化交易想法的建立和測試

也可以自己建立各個模組

並且可以重複使用,組合不同的模組來建立自己獨有的交易程式

且開發組來的

  • 補充說明:內文提供的Moving Average EA只是範例,並無實際盈利能力,具體邏輯還需使用者自行思考設計
1會員
5內容數
RRRRRRRR
留言0
查看全部
發表第一個留言支持創作者!
從 Google News 追蹤更多 vocus 的最新精選內容