From 4bbc548f161634ea91996f7dfa49403126007b0c Mon Sep 17 00:00:00 2001 From: Toh4iem9 Date: Wed, 27 Aug 2025 09:07:40 +0200 Subject: [PATCH] new files added --- .../TSI_Oscillator_HeikinAshi.mq5 | 267 ++++++++++++++++++ 1 file changed, 267 insertions(+) create mode 100644 Indicators/MyIndicators/TSI_Oscillator_HeikinAshi.mq5 diff --git a/Indicators/MyIndicators/TSI_Oscillator_HeikinAshi.mq5 b/Indicators/MyIndicators/TSI_Oscillator_HeikinAshi.mq5 new file mode 100644 index 0000000..9084bd1 --- /dev/null +++ b/Indicators/MyIndicators/TSI_Oscillator_HeikinAshi.mq5 @@ -0,0 +1,267 @@ +//+------------------------------------------------------------------+ +//| TSI_Oscillator_HeikinAshi.mq5 | +//| Copyright 2025, xxxxxxxx | +//| | +//+------------------------------------------------------------------+ +#property copyright "Copyright 2025, xxxxxxxx" +#property link "" +#property version "1.00" +#property description "TSI Oscillator on Heikin Ashi data" + +#include + +//--- Indicator Window and Plot Properties --- +#property indicator_separate_window +#property indicator_buffers 1 +#property indicator_plots 1 +#property indicator_type1 DRAW_HISTOGRAM +#property indicator_color1 clrSilver +#property indicator_width1 1 +#property indicator_label1 "HA_TSI_Osc" +#property indicator_level1 0.0 +#property indicator_levelstyle STYLE_DOT + +//--- Enum for selecting Heikin Ashi price source --- +enum ENUM_HA_APPLIED_PRICE + { + HA_PRICE_CLOSE, HA_PRICE_OPEN, HA_PRICE_HIGH, HA_PRICE_LOW + }; + +//--- Input Parameters --- +input int InpSlowPeriod = 25; +input int InpFastPeriod = 13; +input ENUM_HA_APPLIED_PRICE InpAppliedPrice = HA_PRICE_CLOSE; +input group "Signal Line Settings" +input int InpSignalPeriod = 13; +input ENUM_MA_METHOD InpSignalMAType = MODE_EMA; + +//--- Indicator Buffers --- +double BufferOscillator[]; + +//--- Global Objects and Variables --- +int g_ExtSlowPeriod, g_ExtFastPeriod, g_ExtSignalPeriod; +CHeikinAshi_Calculator *g_ha_calculator; + +//+------------------------------------------------------------------+ +//| 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("HA_TSI_Osc(%d,%d,%d)", g_ExtSlowPeriod, g_ExtFastPeriod, g_ExtSignalPeriod)); + IndicatorSetInteger(INDICATOR_DIGITS, 2); + + g_ha_calculator = new CHeikinAshi_Calculator(); + if(CheckPointer(g_ha_calculator) == POINTER_INVALID) + { + Print("Error creating CHeikinAshi_Calculator object"); + return(INIT_FAILED); + } + return(INIT_SUCCEEDED); + } + +//+------------------------------------------------------------------+ +//| Custom indicator deinitialization function. | +//+------------------------------------------------------------------+ +void OnDeinit(const int reason) + { + if(CheckPointer(g_ha_calculator) != POINTER_INVALID) + { + delete g_ha_calculator; + g_ha_calculator = NULL; + } + } + +//+------------------------------------------------------------------+ +//| TSI Oscillator on Heikin Ashi 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); + +//--- Intermediate Heikin Ashi Buffers + double ha_open[], ha_high[], ha_low[], ha_close[]; + ArrayResize(ha_open, rates_total); + ArrayResize(ha_high, rates_total); + ArrayResize(ha_low, rates_total); + ArrayResize(ha_close, rates_total); + +//--- STEP 1: Calculate Heikin Ashi bars + g_ha_calculator.Calculate(rates_total, open, high, low, close, ha_open, ha_high, ha_low, ha_close); + +//--- STEP 2: Prepare the Heikin Ashi source price array + double ha_price_source[]; + ArrayResize(ha_price_source, rates_total); + switch(InpAppliedPrice) + { + case HA_PRICE_OPEN: + ArrayCopy(ha_price_source, ha_open); + break; + case HA_PRICE_HIGH: + ArrayCopy(ha_price_source, ha_high); + break; + case HA_PRICE_LOW: + ArrayCopy(ha_price_source, ha_low); + break; + default: + ArrayCopy(ha_price_source, ha_close); + break; + } + +//--- STEP 3: Calculate Momentum and its Absolute Value on HA data + double momentum[], abs_momentum[]; + ArrayResize(momentum, rates_total); + ArrayResize(abs_momentum, rates_total); + for(int i=1; i 0) + { + buffer_tsi[i] = 100 * (ema2_momentum[i] / ema2_abs_momentum[i]); + } + } + +//--- STEP 7: 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