量化軟件下載:在赫茲量化中標(biāo)準(zhǔn)庫和重用代碼
正如我在本文簡介部分中所述,我們在構(gòu)建用于包裝之前下載的峰谷指標(biāo)的新的類組時受到 的面向?qū)ο箫L(fēng)格的啟發(fā)。這很簡單,我們只需看一看 Include\Indicators 中的文件,學(xué)習(xí)和理解 MQL5 標(biāo)準(zhǔn)庫背后的某些理念。當(dāng)您查看 文件中的內(nèi)容時,您很快會發(fā)現(xiàn)里面全是代表某些技術(shù)指標(biāo)的類:ADX、布林帶、SAR、移動平均線等。所有這些類都繼承自 CIndicatr。那么,讓我們來實現(xiàn)這個方案。順便一提,從 MQL5 的類 CiCustom 擴(kuò)展新的面向?qū)ο笾笜?biāo)是實現(xiàn)此練習(xí)的另一個備選方案。
我將在下文中討論一些編碼所需的技術(shù)。
//+------------------------------------------------------------------+ //| ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?Include\Indicators\Custom\Trend.mqh | //| ? ? ? ? ? ? ? ? ?Copyright 2013, Laplacianlab - Jordi Bassaga?as | //| ? ? ? ? ? ? ? ? ? ? https://www.mql5.com/en/users/laplacianlab | //+------------------------------------------------------------------+ #include <..\Include\Indicators\Indicator.mqh> //+------------------------------------------------------------------+ //| Class CiZigZag. ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?| //| Purpose: Class of the "ZigZag" indicator. ? ? ? ? ? ? ? ? ? ? ? ?| //| ? ? ? ? ?Derives from class CIndicator. ? ? ? ? ? ? ? ? ? ? ? ? ?| //+------------------------------------------------------------------+ class CiZigZag : public CIndicator ?{ protected: ? int ? ? ? ? ? ? ? m_depth; ? int ? ? ? ? ? ? ? m_deviation; ? int ? ? ? ? ? ? ? m_backstep; public: ? ? ? ? ? ? ? ? ? ? CiZigZag(void); ? ? ? ? ? ? ? ? ? ?~CiZigZag(void); ? //--- methods of access to protected data ? int ? ? ? ? ? ? ? Depth(void) ? ? ? ? ?const { return(m_depth); ? ? ?} ? int ? ? ? ? ? ? ? Deviation(void) ? ? ?const { return(m_deviation); ?} ? int ? ? ? ? ? ? ? Backstep(void) ? ? ? const { return(m_backstep); ? } ? //--- method of creation ? bool ? ? ? ? ? ? ?Create(const string symbol,const ENUM_TIMEFRAMES period, ? ? ? ? ? ? ? ? ? ? ? ? ? ?const int depth,const int deviation_create,const int backstep); ? //--- methods of access to indicator data ? double ? ? ? ? ? ?ZigZag(const int index) const; ? double ? ? ? ? ? ?High(const int index) const; ? double ? ? ? ? ? ?Low(const int index) const; ? //--- method of identifying ? virtual int ? ? ? Type(void) const { return(IND_CUSTOM); } protected: ? //--- methods of tuning ? virtual bool ? ? ?Initialize(const string symbol,const ENUM_TIMEFRAMES period,const int num_params,const MqlParam ¶ms[]); ? bool ? ? ? ? ? ? ?Initialize(const string symbol,const ENUM_TIMEFRAMES period, ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?const int depth,const int deviation_init,const int backstep); ?}; //+------------------------------------------------------------------+ //| Constructor ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?| //+------------------------------------------------------------------+ CiZigZag::CiZigZag(void) : m_depth(-1), ? ? ? ? ? ? ? ? ? ? ? ? m_deviation(-1), ? ? ? ? ? ? ? ? ? ? ? ? m_backstep(-1) ?{ ?} //+------------------------------------------------------------------+ //| Destructor ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? | //+------------------------------------------------------------------+ CiZigZag::~CiZigZag(void) ?{ ?} //+------------------------------------------------------------------+ //| Create indicator "Zig Zag" ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? | //+------------------------------------------------------------------+ bool CiZigZag::Create(const string symbol,const ENUM_TIMEFRAMES period, ? ? ? ? ? ? ? ? ? ? ?const int depth,const int deviation_create,const int backstep) ?{ //--- check history ? if(!SetSymbolPeriod(symbol,period)) ? ? ?return(false); //--- create ? m_handle=iCustom(symbol,period,"zigzag",depth,deviation_create,backstep); //--- check result ? if(m_handle==INVALID_HANDLE) ? ? ?return(false); //--- indicator successfully created ? if(!Initialize(symbol,period,depth,deviation_create,backstep)) ? ? { ? ? ?//--- initialization failed ? ? ?IndicatorRelease(m_handle); ? ? ?m_handle=INVALID_HANDLE; ? ? ?return(false); ? ? } //--- ok ? return(true); ?} //+------------------------------------------------------------------+ //| Initialize the indicator with universal parameters ? ? ? ? ? ? ? | //+------------------------------------------------------------------+ bool CiZigZag::Initialize(const string symbol,const ENUM_TIMEFRAMES period,const int num_params,const MqlParam ¶ms[]) ?{ ? return(Initialize(symbol,period,(int)params[0].integer_value,(int)params[1].integer_value,(int)params[2].integer_value)); ?} //+------------------------------------------------------------------+ //| Initialize indicator with the special parameters ? ? ? ? ? ? ? ? | //+------------------------------------------------------------------+ bool CiZigZag::Initialize(const string symbol,const ENUM_TIMEFRAMES period, ? ? ? ? ? ? ? ? ? ? ? ?const int depth,const int deviation_init,const int backstep) ?{ ? if(CreateBuffers(symbol,period,3)) ? ? { ? ? ?//--- string of status of drawing ? ? ?m_name ?="ZigZag"; ? ? ?m_status="("+symbol+","+PeriodDescription()+","+ ? ? ? ? ? ? ? IntegerToString(depth)+","+IntegerToString(deviation_init)+","+ ? ? ? ? ? ? ? IntegerToString(backstep)+") H="+IntegerToString(m_handle); ? ? ?//--- save settings ? ? ?m_depth=depth; ? ? ?m_deviation=deviation_init; ? ? ?m_backstep=backstep; ? ? ? ? ? ?//--- create buffers ? ? ?((CIndicatorBuffer*)At(0)).Name("ZIGZAG"); ? ? ?((CIndicatorBuffer*)At(1)).Name("HIGH"); ? ? ?((CIndicatorBuffer*)At(2)).Name("LOW"); ? ? ?//--- ok ? ? ?return(true); ? ? } //--- error ? return(false); ?} //+------------------------------------------------------------------+ //| Access to ZigZag buffer of "Zig Zag" ? ? ? ? ? ? ? ? ? ? ? ? ? ? | //+------------------------------------------------------------------+ double CiZigZag::ZigZag(const int index) const ?{ ? CIndicatorBuffer *buffer=At(0); //--- check ? if(buffer==NULL) ? ? ?return(EMPTY_VALUE); //--- ? return(buffer.At(index)); ?} //+------------------------------------------------------------------+ //| Access to High buffer of "Zig Zag" ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? | //+------------------------------------------------------------------+ double CiZigZag::High(const int index) const ?{ ? CIndicatorBuffer *buffer=At(1); //--- check ? if(buffer==NULL) ? ? ?return(EMPTY_VALUE); //--- ? return(buffer.At(index)); ?} //+------------------------------------------------------------------+ //| Access to Low buffer of "Zig Zag" ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?| //+------------------------------------------------------------------+ double CiZigZag::Low(const int index) const ?{ ? CIndicatorBuffer *buffer=At(2); //--- check ? if(buffer==NULL) ? ? ?return(EMPTY_VALUE); //--- ? return(buffer.At(index)); ?} //+------------------------------------------------------------------+
3.1. 面向?qū)ο蠓庋b
面向?qū)ο蠓庋b是一個良好的編程實踐,意味著對象的數(shù)據(jù)成員只能由為其定義的操作所修改。所有在 MetaQuotes 的 中定義的類均實施此理念,所以我們也在做著同樣的事情。
一方面是 CiZigZag 的特定受保護(hù)屬性:
protected: ? int ? ? ? ? ? ? ? m_depth; ? int ? ? ? ? ? ? ? m_deviation; ? int ? ? ? ? ? ? ? m_backstep;
其后是 CiZigZag 的公共接口,用于從 CiZigZag 類型的給定對象的外部訪問上文中定義的受保護(hù)屬性:
public: ? //--- methods of access to protected data ? int ? ? ? ? ? ? ? Depth(void) ? ? ? ? ?const { return(m_depth); ? ? ?} ? int ? ? ? ? ? ? ? Deviation(void) ? ? ?const { return(m_deviation); ?} ? int ? ? ? ? ? ? ? Backstep(void) ? ? ? const { return(m_backstep); ? }
這是用于隔離對象的一個安全措施。此封裝防止不允許訪問對象數(shù)據(jù)的人或物對數(shù)據(jù)進(jìn)行的任意修改。
3.2. 訪問峰谷指標(biāo)的數(shù)據(jù)
正如本文的第一節(jié)所述,名為 zigzag.mq5 的源代碼文件創(chuàng)建了三個緩沖區(qū):
//--- indicator buffers mapping ? SetIndexBuffer(0,ZigzagBuffer,INDICATOR_DATA); ? SetIndexBuffer(1,HighMapBuffer,INDICATOR_CALCULATIONS); ? SetIndexBuffer(2,LowMapBuffer,INDICATOR_CALCULATIONS);