//+------------------------------------------------------------------+
//|                                      smStrengthRange09_v2.4
//+------------------------------------------------------------------+
#property copyright "Copyright 14.06.2016, SwingMan"
#property strict

//-- source: smStrengthClusterMom_v3.1; smStrengthRange_v1.0
/*--------------------------------------------------------------------
04.06.2016  v1.0  - First Version. Values between 0 and 9
05.06.2016  v2.0  - Arrows for changes of the last 30 min, like CM_Strength
05.06.2016  v2.1  - Number of the Input parameters reduced
12.06.2016  v2.3  - input levels 3/7 for lines
14.06.2016  v2.4  - # Arrow for levels as parameters
--------------------------------------------------------------------*/

#property indicator_separate_window
#property indicator_buffers 13

#property indicator_width12 4
#property indicator_width13 4

#property indicator_color12 clrGreen
#property indicator_color13 clrRed

#property indicator_minimum  0 
#property indicator_maximum  10

#property indicator_level1 9 
#property indicator_level2 7
#property indicator_level3 5 
#property indicator_level4 3 
#property indicator_level5 1 


#property indicator_levelcolor clrFireBrick
//
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
//enum MOVING_AVG_METHOD
//  {
//   LWMA,
//   Hull
//  };
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
enum ENUM_HISTOGRAM_VALUES
  {
   histogram_StrengthValues, //Show currencies STRENGTH values
   histogram_SlopeValues,    //Show currencies SLOPE values
  };

//---- input parameters
//string Indicator_ID="NextGen CMS:";
//====================================================================
input bool Show_Divergences=true;
string s=""; //----------------- Symbols 
bool    showOnlySymbolOnChart=false;
bool Sort_CurrencyStrength=true; //Sort Currency Strength values
bool Draw_CurrencyHistograms=true;
ENUM_HISTOGRAM_VALUES Histogram_Values=histogram_StrengthValues;
//
//input string m=""; //----------------- Momentum
//input int Momentum_Period=5; //Momentum Period
////
//input string ma=""; //----------------- Moving Average Type
//input int MA_Period=3; 
//extern ENUM_MA_METHOD MA_Method=MODE_LWMA;
////input int Slow=5; //Slow Period//
extern double Maximum_Level=7.0;
extern double Minimum_Level=3.0;
extern int maxBars=200;
//input string p=""; //----------------- Applied Price Type 
//extern ENUM_APPLIED_PRICE Price=PRICE_WEIGHTED;
input string cc=""; //----------------- Currency Colors 
extern color   Color_USD         = clrGreen;
extern color   Color_EUR         = clrBlue;
extern color   Color_GBP         = clrRed;
extern color   Color_CHF         = clrChocolate;
extern color   Color_JPY         = clrYellow;
extern color   Color_AUD         = clrDarkOrange;
extern color   Color_CAD         = clrDodgerBlue;
extern color   Color_NZD         = clrOrchid;
string mp=""; //----------------- My Parameters -----------------
bool Draw_MarketInfos=false;
//bool ATR_Adjusting      =false;
//bool TickValue_Adjusting=false;
double Signal_Treshold=1.0;
int ATR_Period=100;
int Max_Spread=20;
bool Draw_HorizontalLines=false;
//====================================================================

//---- constants
string sObject="str09_";
string symbolsToWeigh="AUDCAD,AUDCHF,AUDJPY,AUDNZD,AUDUSD,CADCHF,CADJPY,"+
                      "CHFJPY,EURAUD,EURCAD,EURCHF,EURGBP,EURJPY,EURNZD,"+
                      "EURUSD,GBPAUD,GBPCAD,GBPCHF,GBPJPY,GBPNZD,GBPUSD,"+
                      "NZDCAD,NZDCHF,NZDJPY,NZDUSD,USDCAD,USDCHF,USDJPY";
int nDecimals=1;
//---- text
int x,y;
int x0=120;
int y0=5;
int yStep=17;
int    font_size=12;
string font_name="Arial Black";

//---- Currency inputs
#define EPSILON       0.00000001
#define CURRENCYCOUNT 8

//---- indicator buffers
double arrUSD[],arrEUR[],arrGBP[],arrCHF[],arrJPY[],arrAUD[],arrCAD[],arrNZD[];
double arrDiffLong[],arrDiffShort[],arrDiffNone[];
double bufHull[];
double divergUP[],divergDN[];
//---- symbol & currency variables
int      symbolCount;
string   symbolNames[];
string   currencyNames[CURRENCYCOUNT]={ "USD","EUR","GBP","CHF","JPY","AUD","CAD","NZD" };
double   currencyValues[CURRENCYCOUNT];      // Currency slope strength
double   currencyValuesPrior[CURRENCYCOUNT]; // Currency slope strength prior bar
double   currencyOccurrences[CURRENCYCOUNT]; // Holds the number of occurrences of each currency in symbols
color    currencyColors[CURRENCYCOUNT];
bool     currencyDrawing[CURRENCYCOUNT];
double   arrayVal[8][3];

//---- variables
int limit;
int userTimeFrame;
int indexSymbol1,indexSymbol2;
double dATR,dATR_D1;
bool newBar,newDay;
datetime oldTime,oldDay;
string windowsName;
int iWindow;
string winSuffix;
//-- slope calculation
double currstrength[8];
double prevstrength[8];
//
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void Set_LineWidth()
  {
//string   currencyNames[CURRENCYCOUNT]={ "USD","EUR","GBP","CHF","JPY","AUD","CAD","NZD" };

   int width=0;
   int width_simple=1;
   int width_double=4;
   int idx0=3;

   if(0>StringFind(Symbol(),"USD",0)) width=width_simple; else width=width_double;
   SetIndexStyle(idx0+0,DRAW_LINE,STYLE_SOLID,width,Color_USD);

   if(0>StringFind(Symbol(),"EUR",0)) width=width_simple; else width=width_double;
   SetIndexStyle(idx0+1,DRAW_LINE,STYLE_SOLID,width,Color_EUR);

   if(0>StringFind(Symbol(),"GBP",0)) width=width_simple; else width=width_double;
   SetIndexStyle(idx0+2,DRAW_LINE,STYLE_SOLID,width,Color_GBP);

   if(0>StringFind(Symbol(),"CHF",0)) width=width_simple; else width=width_double;
   SetIndexStyle(idx0+3,DRAW_LINE,STYLE_SOLID,width,Color_CHF);

   if(0>StringFind(Symbol(),"JPY",0)) width=width_simple; else width=width_double;
   SetIndexStyle(idx0+4,DRAW_LINE,STYLE_SOLID,width,Color_JPY);

   if(0>StringFind(Symbol(),"AUD",0)) width=width_simple; else width=width_double;
   SetIndexStyle(idx0+5,DRAW_LINE,STYLE_SOLID,width,Color_AUD);

   if(0>StringFind(Symbol(),"CAD",0)) width=width_simple; else width=width_double;
   SetIndexStyle(idx0+6,DRAW_LINE,STYLE_SOLID,width,Color_CAD);

   if(0>StringFind(Symbol(),"NZD",0)) width=width_simple; else width=width_double;
   SetIndexStyle(idx0+7,DRAW_LINE,STYLE_SOLID,width,Color_NZD);
  }
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
   int Histogram_Width=4;

//--Buffers  
   SetIndexBuffer(0,arrDiffLong);  SetIndexLabel(0,"DiffLong");
   SetIndexBuffer(1,arrDiffShort); SetIndexLabel(1,"DiffShort");
   SetIndexBuffer(2,arrDiffNone);  SetIndexLabel(2,NULL);

   SetIndexStyle(0,DRAW_HISTOGRAM,STYLE_SOLID,Histogram_Width,clrDodgerBlue);
   SetIndexStyle(1,DRAW_HISTOGRAM,STYLE_SOLID,Histogram_Width,clrPurple);
   SetIndexStyle(2,DRAW_ARROW,STYLE_SOLID,4,clrGray); SetIndexArrow(2,159);

   SetIndexBuffer(3,arrUSD);   SetIndexLabel(3,"USD");
   SetIndexBuffer(4,arrEUR);   SetIndexLabel(4,"EUR");
   SetIndexBuffer(5,arrGBP);   SetIndexLabel(5,"GBP");
   SetIndexBuffer(6,arrCHF);   SetIndexLabel(6,"CHF");
   SetIndexBuffer(7,arrJPY);   SetIndexLabel(7,"JPY");
   SetIndexBuffer(8,arrAUD);   SetIndexLabel(8,"AUD");
   SetIndexBuffer(9,arrCAD);   SetIndexLabel(9,"CAD");
   SetIndexBuffer(10,arrNZD);  SetIndexLabel(10,"NZD");

   int iArrow=159; // up 217, dn 218
   SetIndexBuffer(11,divergUP); SetIndexStyle(11,DRAW_ARROW); SetIndexArrow(11,iArrow); SetIndexLabel(11,"Diverge UP");
   SetIndexBuffer(12,divergDN); SetIndexStyle(12,DRAW_ARROW); SetIndexArrow(12,iArrow); SetIndexLabel(11,"Diverge DN");

   currencyColors[0]=Color_USD;  currencyDrawing[0]=true; //drawUSD;
   currencyColors[1]=Color_EUR;  currencyDrawing[1]=true; //drawEUR;
   currencyColors[2]=Color_GBP;  currencyDrawing[2]=true; //drawGBP;
   currencyColors[3]=Color_CHF;  currencyDrawing[3]=true; //drawCHF;
   currencyColors[4]=Color_JPY;  currencyDrawing[4]=true; //drawJPY;
   currencyColors[5]=Color_AUD;  currencyDrawing[5]=true; //drawAUD;
   currencyColors[6]=Color_CAD;  currencyDrawing[6]=true; //drawCAD;
   currencyColors[7]=Color_NZD;  currencyDrawing[7]=true; //drawNZD;

   Set_LineWidth();

//for(int i=0; i<CURRENCYCOUNT; i++)
//   SetIndexStyle(i+3,DRAW_LINE,STYLE_SOLID,2,currencyColors[i]);

//-- Hull      
   //IndicatorBuffers(12);
   //SetIndexBuffer(11,bufHull);

//-- window 
   if(Histogram_Values==histogram_StrengthValues)
     {
      windowsName=WindowExpertName()+" STRENGTH ";
      winSuffix  ="STR";
     }
   else
     {
      windowsName=WindowExpertName()+" SLOPE ";
      winSuffix  ="SLO";
     }

   IndicatorShortName(windowsName);

   iWindow=WindowFind(windowsName);
   if(iWindow>=0)
     {
      Draw_LevelLine("nullAbove"+winSuffix, Maximum_Level,indicator_levelcolor);
      Draw_LevelLine("nullNull"+winSuffix,  5.0,indicator_levelcolor);
      Draw_LevelLine("nullBelowe"+winSuffix,Minimum_Level,indicator_levelcolor);      
     }

   sObject=sObject+string(iWindow)+"_";

//-- initialisations
   userTimeFrame=Period();
   IndicatorDigits(2);
//...............   
   initSymbols();
//...............   

   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//-- delete labels
   int windex=WindowFind(windowsName);

   if(windex>0)
     {
      ObjectsDeleteAll(windex);
     }
   Delete_Objects(sObject);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
  {

//-- initialisations
   int Last_Bars=0;
   int i,counted_bars;

   if(iWindow<0)
     {
      iWindow=WindowFind(windowsName);
      Draw_LevelLine("nullAbove"+winSuffix,6.0,indicator_levelcolor);
      Draw_LevelLine("nullNull"+winSuffix,0.0,indicator_levelcolor);
      Draw_LevelLine("nullBelowe"+winSuffix,-6.0,indicator_levelcolor);
     }

//-- Panny
   counted_bars=IndicatorCounted();
//---- checking for possible errors
   if(counted_bars<0)
      return(-1);
//---- the last bar will be recounted
   if(maxBars<1)
      maxBars=Bars;
   if(counted_bars>0 && Last_Bars>0)
      counted_bars-=Last_Bars;
   limit = maxBars;
   limit = Bars-IndicatorCounted()-1;
   if(limit>maxBars)limit=maxBars-IndicatorCounted() -1;

   RefreshRates();

//-- draw values ------------------------------------------
   for(i=limit; i>=0; i--)
     {
      //-- ceck new bar -----------------------------------------
      if(iTime(Symbol(),Period(),i)!=oldTime)
        {
         newBar=true;
         oldTime=iTime(Symbol(),Period(),i);
        }
      else newBar=false;
      //-- ceck new Day -----------------------------------------
      int iBarD1=iBarShift(Symbol(),PERIOD_D1,Time[i],false);
      if(iTime(Symbol(),PERIOD_D1,iBarD1)!=oldDay)
        {
         newDay=true;
         oldDay=iTime(Symbol(),PERIOD_D1,iBarD1);
        }
      else newDay=false;

      double diff=0.0;
      ArrayInitialize(currencyValues,0.0);

      //-- Calculate Slope into currencyValues[] buffer
      //..............................      
      Calculate_CurrencyStrength(userTimeFrame,i);
      //..............................

      //-- Draw Currency Lines
      for(int j=0; j<CURRENCYCOUNT; j++)
        {
         switch(j)
           {
            case 0: Draw_CurrencyLines(i,j,diff,arrUSD); break;
            case 1: Draw_CurrencyLines(i,j,diff,arrEUR); break;
            case 2: Draw_CurrencyLines(i,j,diff,arrGBP); break;
            case 3: Draw_CurrencyLines(i,j,diff,arrCHF); break;
            case 4: Draw_CurrencyLines(i,j,diff,arrJPY); break;
            case 5: Draw_CurrencyLines(i,j,diff,arrAUD); break;
            case 6: Draw_CurrencyLines(i,j,diff,arrCAD); break;
            case 7: Draw_CurrencyLines(i,j,diff,arrNZD); break;
           }
        }

      //-- Draw Divergence Arrows
      double val11=arrayVal[indexSymbol1][1];
      double val12=arrayVal[indexSymbol1][2];
      double val21=arrayVal[indexSymbol2][1];
      double val22=arrayVal[indexSymbol2][2];
      bool valuesOK=(val11!=0 && val11!=EMPTY_VALUE) && 
                    (val12!=0 && val12!=EMPTY_VALUE) &&
                    (val21!=0 && val21!=EMPTY_VALUE) &&
                    (val22!=0 && val22!=EMPTY_VALUE);
      if(Show_Divergences==true && valuesOK)
        {
         double valSlope1=arrayVal[indexSymbol1][1]-arrayVal[indexSymbol1][2];
         double valSlope2=arrayVal[indexSymbol2][1]-arrayVal[indexSymbol2][2];
         if(valSlope1>0 && valSlope2<0)
           {
            divergUP[i]=5;
            divergDN[i]=EMPTY_VALUE;
           }
         else
         if(valSlope1<0 && valSlope2>0)
           {
            divergUP[i]=EMPTY_VALUE;
            divergDN[i]=5;
           }
         else
           {
            divergUP[i]=EMPTY_VALUE;
            divergDN[i]=EMPTY_VALUE;
           }
        }

      //-- Draw Histograms
      if(showOnlySymbolOnChart)
        {
         if(Draw_CurrencyHistograms==true)
           {
            Draw_CurrencyHistogram(i,diff);
            //if(Histogram_Values==histogram_StrengthValues)
            //   Draw_CurrencyHistogram(i,diff);
            //else
            //if(Histogram_Values==histogram_SlopeValues)
            //   Draw_SlopeHistogram(i);
           }
        }

      //-- Draw daily horizontal lines
      if(Draw_HorizontalLines==true)
         Draw_DailyHorizontalLines(i);

      //#########################################
      if(i==1)
        {
         ArrayCopy(currencyValuesPrior,currencyValues);
        }
      if(i==0)
        {
         //-- Show ordered table
         Show_CurrencyTable(userTimeFrame);
         if(Draw_MarketInfos)
            Show_MarketInfos();
        }
     }
//---
   return(0);
  }
//  
//====================================================================
//          END of PROGRAM START
//====================================================================
//                *****************************************
//                *****************************************
//                            TEMP FUNCTIONS
//                *****************************************
//                *****************************************
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void Draw_CurrencyLines(int iBar,int jCurr,double &diffX,double &arrCurrency[])
  {
   if(currencyDrawing[jCurr]==true)
     {
      arrCurrency[iBar]=currencyValues[jCurr];

      if(jCurr==indexSymbol1)
         diffX+=currencyValues[jCurr];
      else
      if(jCurr==indexSymbol2)
         diffX-=currencyValues[jCurr];

      //-- values for slope calculation
      arrayVal[jCurr][1]=arrCurrency[iBar];
      arrayVal[jCurr][2]=arrCurrency[iBar+1];
     }
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void Draw_CurrencyHistogram(int iBar,double diffVal)
  {
   if(iBar>limit-1) return;
//-- Slope  
   double diffValSlope;
   double valSlope1=0,valSlope2=0;
   bool slopeUP=false,slopeDN=false;

   if(Histogram_Values==histogram_StrengthValues)
     {
      slopeUP=true;
      slopeDN=true;
     }
   else
   if(Histogram_Values==histogram_SlopeValues)
     {
      valSlope1=arrayVal[indexSymbol1][1]-arrayVal[indexSymbol1][2];
      valSlope2=arrayVal[indexSymbol2][1]-arrayVal[indexSymbol2][2];

      slopeUP=(valSlope1>0 && valSlope2<0);
      slopeDN=(valSlope1<0 && valSlope2>0);
     }
   diffValSlope=valSlope1-valSlope2;

//-- Histogram     
   if(MathAbs(diffVal)<Signal_Treshold)
     {
      arrDiffNone[iBar]=0;
      arrDiffLong[iBar] =EMPTY_VALUE;
      arrDiffShort[iBar]=EMPTY_VALUE;
     }
   else
     {
      //if(diffVal>=0 && slopeUP==true)
      if(slopeUP==true)
        {
         arrDiffLong[iBar] =diffVal;
         arrDiffShort[iBar]=EMPTY_VALUE;
         arrDiffNone[iBar] =EMPTY_VALUE;
        }
      else
      //if(diffVal<0 && slopeDN==true)
      if(slopeDN==true)
        {
         arrDiffLong[iBar] =EMPTY_VALUE;
         arrDiffShort[iBar]=-diffVal;
         arrDiffNone[iBar] =EMPTY_VALUE;
        }
      else
        {
         arrDiffLong[iBar] =EMPTY_VALUE;
         arrDiffShort[iBar]=EMPTY_VALUE;
         arrDiffNone[iBar] =EMPTY_VALUE;
        }
     }
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void Draw_SlopeHistogram(int iBar)
  {
//###### aici mai trebuie 2 array pentru culoarea gri, cind differenta este mai mica de 6  
   if(iBar>limit-1) return;
   double diffVal;
   double val1=0,val2=0;

   val1=arrayVal[indexSymbol1][1]-arrayVal[indexSymbol1][2];
   val2=arrayVal[indexSymbol2][1]-arrayVal[indexSymbol2][2];

   diffVal=10.0*(val1-val2);

   if(MathAbs(diffVal)<Signal_Treshold)
     {
      arrDiffNone[iBar]=0;
      arrDiffLong[iBar] =EMPTY_VALUE;
      arrDiffShort[iBar]=EMPTY_VALUE;
     }
   else
     {
      if(diffVal>=0)
        {
         arrDiffLong[iBar] =diffVal;
         arrDiffShort[iBar]=EMPTY_VALUE;
         arrDiffNone[iBar] =EMPTY_VALUE;
        }
      else
        {
         arrDiffLong[iBar] =EMPTY_VALUE;
         arrDiffShort[iBar]=diffVal;
         arrDiffNone[iBar] =EMPTY_VALUE;
        }
     }
  }
//  
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void Show_CurrencyTable(int tf)
  {
   int jCurr;
   string objectName;
   string showText;
//int windex=WindowFind(WindowExpertName());
   int windex=WindowFind(windowsName);

//-- values -----------------------------------------------
   double tempCurrencyValues[CURRENCYCOUNT][3];

   if(Histogram_Values==histogram_StrengthValues)
     {
      //-- sort the currency strength values --------------
      for(jCurr=0; jCurr<CURRENCYCOUNT; jCurr++)
        {
         tempCurrencyValues[jCurr][0] = currencyValues[jCurr];
         tempCurrencyValues[jCurr][1] = NormalizeDouble(currencyValuesPrior[jCurr], 2);
         tempCurrencyValues[jCurr][2] = jCurr;
        }
     }
   else
   if(Histogram_Values==histogram_SlopeValues)
     {
      //-- sort the slopes of the currency strenght -------
      for(jCurr=0; jCurr<CURRENCYCOUNT; jCurr++)
        {
         double val1=arrayVal[jCurr][1];
         double val2=arrayVal[jCurr][2];

         //tempCurrencyValues[jCurr][0]=val1-val2; //-- here can be adjusted with dATR and TickValue
         if(val2!=0)
            tempCurrencyValues[jCurr][0]=100*(val1-val2)/MathAbs(val2); //-- here can be adjusted with dATR and TickValue
         //tempCurrencyValues[jCurr][0]=100*(val1-val2)/val2; //-- here can be adjusted with dATR and TickValue
         tempCurrencyValues[jCurr][1] = NormalizeDouble(currencyValuesPrior[jCurr], 2);
         tempCurrencyValues[jCurr][2] = jCurr;
        }
     }

//-- Sort currencys for values
   if(Sort_CurrencyStrength==true)
      ArraySort(tempCurrencyValues,WHOLE_ARRAY,0,MODE_DESCEND);

//-- text -------------------------------------------------
   x=x0;
   y=y0-yStep;
   double valSlope;
   string showArrow;

   color colorArrow;
   color clrUP=clrGreen;
   color clrDN=clrRed;

//-- show values for all currencies --------------------
   if(showOnlySymbolOnChart==false || Sort_CurrencyStrength==true)
     {
      //-- show values
      for(int index=0;index<CURRENCYCOUNT;index++)
        {
         jCurr=int(tempCurrencyValues[index][2]);
         if(currencyDrawing[jCurr]==true)
           {
            y=y+yStep;
            objectName=sObject+string(jCurr);

            double value=tempCurrencyValues[index][0];
            string sValue=DoubleToString(value,nDecimals);

            showText=currencyNames[jCurr];
            showText=showText+"   "+sValue;

            //if(value>=8.0 || value<=2.0) 
            if(value>=Maximum_Level || value<=Minimum_Level)             
               showText=showText+"   #";

            //-- slope to previous bar
            valSlope=arrayVal[jCurr][1]-arrayVal[jCurr][2];
            if(valSlope>=0)
              {
               colorArrow=clrUP;
               showArrow=CharToStr(233);   // ARROW
              }
            else
              {
               colorArrow=clrDN;
               showArrow=CharToStr(234);   // ARROW
              }

            if(Histogram_Values==histogram_SlopeValues) showText=showText+"%";

            SetText(windex,objectName,showText,x,y,currencyColors[jCurr],font_size,font_name);
            SetObjText(windex,objectName+"Dir",showArrow,x+20,y+4,colorArrow,font_size);
           }
        }
     }
   else
//-- show values for currencies on chart ---------------
   if(showOnlySymbolOnChart==true)
     {
      //-- first currency name ------------------
      int index=indexSymbol1;
      jCurr=int(tempCurrencyValues[index][2]);
      y=y+yStep;
      objectName=sObject+string(jCurr);

      double value=tempCurrencyValues[index][0]=0;
      string sValue=DoubleToString(value,nDecimals);

      showText=currencyNames[jCurr];
      showText=showText+"   "+sValue;

      showArrow=CharToStr(233);   // ARROW

      if(Histogram_Values==histogram_SlopeValues) showText=showText+"%";

      SetText(windex,objectName,showText,x,y,currencyColors[jCurr],font_size,font_name);
      SetObjText(windex,objectName+"Dir",showArrow,x+20,y,currencyColors[jCurr],font_size);

      //-- second currency name -----------------
      index=indexSymbol2;
      jCurr=int(tempCurrencyValues[index][2]);
      y=y+yStep;
      objectName=sObject+string(jCurr);

      value=tempCurrencyValues[index][0];
      sValue=DoubleToString(value,nDecimals);

      showText=currencyNames[jCurr];
      showText=showText+"   "+sValue;

      if(Histogram_Values==histogram_SlopeValues) showText=showText+"%";

      SetText(windex,objectName,showText,x,y,currencyColors[jCurr],font_size,font_name);
      SetObjText(windex,objectName+"Dir",showArrow,x+20,y,currencyColors[jCurr],font_size);
     }
  }
//+------------------------------------------------------------------+
//|   write Text                                                     |
//+------------------------------------------------------------------+
void SetText(int windowX,string name,string text,int xx,int yy,color colour,int fontsize=12,string fontName="Arial")
  {
   bool res;
   long chartID=ChartID();
   if(ObjectFind(chartID,name)<0)
      res=ObjectCreate(chartID,name,OBJ_LABEL,windowX,0,0);

   ObjectSetInteger(chartID,name,OBJPROP_CORNER,CORNER_RIGHT_UPPER);
   ObjectSetInteger(chartID,name,OBJPROP_XDISTANCE,xx);
   ObjectSetInteger(chartID,name,OBJPROP_YDISTANCE,yy);
   ObjectSetInteger(chartID,name,OBJPROP_COLOR,colour);
   ObjectSetInteger(chartID,name,OBJPROP_FONTSIZE,fontsize);
   ObjectSetString(chartID,name,OBJPROP_TEXT,text);
   ObjectSetString(chartID,name,OBJPROP_FONT,fontName);
  }
//+------------------------------------------------------------------+
//|   write Wingdings                                                |
//+------------------------------------------------------------------+
void SetObjText(int windowX,string name,string CharToStr,int xx,int yy,color colour,int fontsize=12)
  {
   bool res;
   long chartID=ChartID();
   if(ObjectFind(chartID,name)<0)
      res=ObjectCreate(chartID,name,OBJ_LABEL,windowX,0,0);

   ObjectSetInteger(chartID,name,OBJPROP_CORNER,CORNER_RIGHT_UPPER);
   ObjectSetInteger(chartID,name,OBJPROP_FONTSIZE,fontsize);
   ObjectSetInteger(chartID,name,OBJPROP_COLOR,colour);
   ObjectSetInteger(chartID,name,OBJPROP_BACK,false);
   ObjectSetInteger(chartID,name,OBJPROP_XDISTANCE,xx);
   ObjectSetInteger(chartID,name,OBJPROP_YDISTANCE,yy);
   ObjectSetString(chartID,name,OBJPROP_TEXT,CharToStr);
   ObjectSetString(chartID,name,OBJPROP_FONT,"Wingdings");
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void Show_MarketInfos()
  {
   x=x0;
   y=y0-yStep;

   string objectName;
   string showText;
   color  colorText;
   bool res;
//int windex=WindowFind(WindowExpertName());
   int windex=WindowFind(windowsName);

   color colorInfoText=clrDodgerBlue;
   color colorWarningText=clrRed;

//-- Spread -----------------------------------------------
   y=y+yStep;
   objectName=sObject+"Spread";
   if(ObjectFind(objectName)==-1)
     {
      if(ObjectCreate(objectName,OBJ_LABEL,windex,0,0))
        {
         ObjectSet(objectName,OBJPROP_CORNER,CORNER_RIGHT_UPPER);
         ObjectSet(objectName,OBJPROP_XDISTANCE,x);
         ObjectSet(objectName,OBJPROP_YDISTANCE,y);
        }
     }
   showText="Spread: "+DoubleToString(MarketInfo(Symbol(),MODE_SPREAD),0);

   if(MarketInfo(Symbol(),MODE_SPREAD)<=Max_Spread)
      colorText=colorInfoText;
   else
     {
      showText=showText+" > "+string(Max_Spread);
      colorText=colorWarningText;
     }
   res=ObjectSetText(objectName,showText,font_size,font_name,colorText);

//-- Tick Value -------------------------------------------
   y=y+yStep;
   objectName=sObject+"TickValue";
   if(ObjectFind(objectName)==-1)
     {
      if(ObjectCreate(objectName,OBJ_LABEL,windex,0,0))
        {
         ObjectSet(objectName,OBJPROP_CORNER,CORNER_RIGHT_UPPER);
         ObjectSet(objectName,OBJPROP_XDISTANCE,x);
         ObjectSet(objectName,OBJPROP_YDISTANCE,y);
        }
     }
   showText="TickValue: "+DoubleToString(MarketInfo(Symbol(),MODE_TICKVALUE),2);
   res=ObjectSetText(objectName,showText,font_size,font_name,colorInfoText);

//-- ATR --------------------------------------------------
   y=y+yStep;
   objectName=sObject+"ATR";
   if(ObjectFind(objectName)==-1)
     {
      if(ObjectCreate(objectName,OBJ_LABEL,windex,0,0))
        {
         ObjectSet(objectName,OBJPROP_CORNER,CORNER_RIGHT_UPPER);
         ObjectSet(objectName,OBJPROP_XDISTANCE,x);
         ObjectSet(objectName,OBJPROP_YDISTANCE,y);
        }
     }
   showText="ATR["+string(ATR_Period)+"]: "+DoubleToString(dATR/Point,0);
   res=ObjectSetText(objectName,showText,font_size,font_name,colorInfoText);

//-- ATR D1 -----------------------------------------------
   y=y+yStep;
   objectName=sObject+"ATR_D1";
   if(ObjectFind(objectName)==-1)
     {
      if(ObjectCreate(objectName,OBJ_LABEL,windex,0,0))
        {
         ObjectSet(objectName,OBJPROP_CORNER,CORNER_RIGHT_UPPER);
         ObjectSet(objectName,OBJPROP_XDISTANCE,x);
         ObjectSet(objectName,OBJPROP_YDISTANCE,y);
        }
     }
   showText="ATR[D1]: "+DoubleToString(dATR_D1/Point,0);
   res=ObjectSetText(objectName,showText,font_size,font_name,colorInfoText);

//-- GMT offset -------------------------------------------
   y=y+yStep;
   objectName=sObject+"GMT_Offset";
   colorText=colorInfoText;

   double dOffsetServerLocal=MathFloor(TimeLocal()-TimeCurrent())/(60*60);
   int iGMToffset=TimeGMTOffset()/(60*60);
   double dOffset=MathFloor(iGMToffset+dOffsetServerLocal);
   int iOffset=(int)dOffset;

   showText="GMT offset="+string(iOffset);

   if(ObjectFind(objectName)==-1)
     {
      if(ObjectCreate(objectName,OBJ_LABEL,windex,0,0))
        {
         ObjectSet(objectName,OBJPROP_CORNER,CORNER_RIGHT_UPPER);
         ObjectSet(objectName,OBJPROP_XDISTANCE,x);
         ObjectSet(objectName,OBJPROP_YDISTANCE,y);
        }
     }
   res=ObjectSetText(objectName,showText,font_size,font_name,colorText);
//################
   y=y+yStep;
   objectName=sObject+"GMT_Time";
   colorText=colorInfoText;
//datetime thisTime=TimeCurrent();
   showText="GMT Time="+TimeToString(TimeGMT(),TIME_MINUTES|TIME_SECONDS);
//showText="GMT Time="+TimeGMT(thisTime);
//showText="GMT Time="+string(TimeGMT(thisTime)/(60*60));
   if(ObjectFind(objectName)==-1)
     {
      if(ObjectCreate(objectName,OBJ_LABEL,windex,0,0))
        {
         ObjectSet(objectName,OBJPROP_CORNER,CORNER_RIGHT_UPPER);
         ObjectSet(objectName,OBJPROP_XDISTANCE,x);
         ObjectSet(objectName,OBJPROP_YDISTANCE,y);
        }
     }
   res=ObjectSetText(objectName,showText,font_size,font_name,colorText);
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void Draw_DailyHorizontalLines(int iBar)
  {
   static double dClose,dailyOpen,dPivot,dPivotAvg;
   static string objectNameDO;
   string objectName;

//-- Draw Open, Pivot, PivotAverage lines -----------------
   if(newBar==true)
     {
      if(TimeDayOfWeek(Time[iBar])==SATURDAY || TimeDayOfWeek(Time[iBar])==SUNDAY)
         return;

      if(newDay==true)
        {
         //-- daily bar number
         int iBarD1=iBarShift(Symbol(),PERIOD_D1,Time[iBar],false);
         int sub_window=0;

         datetime firstDailyTime=iTime(Symbol(),PERIOD_D1,iBarD1);
         datetime lastDailyTime=firstDailyTime+(PERIOD_D1/Period()-1)*60*Period();

         dailyOpen=iOpen(Symbol(),PERIOD_D1,iBarD1);
         dPivot   =iMA(Symbol(),PERIOD_D1,1,0,MODE_SMA,PRICE_TYPICAL,iBarD1+1);
         dPivotAvg=iMA(Symbol(),PERIOD_D1,3,0,MODE_SMA,PRICE_TYPICAL,iBarD1+1);

         //-- 1. daily open line
         objectNameDO=sObject+"HLine_DailyOpen_"+TimeToString(firstDailyTime);
         if(ObjectCreate(objectNameDO,OBJ_TREND,sub_window,firstDailyTime,dailyOpen,lastDailyTime,dailyOpen))
           {
            ObjectSet(objectNameDO,OBJPROP_COLOR,clrDodgerBlue);
            ObjectSet(objectNameDO,OBJPROP_WIDTH,2);
            ObjectSet(objectNameDO,OBJPROP_BACK,true);
            ObjectSet(objectNameDO,OBJPROP_RAY,false);
           }
         //-- 2. daily pivot line
         objectName=sObject+"HLine_DailyPivot_"+TimeToString(firstDailyTime);
         if(ObjectCreate(objectName,OBJ_TREND,sub_window,firstDailyTime,dPivot,lastDailyTime,dPivot))
           {
            ObjectSet(objectName,OBJPROP_COLOR,clrFireBrick);
            ObjectSet(objectName,OBJPROP_WIDTH,2);
            ObjectSet(objectName,OBJPROP_BACK,true);
            ObjectSet(objectName,OBJPROP_RAY,false);
           }
         //-- 3. daily pivot average line
         objectName=sObject+"HLine_DailyPivotAvg_"+TimeToString(firstDailyTime);
         if(ObjectCreate(objectName,OBJ_TREND,sub_window,firstDailyTime,dPivotAvg,lastDailyTime,dPivotAvg))
           {
            ObjectSet(objectName,OBJPROP_COLOR,clrOrange);
            ObjectSet(objectName,OBJPROP_WIDTH,3);
            ObjectSet(objectName,OBJPROP_BACK,true);
            ObjectSet(objectName,OBJPROP_RAY,false);
           }
        }

      else
      //-- check allowed trading -----------------
      if(newDay==false)
        {
         dClose=iClose(Symbol(),Period(),iBar);

         //-- identify BUY, SELL, OFF day -----------------
         string showTextAllowed="(don't trade)";
         string showText;
         string empty="                 ";
         //-- Open above Pivot ----------------------------------
         if(dailyOpen>dPivot && dPivot>dPivotAvg)
           {
            showText="BUY day";
            if(dClose>dailyOpen) showTextAllowed="(LONG allowed)";
            else
            if(dClose<dPivot)
              {
               showText="BUY day"+" (REVERSE)";
               showTextAllowed="(SHORT allowed)";
              }
           }
         else
         //-- Open belove Pivot ---------------------------------
         if(dailyOpen<dPivot && dPivot<dPivotAvg)
           {
            showText="SELL day";
            if(dClose<dailyOpen) showTextAllowed="(SHORT allowed)";
            else
            if(dClose>dPivot)
              {
               showText="SELL day"+" (REVERSE)";
               showTextAllowed="(LONG allowed)";
              }
           }
         else
           {
            showText="OFF day";
           }

         showText=empty+showText+"  "+showTextAllowed;
         ObjectSetText(objectNameDO,showText);
         ObjectSet(objectNameDO,OBJPROP_BACK,false);
        }
     }
  }
//                *****************************************
//                *****************************************
//                            FUNCTIONS
//                *****************************************
//                *****************************************
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void initSymbols()
  {
   int i,size;

//-- Get extra characters on this crimmal's symbol names
   string symbolExtraChars=StringSubstr(Symbol(),6,4);

//-- Trim user input
   symbolsToWeigh = StringTrimLeft(symbolsToWeigh);
   symbolsToWeigh = StringTrimRight(symbolsToWeigh);

//-- Add extra comma
   int ll=StringLen(symbolsToWeigh);
   if(StringSubstr(symbolsToWeigh,StringLen(symbolsToWeigh)-1)!=",")
     {
      symbolsToWeigh=StringConcatenate(symbolsToWeigh,",");
     }

//-- Build symbolNames array as the user likes it
//-- Split user input
   i=StringFind(symbolsToWeigh,",");
   while(i!=-1)
     {
      size=ArraySize(symbolNames);
      string newSymbol=StringConcatenate(StringSubstr(symbolsToWeigh,0,i),symbolExtraChars);
      if(MarketInfo(newSymbol,MODE_BID)>EPSILON)
        {
         ArrayResize(symbolNames,size+1);
         // Set array
         symbolNames[size]=newSymbol;
        }
      //-- Trim symbols
      symbolsToWeigh=StringSubstr(symbolsToWeigh,i+1);
      i=StringFind(symbolsToWeigh,",");
     }

//-- Kill unwanted symbols from array ---------------------
   if(showOnlySymbolOnChart)
     {
      symbolCount=ArraySize(symbolNames);
      string tempNames[];

      for(i=0; i<symbolCount; i++)
        {
         for(int j=0; j<CURRENCYCOUNT; j++)
           {
            if(StringFind(Symbol(),currencyNames[j])==-1)
              {
               continue;
              }

            if(StringFind(symbolNames[i],currencyNames[j])!=-1)
              {
               size=ArraySize(tempNames);
               ArrayResize(tempNames,size+1);
               tempNames[size]=symbolNames[i];
               break;
              }
           }
        }
      //-- drawing currencies -----------------------------
      for(int j=0; j<CURRENCYCOUNT; j++)
        {
         if(StringFind(Symbol(),currencyNames[j])>=0)
            currencyDrawing[j]=true;
         else
            currencyDrawing[j]=false;
        }

      for(i=0; i<ArraySize(tempNames); i++)
        {
         ArrayResize(symbolNames,i+1);
         symbolNames[i]=tempNames[i];
        }
     }

   symbolCount=ArraySize(symbolNames);

   for(i=0; i<symbolCount; i++)
     {
      // Increase currency occurrence
      int currencyIndex=GetCurrencyIndex(StringSubstr(symbolNames[i],0,3));

      currencyOccurrences[currencyIndex]++;
      currencyIndex=GetCurrencyIndex(StringSubstr(symbolNames[i],3,3));
      currencyOccurrences[currencyIndex]++;
     }

//-- Get indexes for the current pair Symbol())
   indexSymbol1=GetCurrencyIndex(StringSubstr(Symbol(),0,3));
   indexSymbol2=GetCurrencyIndex(StringSubstr(Symbol(),3,3));
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
int GetCurrencyIndex(string currency)
  {
   for(int i=0; i<CURRENCYCOUNT; i++)
     {
      if(currencyNames[i]==currency)
        {
         return(i);
        }
     }
   return (-1);
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void Delete_Objects(string sText)
  {
   int total = ObjectsTotal();
   for(int i = total-1; i>= 0; i--)
     {
      string sObjectName=ObjectName(i);
      if(StringFind(sObjectName,sText,0)!=-1)
         ObjectDelete(sObjectName);
     }
  }
//                *****************************************
//                *****************************************
//                       Currency Strength Calculation
//                *****************************************
//                *****************************************
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void Calculate_CurrencyStrength(int tf,int iBar)
  {
   int i;
//-- Get Slope for all symbols and totalize for all currencies   
   for(i=0; i<symbolCount; i++)
     {
      double slope=Get_StrengthRange(symbolNames[i],tf,iBar);

      string currencyName1=StringSubstr(symbolNames[i], 0, 3);
      string currencyName2=StringSubstr(symbolNames[i], 3, 3);
      currencyValues[GetCurrencyIndex(currencyName1)] += slope;
      currencyValues[GetCurrencyIndex(currencyName2)] += 9.0-slope;
     }
//-- Get average for occurrencies     
   for(i=0; i<CURRENCYCOUNT; i++)
     {
      if(currencyOccurrences[i]>0) currencyValues[i]/=currencyOccurrences[i]; else currencyValues[i]=0;
     }
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
double Get_StrengthRange(string symbol,int tf,int iBar)
  {
//-- Daily Bar
   int iDailyBar=iBarShift(symbol,PERIOD_D1,iTime(symbol,Period(),iBar));
//-- Daily Range
   double dHigh=iHigh(symbol,PERIOD_D1,iDailyBar);
   double dLow =iLow(symbol,PERIOD_D1,iDailyBar);
   double dailyRange=dHigh-dLow;
//-- current price   
   double dClose=iClose(symbol,Period(),iBar);
   double dCloseLow=dClose-dLow;

//-- strength with daily range
   double dStrengthRange=0;
   if(dailyRange!=0)
      dStrengthRange=100.0*(dCloseLow/dailyRange);

//-- set fixed values; why ???
   double settedValue=0;
   if(dStrengthRange>3.0) settedValue=1;
   if(dStrengthRange>10.0) settedValue=2;
   if(dStrengthRange>25.0) settedValue=3;
   if(dStrengthRange>40.0) settedValue=4;
   if(dStrengthRange>50.0) settedValue=5;
   if(dStrengthRange>60.0) settedValue=6;
   if(dStrengthRange>75.0) settedValue=7;
   if(dStrengthRange>90.0) settedValue=8;
   if(dStrengthRange>97.0) settedValue=9;

   return(settedValue);
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void Draw_LevelLine(string objName,double price1,color dColor)
  {
   datetime time1=0;

   ObjectDelete(objName);
   ObjectCreate(objName,OBJ_HLINE,iWindow,time1,price1);
   ObjectSet(objName,OBJPROP_COLOR,dColor);
   ObjectSet(objName,OBJPROP_STYLE,STYLE_SOLID);
  }
//+------------------------------------------------------------------+
