/*------------------------------------------------------------------------------------
   Name: TMS-Osc.mq4
   Copyright ©2010, Xaphod, http://forexwhiz.appspot.com
   
   Description: Oscillator for the Trading Made Simple System.
                Similar to TDI RSI Signal Line and Trade Signal Line.
                	          
   Change log: 
       2014-05-31. Xaphod, v1.600 
          - Update for build 600
       2011-05-16. Xaphod, v1.00 
          - First Release 
-------------------------------------------------------------------------------------*/
// Indicator properties
#property copyright "©2010, xaphod"
#property link      "http://www.xaphod.com"

#property strict
#property version    "1.600"
#property description "Oscillator for the 'Trading Made Simple' system."
#property description "Same as the TDI RSI Signal Line and Trade Signal Line."
#property description " "
#property description "Troubleshooting: Check the 'Journal' and 'Expert' tabs in the terminal window for errors."

#property indicator_separate_window
#property indicator_buffers 4
#property indicator_color1 CLR_NONE
#property indicator_width1 1
#property indicator_color2 CLR_NONE
#property indicator_width2 1
#property indicator_color3 Red
#property indicator_width3 2
#property indicator_color4 Green
#property indicator_width4 2
#property indicator_level1 32
#property indicator_level2 50
#property indicator_level3 68
#property indicator_levelstyle STYLE_DOT
#property indicator_levelcolor DimGray

//#include <xDebug.mqh>

// Constant definitions
#define INDICATOR_NAME "TMS-Osc"


// 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_Method=MODE_SMA;   
extern int                TradeSignal_Period=7;      
extern ENUM_MA_METHOD     TradeSignal_Method=MODE_SMA; 
extern int                MarketBase_Period=34;      
extern ENUM_MA_METHOD     MarketBase_Method=MODE_SMA;  


// Global module varables
double gadRSI[];
double gadRSISig[];
double gadTradeSig[];
double gadMktBase[];


//-----------------------------------------------------------------------------
// function: init()
// Description: Custom indicator initialization function.
//-----------------------------------------------------------------------------
int init() {
  SetIndexStyle(0, DRAW_LINE);
  SetIndexBuffer(0, gadRSI);
  SetIndexLabel(0,NULL);
  SetIndexStyle(1, DRAW_LINE);
  SetIndexBuffer(1, gadMktBase);
  SetIndexLabel(1,"Market Base");
  SetIndexStyle(2, DRAW_LINE);
  SetIndexBuffer(2, gadTradeSig);
  SetIndexLabel(2,"Trade Signal");
  SetIndexStyle(3, DRAW_LINE);
  SetIndexBuffer(3, gadRSISig);
  SetIndexLabel(3,"RSI Signal");   
  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=MathMin(Bars-iCountedBars, Bars-RSI_Period-1);
  
  // Calc TMS data
  for(i=iNewBars; i>=0; i--) {
    gadRSI[i] = iRSI(NULL,0,RSI_Period,RSI_Price,i); 
  }
  for(i=iNewBars; i>=0; i--) {  
    gadRSISig[i]=iMAOnArray(gadRSI,0,RSISignal_Period,0,RSISignal_Method,i);
    gadTradeSig[i]=iMAOnArray(gadRSI,0,TradeSignal_Period,0,TradeSignal_Method,i);    
    gadMktBase[i]=iMAOnArray(gadRSI,0,MarketBase_Period,0,MarketBase_Method,i);    
  }
  
  return(0);
}
//+------------------------------------------------------------------+


