MT5入门到精通之十四(对象绘制类)

一对象绘制类
1.效果


image.png

2.对象绘制类实现

//+------------------------------------------------------------------+
//|                                                         Draw.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 Draw
  {
private:

public:
   void              draw(
                          string       name,
                          ENUM_OBJECT  type,
                          datetime time1,double price1,
                          color        clr=clrRed,
                          int          sub_window=0);
   void              draw(
                          string       name,
                          ENUM_OBJECT  type,
                          datetime time1,double price1,
                          datetime time2,double price2,
                          color        clr=clrRed,
                          int          sub_window=0);
   void              draw(
                          string       name,
                          ENUM_OBJECT  type,
                          datetime time1,double price1,
                          datetime time2,double price2,
                          datetime time3,double price3,
                          color        clr=clrRed,
                          int          sub_window=0);
   void              hLine(string name,double price,color clr=clrRed,int sub_window=0);
   void              vLine(string name,datetime time,color clr=clrRed,int sub_window=0);
   void              text(string name,string text,datetime time,double price,color clr=clrRed,int fontsize=10,int sub_window=0);
   void              labelArr(string name,string &texts[],int x,int y,int yFlap=15,int corner=0,color clr=clrRed,int fontsize=10,int sub_window=0);
   void              label(string name,string text,int x,int y,int corner=0,color clr=clrRed,int fontsize=10,int sub_window=0);
   void              button(
                            string name,string text,
                            int x,int y,
                            int width,int height,
                            int corner=0,
                            color clr=clrRed,int fontsize=10,int sub_window=0);
   void              edit(
                          string name,string text,
                          int x,int y,
                          int width,int width,
                          int corner=0,
                          color clr=clrRed,int fontsize=10,int sub_window=0);

   void              trend(
                           string       name,
                           datetime time1,double price1,
                           datetime time2,double price2,
                           int leftRay=0,int rightRay=0,
                           color        clr=clrRed,
                           int          sub_window=0);
   void              fibo(
                          string       name,
                          datetime time1,double price1,
                          datetime time2,double price2,
                          int levels=6,
                          color        clr=clrRed,
                          int          sub_window=0);
   void              setObjectColor(long chart_id,string name,color clr=clrRed);
                     Draw();
                    ~Draw();
  };
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
Draw::Draw()
  {
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
Draw::~Draw()
  {
  }
//+------------------------------------------------------------------+
void Draw::draw(string name,ENUM_OBJECT type,datetime time1,double price1,datetime time2,double price2,datetime time3,double price3,color clr=255,int sub_window=0)
  {
   if(ObjectFind(0,name)<0)
     {
      ObjectCreate(0,name,type,sub_window,time1,price1,time2,price2,time3,price3);
     }
   else
     {
      if(ObjectGetDouble(0,name,OBJPROP_PRICE)!=price1)
        {
         ObjectSetDouble(0,name,OBJPROP_PRICE,price1);
        }
      if(ObjectGetInteger(0,name,OBJPROP_TIME)!=time1)
        {
         ObjectSetInteger(0,name,OBJPROP_TIME,time1);
        }
      double p2;
      datetime t2;
      ObjectGetDouble(0,name,OBJPROP_PRICE,1,p2);
      ObjectGetInteger(0,name,OBJPROP_TIME,1,t2);
      if(p2!=price2)
        {
         ObjectSetDouble(0,name,OBJPROP_PRICE,1,price2);
        }
      if(t2!=time2)
        {
         ObjectSetInteger(0,name,OBJPROP_TIME,1,time2);
        }
      double p3;
      datetime t3;
      ObjectGetDouble(0,name,OBJPROP_PRICE,2,p3);
      ObjectGetInteger(0,name,OBJPROP_TIME,2,t3);
      if(p3!=price3)
        {
         ObjectSetDouble(0,name,OBJPROP_PRICE,2,price3);
        }
      if(t3!=time3)
        {
         ObjectSetInteger(0,name,OBJPROP_TIME,2,time3);
        }
     }
   setObjectColor(0,name,clr);
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void Draw::draw(string name,ENUM_OBJECT type,datetime time1,double price1,color clr=255,int sub_window=0)
  {
   if(ObjectFind(0,name)<0)
     {
      ObjectCreate(0,name,type,sub_window,time1,price1);
     }
   else
     {
      if(ObjectGetDouble(0,name,OBJPROP_PRICE)!=price1)
        {
         ObjectSetDouble(0,name,OBJPROP_PRICE,price1);
        }
      if(ObjectGetInteger(0,name,OBJPROP_TIME)!=time1)
        {
         ObjectSetInteger(0,name,OBJPROP_TIME,time1);
        }
     }
   setObjectColor(0,name,clr);
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void Draw::draw(string name,ENUM_OBJECT type,datetime time1,double price1,datetime time2,double price2,color clr=255,int sub_window=0)
  {
   if(ObjectFind(0,name)<0)
     {
      ObjectCreate(0,name,type,sub_window,time1,price1,time2,price2);
     }
   else
     {
      if(ObjectGetDouble(0,name,OBJPROP_PRICE)!=price1)
        {
         ObjectSetDouble(0,name,OBJPROP_PRICE,price1);
        }
      if(ObjectGetInteger(0,name,OBJPROP_TIME)!=time1)
        {
         ObjectSetInteger(0,name,OBJPROP_TIME,time1);
        }
      double p2;
      datetime t2;
      ObjectGetDouble(0,name,OBJPROP_PRICE,1,p2);
      ObjectGetInteger(0,name,OBJPROP_TIME,1,t2);
      if(p2!=price2)
        {
         ObjectSetDouble(0,name,OBJPROP_PRICE,1,price2);
        }
      if(t2!=time2)
        {
         ObjectSetInteger(0,name,OBJPROP_TIME,1,time2);
        }
     }
   setObjectColor(0,name,clr);
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void Draw::hLine(string name,double price,color clr=255,int sub_window=0)
  {
   if(ObjectFind(0,name)<0)
     {
      ObjectCreate(0,name,OBJ_HLINE,sub_window,0,price);
     }
   else
     {
      if(ObjectGetDouble(0,name,OBJPROP_PRICE)!=price)
        {
         ObjectSetDouble(0,name,OBJPROP_PRICE,price);
        }
     }
   setObjectColor(0,name,clr);
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void Draw::vLine(string name,datetime time,color clr=255,int sub_window=0)
  {
   if(ObjectFind(0,name)<0)
     {
      ObjectCreate(0,name,OBJ_VLINE,sub_window,time,0);
     }
   else
     {
      if(ObjectGetInteger(0,name,OBJPROP_TIME)!=time)
        {
         ObjectSetInteger(0,name,OBJPROP_TIME,time);
        }
     }
   setObjectColor(0,name,clr);
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void Draw::text(string name,string text,datetime time,double price,color clr=255,int fontsize=10,int sub_window=0)
  {
   if(ObjectFind(0,name)<0)
     {
      ObjectCreate(0,name,OBJ_TEXT,sub_window,time,price);
     }
   else
     {
      if(ObjectGetInteger(0,name,OBJPROP_TIME)!=time)
        {
         ObjectSetInteger(0,name,OBJPROP_TIME,time);
        }
      if(ObjectGetDouble(0,name,OBJPROP_PRICE)!=price)
        {
         ObjectSetDouble(0,name,OBJPROP_PRICE,price);
        }
     }
   if(ObjectGetInteger(0,name,OBJPROP_FONTSIZE)!=fontsize)
     {
      ObjectSetInteger(0,name,OBJPROP_FONTSIZE,fontsize);
     }
   if(ObjectGetString(0,name,OBJPROP_TEXT)!=text)
     {
      ObjectSetString(0,name,OBJPROP_TEXT,text);
     }
   setObjectColor(0,name,clr);
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void Draw::labelArr(string name,string &texts[],int x,int y,int yFlap=15,int corner=0,color clr=255,int fontsize=10,int sub_window=0)
  {
   for(int i=0;i

3.对象绘制类使用

//+------------------------------------------------------------------+
//|                                                       testEA.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 
#include 
#include 

//zigZag3个参数
input int  ExtDepth=12;
input int  ExtDeviation=5;
input int  ExtBackstep=3;
input int  count=10; //获取个数

Trade td;
Kline kl;
Draw dr;

datetime bar_time;
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
int OnInit()
  {
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
/*//double lots=td.formatLots(Symbol(),1.23456);
   double openPrice=0;
   datetime openTime=0;
   double openLots=0;
   double openStopLoss=0;
   double openTakeProfit=0;
   int ticket=td.getNewestPositionOrder(Symbol(),POSITION_TYPE_SELL,openPrice,openTime,openLots,openStopLoss,openTakeProfit);
  
  //zigZag使用(获取前count对象的zigZag值)
  double zigZag2[][4];
  kl.getZigZag(zigZag2,count,ExtDepth,ExtDeviation,ExtBackstep);*/

//画对象

   MqlRates rates[];
   kl.getRates(rates,200);
/*if(bar_time!=rates[0].time)
     {
      //1.趋势线(注意有新k线了,下标就都变了,都会重新画。所以不想变的画用绝对时间)
      dr.trend("trend",rates[0].time,rates[0].open,rates[5].time,rates[5].open);
      bar_time=rates[0].time;
     }

//2.遍历
   int objTotal=ObjectsTotal(0);
   for(int i=0;i

二.日历对象获取
1.获取如下日历事件

image.png

2.脚本代码实现

//+------------------------------------------------------------------+
//|                                           getCanlendarScript.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"
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
struct eventData
  {
   datetime          time;//时间
   string            country;//国家
   string            title;//内容
  };

eventData edArr[500];
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void OnStart()
  {
//---
   int objTotals=ObjectsTotal(0);
   for(int i=0;i3)
           {
            edArr[i].time=StringToTime(s[0]+" "+s[1]);
            edArr[i].country=countryWithCode(s[2]);
            edArr[i].title=s[3];
           }
         else
           {
            edArr[i].time=StringToTime(s[0]+" "+s[1]);
            edArr[i].country="美国";
            edArr[i].title=s[2];
           }
        }
     }
  }
//+------------------------------------------------------------------+
string countryWithCode(string code)
  {
   string a=code;
   if(code=="DE:")
     {
      a="德国";
     }

   return(a);
  }
//+------------------------------------------------------------------+

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

你可能感兴趣的:(MT5入门到精通之十四(对象绘制类))