https://vocus.cc/article/6625ad41fd897800014e7aa4
紀錄閱讀MQL5論壇上EA交易工具相關的心得或翻譯
https://www.mql5.com/en/articles/230
CExpertMoney是資金風險管理機制的模組。 為了與外界通信,CExpertMoney需要定義下面這些方法
Initialization | Description |
---|---|
virtual Init | 負責初始化類別內容資料的邏輯 |
Percent | 設定風險百分比的值,值從 0.0-100.0。預設為100.0 |
virtual ValidationSettings | 驗證參數是否正確 |
virtual InitIndicators | 創建資金風險管理模組需要的指標 |
Methods for checking the necessity to open/turn/close a position | |
virtual CheckOpenLong | 決定開多頭倉位的大小 |
virtual CheckOpenShort | 決定開空頭倉位的大小 |
virtual CheckReverse | 決定開反轉倉位的大小 |
virtual CheckClose | 確認是否平倉的邏輯 |
內容基本上從上述表格相差不大,此處略過,直接從下一段深入了解
現在在了解需要定義的基本函數後,就可以開始建立自己的模組
建立資金風險管理模組類別
跟前文一樣新增一個mqh檔
注意為了要讓MQL5 Wizard在Meta Editor開啟時偵測到資金風險管理模組,應該把檔案放在 Include\Expert\Money\ 中,為了方便管理,我們多建立一層資料夾Include\Expert\Money\MyMoneys,並新增SampleMoney.mqh
完成後得到如下檔案
移除不必要的程式碼並加入算法註解,在本文中的邏輯是:一般情況使用固定的手數大小,如果之前有虧損則改為雙倍大小(筆者OS:馬丁!?壞壞…)
//+------------------------------------------------------------------+
//| SampleMoney.mqh |
//| Copyright 2024, MetaQuotes Ltd. |
//| <https://www.mql5.com> |
//+------------------------------------------------------------------+
#property copyright "Copyright 2024, MetaQuotes Ltd."
#property link "<https://www.mql5.com>"
//+------------------------------------------------------------------+
//| Include files |
//+------------------------------------------------------------------+
#include <Expert\\ExpertMoney.mqh>
//+------------------------------------------------------------------+
//| Class CSampleMoney. |
//| Purpose: Class for risk and money management |
//| doubling the volume after a loss deal. |
//| It is derived from the CExpertMoney class. |
//+------------------------------------------------------------------+
class CSampleMoney : public CExpertMoney
{
};
//+------------------------------------------------------------------+
接下來定義參數清單,目前只有一個就是正常情況下的開倉手數大小,型態設置成protected,使用public函數來修改,在資金風險管理模組類別被創建的時候會先設定成預設值
//+------------------------------------------------------------------+
//| SampleMoney.mqh |
//| Copyright 2024, MetaQuotes Ltd. |
//| <https://www.mql5.com> |
//+------------------------------------------------------------------+
#property copyright "Copyright 2024, MetaQuotes Ltd."
#property link "<https://www.mql5.com>"
//+------------------------------------------------------------------+
//| Include files |
//+------------------------------------------------------------------+
#include <Expert\\ExpertMoney.mqh>
//+------------------------------------------------------------------+
//| Class CSampleMoney. |
//| Purpose: Class for risk and money management |
//| doubling the volume after a loss deal. |
//| It is derived from the CExpertMoney class. |
//+------------------------------------------------------------------+
class CSampleMoney : public CExpertMoney
{
protected:
//--- Setup parameters
double m_lots; // Deal volume for "normal" conditions
public:
CSampleMoney();
//--- Methods to set the parameters
void Lots(double lots) { m_lots=lots; }
};
//+------------------------------------------------------------------+
//| Constructor CSampleMoney. |
//| INPUT: no. |
//| OUTPUT: no. |
//| REMARK: no. |
//+------------------------------------------------------------------+
void CSampleMoney::CSampleMoney()
{
//--- Setting default values
m_lots=0.1;
}
接下來要檢查參數,根據要重寫ValidationSettings函數
//(以上省略)
class CSampleMoney : public CExpertMoney
{
protected:
//--- Setup parameters
double m_lots; // Deal volume for "normal" conditions
public:
CSampleMoney();
//--- Methods to set the parameters
void Lots(double lots) { m_lots=lots; }
//--- Methods to validate the parameters
virtual bool ValidationSettings();
};
//+------------------------------------------------------------------+
//| Constructor CSampleMoney. |
//| INPUT: no. |
//| OUTPUT: no. |
//| REMARK: no. |
//+------------------------------------------------------------------+
void CSampleMoney::CSampleMoney()
{
//--- Setting default values
m_lots=0.1;
}
//+------------------------------------------------------------------+
//| Validation of the setup parameters. |
//| INPUT: no. |
//| OUTPUT: true if the settings are correct, otherwise false. |
//| REMARK: no. |
//+------------------------------------------------------------------+
bool CSampleMoney::ValidationSettings()
{
//--- Call the base class method
if(!CExpertMoney::ValidationSettings()) return(false);
//--- Validating the parameters
if(m_lots<m_symbol.LotsMin() || m_lots>m_symbol.LotsMax())
{
printf(__FUNCTION__+": The deal volume must be in the range %f to %f",m_symbol.LotsMin(),m_symbol.LotsMax());
return(false);
}
if(MathAbs(m_lots/m_symbol.LotsStep()-MathRound(m_lots/m_symbol.LotsStep()))>1.0E-10)
{
printf(__FUNCTION__+": The deal volume must be multiple of %f",m_symbol.LotsStep());
return(false);
}
//--- Successful completion
return(true);
}