//+------------------------------------------------------------------+
//|                                Damon smSHI_SilverTrendSig_v1.mq4 |
//|                                                         SwingMan |
//|                                                                  |
//+------------------------------------------------------------------+
#property copyright "SwingMan"
#property link      ""
//--------------------------------------------------------------------
//  Source code:
//#property copyright "Copyright © 2005, Shurka"
//#property link      "http://shforex.narod.ru"
//--------------------------------------------------------------------
//  Damon smSHI_SilverTrendSig:
// v1 - no repaint version
//--------------------------------------------------------------------

#property indicator_chart_window
#property indicator_buffers 2
#property indicator_color1 Blue
#property indicator_color2 Red

//--------------------------------------------------------------------
extern int     AllBars=0;
extern int     Otstup=20; //--30
extern double  Per=9;
//--------------------------------------------------------------------

//---- constants
#define  SH_BUY   1
#define  SH_SELL  -1

//---- variables
int      SH,NB,i,UD, lastUD;
double   R,SHMax,SHMin;
double   BufD[];
double   BufU[];
string   sWindows;
datetime thisTime, oldTime;

//--------------------------------------------------------------------

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
{  
   int Ots = Get_DefaultOtstup();
   if (Ots > 0) Otstup = Ots;
   UD = 0;
   lastUD = 0;

   if (Bars<AllBars+Per || AllBars==0) NB=Bars-Per; else NB=AllBars;
   IndicatorBuffers(2);
   sWindows = "Damon smSHI_SilverTrendSig";
   IndicatorShortName(sWindows);
   Comment(sWindows + "  (Otstup=" + Otstup + ")");
   
   SetIndexStyle(0,DRAW_ARROW,0,2);
   SetIndexStyle(1,DRAW_ARROW,0,2);
   SetIndexArrow(0,159);
   SetIndexArrow(1,159);
   SetIndexBuffer(0,BufU);
   SetIndexBuffer(1,BufD);
   SetIndexDrawBegin(0,Bars-NB);
   SetIndexDrawBegin(1,Bars-NB);
   ArrayInitialize(BufD,0.0);
   ArrayInitialize(BufU,0.0);
//----
   return(0);
}
  
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
{
   return(0);
 }
  
  
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
{
   int CB=IndicatorCounted();
   if(CB<0) return(-1); else if(NB>Bars-CB) NB=Bars-CB;
   
   //-- start every minute
   //---- no repaint...  
   thisTime = Time[0];
   if (thisTime == oldTime) return(0);
   oldTime = thisTime;
   
   for (SH=1;SH<NB;SH++)
   {
      for (R=0,i=SH;i<SH+10;i++) {R+=(10+SH-i)*(High[i]-Low[i]);}      R/=55;

      SHMax = High[Highest(NULL,0,MODE_HIGH,Per,SH)];
      SHMin = Low[Lowest(NULL,0,MODE_LOW,Per,SH)];
      
         if (Close[SH]<SHMin+(SHMax-SHMin)*Otstup/100 && UD!=SH_SELL) { 
            BufU[SH]=Low[SH]-R*0.4;  
            UD=SH_SELL;
            if (SH==1) {
               if (lastUD == SH_BUY || lastUD == 0) {
                  lastUD = SH_SELL;
               }
            }
         }
      
         if (Close[SH]>SHMax-(SHMax-SHMin)*Otstup/100 && UD!=SH_BUY) { 
            BufD[SH]=High[SH]+R*0.4; 
            UD=SH_BUY; 
            if (SH==1) {
               if (lastUD == SH_SELL || lastUD == 0) {
                  lastUD = SH_BUY;
               }
            }
         }
   }  
//----
   return(0);
}
//+------------------------------------------------------------------+

int Get_DefaultOtstup()
{
   if (Symbol() == "AUDUSD") return(15);
   if (Symbol() == "EURUSD") return(15);
   if (Symbol() == "GBPUSD") return(20);
   if (Symbol() == "USDCAD") return(10);
   if (Symbol() == "USDCHF") return(15);
   if (Symbol() == "USDJPY") return(15);
   if (Symbol() == "GBPJPY") return(15);
   if (Symbol() == "GBPCHF") return(20);
   if (Symbol() == "EURGBP") return(15);
   if (Symbol() == "NZDUSD") return(10);
   if (Symbol() == "EURJPY") return(15);
   if (Symbol() == "EURCHF") return(20);
   if (Symbol() == "EURAUD") return(30);
   if (Symbol() == "EURCAD") return(20);   
   return(-1);
}