//+------------+-----------------------------------------------------+
//| v.22.04.05 |                                     up and Down.mq4 |
//|                      Copyright © 2005, MetaQuotes Software Corp. |
//|                                       http://www.metaquotes.net/ |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2005, MetaQuotes Software Corp."
#property link      "http://www.metaquotes.net/"

#property indicator_separate_window
#property indicator_buffers 3
#property indicator_color1  C'0,144,0'
#property indicator_color2  C'144,0,0'
#property indicator_color3 clrAqua

#property indicator_style3 STYLE_SOLID

#property indicator_style1  STYLE_DOT
#property indicator_style2  STYLE_DOT

#property indicator_minimum -3
#property indicator_maximum 3
#define PREFIX "xxx"

extern int    period          = 12;
input double           LevelUp                   = 0.3;              // UP level ?
input double           LevelDown                 = -0.3;               // DOWN level ?
extern bool   Arrow           = true;
extern int    ArrowSize       = 1;
extern int    SIGNAL_BAR      = 1;
extern color  clArrowBuy      = Aqua;
extern color  clArrowSell     = Aqua; 
extern string          SoundFile  =  "alert2.wav";   //"stops.wav"   //"news.wav"   //"expert.wav"  //"Trumpet.wav";  //
extern bool        AlertsMessage  =  true,   //false,    
                     AlertsSound  =  true,   //false,
                     AlertsEmail  =  false,
                    AlertsMobile  =  false;


 
double      ExtBuffer0[],mahi[],malo[],valc[];
int SGB; string  messageUP, messageDN;
// -------------------------------------------------------------------------------------------------------------
int init()
  {
  IndicatorBuffers(4);
  SetIndexBuffer(0,mahi);SetIndexStyle(0,DRAW_LINE);
  SetIndexBuffer(1,malo);SetIndexStyle(1,DRAW_LINE);
  SetIndexBuffer(2,ExtBuffer0);  SetIndexStyle(2,DRAW_LINE);
  SetIndexBuffer(3,valc); 
    IndicatorShortName("Up and Down");
     SetIndexLabel(2,"Up and Down ["+(string)period+"]");
    return(0);
  }
// -------------------------------------------------------------------------------------------------------------
int deinit()                            
  {                                           
    for (int i = ObjectsTotal()-1; i >= 0; i--)   
    if (StringSubstr(ObjectName(i), 0, StringLen(PREFIX)) == PREFIX)
       ObjectDelete(ObjectName(i));
    return(0);  
  }
// -------------------------------------------------------------------------------------------------------------
int start()
  {
    int limit;
    int counted_bars;
    //double prev, current, old;
    double Value=0,Value1=0,Value2=0,Fish=0,Fish1=0,Fish2=0;
    double price;
    double MinL=0;
    double MaxH=0;
   
    counted_bars = IndicatorCounted();
    if ( counted_bars > 0 ) counted_bars--;
    limit = Bars - counted_bars;

    for(int i=0; i<limit; i++)
      {
        MaxH = High[iHighest(NULL,0,MODE_CLOSE,period,i)];                       
        MinL = Low[iLowest(NULL,0,MODE_CLOSE,period,i)];                          
        price = (Open[i]+ Close[i])/2;                                             

        if(MaxH-MinL == 0) Value = 0.33*2*(0-0.5) + 0.67*Value1;
        else Value = 0.33*2*((price-MaxH)/(MinL-MaxH)-0.5) + 0.67*Value1;

        Value=MathMin(MathMax(Value,-0.999),0.999);
 
        if(1-Value == 0) ExtBuffer0[i]=0.5+0.5*Fish1;
        else ExtBuffer0[i]=0.5*MathLog((1-Value)/(1+Value))+0.5*Fish1;

        Value1=Value;
        Fish1=ExtBuffer0[i];
        mahi[i] = LevelUp;  
        malo[i] = LevelDown;  
        valc[i] = (ExtBuffer0[i]>mahi[i]) ? 1 : (ExtBuffer0[i]<malo[i]) ? -1 :  0;
      }
    int counted_bars2 = IndicatorCounted();
    if ( counted_bars2 > 0 ) counted_bars2--;
    int limit2 = Bars - counted_bars2;
    for ( int j = limit2; j >= 0; j-- )
      {
        if ( Arrow )
          {
            if ( ExtBuffer0[j+SIGNAL_BAR+1] < mahi[j]  && ExtBuffer0[j+SIGNAL_BAR] > mahi[j] ) manageArr(j+1, clArrowBuy,  233, false);
            if ( ExtBuffer0[j+SIGNAL_BAR+1] >  malo[j]  && ExtBuffer0[j+SIGNAL_BAR] <  malo[j] ) manageArr(j+1, clArrowSell, 234, true );
          }
      }
      
     if (AlertsMessage || AlertsEmail || AlertsMobile || AlertsSound) 
    {        ////WindowExpertName()+ 
     messageUP = "Up and Down:  " +_Symbol+"  >>  cross  UP  >>  BUY";
     messageDN = "Up and Down:  " +_Symbol+"  <<  cross  DN  <<  SELL"; 
   //------
     if ( (ExtBuffer0[j+SIGNAL_BAR+1] < 0.3  && ExtBuffer0[j+SIGNAL_BAR] > 0.3)) {   
         if (AlertsMessage) Alert(messageUP);  
         if (AlertsEmail)   SendMail(_Symbol,messageUP);  
         if (AlertsMobile)  SendNotification(messageUP);  
         if (AlertsSound)   PlaySound(SoundFile);   //"stops.wav"   //"news.wav"   //"alert2.wav"  //"expert.wav"  
        } 
   //------
     else 
     if ((ExtBuffer0[j+SIGNAL_BAR+1] > -0.3  && ExtBuffer0[j+SIGNAL_BAR] < -0.3)) {   
         if (AlertsMessage) Alert(messageDN);  
         if (AlertsEmail)   SendMail(_Symbol,messageDN);  
         if (AlertsMobile)  SendNotification(messageDN);  
         if (AlertsSound)   PlaySound(SoundFile);   //"stops.wav"   //"news.wav"   //"alert2.wav"  //"expert.wav"                
        } 
    }
    return(0);
  }
  /////
// -------------------------------------------------------------------------------------------------------------
void manageArr(int j, color clr, int theCode, bool up)   
  {
    string objName = PREFIX + Time[j];
    double gap  = 3.0*iATR(NULL,0,20,j)/4.0;
    
    ObjectCreate(objName, OBJ_ARROW,0,Time[j],0);
    ObjectSet   (objName, OBJPROP_COLOR, clr);  
    ObjectSet   (objName, OBJPROP_ARROWCODE,theCode);
    ObjectSet   (objName, OBJPROP_WIDTH,ArrowSize);  
    if ( up )
      ObjectSet(objName,OBJPROP_PRICE1,Open[j]+gap);
    else  
      ObjectSet(objName,OBJPROP_PRICE1,Close[j] -gap);
  }
// -------------------------------------------------------------------------------------------------------------

