|
本帖最后由 sanbuxiaozhu 于 2019-7-2 09:05 编辑
- //+------------------------------------------------------------------+
- //| 多空轨道.mq4 |
- //| Copyright 2018, MetaQuotes Software Corp. |
- //| https://www.mql5.com |
- //+------------------------------------------------------------------+
- #property copyright "http://my.fx3q.com"
- #property link "https://www.mql5.com"
- #property version "1.00"
- #property strict
- extern int MA = 60;
- extern ENUM_MA_METHOD method = MODE_SMA;
- int 箭头间隔 = 100,juli = 20;
- extern double 内轨系数 = 1;
- extern double 外轨系数 = 8;
- ENUM_BASE_CORNER Corner =1;//显示位置
- int FontSize =10;
- color Fontcolor=clrWhite;
- color BGcolor=clrBlack;
- color BRcolor=clrCoral;
- int Win=0;
- int x1=0,y1=0;
- double middle,up1,low1;
- bool upp = true,downpp = true;
- #property indicator_chart_window
- #property indicator_buffers 7
- #property indicator_plots 7
- //--- plot ma
- #property indicator_label1 "ma"
- #property indicator_type1 DRAW_LINE
- #property indicator_color1 clrRed
- #property indicator_style1 STYLE_SOLID
- #property indicator_width1 1
- //--- plot maHigh
- #property indicator_label2 "maHigh"
- #property indicator_type2 DRAW_LINE
- #property indicator_color2 clrGold
- #property indicator_style2 STYLE_SOLID
- #property indicator_width2 1
- //--- plot maLow
- #property indicator_label3 "maLow"
- #property indicator_type3 DRAW_LINE
- #property indicator_color3 clrDeepSkyBlue
- #property indicator_style3 STYLE_SOLID
- #property indicator_width3 1
- //--- plot maMax
- #property indicator_label4 "maMax"
- #property indicator_type4 DRAW_LINE
- #property indicator_color4 clrGold
- #property indicator_style4 STYLE_SOLID
- #property indicator_width4 2
- //--- plot maMin
- #property indicator_label5 "maMin"
- #property indicator_type5 DRAW_LINE
- #property indicator_color5 clrDeepSkyBlue
- #property indicator_style5 STYLE_SOLID
- #property indicator_width5 2
- //--- plot up
- #property indicator_label6 "up"
- #property indicator_type6 DRAW_ARROW
- #property indicator_color6 clrRed
- #property indicator_style6 STYLE_SOLID
- #property indicator_width6 3
- //--- plot down
- #property indicator_label7 "down"
- #property indicator_type7 DRAW_ARROW
- #property indicator_color7 clrDeepSkyBlue
- #property indicator_style7 STYLE_SOLID
- #property indicator_width7 3
- //--- indicator buffers
- double maBuffer[];
- double maHighBuffer[];
- double maLowBuffer[];
- double maMaxBuffer[];
- double maMinBuffer[];
- double upBuffer[];
- double downBuffer[];
- //+------------------------------------------------------------------+
- //| Custom indicator initialization function |
- //+------------------------------------------------------------------+
- int OnInit()
- {
- //--- indicator buffers mapping
- mianban();
- IndicatorShortName("多空轨道");
- SetIndexBuffer(1,maHighBuffer);
- SetIndexBuffer(2,maLowBuffer);
- SetIndexBuffer(3,maMaxBuffer);
- SetIndexBuffer(4,maMinBuffer);
- SetIndexBuffer(5,upBuffer);
- SetIndexBuffer(6,downBuffer);
- //--- setting a code from the Wingdings charset as the property of PLOT_ARROW
-
- SetIndexBuffer(0,maBuffer);
- SetIndexLabel(0,MA+"日均线");
-
- SetIndexArrow(5,241);
- SetIndexLabel(5,"up");
-
- SetIndexArrow(6,242);
- SetIndexLabel(6,"down");
-
- if(Period() == 5){ 箭头间隔 = 15;juli = 20;} //Period()返回当前图表的时间表 属于几分钟图
- if(Period() == 15){ 箭头间隔 = 25;juli =30;}
- if(Period() == 30){ 箭头间隔 = 35;juli =40;}
- if(Period() == 60){ 箭头间隔 = 66;juli =50;}
- if(Period() == 240){ 箭头间隔 = 88;juli =66;}
- if(Period() == 1440){ 箭头间隔 = 150;juli = 100;}
- if(Period() == 10080){ 箭头间隔 = 250;juli = 120;}
- if(Period() == 1){ 箭头间隔 = 10;juli =15;}
-
-
- //---
- return(INIT_SUCCEEDED);
- }
-
- int OnDeinit(){
- ObjectsDeleteAll();
- return 0;
- }
- //+------------------------------------------------------------------+
- //| Custom indicator iteration function |
- //+------------------------------------------------------------------+
- int OnCalculate(const int rates_total,
- const int prev_calculated,
- const datetime &time[],
- const double &open[],
- const double &high[],
- const double &low[],
- const double &close[],
- const long &tick_volume[],
- const long &volume[],
- const int &spread[])
- {
-
- int counted_bars=IndicatorCounted();
- if (counted_bars<0) return(-1);
- if (counted_bars>0) counted_bars--;
- int limit=Bars-counted_bars;
-
- double c1,c2,d1,d2,middle1,middle2;
- for(int i=limit-2; i>=0; i--) //必须是limit-2 否则数组下标越界
- {
-
-
-
- middle = iMA(Symbol(),PERIOD_CURRENT,MA,0,method,PRICE_CLOSE,i+1); //最后一个参数表示第几个MA值
- up1 = iMA(Symbol(),PERIOD_CURRENT,MA,0,method,PRICE_HIGH,i+1);
- low1 = iMA(Symbol(),PERIOD_CURRENT,MA,0,method,PRICE_LOW,i+1);
-
- middle1 = iMA(Symbol(),PERIOD_CURRENT,MA,0,method,PRICE_CLOSE,i+2);
-
-
- maBuffer[i] = middle;
- maHighBuffer[i] = (up1-low1)*内轨系数 + middle;
- maLowBuffer[i] = middle - (up1-low1)*内轨系数;
-
- maMaxBuffer[i] = (up1-low1)*外轨系数 + middle;
- maMinBuffer[i] = middle - (up1-low1)*外轨系数;
-
-
-
- if(maHighBuffer[i+1] < Close[i+1] && downpp == true){
- upBuffer[i] = Low[i] - 箭头间隔*Point;
- upp = true;
- downpp = false;
- }
-
- if(maLowBuffer[i+1] > Close[i+1] && upp == true){
- downBuffer[i] = High[i] + 箭头间隔*Point;
- upp = false;
- downpp = true;
- }
-
- }
-
-
-
- return(rates_total);
- }
- //+------------------------------------------------------------------+
- void CreateLabel(long chart_ID,
- string Name,
- int Window,
- int xPos,
- int yPos,
- int 宽,
- int 高,
- int FontSz,
- color FontColor1,
- color BGcolor1,
- color BRcolor1,
- int Corner1,
- string LblTxt,
- string tooltip)
- {
- if(ObjectFind(Name)!=0) ObjectCreate(chart_ID,Name,OBJ_BUTTON,Window,0,0);
- ObjectSetInteger(chart_ID,Name,OBJPROP_CORNER,Corner1);
- ObjectSetInteger(chart_ID,Name,OBJPROP_XDISTANCE,xPos);
- ObjectSetInteger(chart_ID,Name,OBJPROP_YDISTANCE,yPos);
- ObjectSetInteger(chart_ID,Name,OBJPROP_XSIZE,宽);
- ObjectSetInteger(chart_ID,Name,OBJPROP_YSIZE,高);
- ObjectSetString(chart_ID,Name,OBJPROP_FONT,"黑体");
- ObjectSetInteger(chart_ID,Name,OBJPROP_FONTSIZE,FontSz);
- ObjectSetInteger(chart_ID,Name,OBJPROP_COLOR,FontColor1);
- ObjectSetInteger(chart_ID,Name,OBJPROP_BORDER_COLOR,BRcolor1);
- ObjectSetInteger(chart_ID,Name,OBJPROP_BGCOLOR,BGcolor1);
- ObjectSetInteger(chart_ID,Name,OBJPROP_SELECTABLE,0);
- ObjectSetInteger(chart_ID,Name,OBJPROP_SELECTED,0);
- ObjectSetInteger(chart_ID,Name,OBJPROP_BACK,false);
- ObjectSetInteger(chart_ID,Name,OBJPROP_ALIGN,ALIGN_LEFT);
- ObjectSetString(chart_ID,Name,OBJPROP_TEXT,LblTxt);
- ObjectSetString(chart_ID,Name,OBJPROP_TOOLTIP,tooltip);
- }
-
- //+------------------------------------------------------------------+
- bool EditCreate( long chart_ID=0,
- string name="Edit",
- int sub_window=0,
- int x=0,
- int y=0,
- int width=50,
- int height=18,
- color clr=clrYellow,
- string text="Text",
- string tooltip="",
- ENUM_BASE_CORNER corner=CORNER_LEFT_UPPER,
- string font="Arial",
- int font_size=10,
- ENUM_ALIGN_MODE align=ALIGN_CENTER,
- bool read_only=false,
- color back_clr=clrBlack,
- color border_clr=clrFireBrick,
- bool back=false,
- bool selection=false,
- bool hidden=true,
- long z_order=0
- )
- {
- ResetLastError();
- if(!ObjectCreate(chart_ID,name,OBJ_EDIT,sub_window,0,0))
- {
- Print(__FUNCTION__,
- ": failed to create "Edit" object! Error code = ",GetLastError());
- return(false);
- }
- ObjectSetInteger(chart_ID,name,OBJPROP_XDISTANCE,x);
- ObjectSetInteger(chart_ID,name,OBJPROP_YDISTANCE,y);
- ObjectSetInteger(chart_ID,name,OBJPROP_XSIZE,width);
- ObjectSetInteger(chart_ID,name,OBJPROP_YSIZE,height);
- ObjectSetString(chart_ID,name,OBJPROP_TEXT,text);
- ObjectSetString(chart_ID,name,OBJPROP_FONT,font);
- ObjectSetInteger(chart_ID,name,OBJPROP_FONTSIZE,font_size);
- ObjectSetInteger(chart_ID,name,OBJPROP_ALIGN,align);
- ObjectSetInteger(chart_ID,name,OBJPROP_READONLY,read_only);
- ObjectSetInteger(chart_ID,name,OBJPROP_CORNER,corner);
- ObjectSetInteger(chart_ID,name,OBJPROP_COLOR,clr);
- ObjectSetInteger(chart_ID,name,OBJPROP_BGCOLOR,back_clr);
- ObjectSetInteger(chart_ID,name,OBJPROP_BORDER_COLOR,border_clr);
- ObjectSetInteger(chart_ID,name,OBJPROP_BACK,back);
- ObjectSetInteger(chart_ID,name,OBJPROP_SELECTABLE,selection);
- ObjectSetInteger(chart_ID,name,OBJPROP_SELECTED,selection);
- ObjectSetInteger(chart_ID,name,OBJPROP_HIDDEN,hidden);
- ObjectSetInteger(chart_ID,name,OBJPROP_ZORDER,z_order);
- ObjectSetString(chart_ID,name,OBJPROP_TOOLTIP,tooltip);
- return(true);
- }
- //+------------------------------------------------------------------+
- //| 创建控制面板 |
- //+------------------------------------------------------------------+
- void mianban()
- {
- int a1 = 80,b1=140 ,y1 = 50;
-
- for(int j=1;j<=3;j++)
- {
-
- switch(j)
- {
- case 1 : CreateLabel(0,"hekc"+(string)j,Win,b1,290,62,25,FontSize,Fontcolor,clrPurple,clrRed,Corner,"打开图表","打开图表"); break;
- case 2 : EditCreate(0,"hekc"+(string)j,Win,a1-10,290,66,25,clrWhite,"镑美","Symbol",Corner); break;
- case 3 : CreateLabel(0,"hekc"+(string)j,Win,168,290,20,25,FontSize,Fontcolor,clrRed,clrRed,Corner,"X","卸载指标"); break;
- }
- }
- }
- void OnChartEvent(const int id,
- const long &lparam,
- const double &dparam,
- const string &sparam)
- {
- string name,sym="";
-
- if(id==CHARTEVENT_OBJECT_CLICK)
- {
- //获取EDIT值
- if(ObjectFind(0,"hekc"+(string)2)>=0)//zhiying
- {
- name=ObjectGetString(0,"hekc"+(string)2,OBJPROP_TEXT);
- }
- if(sparam=="hekc"+(string)1)
- {
-
- if(name == "美日")sym = "USDJPY";
- if(name == "欧美")sym = "EURUSD";
- if(name == "美加")sym = "USDCAD";
- if(name == "欧日")sym = "EURJPY";
- if(name == "纽美")sym = "NZDUSD";
- if(name == "美瑞")sym = "USDCHF";
- if(name == "镑美")sym = "GBPUSD";
- if(name == "镑日")sym = "GBPJPY";
- if(name == "黄金")sym = "XAUUSD";
- if(name == "澳美")sym = "AUDUSD";
- if(name == "磅美")sym = "GBPUSD";
- if(name == "人民币")sym = "USDCNH";
- if(name == "里拉")sym = "USDTRY";
- if(name == "原油")sym = "XTIUSD";
- if(name == "比特币")sym = "BTCUSD";
-
- if(ChartOpen(sym,PERIOD_H1) == false){
- Alert("输入有误,打开失败");
- }
-
- }
-
- if(sparam=="hekc"+(string)3) //减
- {
-
- ChartIndicatorDelete(0,0,"多空轨道");
- ObjectsDeleteAll();
- }
-
- }
-
- }
-
复制代码 |
|