Indicators: Deviation Rate BIAS MT4

 

Deviation Rate BIAS MT4:

Stock trading indicators. BIAS.

Author: Hung Wen Lin

 
 

//+------------------------------------------------------------------+

//|                                                         BIAS.mq5|

//|                                                        Submarine |

//|                                      WeChart:ExpertAdvisorTrader |

//+------------------------------------------------------------------+

#property copyright "Submarine"

#property link      "https://www.mql5.com/en/users/carllin000"

#property version   "3.00"

#property indicator_buffers 3

#property indicator_plots  3

#property indicator_separate_window

#define EMPTY -1


double         LongBuffer[];

double         MidBuffer[];

double         ShortBuffer[];


ENUM_TIMEFRAMES InPeriod = PERIOD_CURRENT;

input int   InLong  = 6;

input color ClrLong = clrRed;

input int   InMid   = 12;

input color ClrMid  = clrYellow;

input int   InShort = 24;

input color ClrShort= clrGreen;


int ControlBars = 0;


//+------------------------------------------------------------------+

//| Custom indicator initialization function                         |

//+------------------------------------------------------------------+

int OnInit()

  {

  EventSetTimer(1);

//--- indicator buffers mapping

    SetIndexBuffer(0,LongBuffer,INDICATOR_DATA);

    SetIndexBuffer(1,MidBuffer,INDICATOR_DATA);

    SetIndexBuffer(2,ShortBuffer,INDICATOR_DATA);

    SetIndexStyleMQL4(0,DRAW_LINE,0,1,ClrLong);

    SetIndexStyleMQL4(1,DRAW_LINE,0,1,ClrMid);

    SetIndexStyleMQL4(2,DRAW_LINE,0,1,ClrShort);

//---

   return(INIT_SUCCEEDED);

  }

//+------------------------------------------------------------------+

//| 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[])

 {

    ArraySetAsSeries(time,true);

    ArraySetAsSeries(open,true);

    ArraySetAsSeries(high,true);

    ArraySetAsSeries(low,true);

    ArraySetAsSeries(close,true);

    ArraySetAsSeries(tick_volume,true);

    ArraySetAsSeries(volume,true);

    ArraySetAsSeries(spread,true);

    ArraySetAsSeries(LongBuffer,true);

    ArraySetAsSeries(MidBuffer,true);

    ArraySetAsSeries(ShortBuffer,true);


//---

    //RefreshRates();

    ChartRedraw();

   

   int BarsWind;

   if (ControlBars!=0) BarsWind=ControlBars; else BarsWind=ChartGetInteger(0,CHART_FIRST_VISIBLE_BAR,0);

    for(int i=BarsWind; i>=0; i--)

       {

        LongBuffer[i]  = ((close[i]-iMAMQL4(NULL,InPeriod,InLong,0,0,0,i)) / (iMAMQL4(NULL,InPeriod,InLong,0,0,0,i)) *100);

        MidBuffer[i]   = ((close[i]-iMAMQL4(NULL,InPeriod,InMid,0,0,0,i))  / (iMAMQL4(NULL,InPeriod,InMid,0,0,0,i))  *100);

        ShortBuffer[i] = ((close[i]-iMAMQL4(NULL,InPeriod,InShort,0,0,0,i))/ (iMAMQL4(NULL,InPeriod,InShort,0,0,0,i))*100);

        

       /*

       BIAS1 : (CLOSE-MA(close,L1))/MA(close,L1)*100;

       BIAS2 : (CLOSE-MA(close,L2))/MA(close,L2)*100;

       BIAS3 : (CLOSE-MA(close,L3))/MA(close,L3)*100;

       */

       }

   

//--- return value of prev_calculated for next call

    return(rates_total);

  }


void SetIndexStyleMQL4(int index,  int type, int style=EMPTY, int width=EMPTY, color clr=CLR_NONE)

{

   if(width>-1)

      PlotIndexSetInteger(index,PLOT_LINE_WIDTH,width);

   if(clr!=CLR_NONE)

      PlotIndexSetInteger(index,PLOT_LINE_COLOR,clr);

   switch(type)

     {

      case 0:

         PlotIndexSetInteger(index,PLOT_DRAW_TYPE,DRAW_LINE);

      case 1:

         PlotIndexSetInteger(index,PLOT_DRAW_TYPE,DRAW_SECTION);

      case 2:

         PlotIndexSetInteger(index,PLOT_DRAW_TYPE,DRAW_HISTOGRAM);

      case 3:

         PlotIndexSetInteger(index,PLOT_DRAW_TYPE,DRAW_ARROW);

      case 4:

         PlotIndexSetInteger(index,PLOT_DRAW_TYPE,DRAW_ZIGZAG);

      case 12:

         PlotIndexSetInteger(index,PLOT_DRAW_TYPE,DRAW_NONE);


      default:

         PlotIndexSetInteger(index,PLOT_DRAW_TYPE,DRAW_LINE);

     }

   switch(style)

     {

      case 0:

         PlotIndexSetInteger(index,PLOT_LINE_STYLE,STYLE_SOLID);

      case 1:

         PlotIndexSetInteger(index,PLOT_LINE_STYLE,STYLE_DASH);

      case 2:

         PlotIndexSetInteger(index,PLOT_LINE_STYLE,STYLE_DOT);

      case 3:

         PlotIndexSetInteger(index,PLOT_LINE_STYLE,STYLE_DASHDOT);

      case 4:

         PlotIndexSetInteger(index,PLOT_LINE_STYLE,STYLE_DASHDOTDOT);


      default: return;

     }

}


double iMAMQL4(string symbol,

               int tf,

               int period,

               int ma_shift,

               int method,

               int price,

               int shift)

  {

   ENUM_TIMEFRAMES timeframe=TFMigrate(tf);

   ENUM_MA_METHOD ma_method=MethodMigrate(method);

   ENUM_APPLIED_PRICE applied_price=PriceMigrate(price);

   int handle=iMA(symbol,timeframe,period,ma_shift,

                  ma_method,applied_price);

   if(handle<0)

     {

      Print("The iMA object is not created: Error",GetLastError());

      return(-1);

     }

   else

      return(CopyBufferMQL4(handle,0,shift));

  }


ENUM_TIMEFRAMES TFMigrate(int tf)

  {

   switch(tf)

     {

      case 0: return(PERIOD_CURRENT);

      case 1: return(PERIOD_M1);

      case 5: return(PERIOD_M5);

      case 15: return(PERIOD_M15);

      case 30: return(PERIOD_M30);

      case 60: return(PERIOD_H1);

      case 240: return(PERIOD_H4);

      case 1440: return(PERIOD_D1);

      case 10080: return(PERIOD_W1);

      case 43200: return(PERIOD_MN1);

      

      case 2: return(PERIOD_M2);

      case 3: return(PERIOD_M3);

      case 4: return(PERIOD_M4);      

      case 6: return(PERIOD_M6);

      case 10: return(PERIOD_M10);

      case 12: return(PERIOD_M12);

 //    case 16385: return(PERIOD_H1);

       case 16386: return(PERIOD_H2);

      case 16387: return(PERIOD_H3);

      case 16388: return(PERIOD_H4);

      case 16390: return(PERIOD_H6);

      case 16392: return(PERIOD_H8);

      case 16396: return(PERIOD_H12);

      case 16408: return(PERIOD_D1);

      case 32769: return(PERIOD_W1);

      case 49153: return(PERIOD_MN1);      

      default: return(PERIOD_CURRENT);

     }

  }


double CopyBufferMQL4(int handle,int index,int shift)

  {

   double buf[];

   switch(index)

     {

      case 0: if(CopyBuffer(handle,0,shift,1,buf)>0)

         return(buf[0]); break;

      case 1: if(CopyBuffer(handle,1,shift,1,buf)>0)

         return(buf[0]); break;

      case 2: if(CopyBuffer(handle,2,shift,1,buf)>0)

         return(buf[0]); break;

      case 3: if(CopyBuffer(handle,3,shift,1,buf)>0)

         return(buf[0]); break;

      case 4: if(CopyBuffer(handle,4,shift,1,buf)>0)

         return(buf[0]); break;

      default: break;

     }

   return(EMPTY_VALUE);

  }


ENUM_MA_METHOD MethodMigrate(int method)

  {

   switch(method)

     {

      case 0: return(MODE_SMA);

      case 1: return(MODE_EMA);

      case 2: return(MODE_SMMA);

      case 3: return(MODE_LWMA);

      default: return(MODE_SMA);

     }

  }


ENUM_APPLIED_PRICE PriceMigrate(int price)

  {

   switch(price)

     {

      case 1: return(PRICE_CLOSE);

      case 2: return(PRICE_OPEN);

      case 3: return(PRICE_HIGH);

      case 4: return(PRICE_LOW);

      case 5: return(PRICE_MEDIAN);

      case 6: return(PRICE_TYPICAL);

      case 7: return(PRICE_WEIGHTED);

      default: return(PRICE_CLOSE);

     }

  }


ENUM_STO_PRICE StoFieldMigrate(int field)

  {

   switch(field)

     {

      case 0: return(STO_LOWHIGH);

      case 1: return(STO_CLOSECLOSE);

      default: return(STO_LOWHIGH);

     }

  }


enum ALLIGATOR_MODE  { MODE_GATORJAW=1,   MODE_GATORTEETH, MODE_GATORLIPS };

enum UP_LOW_MODE     { MODE_BASE,         MODE_UPPER,      MODE_LOWER };

enum ICHIMOKU_MODE   { MODE_TENKANSEN=1,  MODE_KIJUNSEN, MODE_SENKOUSPANA, MODE_SENKOUSPANB, MODE_CHINKOUSPAN };

enum BASE_IND_MODE { _MODE_MAIN, MODE_PLUSDI, MODE_MINUSDI,  MODE_SIGNAL };


Reason: