Navigation´╝ÜHome > Content >

Multi_pair_stochastic_CG.mq4

Time: 2017-01-28 | Download file:Multi_pair_stochastic_CG.mq4

//------------------------------------------------------------------
#property copyright "mladen"
#property link      "www.forex-tsd.com"
//+------------------------------------------------------------------
#property indicator_separate_window
#property indicator_buffers 4
#property indicator_color1  LimeGreen
#property indicator_color2  PaleVioletRed
#property indicator_color3  PaleVioletRed
#property indicator_color4  Red
#property indicator_width1  2
#property indicator_width2  2
#property indicator_width3  2
#property indicator_style4  STYLE_DOT
#property indicator_level1  0
#property indicator_level2  20
#property indicator_level3 -20
#property indicator_levelcolor DarkGray

//
//
//
//
//

extern string TimeFrame          = "Current time frame";
extern double Length             =  32;
extern int    Price              = PRICE_TYPICAL;
extern bool   alertsOn           = false;
extern bool   alertsOnCurrent    = true;
extern bool   alertsMessage      = true;
extern bool   alertsSound        = false;
extern bool   alertsEmail        = false;
extern int    BarsPerSymbol      = 70;
extern string Symbols            = "EURUSD;GBPUSD;USDCAD";
extern string UniqueID           = "Multi pair stochastic CG";
extern string _                  = "separator colors";
extern color  textColor          = Black;
extern color  backColor          = LightGray;
extern color  signalCrossUpColor   = LimeGreen;
extern color  signalCrossDownColor = HotPink;
extern int    separatorWidth     = 6;

//
//
//
//
//

double stocg[];
double stocgda[];
double stocgdb[];
double stosig[];
double trend[];
double cross[];
double cg[];
double storaw[];

//
//
//
//
//

int      window;  
string   aPairs[];
string   shortName;
double   minValue;
double   maxValue;
double   maxValues[];
int      timeFrame;
bool     calculateValue;
string   indicatorFileName;


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

int init()
{
   IndicatorBuffers(8);
   SetIndexBuffer(0,stocg);
   SetIndexBuffer(1,stocgda);
   SetIndexBuffer(2,stocgdb);
   SetIndexBuffer(3,stosig);
   SetIndexBuffer(4,trend);
   SetIndexBuffer(5,cross);
   SetIndexBuffer(6,cg);
   SetIndexBuffer(7,storaw);
      calculateValue    = (TimeFrame == "calculateValue"); if (calculateValue) return(0);
      indicatorFileName = WindowExpertName();

      //
      //
      //
      //
      //

      Symbols = StringTrimLeft(StringTrimRight(Symbols));
      if (StringSubstr(Symbols,StringLen(Symbols),1) != ";")
                       Symbols = StringConcatenate(Symbols,";");

         //
         //
         //
         //
         //                                   
            
         int s =  0;
         int i =  StringFind(Symbols,";",s);
         while (i > 0)
         {
            string current = StringSubstr(Symbols,s,i-s);
            ArrayResize(aPairs,ArraySize(aPairs)+1);
                        aPairs[ArraySize(aPairs)-1] = current;
                        if (current == Symbol())
                        {
                           string temp      = aPairs[0];
                                  aPairs[0] = current;
                                  aPairs[ArraySize(aPairs)-1] = temp;
                        }                                       
            s = i + 1;
            i = StringFind(Symbols,";",s);
         }
         ArrayResize(maxValues,ArraySize(aPairs));

      //
      //
      //
      //
      //
 
      separatorWidth = MathMax(separatorWidth,4);
      shortName      = UniqueID;
      timeFrame      = stringToTimeFrame(TimeFrame);
   IndicatorShortName(shortName);
   return(0);
}

//
//
//
//
//

int deinit()
{
   for (int i = 0; i < ArraySize(aPairs); i++)
   { 
         ObjectDelete(shortName+i);
         ObjectDelete(shortName+i+i);
   }         
   return(0);
}


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

int start()
{
   if (calculateValue) { calculateIndicator(); return(0); }
   int    i,k,limit = ArraySize(aPairs);
   double max;
   
   //
   //
   //
   //
   //

   window = WindowFind(shortName);  
   ArrayInitialize(maxValues,0);
         minValue = 0;
         maxValue = 0;
                for(i=0,k=0; i=0; i--)
   {   
      stocgda[i+shift] = EMPTY_VALUE;
      stocgdb[i+shift] = EMPTY_VALUE;
         if (trend[i+shift]==-1) PlotPoint(i+shift,stocgda,stocgdb,stocg);
   }
   createLabel(symbol,element,shift+limit+separatorWidth-2,max,isSignalCrossing(shift));
   shift += (limit+separatorWidth-1);
}

//
//
//
//
//

int isSignalCrossing(int shift)
{
   return(cross[shift]);
}

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

void calculateIndicator()
{
   int i,counted_bars=IndicatorCounted();
      if(counted_bars<0) return(-1);
      if(counted_bars>0) counted_bars--;
           int limit=MathMax(Bars-counted_bars,Bars-1);
           
   //
   //
   //
   //
   //

   for(i=limit; i>=0; i--)
   {
      double num = 0;
      double den = 0;
         for (int k=0; kstosig[i]) trend[i] =  1;
               if (stocg[i]0)
                  ObjectSet(name,OBJPROP_COLOR,signalCrossUpColor);
           else   ObjectSet(name,OBJPROP_COLOR,signalCrossDownColor);
      else ObjectSet(name,OBJPROP_COLOR,backColor);
}

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

void manageAlerts()
{
   if (alertsOn)
   {
      if (alertsOnCurrent)
           int whichBar = 0;
      else     whichBar = 1;
      
      //
      //
      //
      //
      //
            
      if (trend[whichBar]!= trend[whichBar+1])
      {
         static datetime time1 = 0;
         static string   mess1 = "";
            if (trend[whichBar] ==  1) doAlert(time1,mess1," Stochastic CG crossed signal line up");
            if (trend[whichBar] == -1) doAlert(time1,mess1," Stochastic CG crossed signal line down");
      }
   }
}

//
//
//
//
//

void doAlert(datetime& previousTime, string& previousAlert, string doWhat)
{
   string message;
   
   if (previousAlert != doWhat || previousTime != Time[0]) {
       previousAlert  = doWhat;
       previousTime   = Time[0];

       //
       //
       //
       //
       //

       message =  timeFrameToString(Period())+" "+Symbol()+" at "+TimeToStr(TimeLocal(),TIME_SECONDS)+doWhat;
          if (alertsMessage) Alert(message);
          if (alertsEmail)   SendMail(Symbol()+" Stochastic CG",message);
          if (alertsSound)   PlaySound("alert2.wav");
   }
}

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

void PlotPoint(int i,double& first[],double& second[],double& from[])
{
   if (first[i+1] == EMPTY_VALUE)
      {
         if (first[i+2] == EMPTY_VALUE) {
                first[i]   = from[i];
                first[i+1] = from[i+1];
                second[i]  = EMPTY_VALUE;
            }
         else {
                second[i]   =  from[i];
                second[i+1] =  from[i+1];
                first[i]    = EMPTY_VALUE;
            }
      }
   else
      {
         first[i]  = from[i];
         second[i] = EMPTY_VALUE;
      }
}


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

string sTfTable[] = {"M1","M5","M15","M30","H1","H4","D1","W1","MN"};
int    iTfTable[] = {1,5,15,30,60,240,1440,10080,43200};

//
//
//
//
//

int stringToTimeFrame(string tfs)
{
   tfs = StringUpperCase(tfs);
   for (int i=ArraySize(iTfTable)-1; i>=0; i--)
         if (tfs==sTfTable[i] || tfs==""+iTfTable[i]) return(MathMax(iTfTable[i],Period()));
                                                      return(Period());
}
string timeFrameToString(int tf)
{
   for (int i=ArraySize(iTfTable)-1; i>=0; i--) 
         if (tf==iTfTable[i]) return(sTfTable[i]);
                              return("");
}

//
//
//
//
//

string StringUpperCase(string str)
{
   string   s = str;

   for (int length=StringLen(str)-1; length>=0; length--)
   {
      int char = StringGetChar(s, length);
         if((char > 96 && char < 123) || (char > 223 && char < 256))
                     s = StringSetChar(s, length, char - 32);
         else if(char > -33 && char < 0)
                     s = StringSetChar(s, length, char + 224);
   }
   return(s);
}

Recommend