//+------------------------------------------------------------------+
//|                                               classy-candles.mq5 |
//|                                          Copyright 2024,JBlanked |
//|                                        https://www.jblanked.com/ |
//+------------------------------------------------------------------+
#property copyright "Copyright 2024,JBlanked"
#property link      "https://www.jblanked.com/"
#property version   "1.00"
#property description "I will draw a buy arrow on the previous candle if the previous candle was bullish, the candle before that was bearish, and the previous candle's low is lower than the candle before last's low"
#property description "I will draw a sell arrow on the previous candle if the previous candle was bearish, the candle before that was bullish, and the previous candle's high is higher than the candle before last's high"
#property indicator_chart_window
#property indicator_buffers 2
#property indicator_plots 2
#property indicator_color1 clrBlue
#property indicator_color2 clrRed
#include <jb-indicator.mqh> // donload from https://github.com/jblanked/MQL-Library/blob/main/JB-Indicator.mqh

CIndicator indi;

double buys[];
double sells[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- indicator buffers mapping
   if(!indi.createBuffer("Buy", DRAW_ARROW, STYLE_SOLID, indicator_color1, 3, 0, buys, true, INDICATOR_DATA, 233))
     {
      return INIT_FAILED;
     }
   if(!indi.createBuffer("Sell", DRAW_ARROW, STYLE_SOLID, indicator_color2, 3, 1, sells, true, INDICATOR_DATA, 234))
     {
      return INIT_FAILED;
     }
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration 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 limit = rates_total - prev_calculated;
   if(prev_calculated < 1)
     {
      ArrayInitialize(buys, EMPTY_VALUE);
      ArrayInitialize(sells, EMPTY_VALUE);
      ArraySetAsSeries(buys, true);
      ArraySetAsSeries(sells, true);
     }
   else
     {
      limit++;
     }
//---
   for(int i = limit - 1; i >= 0; i--)
     {

      if(iLow(_Symbol, PERIOD_CURRENT, i + 1) < iLow(_Symbol, PERIOD_CURRENT, i + 2) &&
         iOpen(_Symbol, PERIOD_CURRENT, i + 2) > iClose(_Symbol, PERIOD_CURRENT, i + 2) &&
         iOpen(_Symbol, PERIOD_CURRENT, i + 1) < iClose(_Symbol, PERIOD_CURRENT, i + 1))
        {
         // buy arrow
         buys[i + 1] = iLow(_Symbol, PERIOD_CURRENT, i + 1);
        }


      if(iHigh(_Symbol, PERIOD_CURRENT, i + 1) > iHigh(_Symbol, PERIOD_CURRENT, i + 2) &&
         iOpen(_Symbol, PERIOD_CURRENT, i + 2) < iClose(_Symbol, PERIOD_CURRENT, i + 2) &&
         iOpen(_Symbol, PERIOD_CURRENT, i + 1) > iClose(_Symbol, PERIOD_CURRENT, i + 1))
        {
         // sell arrow
         sells[i + 1] = iHigh(_Symbol, PERIOD_CURRENT, i + 1);
        }
     }
//--- return value of prev_calculated for next call
   return(rates_total);
  }
//+------------------------------------------------------------------+
