MT5入门到精通之十四(对象绘制类)
2017-05-02 本文已影响0人
旺仔2488
一对象绘制类
1.效果

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<ArraySize(texts);i++)
{
label(name+IntegerToString(i),texts[i],x,y,corner,clr,fontsize,sub_window);
y=y+yFlap;
}
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
void Draw::label(string name,string text,int x,int y,int corner=0,color clr=clrRed,int fontsize=10,int sub_window=0)
{
if(ObjectFind(0,name)<0)
{
ObjectCreate(0,name,OBJ_LABEL,sub_window,0,0);
}
if(ObjectGetInteger(0,name,OBJPROP_CORNER)!=corner)
{
ObjectSetInteger(0,name,OBJPROP_CORNER,corner);
}
if(ObjectGetInteger(0,name,OBJPROP_XDISTANCE)!=x)
{
ObjectSetInteger(0,name,OBJPROP_XDISTANCE,x);
}
if(ObjectGetInteger(0,name,OBJPROP_YDISTANCE)!=y)
{
ObjectSetInteger(0,name,OBJPROP_YDISTANCE,y);
}
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::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)
{
if(ObjectFind(0,name)<0)
{
ObjectCreate(0,name,OBJ_BUTTON,sub_window,0,0);
}
if(ObjectGetInteger(0,name,OBJPROP_CORNER)!=corner)
{
ObjectSetInteger(0,name,OBJPROP_CORNER,corner);
}
if(ObjectGetInteger(0,name,OBJPROP_XDISTANCE)!=x)
{
ObjectSetInteger(0,name,OBJPROP_XDISTANCE,x);
}
if(ObjectGetInteger(0,name,OBJPROP_YDISTANCE)!=y)
{
ObjectSetInteger(0,name,OBJPROP_YDISTANCE,y);
}
//用width height 会显示不全
if(ObjectGetInteger(0,name,OBJPROP_XSIZE)!=width)
{
ObjectSetInteger(0,name,OBJPROP_XSIZE,width);
}
if(ObjectGetInteger(0,name,OBJPROP_YSIZE)!=height)
{
ObjectSetInteger(0,name,OBJPROP_YSIZE,height);
}
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::edit(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)
{
if(ObjectFind(0,name)<0)
{
ObjectCreate(0,name,OBJ_EDIT,sub_window,0,0);
}
if(ObjectGetInteger(0,name,OBJPROP_CORNER)!=corner)
{
ObjectSetInteger(0,name,OBJPROP_CORNER,corner);
}
if(ObjectGetInteger(0,name,OBJPROP_XDISTANCE)!=x)
{
ObjectSetInteger(0,name,OBJPROP_XDISTANCE,x);
}
if(ObjectGetInteger(0,name,OBJPROP_YDISTANCE)!=y)
{
ObjectSetInteger(0,name,OBJPROP_YDISTANCE,y);
}
//用width height 会显示不全
if(ObjectGetInteger(0,name,OBJPROP_XSIZE)!=width)
{
ObjectSetInteger(0,name,OBJPROP_XSIZE,width);
}
if(ObjectGetInteger(0,name,OBJPROP_YSIZE)!=height)
{
ObjectSetInteger(0,name,OBJPROP_YSIZE,height);
}
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::trend(string name,datetime time1,double price1,datetime time2,double price2,int leftRay=0,int rightRay=0,color clr=255,int sub_window=0)
{
draw(name,OBJ_TREND,time1,price1,time2,price2,clr,sub_window);
if(ObjectGetInteger(0,name,OBJPROP_RAY_LEFT)!=leftRay)
{
ObjectSetInteger(0,name,OBJPROP_RAY_LEFT,leftRay);
}
if(ObjectGetInteger(0,name,OBJPROP_RAY_RIGHT)!=rightRay)
{
ObjectSetInteger(0,name,OBJPROP_RAY_RIGHT,rightRay);
}
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
void Draw::fibo(string name,datetime time1,double price1,datetime time2,double price2,int levels=6,color clr=255,int sub_window=0)
{
draw(name,OBJ_FIBO,time1,price1,time2,price2,clr,sub_window);
if(ObjectGetInteger(0,name,OBJPROP_LEVELS)!=levels)
{
ObjectSetInteger(0,name,OBJPROP_LEVELS,levels);
}
}
//+------------------------------------------------------------------+
void Draw::setObjectColor(long chart_id,string name,color clr=clrRed)
{
if(ObjectGetInteger(0,name,OBJPROP_COLOR)!=clr)
{
ObjectSetInteger(0,name,OBJPROP_COLOR,clr);
}
}
//+------------------------------------------------------------------+
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 <wz\Trade.mqh>
#include <wz\Kline.mqh>
#include <wz\Draw.mqh>
//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<objTotal;i++)
{
//2.1获取对象名称
string objName=ObjectName(0,i);
//2.3 对象类型判断
if(ObjectGetInteger(0,objName,OBJPROP_TYPE)==OBJ_TREND)
{
//2.2获取价格和时间 (默认第一个点0)
double price1=ObjectGetDouble(0,objName,OBJPROP_PRICE);
datetime time1=ObjectGetInteger(0,objName,OBJPROP_TIME);
//PrintFormat("price1=%lf",price1);
//2.3第二种获取价格和时间的方法(第二个点)
double price2;
datetime time2;
ObjectGetDouble(0,objName,OBJPROP_PRICE,1,price2);
ObjectGetInteger(0,objName,OBJPROP_TIME,1,time2);
//2.4 获取趋势线上的价格(入参是时间)
double trendPrice=ObjectGetValueByTime(0,objName,rates[3].time);
//2.5设置对象颜色
ObjectSetInteger(0,objName,OBJPROP_COLOR,clrWhite);
//2.6删除对象
ObjectDelete(0,objName);
}
}
//3.画箭头
dr.draw("up",OBJ_ARROW_UP,rates[1].time,rates[1].low);
//4.画线
dr.vLine("vLine",rates[3].time);
dr.hLine("hLine",rates[3].high);
dr.trend("trend2",rates[0].time,rates[0].open,rates[2].time,rates[2].open,1,1,Yellow);*/
//5.斐波那契线
dr.fibo("fibo",rates[30].time,rates[30].low,rates[10].time,rates[10].low);
//6.lable (不会随k线移动)
dr.lable("lable1","你好",20,20);
string sArr[]={"你好这是一个lable","wz","testEA"};
dr.lableArr("duo",sArr,20,40,17,0,White,11);
//7.text(跟随k线移动)
dr.text("text","根K线移动的text",rates[7].time,rates[7].low);
dr.button("button","这是一个按钮",150,20,150,20,0,Green);
dr.edit("edit","这是一个编辑框",350,20,150,20,0,White);
}
//+------------------------------------------------------------------+
二.日历对象获取
1.获取如下日历事件

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;i<objTotals;i++)
{
string objName=ObjectName(0,i);
//财经日历事件
if(ObjectGetInteger(0,objName,OBJPROP_TYPE)==OBJ_EVENT)
{
//string neirong=ObjectGetString(0,objName,OBJPROP_NAME);
string s[];
//PrintFormat("wy="+objName);
//PrintFormat("wy2="+neirong);
StringSplit(objName,StringGetCharacter(" ",0),s);
if(ArraySize(s)>3)
{
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);
}
//+------------------------------------------------------------------+
如果您发现本文对你有所帮助,如果您认为其他人也可能受益,请把它分享出去。