#MTF-Stoch





//+------------------------------------------------------------------+ 
//|            PowerTrend MultiTimeFrame Stochastic                  | 
//|                                                                  | 
//|   Copyright © 2008, Tim Hyder aka Hiachiever                     |
//|                                                                  |
//|   PO BOX 768, Hillarys, Western Australia, Australia, 6923       |
//|                                                                  |
//|   GIFTS AND DONATIONS ACCEPTED                                   | 
//|   All my indicators should be considered donationware. That is   |
//|   you are free to use them for your personal use, and are        |
//|   under no obligation to pay for them. However, if you do find   |
//|   this or any of my other indicators help you with your trading  |
//|   then any Gift or Donation as a show of appreciation is         |
//|   gratefully accepted.                                           |
//|                                                                  |
//|   Gifts or Donations also keep me motivated in producing more    |
//|   great free indicators. :-)                                     |
//|                                                                  |
//|   PayPal - hiachiever@gmail.com                                  |  
//+------------------------------------------------------------------+ 
#property copyright "Copyright © 2007, Tim Hyder."
#property link      "http://www.the4xtrader.com"

#define vers    "18.Feb.2008"
#define major   1
#define minor   0

#property indicator_separate_window
#property indicator_minimum 0
#property indicator_maximum 7
#property indicator_buffers 4 
#property indicator_color1 Aqua //Stoch.Trending.Up
#property indicator_color2 Red  //Stoch.Trending.Dn
#property indicator_color3 Blue //Stoch.Overbought
#property indicator_color4 Maroon  //Stoch.Oversold

extern string NOTE1 = " --- TimeFrame Settings ---";
extern string NOTETF = "Enter 0 to display current TF";
extern int TimeFrame = 0;
extern string NOTE2 = " --- Display Settings ---";
extern string NOTEVS = "Vertically positions indicator";
extern int VertShift = 1;

extern string NOTESTOCH = " --- Stochastic Settings ---";
extern int     Stoch.Period      =  5;    
extern double  Stoch.Kfast       =  3;    // Sensivity Factor for Fast Line
extern double  Stoch.Kslow       =  3;    // Sensivity Factor for Slow Line

extern string NOTEDISPLAY = " --- Other Display options ---";
extern int MaxBars = 1000;
extern string DISPLAYFVS1 = "FineVertShift allows Time Frame label";
extern string DISPLAYFVS2 = "to be moved up and down";
extern double TFLabelVS = -0.6;
//colour of time frame label on far right of indicator
extern color TFTextColor = White;
extern color TFSameTFColor = Lime;
extern int TFTextFontSize = 6;

extern string NOTEALERTS = " --- Alerts ---";
extern bool AllowTextAlerts = false;
extern bool AllowSoundAlerts = false;
extern string LongSound = "alert3.wav";
extern string ShortSound = "alert4.wav";
extern bool AllowEmailAlerts = false;

//---- buffers
double bufUpTrend[], bufOverBought[] ;
double bufDnTrend[], bufOverSold[];

string ShortName = "";
string Prefix = "MTF-STOCH";
int ArrowSize = 110;
int BarWidth = 0;
datetime LastTime = -1;
int TFrame, Window;
//Four constants used in one of the functions below
int Stoch.UpTrend = 1, Stoch.DnTrend = 2, Stoch.OverBought = 3, Stoch.OverSold = 4;

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

void init()
{  
  IndicatorBuffers(4);
  SetIndexStyle(0, DRAW_ARROW, 0, BarWidth);
  SetIndexStyle(1, DRAW_ARROW, 0, BarWidth);
  SetIndexStyle(2, DRAW_ARROW, 0, BarWidth);
  SetIndexStyle(3, DRAW_ARROW, 0, BarWidth);
  
  SetIndexArrow(0, ArrowSize);
  SetIndexArrow(1, ArrowSize);
  SetIndexArrow(2, ArrowSize);
  SetIndexArrow(3, ArrowSize);
  
  SetIndexBuffer(0, bufUpTrend);
  SetIndexBuffer(1, bufDnTrend);
  SetIndexBuffer(2, bufOverBought);
  SetIndexBuffer(3, bufOverSold);
    
  SetIndexEmptyValue(0, 0.0);
  SetIndexEmptyValue(1, 0.0);
  SetIndexEmptyValue(2, 0.0);
  SetIndexEmptyValue(3, 0.0);

  SetIndexLabel(0, NULL);
  SetIndexLabel(1, NULL);
  SetIndexLabel(2, NULL);
  SetIndexLabel(3, NULL);

  IndicatorDigits(0);
 
  //Verify Time Values entered by user
  TFrame = CheckTimeFrame(TimeFrame);
  
  //---- name for DataWindow and indicator subwindow label
  //VertShift = GetNextIndicatorNo(Prefix);
  ShortName = Prefix + VertShift;
  IndicatorShortName(ShortName);
}   

void deinit()
{
  int total = ObjectsTotal();  
  for (int i=total-1; i >= 0; i--) 
  {
    string name = ObjectName(i);
    if (StringFind(name, Prefix) == 0) ObjectDelete(name);
  }
}

void start()
{ 
   int i,tf,x,y=0;
   datetime TimeArray[];
   int Stoch;

   Window = WindowFind(ShortName);
   //This indicator is designed to display mutliple TFs in 1 indicator Window
   //When suqsequent indicators are dropped on the same Window
   //as a previous one WindowFind returns -1 ie can't find it.
   //This loop is to loop through the windows and find the window reference
   //The purpose behind all of this is to fix the problem of the TF labels
   //not being displayed for indicator 2,3,4 etc because of the Window reference
   if (Window == -1) 
   {
      for (i=0; i<20; i++)
      {
         x = WindowFind(Prefix + i);  
         if (x > 0)
         {
            Window = x;
            break;
         }
      }
   }   

   int counted_bars=IndicatorCounted();
   //---- check for possible errors
   if (counted_bars < 0) return(-1);
   //---- the last counted bar will be recounted
   if (counted_bars > 0) counted_bars--;
   int limit = Bars - counted_bars;
   limit = MathMin(limit, MaxBars);

  //-------------------------------1----------------------------------------   
  
   // Plot defined time frame on to current time frame
   ArrayCopySeries(TimeArray,MODE_TIME,Symbol(),TimeFrame); 
   
   for(i=0,y=0;i<limit;i++)
   {
      if (Time[i]<TimeArray[y]) y++;

      /***********************************************************   
         Add your main indicator loop below.  You can reference an existing
         function with its name or an indicator with its iName  or iCustom.
         Rule 1:  Add extern inputs above for all neccesary values   
         Rule 2:  Use 'TFrame' for the indicator time frame
         Rule 3:  Use 'y' for your indicator's shift value
       **********************************************************/  
      Stoch = GetStoch(TFrame,y);

      bufUpTrend[i] = 0;
      bufDnTrend[i] = 0;
      bufOverBought[i] = 0;
      bufOverSold[i] = 0;

     if (Stoch == Stoch.UpTrend) bufUpTrend[i] = VertShift;
      else if (Stoch == Stoch.DnTrend) bufDnTrend[i] = VertShift;
      else if (Stoch == Stoch.OverBought) bufOverBought[i] = VertShift;
      else if (Stoch == Stoch.OverSold) bufOverSold[i] = VertShift;
  }//End For Loop
  
  CreateTFLabel(TFrame);
 
  string MailSubject = "MTF Stoch";
  string msg = "MTF Stoch on " +Symbol()+ " " +(TimeFrame);
  
  int bar = 1;
  
  if (LastTime != Time[0])
  {
    if (bufOverBought[bar] != 0 && bufOverBought[bar+1] == 0)
    {
      msg = msg + " is now Overbought.";
      if (AllowTextAlerts) Alert(msg);
      if (AllowSoundAlerts) PlaySound(LongSound);
      if (AllowEmailAlerts) SendMail(MailSubject, msg);
    }

    if (bufOverBought[bar] == 0 && bufOverBought[bar+1] != 0)
    {
      msg = msg + " has started new down trend.";
      if (AllowTextAlerts) Alert(msg);
      if (AllowSoundAlerts) PlaySound(LongSound);
      if (AllowEmailAlerts) SendMail(MailSubject, msg);
    }

    //-> Red
    if (bufOverBought[bar] != 0 && bufOverBought[bar+1] == 0)
    {
      msg = msg + " is now Oversold.";
      if (AllowTextAlerts) Alert(msg);
      if (AllowSoundAlerts) PlaySound(ShortSound);
      if (AllowEmailAlerts) SendMail(MailSubject, msg);
    }

    //<- Red
    if (bufOverBought[bar] == 0 && bufOverBought[bar+1] != 0)
    {
      msg = msg + " has started new up trend.";
      if (AllowTextAlerts) Alert(msg);
      if (AllowSoundAlerts) PlaySound(ShortSound);
      if (AllowEmailAlerts) SendMail(MailSubject, msg);
    }
    
    LastTime = Time[0];
  }
   

  return(0);
}
  
  
void CreateTFLabel(int TF)
{
   string txt = TF2Str(TF);
   double TimeDiff = Time[0]-Time[1];  
   string name = Prefix+VertShift+"_TF_"+txt;
   color labelcolor = TFTextColor;
   string fontname = "Arial";

   //If Tf = Chart Period change TF Label colour so that it can be easily identified
   if (TF == Period())
   {
      labelcolor = TFSameTFColor;
      fontname = "Arial Bold";
      TFLabelVS = -0.7;
   }
         
   if(VertShift<TFLabelVS) TFLabelVS = 0; //TFLabelVS = TF Label Vertical Shift
   
   if (ObjectFind(name) == -1)
   {  
     ObjectCreate(name, OBJ_TEXT, Window, iTime(NULL,0,0)+2*TimeDiff, VertShift-TFLabelVS);
     ObjectSetText(name, txt,TFTextFontSize,fontname, labelcolor);
   }
   else
     ObjectSet(name, OBJPROP_TIME1, iTime(NULL,0,0)+2*TimeDiff);
}

string TF2Str(int period) 
{
  switch (period) 
  {
    case PERIOD_M1: return("M1");
    case PERIOD_M5: return("M5");
    case PERIOD_M15: return("M15");
    case PERIOD_M30: return("M30");
    case PERIOD_H1: return("H1");
    case PERIOD_H4: return("H4");
    case PERIOD_D1: return("D1");
    case PERIOD_W1: return("W1");
    case PERIOD_MN1: return("MN");
  }
  return (Period());
} 

int CheckTimeFrame(int TimeFrame)
{
   int result;

   if (TimeFrame == 0) 
      result = Period();   
   else
   {
      switch(TimeFrame) 
      {
         case 1    : result = PERIOD_M1;  break; 
         case 5    : result = PERIOD_M5;  break;
         case 15   : result = PERIOD_M15; break;
         case 30   : result = PERIOD_M30; break;
         case 60   : result = PERIOD_H1;  break;
         case 240  : result = PERIOD_H4;  break;
         case 1440 : result = PERIOD_D1;  break;
         case 7200 : result = PERIOD_W1;  break;
         case 28800: result = PERIOD_MN1; break;
         default  : result = Period(); break; //Error so return current TF
      }
   }
   return(result);
}

int GetStoch(int TF, int shift)
{
   double stoch_main = iStochastic(NULL,TF,Stoch.Period,Stoch.Kfast,Stoch.Kslow,MODE_SMA,PRICE_CLOSE,MODE_MAIN,shift);
   double stoch_sig = iStochastic(NULL,TF,Stoch.Period,Stoch.Kfast,Stoch.Kslow,MODE_SMA,PRICE_CLOSE,MODE_SIGNAL,shift);

   if ((stoch_main >= stoch_sig) && (stoch_main >= 20) && (stoch_main <= 80)) return (Stoch.UpTrend); 
   else if (stoch_main > 80) return (Stoch.OverBought);
   else if ((stoch_main <= stoch_sig) && (stoch_main >= 20) && (stoch_main <= 80)) return (Stoch.DnTrend); 
   else if (stoch_main < 20) return (Stoch.OverSold);

}





Sample





Analysis



Market Information Used:

Series array that contains open time of each bar


Indicator Curves created:

Implements a curve of type DRAW_ARROW


Indicators Used:

Stochastic oscillator


Custom Indicators Used:

Order Management characteristics:

Other Features:

It issuies visual alerts to the screen
It plays sound alerts
It sends emails