您的位置 首页 程序化交易

第三代移动平均线 MT4 MT5指标[MT5公式]

该指标适用于MT4 和 MT5。无需使用任何DLL。 第三代移动平均线 MetaTrader 指标 — 是标准移动平均线(MA)的先进版,在较长MA时段执行简单的减少滞后性的程序。…

该指标适用于MT4 和 MT5。无需使用任何DLL。
第三代移动平均线 MetaTrader 指标 — 是标准移动平均线(MA)的先进版,在较长MA时段执行简单的减少滞后性的程序。这种方法由M. Duerschner首次描述于他的文章Gleitende Durchschnitte 3.0(德语)。当前版设定λ = 2,最大程度的减少了滞后性。较高的λ值则会增加与经典移动平均线的相似性。
这是个MetaTrader指标,用于可调整移动平均线 3G智能交易。
MA_Period (默认 = 50) — 第三代移动平均线的时段。
MA_Method (默认 = 1) — 移动平均线的方法。0 — SMA, 1 — EMA, 2 — SMMA, 3 — LWMA.
MA_Applied_Price (默认 = 5) — 移动平均线适用的价格。0 — PRICE_CLOSE, 1 — PRICE_OPEN, 2 — PRICE_HIGH, 3 — PRICE_LOW, 4 — PRICE_MEDIAN, 5 — PRICE_TYPICAL, 6 — PRICE_WEIGHTED.

正如您看到的,第三代移动平均线(红线)比传统的指数移动平均线(蓝线)滞后性更短,而且能更快的反映出价格的变化。不过,它仍有滞后性而且可能会发出错误的信号。您可以将第三代移动平均线外汇指标作为标准移动平均线使用 — 用以侦测当前趋势的方式。
标签:MT4 MT5指标源码
MT4源码: //+——————————————————————+ //| 3rd Generation MA | //| Copyright � 2011, EarnForex | //| http://www.cxh99.com/ | //+——————————————————————+ #property copyright “http://www.cxh99.com” #property link “http://www.cxh99.com” /* 3rd Generation MA based on research paper by Dr. Mafred Durschner: http://www.vtad.de/node/1441 (in German) Offers least possible lag but still provides price smoothing */ #property indicator_chart_window #property indicator_buffers 2 #property indicator_color1 Red //—- indicator parameters extern int MA_Period = 220; extern int MA_Sampling_Period = 50; // Sampling period should be less than 1/4 of MA_Period extern int MA_Method = 1; //0 – SMA, 1 – EMA, 2 – SMMA, 3 – LWMA extern int MA_Applied_Price = 5; // 0 – PRICE_CLOSE, 1 – PRICE_OPEN, 2 – PRICE_HIGH, 3 – PRICE_LOW, 4 – PRICE_MEDIAN, 5 – PRICE_TYPICAL, 6 – PRICE_WEIGHTED //—- indicator buffers double MA3G[]; double MA1[]; //—- double Lambda, Alpha; //+——————————————————————+ //| Custom indicator initialization function | //+——————————————————————+ int init() { int draw_begin; string short_name; IndicatorBuffers(3); //—- drawing settings SetIndexStyle(0, DRAW_LINE); IndicatorDigits(MarketInfo(Symbol(), MODE_DIGITS)); draw_begin = MA_Period – 1; //—- indicator short name switch(MA_Method) { case 1: short_name=”3GEMA(“; draw_begin=0; break; case 2: short_name=”3GSMMA(“; break; case 3: short_name=”3GLWMA(“; break; default: MA_Method = 0; short_name = “3GSMA(“; } IndicatorShortName(short_name + MA_Period + “)”); SetIndexDrawBegin(0, draw_begin); //—- indicator buffers mapping SetIndexBuffer(0, MA3G); SetIndexBuffer(1, MA1); Lambda = 1.0 * MA_Period / (1.0 * MA_Sampling_Period); Alpha = Lambda * (MA_Period – 1) / (MA_Period – Lambda); Print(“Lambda = “, Lambda, “; Alpha = “, Alpha); //—- initialization done return(0); } //+——————————————————————+ //| 3rd Generation Moving Average Custom Indicator | //+——————————————————————+ int start() { int i; if (MA_Period * 2 < MA_Sampling_Period) { Print(“MA_Period should be >= MA_Sampling_Period * 2.”); return(-1); } if (Bars <= MA_Period) return(0); int ExtCountedBars = IndicatorCounted(); //—- check for possible errors if (ExtCountedBars < 0) return(-1); //—- last counted bar will be recounted if (ExtCountedBars > 0) ExtCountedBars–; if (ExtCountedBars < MA_Period) ExtCountedBars = MA_Period; //—- for (i = Bars – ExtCountedBars – 1; i >= 0; i–) MA1[i] = iMA(NULL, 0, MA_Period, 0, MA_Method, MA_Applied_Price, i); for (i = Bars – ExtCountedBars – 1; i >= 0; i–) { double MA2 = iMAOnArray(MA1, 0, MA_Sampling_Period, 0, MA_Method, i); MA3G[i] = (Alpha + 1) * MA1[i] – Alpha * MA2; } //—- done return(0); } //+——————————————————————+ 点击复制上述代码粘贴到到公式管理器
MT5源码://+——————————————————————+ //| 3rd Generation MA | //| Copyright � 2011, EarnForex | //| http://www.cxh99.com/ | //+——————————————————————+ #property copyright “http://www.cxh99.com” #property link “http://www.cxh99.com” #property version “1.01” #property description “3rd Generation MA based on research paper by Dr. Manfred” #property description “Durschner: http://www.vtad.de/node/1441 (in German).” #property description “Offers least possible lag but still provides price smoothing.” #property indicator_chart_window #property indicator_buffers 2 #property indicator_plots 1 #property indicator_color1 clrRed #property indicator_style1 STYLE_SOLID #property indicator_type1 DRAW_LINE #property indicator_width1 1 //—- indicator parameters input int MA_Period = 50; input ENUM_MA_METHOD MA_Method = MODE_EMA; input ENUM_APPLIED_PRICE MA_Applied_Price = PRICE_TYPICAL; //—- indicator buffers double MA3G[]; double MA[]; //—- double MA2[]; double Lambda, Alpha; int handle, MA_Sampling_Period = 2 * MA_Period; bool FirstRun = true; //+——————————————————————+ //| Custom indicator initialization function | //+——————————————————————+ void OnInit() { string short_name; SetIndexBuffer(0, MA3G, INDICATOR_DATA); SetIndexBuffer(1, MA, INDICATOR_CALCULATIONS); ArraySetAsSeries(MA3G, true); ArraySetAsSeries(MA, true); ArraySetAsSeries(MA2, true); //—- indicator short name switch(MA_Method) { case MODE_EMA: short_name = “3GEMA(“; break; case MODE_SMMA: short_name = “3GSMMA(“; break; case MODE_LWMA: short_name = “3GLWMA(“; break; default: short_name = “3GSMA(“; } IndicatorSetString(INDICATOR_SHORTNAME, short_name + IntegerToString(MA_Period) + “)”); IndicatorSetInteger(INDICATOR_DIGITS, _Digits); handle = iMA(NULL, 0, MA_Sampling_Period, 0, MA_Method, MA_Applied_Price); if (handle == INVALID_HANDLE) { Print(“Failed to initialize Moving Average.”); return; } Lambda = 1.0 * MA_Sampling_Period / (1.0 * MA_Period); Alpha = Lambda * (MA_Sampling_Period – 1) / (MA_Sampling_Period – Lambda); Print(“Lambda = “, Lambda, “; Alpha = “, Alpha); } //+——————————————————————+ //| 3rd Generation Moving Average Custom Indicator | //+——————————————————————+ 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 &tickvolume[], const long &volume[], const int &spread[]) { int i; int TotalPeriod = MA_Sampling_Period + MA_Period; if (rates_total <= TotalPeriod) return(0); // Not enough bars int ExtCountedBars; if (FirstRun) ExtCountedBars = 0; else { ExtCountedBars = prev_calculated; if (ExtCountedBars < 0) return(-1); if ((rates_total – ExtCountedBars < TotalPeriod) || (ExtCountedBars == 0)) ExtCountedBars = rates_total – TotalPeriod; } // +2 to use in iMAOnArrayMQL4() // + MA_Period because we need that amount more to calculate the components of the resulting indicator int MABars = rates_total – ExtCountedBars + MA_Period + 2; if (MABars > rates_total) MABars = rates_total; if (CopyBuffer(handle, 0, 0, MABars, MA) != MABars) return(0); int MAonMABars = rates_total – ExtCountedBars + MA_Period; if (MAonMABars > rates_total) MAonMABars = rates_total; iMAOnArrayMQL4(MA, MAonMABars, MA_Period, 0, MA_Method, 0, MA2); for (i = rates_total – ExtCountedBars – 1; i >= 0; i–) MA3G[i] = (Alpha + 1) * MA[i] – Alpha * MA2[i]; FirstRun = false; return(rates_total); } //+——————————————————————+ //| http://www.mql5.com/en/articles/81 | //+——————————————————————+ double iMAOnArrayMQL4(double &array[], int total, int period, int ma_shift, int ma_method, int shift, double &buf[]) { double arr[]; if(total==0) total=ArraySize(array); if(total>0 && total<=period) return(0); if(shift>total-period-ma_shift) return(0); switch(ma_method) { case MODE_SMA : { total=ArrayCopy(arr,array,0,shift+ma_shift,period); if(ArrayResize(buf,total)<0) return(0); double sum=0; int i,pos=total-1; for(i=1;i<period;i++,pos–) sum+=arr[pos]; while(pos>=0) { sum+=arr[pos]; buf[pos]=sum/period; sum-=arr[pos+period-1]; pos–; } return(buf[0]); } case MODE_EMA : { if(ArrayResize(buf,total)<0) return(0); double pr=2.0/(period+1); int pos=total-2; while(pos>=0) { if(pos==total-2) buf[pos+1]=array[pos+1]; buf[pos]=array[pos]*pr+buf[pos+1]*(1-pr); pos–; } return(buf[shift+ma_shift]); } case MODE_SMMA : { if(ArrayResize(buf,total)<0) return(0); double sum=0; int i,k,pos; pos=total-period; while(pos>=0) { if(pos==total-period) { for(i=0,k=pos;i<period;i++,k++) { sum+=array[k]; buf[k]=0; } } else sum=buf[pos+1]*(period-1)+array[pos]; buf[pos]=sum/period; pos–; } return(buf[shift+ma_shift]); } case MODE_LWMA : { if(ArrayResize(buf,total)<0) return(0); double sum=0.0,lsum=0.0; double price; int i,weight=0,pos=total-1; for(i=1;i<=period;i++,pos–) { price=array[pos]; sum+=price*i; lsum+=price; weight+=i; } pos++; i=pos+period; while(pos>=0) { buf[pos]=sum/weight; if(pos==0) break; pos–; i–; price=array[pos]; sum=sum-lsum+price*period; lsum-=array[i]; lsum+=price; } return(buf[shift+ma_shift]); } default: return(0); } return(0); } //+——————————————————————+ 点击复制上述代码粘贴到到公式管理器
本文来自网络,不代表隽焱网立场,转载请注明出处:https://www.780008.com/future/%e7%a8%8b%e5%ba%8f%e5%8c%96%e4%ba%a4%e6%98%93/55415.html

作者: junyanwang

为您推荐

发表评论

联系我们

联系我们

在线咨询: QQ交谈

邮箱: 2265783423@qq.com

工作时间:周一至周五,9:00-17:30,节假日休息

关注微信
微信扫一扫关注我们

微信扫一扫关注我们

返回顶部