https://vocus.cc/article/6625b793fd897800014f1b3e
紀錄閱讀MQL5論壇上EA交易工具相關的心得或翻譯
https://www.mql5.com/en/articles/231
Initialization | Description |
---|---|
virtual Init | 初始化類別內容資料的邏輯 |
virtual ValidationSettings | 驗證參數是否正確 |
virtual InitIndicators | 創建模組所需的指標 |
Signals of Modification of Positions | |
virtual CheckTrailingStopLong | 產生修改多頭部位的訊號,並確定止損訂單的新價格 |
virtual CheckTrailingStopShort | 產生修改空頭部位的訊號,並確定止損訂單的新價格 |
建立類別
新增mqh檔
注意為了要讓MQL5 Wizard在Meta Editor開啟時偵測到追蹤開倉模組,應該把檔案放在 Include\Expert\Trailing\ 中,為了方便管理,我們多建立一層資料夾Include\Expert\Trailing\MyTrailings,並新增SampleTrailing.mqh
完成後得到如下檔案
移除不必要的程式碼並加入算法註解
//+------------------------------------------------------------------+
//| SampleTrailing.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\\ExpertTrailing.mqh>
//+------------------------------------------------------------------+
//| Class CSampleTrailing. |
//| Purpose: Class for trailing of open positions |
//| by moving Stop order to a lossless level. |
//| Is derived from the CExpertTrailing class. |
//+------------------------------------------------------------------+
class CSampleTrailing : public CExpertTrailing
{
};
接下來定義需要的參數
class CSampleTrailing : public CExpertTrailing
{
protected:
int m_profit; // threshold level of profit
int m_stop_level; // lossless level
public:
CSampleTrailing();
//--- method of setting adjustable parameters
void Profit(int value) { m_profit=value; }
void StopLevel(int value) { m_stop_level=value; }
};
//+------------------------------------------------------------------+
//| Constructor CSampleTrailing. |
//| INPUT: no. |
//| OUTPUT: no. |
//| REMARK: no. |
//+------------------------------------------------------------------+
void CSampleTrailing::CSampleTrailing()
{
//--- setting default values
m_profit =20;
m_stop_level=0;
}
ValidationSettings宣告
class CSampleTrailing : public CExpertTrailing
{
protected:
int m_profit; // threshold level of profit
int m_stop_level; // lossless level
public:
CSampleTrailing();
//--- method of setting adjustable parameters
void Profit(int value) { m_profit=value; }
void StopLevel(int value) { m_stop_level=value; }
//--- method of validation of adjustable settings
virtual bool ValidationSettings();
};
驗證參數內容
//+------------------------------------------------------------------+
//| Check of adjustable parameters. |
//| INPUT: no. |
//| OUTPUT: true if the parameters are correct, false if not. |
//| REMARK: no. |
//+------------------------------------------------------------------+
bool CSampleTrailing::ValidationSettings()
{
//--- what if the Init has not been called?
if(m_symbol==NULL) return(false);
//--- check of parameters
if((m_profit-m_stop_level)*m_adjusted_point<=m_symbol.StopsLevel()*m_symbol.Point() && m_profit!=0.0)
{
printf(__FUNCTION__+": threshold level of profit must be greater than the level of setting stop orders");
return(false);
}
//--- ok
return(true);
}
準備工作完成,接下來是演算法的內容,需要改寫CheckTrailingStopLong和CheckTrailingStopShort這兩個方法
class CSampleTrailing : public CExpertTrailing
{
protected:
int m_profit; // threshold level of profit
int m_stop_level; // lossless level
public:
CSampleTrailing();
//--- method of setting adjustable parameters
void Profit(int value) { m_profit=value; }
void StopLevel(int value) { m_stop_level=value; }
//--- method of validation of adjustable settings
virtual bool ValidationSettings();
//--- methods of generation of position modification signals
virtual bool CheckTrailingStopLong(CPositionInfo* position,double& sl,double& tp);
virtual bool CheckTrailingStopShort(CPositionInfo* position,double& sl,double& tp);
};
實作CheckTrailingStopLong 和 CheckTrailingStopShort
//+------------------------------------------------------------------+
//| Check for modification of stop orders of a long position. |
//| INPUT: position - pointer to a position object, |
//| sl - link for a new price of stop loss order, |
//| tp - link for a new price of take profit order. |
//| OUTPUT: true if condition is satisfied, false if not. |
//| REMARK: no. |
//+------------------------------------------------------------------+
bool CSampleTrailing::CheckTrailingStopLong(CPositionInfo* position,double& sl,double& tp)
{
//--- check of pointer
if(position==NULL) return(false);
//--- check of parameters
if(m_profit==0.0) return(false);
//--- already in a lossless zone?
double open=position.PriceOpen();
if(position.StopLoss()>=open) return(false);
//--- check of profit
sl=EMPTY_VALUE;
tp=EMPTY_VALUE;
if(m_symbol.Bid()-open>m_profit*m_adjusted_point)
sl=m_symbol.NormalizePrice(open+m_stop_level*m_adjusted_point);
//---
return(sl!=EMPTY_VALUE);
}
//+------------------------------------------------------------------+
//| Check for modification of stop orders of a short position. |
//| INPUT: position - pointer to a position object, |
//| sl - link for a new price of stop loss order, |
//| tp - link for a new take profit order. |
//| OUTPUT: true if condition is satisfied, false if not. |
//| REMARK: no. |
//+------------------------------------------------------------------+
bool CSampleTrailing::CheckTrailingStopShort(CPositionInfo* position,double& sl,double& tp)
{
//--- check of pointer
if(position==NULL) return(false);
//--- check of parameters
if(m_profit==0.0) return(false);
//--- already in a lossless zone?
double open=position.PriceOpen();
if(position.StopLoss()<=open) return(false);
//--- check of profit
sl=EMPTY_VALUE;
tp=EMPTY_VALUE;
if(open-m_symbol.Ask()>m_profit*m_adjusted_point)
sl=m_symbol.NormalizePrice(open-m_stop_level*m_adjusted_point);
//---
return(sl!=EMPTY_VALUE);
}