/*------------------------------------------------------------------------------------
   Name: TDI-sub.mq4
   
   Description: A derivative of the TDI indicator. Shows a subset of the TDI signals:
                 - Draws the RSI Signal Line, Trade Signal Line and Market Base Line.
                 - No Volatility Bands!
                	          
//Modified, 10/jan/2024, by jeanlouie, www.forexfactory.com/jeanlouie
// - alert option, green rsi signal line crosses red trade signal line

-------------------------------------------------------------------------------------*/
// Indicator properties
#property copyright "www.xaphod.com"
#property link      "www.xaphod.com"
#property strict
#property version "1.600"
#property description "A derivative of the TDI indicator. Shows a subset of the TDI signals:"
#property description "   - Draws the RSI Signal Line, Trade Signal Line and Market Base Line."
#property description "   - No Volatility Bands!"
#property indicator_separate_window
#property indicator_buffers 4
#property indicator_color1 clrYellow
#property indicator_width1 1
#property indicator_color2 clrRed
#property indicator_width2 2
#property indicator_color3 clrGreen
#property indicator_width3 2
#property indicator_color4 CLR_NONE
#property indicator_width4 1
#property indicator_level1 32
#property indicator_level2 50
#property indicator_level3 68
#property indicator_levelstyle STYLE_DOT
#property indicator_levelcolor DimGray

#define INDICATOR_NAME "TDI-sub"

// Indicator parameters
extern int                RSI_Period=13;            
extern ENUM_APPLIED_PRICE RSI_Price=PRICE_CLOSE;    
extern int                RSISignal_Period=2;   
extern ENUM_MA_METHOD     RSISignal_Mode=MODE_SMA;
extern int                TradeSignal_Period=7;      
extern ENUM_MA_METHOD     TradeSignal_Mode=MODE_SMA; 
extern int                MarketBase_Period=34;      
extern ENUM_MA_METHOD     MarketBase_Mode=MODE_SMA;  
extern bool               AlertPopUp = false;
extern bool               AlertEmail = false;
extern bool               AlertPush = false;



// Global module varables
double gdaRSI[];
double gdaRSISig[];
double gdaTradeSig[];
double gdaMktBase[];


//-----------------------------------------------------------------------------
// function: init()
// Description: Custom indicator initialization function.
//-----------------------------------------------------------------------------
int init() {
  SetIndexStyle(0, DRAW_LINE);
  SetIndexBuffer(0, gdaMktBase);
  SetIndexLabel(0,"Market Base");
  SetIndexStyle(1, DRAW_LINE);
  SetIndexBuffer(1, gdaTradeSig);
  SetIndexLabel(1,"Trade Signal");
  SetIndexStyle(2, DRAW_LINE);
  SetIndexBuffer(2, gdaRSISig);
  SetIndexLabel(2,"RSI Signal");   
  SetIndexStyle(3, DRAW_NONE);
  SetIndexBuffer(3, gdaRSI);
  SetIndexLabel(3,NULL);
  IndicatorDigits(1);
  IndicatorShortName(INDICATOR_NAME);
  return(0);
}

//-----------------------------------------------------------------------------
// function: deinit()
// Description: Custom indicator deinitialization function.
//-----------------------------------------------------------------------------
int deinit() {
   return (0);
}


///-----------------------------------------------------------------------------
// function: start()
// Description: Custom indicator iteration function.
//-----------------------------------------------------------------------------
int start() {
  int iNewBars;
  int iCountedBars; 
  int i;  
  
  // Get unprocessed ticks
  iCountedBars=IndicatorCounted();
  if(iCountedBars < 0) return (-1); 
  if(iCountedBars>0) iCountedBars--;
  iNewBars=Bars-iCountedBars;

  // Calc TDI data
  for(i=iNewBars-1; i>=0; i--) {
    gdaRSI[i] = iRSI(NULL,0,RSI_Period,RSI_Price,i); 
  }
  for(i=iNewBars-1; i>=0; i--) {  
    gdaRSISig[i]=iMAOnArray(gdaRSI,0,RSISignal_Period,0,RSISignal_Mode,i);
    gdaTradeSig[i]=iMAOnArray(gdaRSI,0,TradeSignal_Period,0,TradeSignal_Mode,i);
    gdaMktBase[i]=iMAOnArray(gdaRSI,0,MarketBase_Period,0,MarketBase_Mode,i);    
  } 
  if(AlertPopUp||AlertPush||AlertEmail){
      static long t_prev;
      if(IndicatorCounted()>0 && t_prev!=Time[0]){
         t_prev = Time[0];
         int dir = 0;
         if(gdaRSISig[2]<gdaTradeSig[2] && gdaRSISig[1]>gdaTradeSig[1])dir=1;
         if(gdaRSISig[2]>gdaTradeSig[2] && gdaRSISig[1]<gdaTradeSig[1])dir=-1;
         if(dir!=0){
            string msg = _Symbol+" "+TimeToString(Time[1],TIME_DATE|TIME_MINUTES)+" RSI Signal CROSS Trade Signal: ";
            msg+= dir==1 ? "UP":"DOWN";
            if(AlertPopUp)Alert(msg);
            if(AlertPush)SendNotification(msg);
            if(AlertEmail)SendMail(msg,msg);
         }
      }
  }
  return(0);
}
//+------------------------------------------------------------------+


