MT5入门到精通之八指标获取(类封装)

一.封装类Kline
1.实现
(如果需要用多个同一类型的指标,建议接口传入数组参数)
(如果同一类型的指标只用到一次,可以写在接口里面)

//+------------------------------------------------------------------+
//|                                                        Kline.mqh |
//|                        Copyright 2017, MetaQuotes Software Corp. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2017, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
class Kline
  {
private:

protected:

public:
   //商品
   string            symbol;
   //周期
   ENUM_TIMEFRAMES   tf;
   //开高低收,时间,数组
   double            open[];
   double            close[];
   double            high[];
   double            low[];
   datetime          time[];

   //adx指标数组
   double            adx0[];
   double            adx1[];
   double            adx2[];

   double getAsk()
     {
      return getAsk(symbol);
     }
   double getAsk(string symbol)
     {
      return SymbolInfoDouble(symbol,SYMBOL_ASK);
     }
   double getBid()
     {
      return getBid(symbol);
     }
   double getBid(string symbol)
     {
      return SymbolInfoDouble(symbol,SYMBOL_BID);
     }
   int getOpen(int count,string symbol,ENUM_TIMEFRAMES tf)
     {
      ArraySetAsSeries(open,true);
      int num=CopyOpen(symbol,tf,0,count,open);
      return num;
     }

   int getOpen(int count)
     {
      return getOpen(count,symbol,tf);
     }
   int getClose(int count,string symbol,ENUM_TIMEFRAMES tf)
     {
      ArraySetAsSeries(close,true);
      int num=CopyClose(symbol,tf,0,count,close);
      return num;
     }
   int getClose(int count)
     {
      return getClose(count,symbol,tf);
     }

   int getHigh(int count,string symbol,ENUM_TIMEFRAMES tf)
     {
      ArraySetAsSeries(high,true);
      int num=CopyHigh(symbol,tf,0,count,high);
      return num;
     }
   int getHigh(int count)
     {
      return getHigh(count,symbol,tf);
     }
   int getLow(int count,string symbol,ENUM_TIMEFRAMES tf)
     {
      ArraySetAsSeries(low,true);
      int num=CopyLow(symbol,tf,0,count,low);
      return num;
     }
   int getLow(int count)
     {
      return getLow(count,symbol,tf);
     }
   int getTime(int count,string symbol,ENUM_TIMEFRAMES tf)
     {
      ArraySetAsSeries(time,true);
      int num=CopyTime(symbol,tf,0,count,time);
      return num;
     }
   int getTime(int count)
     {
      return getTime(count,symbol,tf);
     }
   void MA(double            &ma[],
           int count,//获取几个值
           string               symbol,            // 交易品种名称
           ENUM_TIMEFRAMES      period,            // 周期
           int                  ma_period,         // 平均周期
           int                  ma_shift,          // 平移
           ENUM_MA_METHOD       ma_method,         // 平滑类型
           ENUM_APPLIED_PRICE   applied_price      // 价格或者处理程序类型
           )
     {
      int ma_h=iMA(symbol,period,ma_period,ma_shift,ma_method,applied_price);
      ArraySetAsSeries(ma,true);
      CopyBuffer(ma_h,0,0,count,ma);
      //释放句柄
      IndicatorRelease(ma_h);
     }
   void ADX(int count,//获取几个值
            string           symbol,         // 交易品种名称
            ENUM_TIMEFRAMES  period,         // 周期
            int              adx_period      // 平均周期
            )
     {
      int adx_h=iADX(symbol,period,adx_period);
      ArraySetAsSeries(adx0,true);
      ArraySetAsSeries(adx1,true);
      ArraySetAsSeries(adx2,true);
      CopyBuffer(adx_h,0,0,count,adx0);
      CopyBuffer(adx_h,1,0,count,adx1);
      CopyBuffer(adx_h,2,0,count,adx2);
      IndicatorRelease(adx_h);
     }

                     Kline(string symbol,ENUM_TIMEFRAMES tf);
                    ~Kline();
  };
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
Kline::Kline(string symbol,ENUM_TIMEFRAMES tf)
  {
   this.symbol=symbol;
   this.tf=tf;
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
Kline::~Kline()
  {
  }
//+------------------------------------------------------------------+

2.具体使用

//+------------------------------------------------------------------+
//|                                            testKSeriesScript.mq5 |
//|                        Copyright 2017, MetaQuotes Software Corp. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2017, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
#include 

Kline kl(Symbol(),0);
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void OnStart()
  {
//1.获取ask
//1.1直接获取
   double ask=SymbolInfoDouble(Symbol(),SYMBOL_ASK);
//1.2 类获取
   ask=kl.getAsk();
//2. 获取开盘价
//2.1直接获取
   double open[];
   ArraySetAsSeries(open,true);
   CopyOpen(Symbol(),0,0,10,open);
//2.2类获取
   kl.getOpen(10,Symbol(),0);
   double open0=kl.open[0];

//3.类获取时间
   kl.getTime(5);
   datetime time0=kl.time[0];

//4.获取均线指标
   int ma_h=iMA(Symbol(),0,12,0,MODE_SMA,PRICE_CLOSE);
   double ma[];
   ArraySetAsSeries(ma,true);
   CopyBuffer(ma_h,0,0,10,ma);
   double ma0=ma[1];

//4.1用类获取均线(如果需要用多个同一类型的指标,建议接口传入数组参数)
//12日均线
   double ma12[];
   kl.MA(ma12,10,Symbol(),0,12,0,MODE_SMA,PRICE_CLOSE);
   double ma12_1=ma12[1];
//20日均线
   double ma20[];
   kl.MA(ma20,10,Symbol(),0,20,0,MODE_SMA,PRICE_CLOSE);
   double ma20_1=ma20[1];

//5.获取ADX指标 (如果同一类型的指标只用到一次,可以写在接口里面)
   kl.ADX(10,Symbol(),0,14);
   double adx0_1=kl.adx0[1];
   double adx1_1=kl.adx1[1];
   double adx2_1=kl.adx2[1];

//6.自定义指标如何获取
   int cus_h=iCustom(Symbol(),0,"BB",20,0,2);
   double cus0[];
   double cus1[];
   double cus2[];
   ArraySetAsSeries(cus0,true);
   ArraySetAsSeries(cus1,true);
   ArraySetAsSeries(cus2,true);
   CopyBuffer(cus_h,0,0,10,cus0);
   CopyBuffer(cus_h,1,0,10,cus1);
   CopyBuffer(cus_h,2,0,10,cus2);
   double cus0_1=cus0[1];
   double cus1_1=cus1[1];
   double cus2_1=cus2[1];

   int a=1;

  }
//+------------------------------------------------------------------+

如果您发现本文对你有所帮助,如果您认为其他人也可能受益,请把它分享出去。

你可能感兴趣的:(MT5入门到精通之八指标获取(类封装))