//+------------------------------------------------------------------+
//|                                               BH_DoubleStoch.mq4 |
//|                                 Copyright � 2007, 2008, BizHobby |
//|                                          http://www.bizhobby.com |
//+------------------------------------------------------------------+
#property copyright "Copyright � 2007, 2008, BizHobby"
#property link      "http://www.bizhobby.com"

#property indicator_chart_window

extern int bars_to_check = 1000;
extern int search_len = 200;

int mode = MODE_MAIN;

void find_last_stoch_high( int x, int& p, int& s, int& e )
{
  int i;
  s = -1;
  e = -1;
  p = x;

  for ( i = x; i < x + search_len; i++ )
  {
    if ( iStochastic( Symbol(), 0, 5, 3, 3, MODE_SMA, 0, mode, i ) > 80 )
    {
      p = i;
      s = i;
      break;
    }
  }

  if ( s > 0 )
  {
    for ( i = s + 1; i < s + search_len; i++ )
    {
      if ( iStochastic( Symbol(), 0, 5, 3, 3, MODE_SMA, 0, mode, i ) < 80 )
      {
        e = i - 1;
        break;
      }
      else
      {
        if ( High[i] > High[p] ) p = i;
      }
    }
  }
}

void find_last_stoch_low( int x, int& p, int& s, int& e )
{
  int i;
  s = -1;
  e = -1;
  p = x;

  for ( i = x; i < x + search_len; i++ )
  {
    if ( iStochastic( Symbol(), 0, 5, 3, 3, MODE_SMA, 0, mode, i ) < 20 )
    {
      p = i;
      s = i;
      break;
    }
  }
  
  if ( s > 0 )
  {
    for ( i = s + 1; i < s + search_len; i++ )
    {
      if ( iStochastic( Symbol(), 0, 5, 3, 3, MODE_SMA, 0, mode, i ) > 20 )
      {
        e = i - 1;
        break;
      }
      else
      {
        if ( Low[i] < Low[p] ) p = i;
      }
    }
  }
}

int init()
{
  return(0);
}

int deinit()
{
  return(0);
}

void delete_objects_containing(string s)
{
  for( int i = ObjectsTotal() - 1; i>=0; i-- )
  {
    if ( StringFind( ObjectName(i), s ) >= 0 )
    {
      ObjectDelete(ObjectName(i));
    }
  }
}

int start()
{
  int    counted_bars=IndicatorCounted();

  delete_objects_containing("bh_");

  int h, s, e;
  double p;
  string o;
  
  int last_h = 0;

  e = 0;
  while ( e >= 0 && e < bars_to_check )
  {
    find_last_stoch_high( e + 1, h, s, e );
    p = High[h];

    if ( e > 0 )
    {
      o = "bh_h" + s;
      ObjectCreate( o, OBJ_RECTANGLE, 0, Time[s-1], p + 10 * Point, Time[e+1], Low[s] - 10 * Point );
      ObjectSet( o, OBJPROP_COLOR, 0x4040a0 );
    }

    if ( last_h > 0 ) //  && High[last_h] < p )
    {
      ObjectCreate( "bh_d"+s, OBJ_TREND, 0, Time[h], p, Time[last_h], High[last_h] );
      ObjectSet( "bh_d"+s, OBJPROP_RAY, false );
    }

    last_h = h;
  }

  e = 0;
  last_h = 0;
  while ( e >= 0 && e < bars_to_check )
  {
    find_last_stoch_low( e + 1, h, s, e );
    p = Low[h];

    if ( e > 0 )
    {
      o = "bh_l" + s;
      ObjectCreate( o, OBJ_RECTANGLE, 0, Time[s-1], p - 10 * Point, Time[e+1], High[s] + 10 * Point );
      ObjectSet( o, OBJPROP_COLOR, 0x804040 );
    }

    if ( last_h > 0 ) // && Low[last_h] > p )
    {
//      Print( h + " " + last_h );
      ObjectCreate( "bh_u"+s, OBJ_TREND, 0, Time[h], p, Time[last_h], Low[last_h] );
      ObjectSet( "bh_u"+s, OBJPROP_RAY, false );
    }

    last_h = h;
  }

  return(0);
}