//+------------------------------------------------------------------+ //| TSI_Oscillator.mq5 | //| Copyright 2025, xxxxxxxx | //| | //+------------------------------------------------------------------+ #property copyright "Copyright 2025, xxxxxxxx" #property link "" #property version "1.00" #property description "TSI Oscillator (Histogram of TSI vs Signal Line)" //--- Indicator Window and Plot Properties --- #property indicator_separate_window #property indicator_buffers 1 // Only the final Histogram buffer is needed #property indicator_plots 1 #property indicator_type1 DRAW_HISTOGRAM #property indicator_color1 clrSilver #property indicator_width1 1 #property indicator_label1 "TSI Oscillator" #property indicator_level1 0.0 #property indicator_levelstyle STYLE_DOT //--- Input Parameters --- input int InpSlowPeriod = 25; input int InpFastPeriod = 13; input ENUM_APPLIED_PRICE InpAppliedPrice = PRICE_CLOSE; input group "Signal Line Settings" input int InpSignalPeriod = 13; input ENUM_MA_METHOD InpSignalMAType = MODE_EMA; //--- Indicator Buffers --- double BufferOscillator[]; //--- Global Variables --- int g_ExtSlowPeriod, g_ExtFastPeriod, g_ExtSignalPeriod; //+------------------------------------------------------------------+ //| Custom indicator initialization function. | //+------------------------------------------------------------------+ int OnInit() { g_ExtSlowPeriod = (InpSlowPeriod < 1) ? 1 : InpSlowPeriod; g_ExtFastPeriod = (InpFastPeriod < 1) ? 1 : InpFastPeriod; g_ExtSignalPeriod = (InpSignalPeriod < 1) ? 1 : InpSignalPeriod; SetIndexBuffer(0, BufferOscillator, INDICATOR_DATA); ArraySetAsSeries(BufferOscillator, false); int draw_begin = g_ExtSlowPeriod + g_ExtFastPeriod + g_ExtSignalPeriod - 1; PlotIndexSetInteger(0, PLOT_DRAW_BEGIN, draw_begin); IndicatorSetString(INDICATOR_SHORTNAME, StringFormat("TSI Osc(%d,%d,%d)", g_ExtSlowPeriod, g_ExtFastPeriod, g_ExtSignalPeriod)); IndicatorSetInteger(INDICATOR_DIGITS, 2); return(INIT_SUCCEEDED); } //+------------------------------------------------------------------+ //| TSI Oscillator calculation function. | //+------------------------------------------------------------------+ int OnCalculate(const int rates_total, const int prev_calculated, const datetime &time[], const double &open[], const double &high[], const double &low[], const double &close[], const long &tick_volume[], const long &volume[], const int &spread[]) { int start_pos = g_ExtSlowPeriod + g_ExtFastPeriod + g_ExtSignalPeriod - 1; if(rates_total <= start_pos) return(0); //--- STEP 1: Prepare the source price array double price_source[]; ArrayResize(price_source, rates_total); for(int i=0; i 0) { buffer_tsi[i] = 100 * (ema2_momentum[i] / ema2_abs_momentum[i]); } } //--- STEP 6: Calculate the Signal Line (internal buffer) double buffer_signal[]; ArrayResize(buffer_signal, rates_total); int signal_start_pos = ema2_start_pos + g_ExtSignalPeriod - 1; for(int i = signal_start_pos; i < rates_total; i++) { // --- FIX: Full, robust switch block for all MA types --- switch(InpSignalMAType) { case MODE_EMA: case MODE_SMMA: if(i == signal_start_pos) { double sum=0; for(int j=0; j0) buffer_signal[i]=lwma_sum/weight_sum; } break; default: // MODE_SMA { double sum=0; for(int j=0; j