//+------------------------------------------------------------------+ //| MA of RSI.mq4 | //| Copyright © 2008, RoboFX | //| http://www.robofx.org | //+------------------------------------------------------------------+ #property copyright "Copyright © 2008, RoboFX" #property link "http://www.robofx.org" #property indicator_separate_window #property indicator_minimum 0 #property indicator_maximum 100 #property indicator_buffers 6 #property indicator_color1 SteelBlue #property indicator_color2 Red extern int FastPeriod = 13; extern int SlowPeriod = 25; extern int MAmethod = 0; int BuyLevel = 20; int SellLevel = 80; //Buffers double FastBuffer[]; double SlowBuffer[]; //Additional buffers double FastPosBuffer[]; double FastNegBuffer[]; double SlowPosBuffer[]; double SlowNegBuffer[]; //+------------------------------------------------------------------+ //| Custom indicator initialization function | //+------------------------------------------------------------------+ int init() { //---- indicators IndicatorBuffers(6); SetIndexBuffer(0, FastBuffer); SetIndexBuffer(1, SlowBuffer); SetIndexBuffer(2, FastPosBuffer); SetIndexBuffer(3, FastNegBuffer); SetIndexBuffer(4, SlowPosBuffer); SetIndexBuffer(5, SlowNegBuffer); SetIndexStyle(0, DRAW_LINE, STYLE_SOLID, 1); SetIndexStyle(1, DRAW_LINE, STYLE_SOLID, 1); SetIndexDrawBegin(0, FastPeriod); SetIndexDrawBegin(1, SlowPeriod); SetIndexLabel(0, "Fast line"); SetIndexLabel(1, "Slow line"); SetIndexLabel(2, NULL); SetIndexLabel(3, NULL); SetIndexLabel(4, NULL); SetIndexLabel(5, NULL); IndicatorShortName("MA of RSI"); //---- return(0); } //+------------------------------------------------------------------+ //| Custom indicator deinitialization function | //+------------------------------------------------------------------+ int deinit() { //---- //---- return(0); } //+------------------------------------------------------------------+ //| Custom indicator iteration function | //+------------------------------------------------------------------+ int start() { int counted_bars=IndicatorCounted(); int i; double RelativeFast, PositiveFast, NegativeFast; double RelativeSlow, PositiveSlow, NegativeSlow; //---- SetLevelValue(0, BuyLevel); SetLevelValue(1, SellLevel); SetLevelStyle(STYLE_DOT, 1, LightBlue); if(Bars<=SlowPeriod) { Print("Not enough bars for running this indicator!"); return(0); } // First run if(counted_bars<1) { for(i=1;i<=FastPeriod;i++) FastBuffer[Bars-i]=0.0; for(i=1;i<=SlowPeriod;i++) SlowBuffer[Bars-i]=0.0; } i=Bars-counted_bars-1; while(i>=0) { double FastPositiveSum=0.0, FastNegativeSum=0.0; double SlowPositiveSum=0.0, SlowNegativeSum=0.0; // First run if(i==Bars-FastPeriod-1) { int j=Bars-2; while(j>=i) { RelativeFast=iMA(Symbol(), 0, FastPeriod, 0, MAmethod, PRICE_CLOSE, j)-iMA(Symbol(), 0, FastPeriod, 0, MAmethod, PRICE_CLOSE, j+1); if(RelativeFast>0) FastPositiveSum+=RelativeFast; else FastNegativeSum-=RelativeFast; j--; } PositiveFast=FastPositiveSum/FastPeriod; NegativeFast=FastNegativeSum/FastPeriod; } if(i==Bars-SlowPeriod-1) { int k=Bars-2; while(k>=i) { RelativeSlow=iMA(Symbol(), 0, SlowPeriod, 0, MAmethod, PRICE_CLOSE, k)-iMA(Symbol(), 0, SlowPeriod, 0, MAmethod, PRICE_CLOSE, k+1); if(RelativeSlow>0) SlowPositiveSum+=RelativeSlow; else SlowNegativeSum-=RelativeSlow; k--; } PositiveSlow=SlowPositiveSum/SlowPeriod; NegativeSlow=SlowNegativeSum/SlowPeriod; } RelativeFast=iMA(Symbol(), 0, FastPeriod, 0, MAmethod, PRICE_CLOSE, i)-iMA(Symbol(), 0, FastPeriod, 0, MAmethod, PRICE_CLOSE, i+1); if(RelativeFast>0) FastPositiveSum+=RelativeFast; else FastNegativeSum-=RelativeFast; RelativeSlow=iMA(Symbol(), 0, SlowPeriod, 0, MAmethod, PRICE_CLOSE, i)-iMA(Symbol(), 0, SlowPeriod, 0, MAmethod, PRICE_CLOSE, i+1); if(RelativeSlow>0) SlowPositiveSum+=RelativeSlow; else SlowNegativeSum-=RelativeSlow; PositiveFast=(FastPosBuffer[i+1]*(FastPeriod-1)+FastPositiveSum)/FastPeriod; PositiveSlow=(SlowPosBuffer[i+1]*(SlowPeriod-1)+SlowPositiveSum)/SlowPeriod; NegativeFast=(FastNegBuffer[i+1]*(FastPeriod-1)+FastNegativeSum)/FastPeriod; NegativeSlow=(SlowNegBuffer[i+1]*(SlowPeriod-1)+SlowNegativeSum)/SlowPeriod; FastPosBuffer[i]=PositiveFast; SlowPosBuffer[i]=PositiveSlow; FastNegBuffer[i]=NegativeFast; SlowNegBuffer[i]=NegativeSlow; if(NegativeFast==0.0) FastBuffer[i]=0.0; else { FastBuffer[i]=100.0-100.0/(1+PositiveFast/NegativeFast); } if(NegativeSlow==0.0) SlowBuffer[i]=0.0; else { SlowBuffer[i]=100.0-100.0/(1+PositiveSlow/NegativeSlow); } i--; } //---- return(0); } //+------------------------------------------------------------------+