为什么只有 200 bar 的限制?
好指标...干得好
指标不起作用
由于错误而无法编译
//有趣的指示器 更正后的代码
//+------------------------------------------------------------------+ //|iGDR_Fractal_Levels.mq5 Copyright © 2008-2009, GreenDog, Russia | //| 版权所有。 //|krot@inbox.ru || //+------------------------------------------------------------------+ //---- 指标作者 #property copyright "版权 © 2008-2009, GreenDog"。 //---- 作者网站链接 #property link "krot@inbox.ru" //---- 指标版本号 #property version "1.00" //---- 在主窗口中绘制指示器 #property indicator_chart_window #property indicator_buffers 1 #property indicator_plots 1 //+----------------------------------------------+ //|| 声明一个枚举 //+----------------------------------------------+ enum FRACTAL_MODE { MODE_HIGH_LOW=0, // by extrema MODE_LOW, // 按顶点 MODE_HIGH // 由波谷 }; //+----------------------------------------------+ //|| 声明一个枚举 //+----------------------------------------------+ enum FRACTAL_TYPE { TYPE1=0, // 严格 TYPE2 // 非严格 }; //+----------------------------------------------+ //|| 指示器输入参数 //+----------------------------------------------+ input uint frNum_=2; // 分形的条数,2=5-条分形,3=7-条分形,等等。 input FRACTAL_TYPE frType= TYPE2; // 分形定义类型 0=严格,1=非严格 input FRACTAL_MODE frMode =MODE_HIGH_LOW; // 模式 input double dlt_=0.24; // 与平均条形图高度的误差 input uint sBars_=200; // 条数(最多 200 条) //---- input color BG1_Color = PaleGreen; // 第一层的背景颜色 input color TL1Color = Green; // 第一层行的颜色 input color BG2_Color = Yellow; // 第二层的背景颜色 input color TL2Color = DarkOrange; // 第二层线条的颜色 input color BG3_Color = Pink; // 第三层的背景颜色 input color TL3Color = Red; // 第三级线条的颜色 //+----------------------------------------------+ uint lastBars=0; uint frNum,sBars; datetime lastTime=0; color BGColor[3],TLColor[3]; double aData[240][2],aRes[3][2]; //+------------------------------------------------------------------+ //| 自定义指示器初始化函数 //+------------------------------------------------------------------+ void OnInit() { //---- 初始化常量 frNum=frNum_; if(frNum_<2) frNum=2; sBars=sBars_; if(sBars_>200) sBars=200; if(sBars_<10) sBars=10; BGColor[0]=BG1_Color; BGColor[1]=BG2_Color; BGColor[2]=BG3_Color; TLColor[0]=TL1Color; TLColor[1]=TL2Color; TLColor[2]=TL3Color; //---- } //+------------------------------------------------------------------+ //| 创建文本标签| //+------------------------------------------------------------------+ void CreateTextLabel(long chart_id, // 图表标识符 string name, // 对象名称 int nwin, // 窗口索引 datetime time1, // 1 个价格水平的时间 double price1, // 1 个价格水平 color Color, // 线的颜色 string text, // 文本 string font, // 字体 int fontsize) // 字体大小 { //---- ObjectCreate(chart_id,name,OBJ_TEXT,nwin,time1,price1); ObjectSetInteger(chart_id,name,OBJPROP_COLOR,Color); ObjectSetInteger(chart_id,name,OBJPROP_BACK,true); ObjectSetInteger(chart_id,name,OBJPROP_SELECTED,true); ObjectSetInteger(chart_id,name,OBJPROP_SELECTABLE,true); ObjectSetInteger(chart_id,name,OBJPROP_ZORDER,true); ObjectSetString(chart_id,name,OBJPROP_FONT,font); ObjectSetString(chart_id,name,OBJPROP_TEXT,text); ObjectSetInteger(chart_id,name,OBJPROP_FONTSIZE,fontsize); //---- } //+------------------------------------------------------------------+ //|| 重置文本标签| //+------------------------------------------------------------------+ void SetTextLabel(long chart_id, // 图表标识符 string name, // 对象名称 int nwin, // 窗口索引 datetime time1, // 1 个价格水平的时间 double price1, // 1 个价格水平 color Color, // 线的颜色 int style, // 线条样式 int width, // 线条粗细 string text, // 文本 string font, // 字体 int fontsize) // 字体大小 { //---- if(ObjectFind(chart_id,name)==-1) { CreateTextLabel(chart_id,name,nwin,time1,price1,Color,text,font,fontsize); } else { ObjectSetString(chart_id,name,OBJPROP_TEXT,text); ObjectMove(chart_id,name,0,time1,price1); ObjectSetInteger(chart_id,name,OBJPROP_COLOR,Color); } //---- } //+------------------------------------------------------------------+ //| 创建频道| //+------------------------------------------------------------------+ void CreateChannel(long chart_id, // 图表标识符 string name, // 对象名称 int nwin, // 窗口索引 datetime time1, // 1 个价格水平的时间 double price1, // 1 个价格水平 datetime time2, // 价格水平 2 的时间 double price2, // 2 价格水平 datetime time3, // 价格水平 3 的时间 double price3, // 3 价格水平 color Color, // 线的颜色 int style, // 线条样式 int width, // 线条粗细 string text) // 文本 { //---- ObjectCreate(chart_id,name,OBJ_CHANNEL,nwin,time1,price1,time2,price2); ObjectSetInteger(chart_id,name,OBJPROP_COLOR,Color); ObjectSetInteger(chart_id,name,OBJPROP_STYLE,style); ObjectSetInteger(chart_id,name,OBJPROP_WIDTH,width); ObjectSetString(chart_id,name,OBJPROP_TEXT,text); ObjectSetInteger(chart_id,name,OBJPROP_FILL,true); ObjectSetInteger(chart_id,name,OBJPROP_BACK,true); ObjectSetInteger(chart_id,name,OBJPROP_RAY_RIGHT,true); ObjectSetInteger(chart_id,name,OBJPROP_RAY,true); ObjectSetInteger(chart_id,name,OBJPROP_SELECTED,true); ObjectSetInteger(chart_id,name,OBJPROP_SELECTABLE,true); ObjectSetInteger(chart_id,name,OBJPROP_ZORDER,true); //---- } //+------------------------------------------------------------------+ //|| 重新安装通道| //+------------------------------------------------------------------+ void SetChannel(long chart_id, // 图表标识符 string name, // 对象名称 int nwin, // 窗口索引 datetime time1, // 1 个价格水平的时间 double price1, // 1 个价格水平 datetime time2, // 价格水平 2 的时间 double price2, // 2 价格水平 datetime time3, // 价格水平 3 的时间 double price3, // 3 价格水平 color Color, // 线的颜色 int style, // 线条样式 int width, // 线条粗细 string text) // 文本 { //---- if(ObjectFind(chart_id,name)==-1) { CreateChannel(chart_id,name,nwin,time1,price1,time2,price2,time3,price3,Color,style,width,text); } else { ObjectSetString(chart_id,name,OBJPROP_TEXT,text); ObjectMove(chart_id,name,0,time1,price1); ObjectMove(chart_id,name,1,time2,price2); ObjectMove(chart_id,name,2,time3,price3); ObjectSetInteger(chart_id,name,OBJPROP_COLOR,Color); } //---- } //+------------------------------------------------------------------+ //| 创建趋势线| //+------------------------------------------------------------------+ void CreateTline(long chart_id, // 图表标识符 string name, // 对象名称 int nwin, // 窗口索引 datetime time1, // 1 个价格水平的时间 double price1, // 1 个价格水平 datetime time2, // 价格水平 2 的时间 double price2, // 2 价格水平 color Color, // 线的颜色 int style, // 线条样式 int width, // 线条粗细 string text) // 文本 { //---- ObjectCreate(chart_id,name,OBJ_TREND,nwin,time1,price1,time2,price2); ObjectSetInteger(chart_id,name,OBJPROP_COLOR,Color); ObjectSetInteger(chart_id,name,OBJPROP_STYLE,style); ObjectSetInteger(chart_id,name,OBJPROP_WIDTH,width); ObjectSetString(chart_id,name,OBJPROP_TEXT,text); ObjectSetInteger(chart_id,name,OBJPROP_BACK,false); ObjectSetInteger(chart_id,name,OBJPROP_RAY_RIGHT,true); ObjectSetInteger(chart_id,name,OBJPROP_RAY,true); ObjectSetInteger(chart_id,name,OBJPROP_SELECTED,true); ObjectSetInteger(chart_id,name,OBJPROP_SELECTABLE,true); ObjectSetInteger(chart_id,name,OBJPROP_ZORDER,true); //---- } //+------------------------------------------------------------------+ //|| 重置趋势线| //+------------------------------------------------------------------+ void SetTline(long chart_id, // 图表标识符 string name, // 对象名称 int nwin, // 窗口索引 datetime time1, // 1 个价格水平的时间 double price1, // 1 个价格水平 datetime time2, // 价格水平 2 的时间 double price2, // 2 价格水平 color Color, // 线的颜色 int style, // 线条样式 int width, // 线条粗细 string text) // 文本 { //---- if(ObjectFind(chart_id,name)==-1) { CreateTline(chart_id,name,nwin,time1,price1,time2,price2,Color,style,width,text); } else { ObjectSetString(chart_id,name,OBJPROP_TEXT,text); ObjectMove(chart_id,name,0,time1,price1); ObjectMove(chart_id,name,1,time2,price2); ObjectSetInteger(chart_id,name,OBJPROP_COLOR,Color); } //---- } //+------------------------------------------------------------------+ //| 找到顶分形| //+------------------------------------------------------------------+ bool upFrac(int cnt,const double &High[]) { //---- for(int i=1; i<=int(frNum); i++) { if(frType==TYPE1) { //---- 如果条件严格,且后条或前条高于相邻条 - 无分形 if(High[cnt+i]>=High[cnt+i-1] || High[cnt-i]>=High[cnt-i+1]) return(false); } else { //---- 如果条件不严格,且后杠或前杠高于中心 - 无分形 if(High[cnt+i]>High[cnt] || High[cnt-i]>High[cnt]) return(false); } } //---- return(true); } //+------------------------------------------------------------------+ //| 找到底部分形| //+------------------------------------------------------------------+ bool dwFrac(int cnt,const double &Low[]) { //---- for(int i=1; i<=int(frNum); i++) { if(frType==TYPE1) { //---- 如果条件严格,且后杠或前杠与相邻杠的等分值较低 - 无分形 if(Low[cnt+i]<=Low[cnt+i-1] || Low[cnt-i]<=Low[cnt-i+1]) return(false); } else { //---- 如果条件不严格,且后杠或前杠低于中心 - 无分形 if(Low[cnt+i]<Low[cnt] || Low[cnt-i]<Low[cnt]) return(false); } } //---- return(true); } //+------------------------------------------------------------------+ //| 自定义指标去初始化功能 //+------------------------------------------------------------------+ void OnDeinit(const int reason) { //---- Comment(""); for(int i=0; i<3; i++) { string name="IPGDR_Lv"+string(i); ObjectDelete(0,name); ObjectDelete(0,name+"Up"); ObjectDelete(0,name+"Dw"); ObjectDelete(0,name+"Tx"); } //---- } //+------------------------------------------------------------------+ //| 自定义指标迭代函数 //+------------------------------------------------------------------+ 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[]) { //---- 检查计算所需的条数是否足够 if(rates_total<int(sBars)) return(0); //---- 数组中元素的索引,如时间序列中的索引 ArraySetAsSeries(time,true); ArraySetAsSeries(close,true); ArraySetAsSeries(open,true); ArraySetAsSeries(high,true); ArraySetAsSeries(low,true); lastBars=rates_total; //---- 搜索分形,将其放入数组,但不能超过允许的分形数量。 int sh=0; ArrayInitialize(aData,0); double lastExt=0; int total=int(sBars-2*frNum); int start=int(frNum+1); for(int i=start; i<total; i++) { // if(frMode!=MODE_LOW && upFrac(i,high)) if(frMode!=FRACTAL_MODE::MODE_LOW && upFrac(i,high)) { //---- 添加顶点 [槽模式除外] //---- 在非严格条件下跳过重复的相邻顶点 if(!(frType!=TYPE1 && lastExt>0 && lastExt==high[i])) { aData[sh][0]=high[i]; lastExt=high[i]; sh++; } } if(sh>=240) break; // if(frMode!=MODE_HIGH && dwFrac(i,low)) if(frMode!=FRACTAL_MODE::MODE_HIGH && dwFrac(i,low)) { //---- 添加凹陷[顶点模式除外] //---- 在非严格条件下跳过重复的相邻波谷 if(!frType!=TYPE1 && lastExt>0 && lastExt==low[i]) { aData[sh][0]=low[i]; lastExt=low[i]; sh++; } } if(sh>=240) break; } //---- 确定平均蜡烛价差和相应的公差 double dlt,sHL=0; for(int i=1; i<=int(sBars); i++) sHL+=(high[i]-low[i]); sHL/=sBars; dlt=sHL*dlt_; //---- 为每个级别定义评级 for(int i=0; i<sh; i++) for(int j=i+1; j<sh; j++) if(aData[j][0]>aData[i][0]-dlt && aData[j][0]<aData[i][0]+dlt) { aData[i][1]+=1; aData[j][1]+=1; } //---- 确定三个最强级别 double cur[2],tmp[2]; aRes[0][0]=aData[0][0]; aRes[0][1]=aData[0][1]; for(int i=1; i<sh; i++) { cur[0]=aData[i][0]; cur[1]=aData[i][1]; if(cur[1]>aRes[0][1]) { //---- 如果评级大于等级 1,则等级 1 成为当前评级 tmp[0]=aRes[0][0]; tmp[1]=aRes[0][1]; aRes[0][0]=cur[0]; aRes[0][1]=cur[1]; cur[0]=tmp[0]; cur[1]=tmp[1]; } //---- 如果评分高于第 2 位,但不属于第 1 位,则替换第 2 位 if(cur[1]>aRes[1][1] && (cur[0]<aRes[0][0]-dlt || cur[0]>aRes[0][0]+dlt)) { tmp[0]=aRes[1][0]; tmp[1]=aRes[1][1]; aRes[1][0]=cur[0]; aRes[1][1]=cur[1]; cur[0]=tmp[0]; cur[1]=tmp[1]; } //---- 如果评分高于第 3 名,但不属于第 1 名和第 2 名,则用第 3 名代替。 if(cur[1]>aRes[2][1] && (cur[0]<aRes[0][0]-dlt || cur[0]>aRes[0][0]+dlt) && (cur[0]<aRes[1][0]-dlt || cur[0]>aRes[1][0]+dlt)) { aRes[2][0]=cur[0]; aRes[2][1]=cur[1]; } } for(int i=0; i<3; i++) { double dwL=aRes[i][0]-dlt,upL=aRes[i][0]+dlt; string name="IPGDR_Lv"+string(i); SetChannel(0,name,0,time[24],upL,time[1],upL,time[24],dwL,BGColor[i],STYLE_SOLID,1,name); SetTline(0,name+"Up",0,time[24],upL,time[1],upL,TLColor[i],STYLE_SOLID,1,name+"Up"); SetTline(0,name+"Dw",0,time[24],dwL,time[1],dwL,TLColor[i],STYLE_SOLID,1,name+"Dw"); SetTextLabel(0,name+"Tx",0,time[32],upL+2*_Point,TLColor[i],STYLE_SOLID,1,DoubleToString(aRes[i][0],_Digits),"tahoma",14); } //---- 显示评论 string rem1="",rem2=""; if(frType==TYPE2) rem1=rem1+"经典"; else rem1=rem1+"严格"; rem1=rem1+string(frNum*2+1)+"条状分形"; // if(frMode==MODE_LOW) rem1=rem1+", troughs"; if(frMode==FRACTAL_MODE::MODE_LOW) rem1=rem1+",底裤。"; //else if(frMode==MODE_HIGH) rem1=rem1+", vertex"; else if(frMode==FRACTAL_MODE::MODE_HIGH) rem1=rem1+",鞋面。"; rem1=rem1+"Found."+string(sh)+"分形 \n 最强级别 "; StringConcatenate(rem2,aRes[0][0],"[",aRes[0][1],"], ", aRes[1][0],"[",aRes[1][1],"], ",aRes[2][0],"[",aRes[2][1], "],错误 ±",NormalizeDouble(dlt/_Point,1),"段落"); Comment(rem1+rem2); //---- ChartRedraw(0); //---- return(rates_total); } //+------------------------------------------------------------------+
iGDR_Fractal_Levels:
iGDR_Fractal_Levels 显示覆盖一段确定周期时间的分形均值。
作者: Nikolay Kositsin