//+-----------------------------------------------------------------------+
//|                                                    ATM Indicators.mq4 |
//| Indicates CEC with trends                                             |
//+-----------------------------------------------------------------------+
//| v1.0 Initial ATM Indicators coding                                    |
//| v1.1 Added position option for CEC arrows and CEC_Trigger_Pips        |
//| v1.2 Added style options for Trigger and Buffer lines                 |
//| v1.3 Added trigger arrow display options                              |
//| v1.4 Added Pip Summary                                                |
//+-----------------------------------------------------------------------+
#property copyright "Copyright © 2009, the Guru Online."
#property link      "http://www.theguruonline.biz"

#property indicator_chart_window
#property indicator_buffers 7
#property indicator_style1 STYLE_SOLID
#property indicator_width1 7
#property indicator_color1 White // Trend MA flat
#property indicator_style2 STYLE_SOLID
#property indicator_width2 7
#property indicator_color2 Green // Trend MA up
#property indicator_style3 STYLE_SOLID
#property indicator_width3 7
#property indicator_color3 FireBrick // Trend MA dn
#property indicator_style4 STYLE_SOLID
#property indicator_width4 1
#property indicator_color4 White // Trigger MA
#property indicator_style5 STYLE_DOT
#property indicator_width5 1
#property indicator_color5 White // Buffer MA
#property indicator_style6 DRAW_ARROW
#property indicator_width6 2
#property indicator_color6 FireBrick // CEC dn
#property indicator_style7 DRAW_ARROW
#property indicator_width7 2
#property indicator_color7 Green // CEC up

static string   Indicator_Name      ="ATM Indicators";
static string   Version             ="1.4";

double Trend[], Trend_Up[], Trend_Dn[], Trigger[], Buffer[];
double CEC_Up[], CEC_Dn[];

extern string   d5_                     =" : ShowOnTrend : TRUE will only show with trend";
extern bool     CEC_ShowOnTrend         =FALSE;
extern string   d6a_                    =" : ShowOnTrigger : TRUE will only show if near trigger line";
extern bool     CEC_ShowOnTrigger       =TRUE;
extern string   d6b_                    =" : ShowOnBuffer : TRUE will show if buffer line touched";
extern bool     CEC_ShowOnBuffer        =TRUE;
extern string   d7_                     =" : TriggerBetween : TRUE will show if between trigger and buffer";
extern bool     CEC_ShowBetween         =TRUE;
extern string   d8_                     =" : TriggerOnPullBack : TRUE will show if pullback";
extern bool     CEC_ShowOnAnyPullBack   =FALSE;
extern bool     CEC_ShowOnPullBackToBuffer=TRUE;
extern bool     SingleColorPullBack     =TRUE;
extern string   d9_                     =" : TriggerOnTriggerSlope : TRUE will show if trigger slope>minimum";
extern bool     CEC_ShowOnTriggerSlope  =FALSE;
extern string   d10_                    =" : TriggerOnBufferSlope : TRUE will show if buffer slope>minimum";
extern bool     CEC_ShowOnBufferSlope   =FALSE;
extern string   d11_                    =" : TriggerOnBothSlopes : TRUE will show if trigger & buffer slope>minimum";
extern bool     CEC_ShowOnBothSlopes    =FALSE,
                Clear_Broken_CEC        =FALSE,
                Show_Candle_Time        =TRUE,
                Show_Spread             =TRUE,
                Show_Market_Price       =TRUE,
                Show_Pip_Summary        =TRUE;
extern int      TFSummary_Period        =5,
                TFSummary_Bars          =5,
                TFSummary_Offset        =15;
//+------------------------------------------------------------------+
//| MA variables                                                     |
//+------------------------------------------------------------------+
extern string   a_                      =" : Moving Average Variables :";
extern string   a1_                     =" : : Trend";
extern int      Trend_Period            =21;
extern double   Trend_MinSlope          =0;//1.0;
extern int      Trend_SlopeBars         =1,//5,
                Trend_Width             =7;
extern color    Trend_DnColor           =FireBrick,
                Trend_UpColor           =Green,
                Trend_Color             =White;
extern string   a2_                     =" : : Trigger";
extern int      Trigger_Period          =21;
extern double   Trigger_MinSlope        =1;
extern int      Trigger_SlopeBars       =5;
extern string   style_a              =" : Style Options :",
                style_b              =" : : 0=Solid",
                style_c              =" : : 1=Dashed",
                style_d              =" : : 2=Dotted",
                style_e              =" : : 3=Alternating dashes and dots",
                style_f              =" : : 4=Alternating dashes and double dots";
extern int      Trigger_Style           =0,
                Trigger_Width           =1;
extern color    Trigger_Color           =White;
extern int      MA_Near_Zone            =5;
extern string   a3_                     =" : : Buffer";
extern int      Buffer_Period           =8;
extern double   Buffer_MinSlope         =1;
extern int      Buffer_SlopeBars        =5,
                Buffer_Style            =2,
                Buffer_Width            =1;
extern color    Buffer_Color            =White;
extern string   b_                      =" : Methods :",
                b1_                     =" : : 0=Simple moving average",
                b2_                     =" : : 1=Exponential moving average",
                b3_                     =" : : 2=Smoothed moving average",
                b4_                     =" : : 3=Linear weighted moving average";
extern int      MA_Method               =0;
extern string   c_                      =" : Price :",
                c1_                     =" : : 0=Close price",
                c2_                     =" : : 1=Open price",
                c3_                     =" : : 2=High price",
                c4_                     =" : : 3=Low price",
                c5_                     =" : : 4=Median price, (high+low)/2",
                c6_                     =" : : 5=Typical price, (high+low+close)/3",
                c7_                     =" : : 6=Weighted close price, (high+low+close+close)/4";
extern int      MA_Price                =0;

extern string   d_                      =" : Display Variables :";
extern bool     Sell_Arrow_Above        =FALSE;
extern int      CEC_ArrowUp             =233,
                CEC_ArrowDn             =234,
                CEC_Trigger_Pips        =0,
                Arrow_Width             =2,
                Arrow_Shift             =10;
extern bool     Arrow_Shift_ATR         =FALSE;
extern color    Arrow_DnColor           =FireBrick,
                Arrow_UpColor           =Green;
extern string   d1_                     =" : Corner : 0=Top Left",
                d2_                     =" : : 1=Top Right",
                d3_                     =" : : 2=Bottom Left",
                d4_                     =" : : 3=Bottom Right";
extern int      Display_Corner          =3,
                label_textsize          =6,
                CandleTime_Shift        =3,
                Size_Market_Price       =26,
                Extra_Digit_SizeDiff    =10,
                ChartGMTOffset          =0;
static double   Market_Old_Price,
                TFSummary_Scale=0.4,
                TFSummary_TopOffset=0.1;
static int      Orders_Old_Total=0,
                PipDelta=1,
                TFS, TFSummary_High[], TFSummary_Low[], TFSummary_Open[], TFSummary_Close[];

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
   IndicatorBuffers( 7 );
   SetIndexStyle( 0, DRAW_LINE, STYLE_SOLID,   Trend_Width,   Trend_Color );
   SetIndexBuffer( 0, Trend );
   SetIndexStyle( 1, DRAW_LINE, STYLE_SOLID,   Trend_Width,   Trend_UpColor );
   SetIndexBuffer( 1, Trend_Up );
   SetIndexStyle( 2, DRAW_LINE, STYLE_SOLID,   Trend_Width,   Trend_DnColor );
   SetIndexBuffer( 2, Trend_Dn );
   SetIndexStyle( 3, DRAW_LINE, Trigger_Style, Trigger_Width, Trigger_Color );
   SetIndexBuffer( 3, Trigger );
   SetIndexStyle( 4, DRAW_LINE, Buffer_Style,  Buffer_Width,  Buffer_Color );
   SetIndexBuffer( 4, Buffer );
   SetIndexStyle( 5, DRAW_ARROW, STYLE_SOLID,  Arrow_Width,   Arrow_DnColor );
   SetIndexArrow( 5, CEC_ArrowDn );
   SetIndexBuffer( 5, CEC_Dn );
   SetIndexStyle( 6, DRAW_ARROW, STYLE_SOLID,  Arrow_Width,   Arrow_UpColor );
   SetIndexArrow( 6, CEC_ArrowUp );
   SetIndexBuffer( 6, CEC_Up );
   string indicatorShortName="ATM ["+Trend_Period+","+Trigger_Period+","+Buffer_Period+","+","+MA_Method+","+","+MA_Price+","+"]";
   IndicatorShortName( indicatorShortName );
   if( Digits==3 || Digits==5 )
     {
      PipDelta=PipDelta*10;
     }
   if( TFSummary_Bars>0 )
     {
      _Draw_Label(  5, 1, 10, "TFSummary", "M"+DoubleToStr( TFSummary_Period, 0 )+" Summary", Trend_Color, "Arial Black", 1 );
      ArrayResize( TFSummary_High,  TFSummary_Bars );
      ArrayResize( TFSummary_Low,   TFSummary_Bars );
      ArrayResize( TFSummary_Open,  TFSummary_Bars );
      ArrayResize( TFSummary_Close, TFSummary_Bars );
      int WindowRange=( WindowPriceMax()-WindowPriceMin() )/Point*PipDelta;
      int WindowHeight=WindowRange*TFSummary_Scale;
      int WindowOffset=WindowRange*TFSummary_TopOffset;
      for( TFS=0; TFS<TFSummary_Bars; TFS++ )
        {
         ObjectCreate( "["+Indicator_Name+"] TFSwick"+TFS, OBJ_TREND, 0, Time[0]+( ( TFSummary_Offset-TFS )*60*Period() ), WindowPriceMax()-( WindowOffset*Point*PipDelta ), Time[0]+( ( TFSummary_Offset-TFS )*60*Period() ), WindowPriceMax()-( ( WindowOffset+WindowHeight )*Point*PipDelta ) );
         ObjectSet( "["+Indicator_Name+"] TFSwick"+TFS, OBJPROP_RAY, FALSE );
         ObjectSet( "["+Indicator_Name+"] TFSwick"+TFS, OBJPROP_WIDTH, 1 );
         ObjectSet( "["+Indicator_Name+"] TFSwick"+TFS, OBJPROP_COLOR, White );

         ObjectCreate( "["+Indicator_Name+"] TFSbody"+TFS, OBJ_TREND, 0, Time[0]+( ( TFSummary_Offset-TFS )*60*Period() ), WindowPriceMax()-( WindowOffset*Point*PipDelta ), Time[0]+( ( TFSummary_Offset-TFS )*60*Period() ), WindowPriceMax()-( ( WindowOffset+WindowHeight )*Point*PipDelta ) );
         ObjectSet( "["+Indicator_Name+"] TFSbody"+TFS, OBJPROP_RAY, FALSE );
         ObjectSet( "["+Indicator_Name+"] TFSbody"+TFS, OBJPROP_WIDTH, 7 );
         ObjectSet( "["+Indicator_Name+"] TFSbody"+TFS, OBJPROP_COLOR, White );
         
         ObjectCreate( "["+Indicator_Name+"] TFScec"+TFS, OBJ_ARROW, 0, 0, Time[0]+( ( TFSummary_Offset-TFS )*60*Period() ) );
         ObjectSet( "["+Indicator_Name+"] TFScec"+TFS, OBJPROP_WIDTH, 2 );
        }
     }
   return(0);
  }

//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
  {
   int _ObjectsTotal=ObjectsTotal();
   for( int _Object=_ObjectsTotal; _Object>=0; _Object-- )
     {
      string _ObjectName=ObjectName( _Object );
      if( StringSubstr( _ObjectName, 0, StringLen( Indicator_Name )+2 )=="["+Indicator_Name+"]" ) ObjectDelete( _ObjectName );
     }    
   return(0);
  }

//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
  {
   if ( Arrow_Shift_ATR ) Arrow_Shift=iATR( Symbol(), Period(), WindowBarsPerChart(), 0 )/4/Point*PipDelta;
   _Draw_Label(  5,  1,  7, "Copyright", CharToStr( 169 )+" "+Year()+", ElectroLegal.com & the Guru : [v"+Version+"] "+Indicator_Name, Trend_Color, "Arial Narrow" );
   _Draw_Label(  5, 10, 14, "ATM",       "ATM",                                                                                        Trend_Color, "Arial Black" );
   int shift, bar, limit, counted_bars=IndicatorCounted();
//---- check for possible errors
   if(
         counted_bars<0
      || Trend_Period<=0
      || Trigger_Period<=0
      || Buffer_Period<=0
     ) return(-1);
   if( counted_bars==0 ) limit=Bars-1;
//---- last counted bar will be recounted
   if( counted_bars>0 ) limit=Bars-counted_bars;
   for( shift=limit; shift>=0; shift-- )
     {
      double Trend_Cur=iMA( Symbol(), Period(), Trend_Period,   0, MA_Method, MA_Price, shift );
      double Trend_Pre=iMA( Symbol(), Period(), Trend_Period,   0, MA_Method, MA_Price, shift+Trend_SlopeBars );
      Trigger[shift]  =iMA( Symbol(), Period(), Trigger_Period, 0, MA_Method, MA_Price, shift );
      Buffer[shift]   =iMA( Symbol(), Period(), Buffer_Period,  0, MA_Method, MA_Price, shift );
      Trend[shift]   =Trend_Cur;
      Trend_Up[shift]=Trend_Cur;
      Trend_Dn[shift]=Trend_Cur;
      double Trend_Slope=_GetSlope( Trend_Period, MA_Method, MA_Price, shift, Trend_SlopeBars );
//      if( ( Trend_Cur<Trend_Pre && Trend_MinSlope==0 ) || ( Trend_MinSlope!=0 && Trend_Slope<-Trend_MinSlope ) )
      if( Trend_Slope<-Trend_MinSlope )
        {
         Trend_Up[shift]=EMPTY_VALUE;
        }
//      else if( ( Trend_Cur<Trend_Pre && Trend_MinSlope==0 ) || ( Trend_MinSlope!=0 && Trend_Slope>Trend_MinSlope ) )
      else if( Trend_Slope>Trend_MinSlope )
        {
         Trend_Dn[shift]=EMPTY_VALUE;
        }
      else 
        {
         Trend_Up[shift]=EMPTY_VALUE;
         Trend_Dn[shift]=EMPTY_VALUE;
        }
//--- CEC Bar
      bool Pullback_High=FALSE, Pullback_Low=FALSE;
      if(
         (
          ( High[shift+1]>=High[shift+2] && High[shift+2]>=High[shift+3] )//&& High[shift+3]>=High[shift+4] )
          ||
          ( Low[shift+1]>=Low[shift+2] && Low[shift+2]>=Low[shift+3] )//&& High[shift+3]>=High[shift+4] )
         )
         &&
         (
          ( Open[shift+1]<Close[shift+1] && Open[shift+2]<Close[shift+2] && Open[shift+3]<Close[shift+3] )
          || !SingleColorPullBack
         )
        ) Pullback_Low=TRUE;
      if(
         (
          ( High[shift+1]<=High[shift+2] && High[shift+2]<=High[shift+3] )//&& Low[shift+3]<=Low[shift+4] )
          ||
          ( Low[shift+1]<=Low[shift+2] && Low[shift+2]<=Low[shift+3] )//&& Low[shift+3]<=Low[shift+4] )
         )
         &&
         (
          ( Open[shift+1]>Close[shift+1] && Open[shift+2]>Close[shift+2] && Open[shift+3]>Close[shift+3] )
          || !SingleColorPullBack
         )
        ) Pullback_High=TRUE;
      double Trigger_Slope=_GetSlope( Trigger_Period, MA_Method, MA_Price, shift, Trigger_SlopeBars );
      double Buffer_Slope =_GetSlope( Buffer_Period,  MA_Method, MA_Price, shift, Buffer_SlopeBars );
      if(
            Open[shift+1]>Close[shift+1]
         && ( Open[shift]<Close[shift] || !Clear_Broken_CEC )
         && High[shift]>High[shift+1]+( CEC_Trigger_Pips*Point*PipDelta )
//         && ( Trend_Up[shift]==Trend[shift] || !CEC_ShowOnTrend )
         && ( Trend_Dn[shift]!=Trend[shift] || !CEC_ShowOnTrend )
         && (
                  //-- Close to trigger
                ( CEC_ShowOnTrigger && _Between_Range( High[shift+1]+( MA_Near_Zone*Point*PipDelta ), Low[shift+1]-( MA_Near_Zone*Point*PipDelta ), Trigger[shift+1] ) )
                  //-- Any pullback
             || ( CEC_ShowOnAnyPullBack && Pullback_High )
                  //-- Pullback and high/low between trigger and buffer
             || ( CEC_ShowOnPullBackToBuffer && Pullback_High && ( _Between_Range( Trigger[shift+1], Buffer[shift+1], High[shift+1] ) || _Between_Range( Trigger[shift+1], Buffer[shift+1], Low[shift+1] ) ) )
                  //-- Between trigger and buffer
             || ( CEC_ShowBetween && _Between_Range( Trigger[shift+1], Buffer[shift+1], High[shift+1] ) )
                  //-- touches buffer
             || ( CEC_ShowOnBuffer && _Between_Range( High[shift+1], Low[shift+1], Buffer[shift+1] ) )
                  //-- trigger slope
             || ( CEC_ShowOnTriggerSlope && Trigger_Slope>Trigger_MinSlope )
                  //-- buffer slope
             || ( CEC_ShowOnBufferSlope && Buffer_Slope>Buffer_MinSlope )
                  //-- both slopes
             || ( CEC_ShowOnBothSlopes && Trigger_Slope>Trigger_MinSlope && Buffer_Slope>Buffer_MinSlope )
            )
        )
        {
         if( Sell_Arrow_Above ) CEC_Up[shift]=Low[shift]-( Arrow_Shift*Point*PipDelta );
         else                   CEC_Up[shift]=High[shift]+( Arrow_Shift*Point*PipDelta );
        }
      else if( Clear_Broken_CEC )
        {
         CEC_Up[shift]=EMPTY_VALUE;
        }
      if(
            Open[shift+1]<Close[shift+1]
         && ( Open[shift]>Close[shift] || !Clear_Broken_CEC )
         && Low[shift]<Low[shift+1]-( CEC_Trigger_Pips*Point*PipDelta )
//         && ( Trend_Dn[shift]==Trend[shift] || !CEC_ShowOnTrend )
         && ( Trend_Up[shift]!=Trend[shift] || !CEC_ShowOnTrend )
         && (
                  //-- Close to trigger
                ( CEC_ShowOnTrigger && _Between_Range( High[shift+1]+( MA_Near_Zone*Point*PipDelta ), Low[shift+1]-( MA_Near_Zone*Point*PipDelta ), Trigger[shift+1] ) )
                  //-- Any pullback
             || ( CEC_ShowOnAnyPullBack && Pullback_Low )
                  //-- Pullback and high/low between trigger and buffer
             || ( CEC_ShowOnPullBackToBuffer && Pullback_Low && ( _Between_Range( Trigger[shift+1], Buffer[shift+1], High[shift+1] ) || _Between_Range( Trigger[shift+1], Buffer[shift+1], Low[shift+1] ) ) )
                  //-- Between trigger and buffer
             || ( CEC_ShowBetween && _Between_Range( Trigger[shift+1], Buffer[shift+1], High[shift+1] ) )
                  //-- touches buffer
             || ( CEC_ShowOnBuffer && _Between_Range( High[shift+1], Low[shift+1], Buffer[shift+1] ) )
                  //-- trigger slope
             || ( CEC_ShowOnTriggerSlope && Trigger_Slope<Trigger_MinSlope )
                  //-- buffer slope
             || ( CEC_ShowOnBufferSlope && Buffer_Slope<Buffer_MinSlope )
                  //-- both slopes
             || ( CEC_ShowOnBufferSlope && Trigger_Slope<Trigger_MinSlope && Buffer_Slope<Buffer_MinSlope )
            )
        )
        {
         if( Sell_Arrow_Above ) CEC_Dn[shift]=High[shift]+( Arrow_Shift*Point*PipDelta );
         else                   CEC_Dn[shift]=Low[shift]-( Arrow_Shift*Point*PipDelta );
        }
      else if( Clear_Broken_CEC )
        {
         CEC_Dn[shift]=EMPTY_VALUE;
        }
     } 
   if( Show_Candle_Time )
     {
      int m,s,k;
      m=Time[0]+Period()*60-CurTime();
      s=m%60;
      m=(m-m%60)/60;
      string candle_text="<"+m;
      if( s<10 )
        {
         candle_text=candle_text+":0"+s;
        }
      else
        {
         candle_text=candle_text+":"+s;
        }
      if( Show_Spread )
         candle_text=candle_text+" ("+DoubleToStr( MarketInfo( Symbol(), MODE_SPREAD )/PipDelta, 0 )+"pips)";
      if( ObjectFind( "["+Indicator_Name+"] CandleTime")!=0 ) ObjectCreate( "["+Indicator_Name+"] CandleTime", OBJ_TEXT, 0, Time[0]+( CandleTime_Shift*Period()*60 ), Close[0] );
      else                                                    ObjectMove( "["+Indicator_Name+"] CandleTime", 0, Time[0]+( CandleTime_Shift*Period()*60 ), Close[0] );
      ObjectSetText( "["+Indicator_Name+"] CandleTime", candle_text, label_textsize+2, "Arial Black", Trend_Color);
     }
   if( Show_Market_Price )
     {
      datetime StartOfDay=StrToTime( TimeToStr( TimeCurrent(), TIME_DATE )+" 00:00" )+ChartGMTOffset;
      color Market_Color=Trend_Color;
      string BidA, BidB, Market_Price; 
      BidA=DoubleToStr( Bid, Digits );   // Full base as string
      int SLength=StringLen( BidA ), DigitOffset=0;
      BidB=StringSubstr( BidA, SLength-1, 1 );
      if( Bid>Market_Old_Price ) Market_Color=Trend_UpColor;
      if( Bid<Market_Old_Price ) Market_Color=Trend_DnColor;
      Market_Old_Price=Bid;
      if( Digits==3 || Digits==5 )
        {
         DigitOffset=Size_Market_Price-( Extra_Digit_SizeDiff/2 );
         BidA=StringSubstr( BidA, 0, SLength-1 );
         _Draw_Label( 5, 25, Size_Market_Price-Extra_Digit_SizeDiff, "Market_PriceB", BidB, Market_Color, "Arial Black" );
         _Draw_Label( DigitOffset, 25, Size_Market_Price, "Market_PriceA", BidA, Market_Color, "Arial Black" );
        }
      else
         _Draw_Label( 5, 25, Size_Market_Price, "Market_PriceA", BidA, Market_Color, "Arial Black" );
     }
   if( Show_Pip_Summary )
     {
      _Draw_Label( 5,                            25+( Size_Market_Price*3.7 ), 8, "All Pip Summary",      "All trades", Trend_Color, "Arial" );
      _Draw_Label( 5+( Extra_Digit_SizeDiff*6 ), 25+( Size_Market_Price*3.7 ), 8, "Currency Pip Summary", Symbol(),     Trend_Color, "Arial" );
      double current_all_pips=0, current_currency_pips=0, history_all_pips=0, history_currency_pips=0, yesterdays_pips=0;
      int order, tmpPipDelta;
      color Summary_Color=Trend_Color;
      if( OrdersTotal()!=0 )
        {
         bool Current_Currency_Order=FALSE;
         for( order=0; order<OrdersTotal(); order++ )
           {
            tmpPipDelta=1;
            if( OrderSelect( order, SELECT_BY_POS, MODE_TRADES )==FALSE )
              {
               Print( "Access to orders failed with error (", GetLastError(), ")" );
               break;
              }
            else
              {
               if( MarketInfo( OrderSymbol(), MODE_DIGITS )==3 || MarketInfo( OrderSymbol(), MODE_DIGITS )==5 ) tmpPipDelta=10;
               if( OrderType()==OP_BUY && OrderOpenTime()>=StartOfDay )
                 {
                  current_all_pips=current_all_pips+( ( OrderClosePrice()-OrderOpenPrice() )/MarketInfo( OrderSymbol(), MODE_POINT )*tmpPipDelta );
                  if( OrderSymbol()==Symbol() )
                    {
                     Current_Currency_Order=TRUE;
                     current_currency_pips=current_currency_pips+( ( OrderClosePrice()-OrderOpenPrice() )/MarketInfo( OrderSymbol(), MODE_POINT )*tmpPipDelta );
                    }
                 }
               else if( OrderType()==OP_SELL && OrderOpenTime()>=StartOfDay )
                 {
                  current_all_pips=current_all_pips+( ( OrderOpenPrice()-OrderClosePrice() )/MarketInfo( OrderSymbol(), MODE_POINT )*tmpPipDelta );
                  if( OrderSymbol()==Symbol() )
                    {
                     Current_Currency_Order=TRUE;
                     current_currency_pips=current_currency_pips+( ( OrderOpenPrice()-OrderClosePrice() )/MarketInfo( OrderSymbol(), MODE_POINT )*tmpPipDelta );
                    }
                 }
               else if( OrderOpenTime()>=StartOfDay-( 60*60*24 ) )
                 {
                  if( OrderType()==OP_BUY ) yesterdays_pips=yesterdays_pips+( ( OrderClosePrice()-OrderOpenPrice() )/MarketInfo( OrderSymbol(), MODE_POINT )*tmpPipDelta );
                  else if( OrderType()==OP_SELL ) yesterdays_pips=yesterdays_pips+( ( OrderOpenPrice()-OrderClosePrice() )/MarketInfo( OrderSymbol(), MODE_POINT )*tmpPipDelta );
                 }
              }
           }
        }
      if( Current_Currency_Order )
        {
         if( current_all_pips>0 ) Summary_Color=Trend_UpColor;
         else if( current_all_pips<0 ) Summary_Color=Trend_DnColor;
         _Draw_Label( 5,                            25+( Size_Market_Price*1.5 ), Size_Market_Price-Extra_Digit_SizeDiff, "Current All Pip Summary",          DoubleToStr( current_all_pips, 0 ),      Summary_Color, "Arial Black" );
         if( current_currency_pips>0 ) Summary_Color=Trend_UpColor;
         else if( current_currency_pips<0 ) Summary_Color=Trend_DnColor;
         _Draw_Label( 5+( Extra_Digit_SizeDiff*6 ), 25+( Size_Market_Price*1.5 ), Size_Market_Price,                      "Current "+Symbol()+" Pip Summary", DoubleToStr( current_currency_pips, 0 ), Summary_Color, "Arial Black" );
        }
      else if( OrdersTotal()!=0 )
        {
         if( current_all_pips>0 ) Summary_Color=Trend_UpColor;
         else if( current_all_pips<0 ) Summary_Color=Trend_DnColor;
         _Draw_Label( 5,                            25+( Size_Market_Price*1.5 ), Size_Market_Price-Extra_Digit_SizeDiff, "Current All Pip Summary",          DoubleToStr( current_all_pips, 0 ),      Summary_Color, "Arial Black" );
         _Draw_Label( 5+( Extra_Digit_SizeDiff*6 ), 25+( Size_Market_Price*1.5 ), Size_Market_Price,                      "Current "+Symbol()+" Pip Summary", " ",                                     Summary_Color, "Arial Black" );
        }
      else
        {
         _Draw_Label( 5,                            25+( Size_Market_Price*1.5 ), Size_Market_Price-Extra_Digit_SizeDiff, "Current All Pip Summary",          " ", Summary_Color, "Arial Black" );
         _Draw_Label( 5+( Extra_Digit_SizeDiff*6 ), 25+( Size_Market_Price*1.5 ), Size_Market_Price,                      "Current "+Symbol()+" Pip Summary", " ", Summary_Color, "Arial Black" );
        }
      if( OrdersHistoryTotal()!=Orders_Old_Total || current_all_pips!=0 )
        {
         Orders_Old_Total=OrdersHistoryTotal();
         for( order=0; order<OrdersHistoryTotal(); order++ )
           {
            tmpPipDelta=1;
            if( OrderSelect( order, SELECT_BY_POS, MODE_HISTORY)==FALSE )
              {
               Print( "Access to orders failed with error (", GetLastError(), ")" );
               break;
              }
            else
              {
               if( MarketInfo( OrderSymbol(), MODE_DIGITS )==3 || MarketInfo( OrderSymbol(), MODE_DIGITS )==5 ) tmpPipDelta=10;
               if( OrderType()==OP_BUY && OrderOpenTime()>=StartOfDay-( 60*60*24*TimeDayOfWeek( StartOfDay ) ) )
                 {
                  history_all_pips=history_all_pips+( ( OrderClosePrice()-OrderOpenPrice() )/MarketInfo( OrderSymbol(), MODE_POINT )*tmpPipDelta );
                  if( OrderSymbol()==Symbol() ) history_currency_pips=history_currency_pips+( ( OrderClosePrice()-OrderOpenPrice() )/MarketInfo( OrderSymbol(), MODE_POINT )*tmpPipDelta );
                 }
               else if( OrderType()==OP_SELL && OrderOpenTime()>=StartOfDay-( 60*60*24*TimeDayOfWeek( StartOfDay ) ) )
                 {
                  history_all_pips=history_all_pips+( ( OrderOpenPrice()-OrderClosePrice() )/MarketInfo( OrderSymbol(), MODE_POINT )*tmpPipDelta );
                  if( OrderSymbol()==Symbol() ) history_currency_pips=history_currency_pips+( ( OrderOpenPrice()-OrderClosePrice() )/MarketInfo( OrderSymbol(), MODE_POINT )*tmpPipDelta );
                 }
/*
               else if( OrderOpenTime()>=StartOfDay-( 60*60*24*TimeDayOfWeek( StartOfDay ) ) )
                 {
                  if( OrderType()==OP_BUY ) yesterdays_pips=yesterdays_pips+( ( OrderClosePrice()-OrderOpenPrice() )/MarketInfo( OrderSymbol(), MODE_POINT )*tmpPipDelta );
                  else if( OrderType()==OP_SELL ) yesterdays_pips=yesterdays_pips+( ( OrderOpenPrice()-OrderClosePrice() )/MarketInfo( OrderSymbol(), MODE_POINT )*tmpPipDelta );
                 }
*/
              }
           }
         _Draw_Label( 5, 27+( Size_Market_Price*4 ), Size_Market_Price/3, "Weekly Pip Summary", "Weekly Total : "+DoubleToStr( history_all_pips, 0 ), Trend_Color, "Arial Black" );
         Summary_Color=Trend_Color;
         if( history_all_pips+current_all_pips>0 ) Summary_Color=Trend_UpColor;
         else if( history_all_pips+current_all_pips<0 ) Summary_Color=Trend_DnColor;
         _Draw_Label( 5, 25+( Size_Market_Price*3 ), Size_Market_Price/2, "Secured All Pip Summary", DoubleToStr( history_all_pips+current_all_pips, 0 ), Summary_Color, "Arial Black" );
         Summary_Color=Trend_Color;
         if( history_currency_pips+current_currency_pips>0 ) Summary_Color=Trend_UpColor;
         else if( history_currency_pips+current_currency_pips<0 ) Summary_Color=Trend_DnColor;
         _Draw_Label( 5+( Extra_Digit_SizeDiff*6 ), 25+( Size_Market_Price*3 ), Size_Market_Price/2, "Secured "+Symbol()+" Pip Summary", DoubleToStr( history_currency_pips+current_currency_pips, 0 ), Summary_Color, "Arial Black" );
        }
     }

   if( TFSummary_Period>0 )
     {
      int WindowRange=( WindowPriceMax()-WindowPriceMin() )/Point;
      int WindowHeight=WindowRange*TFSummary_Scale;
      int WindowOffset=WindowRange*TFSummary_TopOffset;
      double TFSummary_Height=iHigh( Symbol(), TFSummary_Period, iHighest( Symbol(), TFSummary_Period, MODE_HIGH, TFSummary_Bars, 0 ) );
      int TFSummary_Range=( TFSummary_Height-iLow( Symbol(), TFSummary_Period, iLowest( Symbol(), TFSummary_Period, MODE_LOW, TFSummary_Bars, 0 ) ) )/Point;
      for( TFS=TFSummary_Bars-1; TFS>=0; TFS-- )
        {
         ObjectSet( "["+Indicator_Name+"] TFSwick"+TFS, OBJPROP_TIME1, Time[0]+( ( TFSummary_Offset-TFS )*60*Period() ) );
         ObjectSet( "["+Indicator_Name+"] TFSwick"+TFS, OBJPROP_TIME2, Time[0]+( ( TFSummary_Offset-TFS )*60*Period() ) );
         if( iOpen( Symbol(), TFSummary_Period, TFS )>iClose( Symbol(), TFSummary_Period, TFS ) ) ObjectSet( "["+Indicator_Name+"] TFSwick"+TFS, OBJPROP_COLOR, Trend_DnColor );
         else if( iOpen( Symbol(), TFSummary_Period, TFS )<iClose( Symbol(), TFSummary_Period, TFS ) ) ObjectSet( "["+Indicator_Name+"] TFSwick"+TFS, OBJPROP_COLOR, Trend_UpColor );
         else ObjectSet( "["+Indicator_Name+"] TFSwick"+TFS, OBJPROP_COLOR, Trend_Color );
         int TFSummary_High=( TFSummary_Height-iHigh( Symbol(), TFSummary_Period, TFS ) )/Point*TFSummary_Scale;
         int TFSummary_Low=( TFSummary_Height-iLow( Symbol(), TFSummary_Period, TFS ) )/Point*TFSummary_Scale;
         ObjectSet( "["+Indicator_Name+"] TFSwick"+TFS, OBJPROP_PRICE1, WindowPriceMax()-( ( WindowOffset+TFSummary_High )*Point ) );
         ObjectSet( "["+Indicator_Name+"] TFSwick"+TFS, OBJPROP_PRICE2, WindowPriceMax()-( ( WindowOffset+TFSummary_Low )*Point ) );

         int TFSummary_Open=( TFSummary_Height-iOpen( Symbol(), TFSummary_Period, TFS ) )/Point*TFSummary_Scale;
         int TFSummary_Close=( TFSummary_Height-iClose( Symbol(), TFSummary_Period, TFS ) )/Point*TFSummary_Scale;
         ObjectSet( "["+Indicator_Name+"] TFSbody"+TFS, OBJPROP_TIME1, Time[0]+( ( TFSummary_Offset-TFS )*60*Period() ) );
         ObjectSet( "["+Indicator_Name+"] TFSbody"+TFS, OBJPROP_TIME2, Time[0]+( ( TFSummary_Offset-TFS )*60*Period() ) );
         ObjectSet( "["+Indicator_Name+"] TFScec"+TFS, OBJPROP_TIME1, Time[0]+( ( TFSummary_Offset-TFS )*60*Period() ) );
//         if( TFS!=0 ) ObjectSet( "["+Indicator_Name+"] TFScec"+TFS, OBJPROP_PRICE1, ObjectGet( "["+Indicator_Name+"] TFScec"+( TFS-1 ), OBJPROP_PRICE1 ) );
         if( iOpen( Symbol(), TFSummary_Period, TFS )>iClose( Symbol(), TFSummary_Period, TFS ) )
           {
            ObjectSet( "["+Indicator_Name+"] TFSbody"+TFS, OBJPROP_COLOR, Trend_DnColor );
            ObjectSet( "["+Indicator_Name+"] TFSbody"+TFS, OBJPROP_PRICE1, WindowPriceMax()-( ( WindowOffset+TFSummary_Open )*Point ) );
            ObjectSet( "["+Indicator_Name+"] TFSbody"+TFS, OBJPROP_PRICE2, WindowPriceMax()-( ( WindowOffset+TFSummary_Close )*Point ) );
           }
         else if( iOpen( Symbol(), TFSummary_Period, TFS )<iClose( Symbol(), TFSummary_Period, TFS ) )
           {
            ObjectSet( "["+Indicator_Name+"] TFSbody"+TFS, OBJPROP_COLOR, Trend_UpColor );
            ObjectSet( "["+Indicator_Name+"] TFSbody"+TFS, OBJPROP_PRICE2, WindowPriceMax()-( ( WindowOffset+TFSummary_Open )*Point ) );
            ObjectSet( "["+Indicator_Name+"] TFSbody"+TFS, OBJPROP_PRICE1, WindowPriceMax()-( ( WindowOffset+TFSummary_Close )*Point ) );
           }
         else
           {
            ObjectSet( "["+Indicator_Name+"] TFSbody"+TFS, OBJPROP_COLOR, Trend_Color );
            ObjectSet( "["+Indicator_Name+"] TFSbody"+TFS, OBJPROP_PRICE1, WindowPriceMax()-( ( WindowOffset+TFSummary_Open )*Point ) );
            ObjectSet( "["+Indicator_Name+"] TFSbody"+TFS, OBJPROP_PRICE2, WindowPriceMax()-( ( WindowOffset+TFSummary_Close )*Point ) );
           }
         if(
               iOpen( Symbol(), TFSummary_Period, TFS+1 )<iClose( Symbol(), TFSummary_Period, TFS+1 )
            && iClose( Symbol(), TFSummary_Period, TFS )<iLow( Symbol(), TFSummary_Period, TFS+1 )-( CEC_Trigger_Pips*Point*PipDelta )
            && iOpen( Symbol(), TFSummary_Period, TFS )>iClose( Symbol(), TFSummary_Period, TFS )
           )
           {
            ObjectSet( "["+Indicator_Name+"] TFScec"+TFS, OBJPROP_PRICE1, WindowPriceMax()-( ( WindowOffset+TFSummary_Low )*Point ) );
            ObjectSet( "["+Indicator_Name+"] TFScec"+TFS, OBJPROP_COLOR, Trend_DnColor );
            ObjectSet( "["+Indicator_Name+"] TFScec"+TFS, OBJPROP_ARROWCODE, CEC_ArrowDn );
           }
         else if(
               iOpen( Symbol(), TFSummary_Period, TFS+1 )>iClose( Symbol(), TFSummary_Period, TFS+1 )
            && iClose( Symbol(), TFSummary_Period, TFS )>iHigh( Symbol(), TFSummary_Period, TFS+1 )+( CEC_Trigger_Pips*Point*PipDelta )
            && iOpen( Symbol(), TFSummary_Period, TFS )<iClose( Symbol(), TFSummary_Period, TFS )
           )
           {
            ObjectSet( "["+Indicator_Name+"] TFScec"+TFS, OBJPROP_PRICE1, WindowPriceMax()-( ( WindowOffset+TFSummary_High-( Arrow_Shift*TFSummary_Scale*3 ) )*Point ) );
            ObjectSet( "["+Indicator_Name+"] TFScec"+TFS, OBJPROP_COLOR, Trend_UpColor );
            ObjectSet( "["+Indicator_Name+"] TFScec"+TFS, OBJPROP_ARROWCODE, CEC_ArrowUp );
           }
         else
           {
            ObjectSet( "["+Indicator_Name+"] TFScec"+TFS, OBJPROP_PRICE1, 0 );
           }
        }
     }
   return(0);
  }
//+------------------------------------------------------------------+
//| Between Range function                                           |
//|    used to determine if 1 price is between 2 other prices        |
//+------------------------------------------------------------------+
bool _Between_Range( double _First, double _Second, double _Check )
  {
   if(
      ( _First>_Second && _Check<=_First && _Check>=_Second )
      ||
      ( _First<_Second && _Check>=_First && _Check<=_Second )
     )
     {
      return( TRUE );
     }
   else
     {
      return( FALSE );
     }
  }
//+------------------------------------------------------------------+
//| Draws a label                                                    |
//+------------------------------------------------------------------+
void _Draw_Label( int _x, int _y, int _fontSize, string _name, string _text, color _color, string _fontName, int _Display_Corner=-1 )
  {
   if( _Display_Corner==-1 ) _Display_Corner=Display_Corner;
   ObjectDelete( "["+Indicator_Name+"] "+_name );
   ObjectCreate( "["+Indicator_Name+"] "+_name, OBJ_LABEL, 0, 0, 0 );
   ObjectSetText( "["+Indicator_Name+"] "+_name, _text, _fontSize, _fontName, _color );
   ObjectSet( "["+Indicator_Name+"] "+_name, OBJPROP_BACK, TRUE );
   ObjectSet( "["+Indicator_Name+"] "+_name, OBJPROP_CORNER, _Display_Corner );
   ObjectSet( "["+Indicator_Name+"] "+_name, OBJPROP_XDISTANCE, _x );
   ObjectSet( "["+Indicator_Name+"] "+_name, OBJPROP_YDISTANCE, _y );
  }
//+------------------------------------------------------------------+
//| Determines slope                                                 |
//+------------------------------------------------------------------+
int _GetSlope( int slope_period, int slope_method, int slope_price, int slope_shift, int slope_bars )
  {
//   double delta=1;
   double slope_previous=iMA( Symbol(), Period(), slope_period, 0, slope_method, slope_price, slope_shift+slope_bars );
   double slope_current =iMA( Symbol(), Period(), slope_period, 0, slope_method, slope_price, slope_shift );
   double slope=( ( slope_current-slope_previous )/Point*PipDelta );///slope_bars;
   return( slope );                            
  }
//+------------------------------------------------------------------+