RSI_7TF.1.2

Author: Copyright 2015, Tor
8 Views
0 Downloads
0 Favorites
RSI_7TF.1.2
ÿþ//+------------------------------------------------------------------+

//|                                                      RSI_7TF.mq4 |

//|                                              Copyright 2016, Tor |

//|                                             http://einvestor.ru/ |

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

#property copyright "Copyright 2015, Tor"

#property link      "http://einvestor.ru/"

#property version   "1.2"

#property strict

#property description "This indicator is based on standard indicator Relative Strength Index"

#property description "from 7 timeframes, and makes recommendations for trade, arrows, alerts, notifications"

//#property icon      "\\Images\\rsi-tf7.ico"; 



#property indicator_chart_window

#property indicator_buffers 13

#property indicator_plots   2



input int barlimit=1000;

// Limiting the number of bars

input int RSIPeriod=14;

// RSI Period

input int BarShift=1;//Bar shift (0=current)

input bool alerts=false;

// Enable alerts



input bool alertReversSignal=true;

// Alert when changing trend

enum TypeFR

  {

   DeltaRSI  = 0,    // RSI Trend for 1 bar

   DeltaRSI2 = 1,    // RSI Trend for 2 bar

   Znachenie = 2,    // OverSell / OverBuy

   Chena     = 3,    // Price difference

   Crossing  = 4,    // Crossing line

   TrendCross = 5,   // Trend + Crossing line

  };

//--- input parameters

input TypeFR TypePostroenia=DeltaRSI; // Mode 



input bool M5Arrows=true; // Draw M5 Arrows

input bool M15Arrows = true; // Draw M15 Arrows

input bool M30Arrows = true; // Draw M30 Arrows

input bool H1Arrows = true; // Draw H1 Arrows

input bool H4Arrows = true; // Draw H4 Arrows

input bool D1Arrows = true; // Draw D1 Arrows

input bool W1Arrows = true; // Draw W1 Arrows



double increaser=1.2; // Sensitivity (1 - 1.5)

input color BuyClr=clrBlue;

input color SellClr=clrRed;



double         Buffer[];

double         Buffer1[];

double         Buffer2[];

double         Buffer3[];

double         Buffer4[];

double         Buffer5[];

double         Buffer6[];

double         Buffer7[];

double         BufferMA[];

double         BufferMA2[];

double         BufferBS[];

double BufferBuy[];

double BufferSell[];



int TF[6];

int Maperiod=5;

int alertLevel1=90; // Signal level

int alertLevel2=70; // Signal level

int alertLevel = 0;

int sto=100;

int Maperiod2=26;



bool ArrowTrue[6];

static datetime lastsig[6];

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

//| Custom indicator initialization function                         |

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

int OnInit()

  {

//--- indicator buffers mapping



   SetIndexBuffer(0,Buffer);

   SetIndexBuffer(1,Buffer1);

   SetIndexBuffer(2,Buffer2);

   SetIndexBuffer(3,Buffer3);

   SetIndexBuffer(4,Buffer4);

   SetIndexBuffer(5,Buffer5);

   SetIndexBuffer(6,Buffer6);

   SetIndexBuffer(7,Buffer7);

   SetIndexBuffer(8,BufferMA);

   SetIndexBuffer(9,BufferMA2);

   SetIndexBuffer(10,BufferBS);

   SetIndexBuffer(11,BufferBuy);

   SetIndexBuffer(12,BufferSell);



   IndicatorShortName("RSI 7TF");



   SetIndexStyle(0,DRAW_NONE);// 8B>3>2>5 7=0G5=85

   SetIndexStyle(1,DRAW_NONE);//TF1

   SetIndexStyle(2,DRAW_NONE);//TF2

   SetIndexStyle(3,DRAW_NONE);//TF3

   SetIndexStyle(4,DRAW_NONE);//TF4

   SetIndexStyle(5,DRAW_NONE);//TF5

   SetIndexStyle(6,DRAW_NONE);//TF6

   SetIndexStyle(7,DRAW_NONE);//TF7

   SetIndexStyle(8,DRAW_NONE);//Ma

   SetIndexStyle(9,DRAW_NONE);//Ma2

   SetIndexStyle(10,DRAW_NONE);//   Buy / Sell signal

   SetIndexStyle(11,DRAW_ARROW,STYLE_SOLID,1,BuyClr);//Buy

   SetIndexStyle(12,DRAW_ARROW,STYLE_SOLID,1,SellClr);//Sell



   SetIndexArrow(11,233);

   SetIndexArrow(12,234);

   SetIndexLabel(11,"Buy");

   SetIndexLabel(12,"Sell");



   TF[0] = 5;

   TF[1] = 15;

   TF[2] = 30;

   TF[3] = 60;

   TF[4] = 240;

   TF[5] = 1440;



   ArrowTrue[0] = M5Arrows;

   ArrowTrue[1] = M15Arrows;

   ArrowTrue[2] = M30Arrows;

   ArrowTrue[3] = H1Arrows;

   ArrowTrue[4] = H4Arrows;

   ArrowTrue[5] = D1Arrows;



   ArrayResize(TF,7);

   ArrayResize(lastsig,7);

   ArrayResize(ArrowTrue,7);

   TF[6]=10080;

   ArrowTrue[6]=W1Arrows;

   ArrayInitialize(lastsig,0);



   for(int tfp=0; tfp<ArrayRange(TF,0);tfp++)

     {

      if(iTime(Symbol(),TF[tfp],100)>0)

        {

         //Print("History "+(string)TF[tfp]+" "+Symbol()+" ok");

           }else{

         Print("History "+(string)TF[tfp]+" "+Symbol()+" loading");

        }

     }



   if(TypePostroenia==2 || TypePostroenia==4)

     {

      alertLevel=alertLevel2;

        }else{

      alertLevel=alertLevel1;

     }



//---

   return(INIT_SUCCEEDED);

  }

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

//|                                                                  |

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

int deinit()

  {

//del("Sin_");

   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 i,limit; static bool alertTF=false;



   if(alertTF){ return(0); }

   if(_Period>10080){ alertTF=true; Alert("Max TF W1, please change TF"); return(0); }

//---

   if(rates_total<=0)

      return(0);

//--- last counted bar will be recounted

   string com=""; static string LastSignal=""; static string type5="";

   static bool alertTrue=false; static datetime alertTime=0;

   if(alertTrue && alertTime<Time[0]){ alertTrue=false; }



   int seletedTF;

   for(int chtf=0; chtf<ArrayRange(TF,0);chtf++)

     {

      if(TF[chtf]==_Period){ seletedTF=chtf; }

     }

//if(_Period!=5){ Alert("5@5:;NG8B5 "09<$@59< "+Symbol()+" =0 5"); }

   limit=rates_total-prev_calculated;

   if(prev_calculated>0)

      limit++;



   if(barlimit>0 && prev_calculated==0){ limit=barlimit; }

   if(barlimit>rates_total){ limit=rates_total; }

//if(limit<1){ limit = 1; }else{ limit=limit; }

   if(limit>rates_total){ limit=rates_total; }else{ limit=limit; }

//com = com + "prev_calculated="+prev_calculated+"  limit="+limit+" \n";

   double tmpSinf; int cnt;

   static datetime lastFR[7]; ArrayInitialize(lastFR,0); static int LastFRZnak[7]; ArrayInitialize(LastFRZnak,0);



   for(i=limit-1; i>=0; i--)

     {

      BufferBS[i]=0; type5="";

      cnt=0; tmpSinf=0;

      double a=2;

      for(int tf0=0; tf0<ArrayRange(TF,0); tf0++)

        {

         double sinf=0; double c=0;  double b;

         double Tfsinf=0;



         int shiftFF=iBarShift(Symbol(),TF[tf0],iTime(Symbol(),TF[seletedTF],i));//>B>A8B5;L=> 2K1@0==>3> TF

         if(shiftFF<BarShift){ shiftFF=BarShift; }

         double rsi0 = iRSI(Symbol(), TF[tf0], RSIPeriod, PRICE_CLOSE, shiftFF);

         double rsi1 = iRSI(Symbol(), TF[tf0], RSIPeriod, PRICE_CLOSE, shiftFF+1);

         double rsi2 = iRSI(Symbol(), TF[tf0], RSIPeriod, PRICE_CLOSE, shiftFF+2);

         datetime optime=iTime(Symbol(),TF[tf0],shiftFF); // 2@5<O >B:@KB8O =0 MB>9 A25G5



         double DeltaClose=0;



         //com = com+TF[tf0]+"   a="+a+"   b="+b+"   c="+c+"  sin="+sinf+"\n";



         if(TypePostroenia==0)

           {

            a=1;

            DeltaClose=rsi0-rsi1;

            b = MathAbs(DeltaClose);

            c = MathSqrt(MathPow(a,2)+MathPow(b,2));

            if(c==0.0 || b==0.0 || a==0.0){ continue; }

            sinf=(b/c);

            if(rsi0>rsi1)

              {

               Tfsinf=sto*sinf;

              }

            if(rsi0<rsi1)

              {

               Tfsinf=-sinf*sto;

              }

            Tfsinf=increaser*Tfsinf;

           }

         if(TypePostroenia==1)

           {

            DeltaClose=rsi0-rsi2;

            a =2;

            b = MathAbs(DeltaClose);

            c = MathSqrt(MathPow(a,2)+MathPow(b,2));

            if(c==0.0 || b==0.0 || a==0.0){ continue; }

            sinf=(b/c);

            if(rsi0>rsi2)

              {

               Tfsinf=sto*sinf;

              }

            if(rsi0<rsi2)

              {

               Tfsinf=-sinf*sto;

              }

            Tfsinf=increaser*Tfsinf;

           }



         if(TypePostroenia==2)

           {

            Tfsinf=(rsi0-50)*5;

            //Tfsinf = Tfsinf+0.2*Tfsinf;

           }

         if(TypePostroenia==3)

           {

            a=2;

            DeltaClose=iClose(Symbol(),TF[tf0],shiftFF)-iClose(Symbol(),TF[tf0],shiftFF+2);

            b = MathAbs(DeltaClose)/_Point;

            c = MathSqrt(MathPow(a,2)+MathPow(b,2));

            if(c==0.0 || b==0.0 || a==0.0){ continue; }

            sinf=(b/c);

            if(DeltaClose>0)

              {

               Tfsinf=sto*sinf;

              }

            if(DeltaClose<0)

              {

               Tfsinf=-sinf*sto;

              }

            Tfsinf=increaser*Tfsinf;

           }

         if(TypePostroenia==4)

           {

            Tfsinf=(rsi0-50)*2;

            double levelUp   = alertLevel;

            double levelDown = 50-(alertLevel-50);

            int asize=tf0-1; if(asize<1){ asize=1; }

            if(asize>1 && asize<=4){ asize=2; }

            if(asize>4){ asize=3; }

            if(rsi0>levelDown && rsi1<=levelDown && lastsig[tf0]!=optime)

              {

               //StrelkaVverh(iTime(Symbol(),0,i),iLow(Symbol(),0,i),IntegerToString(TF[tf0]),asize);

               lastsig[tf0]=optime;

               if(ArrowTrue[tf0]){ BufferBuy[i]=iLow(Symbol(),0,i); }

               LastSignal="Buy";

               BufferBS[i]=1;

               if(i==0 && alerts){  Alert("Signal RSI "+ReturnTF(TF[tf0])+" - BUY "+Symbol()); }

              }

            if(rsi0<levelUp && rsi1>=levelUp && lastsig[tf0]!=optime)

              {

               lastsig[tf0]=optime;

               if(ArrowTrue[tf0]){ BufferSell[i]=iHigh(Symbol(),0,i); }

               LastSignal="Sell";

               BufferBS[i]=-1;

               if(i==0 && alerts){  Alert("Signal RSI "+ReturnTF(TF[tf0])+" - SELL "+Symbol()); }

              }

           }



         if(TypePostroenia==5)

           {

            a=1;

            DeltaClose=rsi0-rsi1;

            b = MathAbs(DeltaClose);

            c = MathSqrt(MathPow(a,2)+MathPow(b,2));

            if(c==0.0 || b==0.0 || a==0.0){ continue; }

            sinf=(b/c);

            if(rsi0>rsi1)

              {

               Tfsinf=sto*sinf;

              }

            if(rsi0<rsi1)

              {

               Tfsinf=-sinf*sto;

              }

            Tfsinf=increaser*Tfsinf;

            //------------------------------

            double levelUp   = 70;

            double levelDown = 50-(70-50);

            //int asize = tf0-1; if(asize<1){ asize = 1; }

            //if(asize>1 && asize<=4){ asize = 2; }

            //if(asize>4){ asize = 3; }

            if(rsi0>levelDown && (rsi1<=levelDown))

              {

               if(ArrowTrue[tf0] && lastsig[tf0]!=optime){ type5="Buy"; lastsig[tf0]=optime; }

              }

            if(rsi0<levelUp && (rsi1>=levelUp))

              {

               if(ArrowTrue[tf0] && lastsig[tf0]!=optime){ type5="Sell"; lastsig[tf0]=optime; }

              }

           }



         if(Tfsinf>110){ Tfsinf=110; }

         if(Tfsinf<-110){ Tfsinf=-110; }

         tmpSinf=tmpSinf+Tfsinf;

         //Print(i+"  LastFRZnak[tf0]="+LastFRZnak[tf0]+"   tf="+TF[tf0]+"  lastFR[tf0]="+lastFR[tf0]);

         if(tf0==0) Buffer1[i] = Tfsinf;

         if(tf0==1) Buffer2[i] = Tfsinf;

         if(tf0==2) Buffer3[i] = Tfsinf;

         if(tf0==3) Buffer4[i] = Tfsinf;

         if(tf0==4) Buffer5[i] = Tfsinf;

         if(tf0==5) Buffer6[i] = Tfsinf;

         if(tf0==6) Buffer7[i] = Tfsinf;

        }

      double prebuf=tmpSinf/(ArrayRange(TF,0));

      if(prebuf>110){ prebuf=110; }

      if(prebuf<-110){ prebuf=-110; }

      Buffer[i]=prebuf;



      if(prebuf>=alertLevel && TypePostroenia<4)

        {

         //StrelkaVverh(iTime(Symbol(),0,i),iLow(Symbol(),0,i),"",2);

         LastSignal="Buy";

         BufferBS[i]=1;

         BufferBuy[i]=iLow(Symbol(),0,i);

        }

      if(prebuf<=-alertLevel && TypePostroenia<4)

        {

         //StrelkaVniz(iTime(Symbol(),0,i),iHigh(Symbol(),0,i),"",2);

         BufferSell[i]=iHigh(Symbol(),0,i);

         LastSignal="Sell";

         BufferBS[i]=-1;

        }



      if(prebuf>=alertLevel && TypePostroenia==5)

        {

         LastSignal="Buy";

        }

      if(prebuf<=-alertLevel && TypePostroenia==5)

        {

         LastSignal="Sell";

        }

      if(TypePostroenia==5 && LastSignal=="Sell" && type5=="Sell")

        {

         //StrelkaVniz(iTime(Symbol(),0,i),iHigh(Symbol(),0,i),"",2);

         BufferSell[i]=iHigh(Symbol(),0,i);

         BufferBS[i]=-1;

        }

      if(TypePostroenia==5 && LastSignal=="Buy" && type5=="Buy")

        {

         //StrelkaVverh(iTime(Symbol(),0,i),iLow(Symbol(),0,i),"",2);

         BufferBS[i]=1;

         BufferBuy[i]=iLow(Symbol(),0,i);

        }



     }

//Comment(TimeCurrent()," ",com);



   for(int x=limit-1; x>=0; x--)

     {

      if(barlimit>0 && (barlimit-Maperiod)<x)

        {

         BufferMA[x]=EMPTY_VALUE;

           }else{

         BufferMA[x]=iMAOnArray(Buffer,0,Maperiod,0,MODE_LWMA,x);

        }

     }



   for(int x2=limit-1; x2>=0; x2--)

     {

      if(barlimit>0 && (barlimit-Maperiod2)<x2)

        {

         BufferMA2[x2]=EMPTY_VALUE;

           }else{

         BufferMA2[x2]=iMAOnArray(Buffer,0,Maperiod2,0,MODE_LWMA,x2);

        }

     }



   if(alerts && BufferMA[0]>alertLevel && BufferMA[1]>alertLevel && TypePostroenia<4)

     {

      Alert("Signal RSI TF7 - BUY "+Symbol());

     }

   if(alerts && BufferMA[0]<-alertLevel && BufferMA[1]<-alertLevel && TypePostroenia<4)

     {

      Alert("Signal RSI TF7 - SELL "+Symbol());

     }



   if(BufferMA[0]>=alertLevel && TypePostroenia<4)

     {

      //StrelkaVverh(iTime(Symbol(),0,0),iLow(Symbol(),0,0),"",2);

      BufferBuy[0]=iLow(Symbol(),0,0);

      if(alertReversSignal && LastSignal=="Sell"){ Alert("RSI TF7 "+Symbol()+" revers signal to BUY"); }

     }

   if(BufferMA[0]<=-alertLevel && TypePostroenia<4)

     {

      //StrelkaVniz(iTime(Symbol(),0,0),iHigh(Symbol(),0,0),"",2);

      BufferSell[0]=iHigh(Symbol(),0,0);

      if(alertReversSignal && LastSignal=="Buy"){ Alert("RSI TF7 "+Symbol()+" revers signal to Sell"); }

     }

   ObjectCreate(0,"lastsignal",OBJ_LABEL,ChartWindowFind(),TimeCurrent(),0);

   ObjectSetString(0,"lastsignal",OBJPROP_TEXT,"Last signal "+LastSignal);

   ObjectSetInteger(0,"lastsignal",OBJPROP_FONTSIZE,0,20);

   if(LastSignal=="Sell")

     {

      ObjectSetInteger(0,"lastsignal",OBJPROP_COLOR,0,clrRed);

      //BufferBS[0] = -1;

     }

   if(LastSignal=="Buy")

     {

      ObjectSetInteger(0,"lastsignal",OBJPROP_COLOR,0,clrBlue);

      //BufferBS[0] = 1;

     }

   ObjectSetInteger(0,"lastsignal",OBJPROP_CORNER,0,CORNER_RIGHT_UPPER);

   ObjectSetInteger(0,"lastsignal",OBJPROP_ANCHOR,ANCHOR_CENTER);



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

   return(rates_total);

  }

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

void StrelkaVverh(datetime Time0,double Price0,string Comm,int size)

  {

   string tick=IntegerToString(Time0);

   ObjectCreate(0,"Sin_UP_"+tick+"_"+Comm,OBJ_ARROW,0,Time0,Price0);

   ObjectSetInteger(0,"Sin_UP_"+tick+"_"+Comm,OBJPROP_WIDTH,size);

   ObjectSetString(0,"Sin_UP_"+tick+"_"+Comm,OBJPROP_TOOLTIP,"Buy TF "+Comm);

   ObjectSetInteger(0,"Sin_UP_"+tick+"_"+Comm,OBJPROP_COLOR,clrBlue);

   ObjectSetInteger(0,"Sin_UP_"+tick+"_"+Comm,OBJPROP_ARROWCODE,233);

  }

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

//|                                                                  |

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

void StrelkaVniz(datetime Time0,double Price0,string Comm,int size)

  {

   string tick=IntegerToString(Time0);

   ObjectCreate(0,"Sin_Down_"+tick+"_"+Comm,OBJ_ARROW,0,Time0,Price0);

   ObjectSetInteger(0,"Sin_Down_"+tick+"_"+Comm,OBJPROP_WIDTH,size);

   ObjectSetString(0,"Sin_Down_"+tick+"_"+Comm,OBJPROP_TOOLTIP,"Sell TF "+Comm);

   ObjectSetInteger(0,"Sin_Down_"+tick+"_"+Comm,OBJPROP_COLOR,clrRed);

   ObjectSetInteger(0,"Sin_Down_"+tick+"_"+Comm,OBJPROP_ARROWCODE,234);

  }

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

//|                                                                  |

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

int del(string name)

  {

   for(int n=ObjectsTotal()-1; n>=0; n--)

     {

      string Obj_Name=ObjectName(n);

      if(StringFind(Obj_Name,name,0)!=-1)

        {

         ObjectDelete(Obj_Name);

        }

     }

   return 0;

  }

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

string ReturnTF(int tfw=0)

  {

   switch(tfw)

     {

      case 1:

         return "M1";

         break;

      case 5:

         return "M5";

         break;

      case 15:

         return "M15";

         break;

      case 30:

         return "M30";

         break;

      case 60:

         return "H1";

         break;

      case 240:

         return "H4";

         break;

      case 1440:

         return "D1";

         break;

      case 10080:

         return "W1";

         break;

     }

   return "";

  }

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

Comments