//+------------------------------------------------------------------+
//|                                       SymphonieMatrix v3.0.1.mq4 |
//|                     Copyright © 2011, Evaluator-Symphonie System |
//|                                        http://www.metaquotes.net |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2011, Symphonie Trader System"
#property link      "http://www.metaquotes.net"

#property indicator_separate_window
#property indicator_minimum 0
#property indicator_maximum 5
#property indicator_buffers 8
#property indicator_color1 Blue
#property indicator_color2 Red
#property indicator_color3 Blue
#property indicator_color4 Red
#property indicator_color5 Blue
#property indicator_color6 Red
#property indicator_color7 Blue
#property indicator_color8 Red

//---- parameters
extern bool      CalcOnNewBar = true;
extern bool      AlarmOn = false;
extern bool      AlarmMail = false;
extern string    CmtTF0 = "TimeFrame Values";
extern string    CmtTF1 = "0-Current Chart";
extern string    CmtTF2 = "1-1 Min,5-5 Min,15-15 Min";
extern string    CmtTF3 = "30-30 Min,60-1 HR,240-4 HR";
extern string    CmtTF4 = "1440=1 Day";
extern int       TimeFrame = 0;
extern string    Cmt0 = "Sentiment Variables";
extern int       SentimentPeriod = 12;
extern string    Cmt1 = "Emotion Variables";
extern int       EmotionSSP = 7;
extern double    EmotionKmax = 50.6; //24 21.6 21.6 
extern int       EmotionCountBars = 500;
extern string    Cmt2 = "Trendline Variables";
extern int       TrendlineCciPeriod = 20;
extern int       TrendlineAtrPeriod = 5;
extern string    Cmt3 = "Extreme Variables";
extern int r = 12;
extern int s = 12;
extern int u = 5;
extern int barCount = 500;

extern int BarWidth = 0;
extern color BarColorUp = Blue;
extern color BarColorDown = Red;
extern color TextColor = White;

double Gap = 1; // Gap between the lines of bars
//---- buffers
double buf4_up[];
double buf4_down[];
double buf3_up[];
double buf3_down[];
double buf2_up[];
double buf2_down[];
double buf1_up[];
double buf1_down[];
double haOpen;
double haClose;
double haOpen2;
double haClose2;


string shortname = "Symphonie Matrix v3.0.1";
string Per = "";

int ArrSize = 110;//159;
int UniqueNum = 2282;
int Period_1, Period_2, Period_3, Period_4;
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
  
  
   SetIndexStyle(0,DRAW_ARROW,0,BarWidth,BarColorUp);
   SetIndexArrow(0,ArrSize);
   SetIndexBuffer(0,buf1_up);
   SetIndexEmptyValue(0,0.0);
   SetIndexStyle(1,DRAW_ARROW,0,BarWidth,BarColorDown);
   SetIndexArrow(1,ArrSize);
   SetIndexBuffer(1,buf1_down);
   SetIndexEmptyValue(1,0.0);
   SetIndexStyle(2,DRAW_ARROW,0,BarWidth,BarColorUp);
   SetIndexArrow(2,ArrSize);
   SetIndexBuffer(2,buf2_up);
   SetIndexEmptyValue(2,0.0);
   SetIndexStyle(3,DRAW_ARROW,0,BarWidth,BarColorDown);
   SetIndexArrow(3,ArrSize);
   SetIndexBuffer(3,buf2_down);
   SetIndexEmptyValue(3,0.0);
   SetIndexStyle(4,DRAW_ARROW,0,BarWidth,BarColorUp);
   SetIndexArrow(4,ArrSize);
   SetIndexBuffer(4,buf3_up);
   SetIndexEmptyValue(4,0.0);
   SetIndexStyle(5,DRAW_ARROW,0,BarWidth,BarColorDown);
   SetIndexArrow(5,ArrSize);
   SetIndexBuffer(5,buf3_down);
   SetIndexEmptyValue(5,0.0);
   SetIndexStyle(6,DRAW_ARROW,0,BarWidth,BarColorUp);
   SetIndexArrow(6,ArrSize);
   SetIndexBuffer(6,buf4_up);
   SetIndexEmptyValue(6,0.0);
   SetIndexStyle(7,DRAW_ARROW,0,BarWidth,BarColorDown);
   SetIndexArrow(7,ArrSize);
   SetIndexBuffer(7,buf4_down);
   SetIndexEmptyValue(7,0.0);
   
   SetIndexLabel(0,NULL);
   SetIndexLabel(1,NULL);
   SetIndexLabel(2,NULL);
   SetIndexLabel(3,NULL);
   SetIndexLabel(4,NULL);
   SetIndexLabel(5,NULL);
   SetIndexLabel(6,NULL);
   SetIndexLabel(7,NULL);
   
   IndicatorDigits(0);
   getPeriod();
   
   shortname = "SymphonieMatrixV3.0.1 (" + Period() + ")";
   shortname = "SymphonieMatrixV3.0.1 (" + TimeToStr(TimeCurrent()) + ")";
   IndicatorShortName(shortname);
   
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
  {

   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+



bool bNewBar()
{
  static int iTimeCheck = 0;
  
  if (iTimeCheck < iTime(0,0,0))
  {
    iTimeCheck = iTime(0,0,0);
    return(true);
  }
  else
  {
    return(false);
  }
}
  
int start()
{
     if (CalcOnNewBar)
     {
       if (!bNewBar())
       {
         return(0);
       }
     }

  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;
  int i, tf;

  //-------------------------------1----------------------------------------   
  double dif = Time[0] - Time[1];
  for (i = ObjectsTotal() -1;i >- 1;i--)
  {
    if (StringFind(ObjectName(i),"FF2_" + UniqueNum + "_") >= 0)
    {
      ObjectDelete(ObjectName(i));
    }
  }
     
  double shift = 0.2;
  for(tf = 1;tf <= 4;tf++)
  {  
    string txt = "??";
    double gp;
     
    switch(tf)
    {
      case 1: 
        txt = "Senti"; 
        gp = 1 + shift;
        break;
      case 2: 
        txt = "Emoti";  
        gp = 1 + Gap + shift; 
        break;
      case 3: 
        txt = "Trend";  
        gp = 1 + Gap * 2 + shift;
        break;
      case 4: 
        txt = "Extr     ";  
        gp = 1 + Gap * 3 + shift; 
        break;
    }
   
    string name = "FF2_"+UniqueNum+"_"+tf+"_"+txt;
    if (!ObjectCreate(name, OBJ_TEXT, WindowFind(shortname), iTime(NULL,0,0)+dif*3, gp))
    {
      Print("error: can't create text_object! code #",GetLastError());
    }
    ObjectSetText(name, txt,8,"Arial", TextColor);
  }
   
   if (EmotionCountBars >= Bars) 
   {
     EmotionCountBars = Bars;
   }
   
   if (Bars <= (EmotionSSP + 1)) 
   {
     return(0);
   }
   
  SetIndexDrawBegin(0,Bars - EmotionCountBars + EmotionSSP);
  SetIndexDrawBegin(1,Bars - EmotionCountBars + EmotionSSP);
  
  double SentimentValue0 = 0.0;
  double EmotionValue0 = 0.0;
  double EmotionValue1 = 0.0;
  double TrendValue0 = 0.0;
  double TrendValue1 = 0.0;
  double ExtremeValue0 = 0.0;
  double ExtremeValue1 = 0.0;
  double ExtremeValue2 = 0.0;
  double tviUpUp, tviUpDown;
  
  for(i = EmotionCountBars - EmotionSSP;i >= 0;i--) 
  { 
     
     SentimentValue0 = iCustom(NULL,TimeFrame,"Symphonie_Sentiment_Indikator",SentimentPeriod,0,i);
     EmotionValue0 = iCustom(NULL,TimeFrame,"Symphonie_Emotion_Indikator_v3.0",EmotionSSP,EmotionKmax,EmotionCountBars,0,i);
     EmotionValue1 = iCustom(NULL,TimeFrame,"Symphonie_Emotion_Indikator_v3.0",EmotionSSP,EmotionKmax,EmotionCountBars,1,i);
     TrendValue0 = iCustom(NULL,TimeFrame,"Symphonie_Trendline_Indikator_v3.0",TrendlineCciPeriod,TrendlineAtrPeriod,0,i);
     TrendValue1 = iCustom(NULL,TimeFrame,"Symphonie_Trendline_Indikator_v3.0",TrendlineCciPeriod,TrendlineAtrPeriod,1,i);
     //ExtremeValue0 = iCustom(NULL,TimeFrame,"Symphonie_Extreme_Cycle_Indikator",PriceActionFilter,Length,MajorCycle,useCycleFilter,UseFilterSMAorRSI,FilterStrengthSMA,FilterStrengthRSI,false,true,"","",4,0,i);
     //ExtremeValue1 = iCustom(NULL,TimeFrame,"Symphonie_Extreme_Cycle_Indikator",PriceActionFilter,Length,MajorCycle,useCycleFilter,UseFilterSMAorRSI,FilterStrengthSMA,FilterStrengthRSI,false,true,"","",4,1,i);
     //ExtremeValue2 = iCustom(NULL,TimeFrame,"Symphonie_Extreme_Cycle_Indikator",PriceActionFilter,Length,MajorCycle,useCycleFilter,UseFilterSMAorRSI,FilterStrengthSMA,FilterStrengthRSI,false,true,"","",4,2,i);
     tviUpUp = iCustom(NULL, TimeFrame, "Symphone_Extreme_Indikator_v3.0",r,s,u,"",barCount,"","",0,false,"","" , 0,i);
     tviUpDown = iCustom(NULL, TimeFrame, "Symphone_Extreme_Indikator_v3.0",r,s,u,"",barCount,"","",0,false,"","" , 2,i);
     
        
     buf1_down[i] = 0;
     buf1_up[i] = 0;
     if (SentimentValue0 < 0.0)  
     {
       buf1_down[i] = 1; 
     }          
     else 
     {
       buf1_up[i] = 1;
     }
     
     buf2_down[i] = 0;
     buf2_up[i] = 0;
     if (EmotionValue1 == 1.0)  
     {
       buf2_down[i] = 1 + Gap * 1; 
     }          
     else 
     {
       buf2_up[i] = 1 + Gap * 1;
     }
     
     buf3_down[i] = 0;
     buf3_up[i] = 0;
     if ((TrendValue1 > 0.0) && (TrendValue1 != EMPTY_VALUE))  
     {
       buf3_down[i] = 1 + Gap * 2; 
     }          
     else 
     {
       if ((TrendValue0 > 0.0) && (TrendValue0 != EMPTY_VALUE))
       {
         buf3_up[i] = 1 + Gap * 2;
       }
     }
     
     static int savedState = 0;
     buf4_down[i] = 0;
     buf4_up[i] = 0;
     if (tviUpUp!=EMPTY_VALUE || tviUpDown!=EMPTY_VALUE)  
     {
       buf4_up[i] = 1 + Gap * 3; 
     }          
     else 
     {
        buf4_down[i] = 1 + Gap * 3; 
     }
   } 

  if ((AlarmOn || AlarmMail) && (Period() != PERIOD_W1))
   {
    double prev_signal = 0;
    if (!GlobalVariableCheck("prev_signal_"+Symbol()+Per))
        GlobalVariableSet("prev_signal_"+Symbol()+Per,0); 
    prev_signal = GlobalVariableGet("prev_signal_"+Symbol()+Per); 
   
    if (buf1_up[0] > 0 && buf2_up[0] > 0 && buf3_up[0] > 0 && buf4_up[0] > 0)
     {
      if (prev_signal != 1)
       {
        if (AlarmOn) Alert(Symbol(), " , ", Per, ", LONG SYMPHONIE @ ", DoubleToStr(Open[0],Digits));
        if (AlarmMail) SendMail("LONG SYMPHONIE", Symbol() + " , " + Per + ", LONG SYMPHONIE @ " + DoubleToStr(Open[0],Digits));
        prev_signal = 1;
       }
     }
    else
     {
      if (buf1_down[0] > 0 && buf2_down[0] > 0 && buf3_down[0] > 0 && buf4_down[0] > 0)
       {
        if (prev_signal != -1)
         {
          if (AlarmOn) Alert(Symbol(), " , ", Per, ", SHORT SYMPHONIE @ ", DoubleToStr(Open[0],Digits));
          if (AlarmMail) SendMail("SHORT SYMPHONIE", Symbol() + " , " + Per + ", SHORT SYMPHONIE @ " + DoubleToStr(Open[0],Digits));
          prev_signal = -1;
         }
       }
      else prev_signal = 0;
     }
    GlobalVariableSet("prev_signal_"+Symbol()+Per,prev_signal); 
   }
    
  return(0);
}
  
  
  
//+------------------------------------------------------------------+
void getPeriod()
{
   switch(Period()) 
      {
         case 1: 
            Period_1=60; Period_2=240; Period_3=1440; Period_4=10080;
            Per = "M1";
            break;
         case 5: 
            Period_1=240; Period_2=1440; Period_3=10080; Period_4=43200;
            Per = "M5";
            break;
         case 15: 
            Period_1=1440; Period_2=10080; Period_3=43200; Period_4=43200;
            Per = "M15";
            break;
         case 30: 
            Period_1=10080; Period_2=43200; Period_3=43200; Period_4=43200;
            Per = "M30";
            break;
         case 60: 
            Period_1=43200; Period_2=43200; Period_3=43200; Period_4=43200;
            Per = "H1";
            break;
         case 240: 
            Period_1=43200; Period_2=43200; Period_3=43200; Period_4=43200;
            Per = "H4";
            break;
         case 1440: 
            Period_1=43200; Period_2=43200; Period_3=43200; Period_4=43200;
            Per = "D1";
            break;
         case 10080: 
            Period_1=43200; Period_2=43200; Period_3=43200; Period_4=43200;
            Per = "W1";
            break;
         case 43200: 
            Period_1=43200; Period_2=43200; Period_3=43200; Period_4=43200;
            Per = "M1";
            break;
         
      }
}

string tf2txt(int tf)
{
   if (tf == PERIOD_M1)    return("M1");
   if (tf == PERIOD_M5)    return("M5");
   if (tf == PERIOD_M15)   return("M15");
   if (tf == PERIOD_M30)   return("M30");
   if (tf == PERIOD_H1)    return("H1");
   if (tf == PERIOD_H4)    return("H4");
   if (tf == PERIOD_D1)    return("D1");
   if (tf == PERIOD_W1)    return("W1");
   if (tf == PERIOD_MN1)   return("MN");
   
   return("??");
}



