//+------------------------------------------------------------------+ //| ZigZag.mq5 | //| Copyright © 2024 | //| Indicator for potential 3rd wave | //+------------------------------------------------------------------+ #property indicator_separate_window #property indicator_buffers 6 #property indicator_plots 1 #property indicator_color1 Lime #property indicator_color2 Red // Input parameters input int prd = 8; // ZigZag period input double ret_rate_min = 0.382; // Minimum retracement rate input double ret_rate_max = 0.786; // Maximum retracement rate input bool check_vol_support = true; // Check volume support input double target1_ret = 1.0; // Target 1 retracement input double target2_ret = 1.618; // Target 2 retracement // Indicator buffers double ZigZagBuffer[]; double Target1Buffer[]; double Target2Buffer[]; // Function to calculate ZigZag void CalculateZigZag(int period, double &zigzagBuffer[]) { int bars = Bars; for(int i = period; i < bars; i++) { double highValue = High[iHighest(NULL, 0, MODE_HIGH, period, i)]; double lowValue = Low[iLowest(NULL, 0, MODE_LOW, period, i)]; double zigzagValue = (i % 2 == 0) ? highValue : lowValue; // Alternating zigzag zigzagBuffer[i] = zigzagValue; } } // Initialization function int OnInit() { SetIndexBuffer(0, ZigZagBuffer, INDICATOR_DATA); SetIndexBuffer(1, Target1Buffer, INDICATOR_DATA); SetIndexBuffer(2, Target2Buffer, INDICATOR_DATA); return(INIT_SUCCEEDED); } // Main 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[]) { // Calculate ZigZag pattern CalculateZigZag(prd, ZigZagBuffer); // Iterate through bars and calculate retracement levels for (int i = prd; i < rates_total; i++) { double wave1 = MathAbs(ZigZagBuffer[i-2] - ZigZagBuffer[i-4]); double wave2 = MathAbs(ZigZagBuffer[i-4] - ZigZagBuffer[i-6]); double retracement = wave1 / wave2; bool volSupport = (!check_vol_support || (check_vol_support && (volume[i] > volume[i-1]))); if (retracement >= ret_rate_min && retracement <= ret_rate_max && volSupport) { double target1 = close[i] + target1_ret * wave1; double target2 = close[i] + target2_ret * wave1; Target1Buffer[i] = target1; Target2Buffer[i] = target2; // Place alerts here if breakout occurs if (high[i] > ZigZagBuffer[i-2]) { Alert("Breakout Long detected at ", TimeToString(time[i]), ", Price: ", close[i]); } else if (low[i] < ZigZagBuffer[i-2]) { Alert("Breakout Short detected at ", TimeToString(time[i]), ", Price: ", close[i]); } } } return(rates_total); }