From 6410ec4a3d7d5a3df2226e416131ac0f7e7fb42f Mon Sep 17 00:00:00 2001 From: Toh4iem9 Date: Fri, 15 Aug 2025 15:37:43 +0200 Subject: [PATCH] new files added --- Indicators/MyIndicators/Supertrend.mq5 | 146 +++++++++++++++++++++++++ 1 file changed, 146 insertions(+) create mode 100644 Indicators/MyIndicators/Supertrend.mq5 diff --git a/Indicators/MyIndicators/Supertrend.mq5 b/Indicators/MyIndicators/Supertrend.mq5 new file mode 100644 index 0000000..dcbc9ae --- /dev/null +++ b/Indicators/MyIndicators/Supertrend.mq5 @@ -0,0 +1,146 @@ +//+------------------------------------------------------------------+ +//| Supertrend.mq5 | +//| Copyright 2025, xxxxxxxx | +//| | +//+------------------------------------------------------------------+ +#property copyright "Copyright 2025, xxxxxxxx" +#property link "" +#property version "1.04" // Final stable version, logic aligned with Pine Script +#property description "Supertrend Indicator for trend identification" + +//--- Indicator Window and Plot Properties --- +#property indicator_chart_window +#property indicator_buffers 5 // Supertrend, Color, ATR, UpperBand, LowerBand +#property indicator_plots 1 + +//--- Plot 1: Supertrend line +#property indicator_label1 "Supertrend" +#property indicator_type1 DRAW_COLOR_LINE +#property indicator_color1 clrLimeGreen, clrTomato +#property indicator_style1 STYLE_SOLID +#property indicator_width1 2 + +//--- Input Parameters --- +input int InpAtrPeriod = 10; +input double InpFactor = 3.0; + +//--- Indicator Buffers --- +double BufferSupertrend[]; +double BufferColor[]; +double BufferATR[]; +double BufferUpperBand[]; +double BufferLowerBand[]; + +//--- Global Variables --- +int ExtAtrPeriod; +double ExtFactor; +int handle_atr; + +//+------------------------------------------------------------------+ +//| Custom indicator initialization function. | +//+------------------------------------------------------------------+ +void OnInit() + { + ExtAtrPeriod = (InpAtrPeriod < 1) ? 1 : InpAtrPeriod; + ExtFactor = (InpFactor <= 0) ? 3.0 : InpFactor; + + SetIndexBuffer(0, BufferSupertrend, INDICATOR_DATA); + SetIndexBuffer(1, BufferColor, INDICATOR_COLOR_INDEX); + SetIndexBuffer(2, BufferATR, INDICATOR_CALCULATIONS); + SetIndexBuffer(3, BufferUpperBand, INDICATOR_CALCULATIONS); + SetIndexBuffer(4, BufferLowerBand, INDICATOR_CALCULATIONS); + + ArraySetAsSeries(BufferSupertrend, false); + ArraySetAsSeries(BufferColor, false); + ArraySetAsSeries(BufferATR, false); + ArraySetAsSeries(BufferUpperBand, false); + ArraySetAsSeries(BufferLowerBand, false); + + handle_atr = iATR(_Symbol, _Period, ExtAtrPeriod); + if(handle_atr == INVALID_HANDLE) + Print("Error creating iATR handle."); + + IndicatorSetInteger(INDICATOR_DIGITS, _Digits); + PlotIndexSetInteger(0, PLOT_DRAW_BEGIN, ExtAtrPeriod); + IndicatorSetString(INDICATOR_SHORTNAME, StringFormat("Supertrend(%d, %.1f)", ExtAtrPeriod, ExtFactor)); + } + +//+------------------------------------------------------------------+ +//| Supertrend 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[]) + { + if(rates_total < ExtAtrPeriod) + return(0); + +//--- STEP 1: Get ATR values + if(BarsCalculated(handle_atr) < rates_total) + return(0); + if(CopyBuffer(handle_atr, 0, 0, rates_total, BufferATR) <= 0) + return(0); + +//--- STEP 2: Main calculation loop + for(int i = 1; i < rates_total; i++) + { + double hl2 = (high[i] + low[i]) / 2.0; + + double upper_basic = hl2 + (ExtFactor * BufferATR[i]); + double lower_basic = hl2 - (ExtFactor * BufferATR[i]); + + // --- Stair-step logic + if(upper_basic < BufferUpperBand[i-1] || close[i-1] > BufferUpperBand[i-1]) + BufferUpperBand[i] = upper_basic; + else + BufferUpperBand[i] = BufferUpperBand[i-1]; + + if(lower_basic > BufferLowerBand[i-1] || close[i-1] < BufferLowerBand[i-1]) + BufferLowerBand[i] = lower_basic; + else + BufferLowerBand[i] = BufferLowerBand[i-1]; + + // --- Trend direction + int trend = 0; + if(BufferSupertrend[i-1] == BufferUpperBand[i-1]) + trend = (close[i] > BufferUpperBand[i]) ? 1 : -1; // Previous was Down + else + trend = (close[i] < BufferLowerBand[i]) ? -1 : 1; // Previous was Up + + // --- Set final Supertrend value and color + if(trend == 1) // Current trend is UP + { + BufferSupertrend[i] = BufferLowerBand[i]; + BufferColor[i] = 0; // Green + + // --- FIX: Create the gap on trend change --- + // If the previous trend was DOWN, invalidate its last point + if(BufferSupertrend[i-1] == BufferUpperBand[i-1]) + { + BufferSupertrend[i-1] = EMPTY_VALUE; + } + } + else // Current trend is DOWN + { + BufferSupertrend[i] = BufferUpperBand[i]; + BufferColor[i] = 1; // Red + + // --- FIX: Create the gap on trend change --- + // If the previous trend was UP, invalidate its last point + if(BufferSupertrend[i-1] == BufferLowerBand[i-1]) + { + BufferSupertrend[i-1] = EMPTY_VALUE; + } + } + } + + return(rates_total); + } +//+------------------------------------------------------------------+