﻿//+------------------------------------------------------------------+
//|                                            MA_Touch_Arrows.mq5   |
//+------------------------------------------------------------------+
#property indicator_chart_window
#property indicator_buffers 2
#property indicator_plots   2
//--- BUY arrows
#property indicator_label1  "Buy Touch"
#property indicator_type1   DRAW_ARROW
#property indicator_color1  clrLime
#property indicator_width1  2
//--- SELL arrows
#property indicator_label2  "Sell Touch"
#property indicator_type2   DRAW_ARROW
#property indicator_color2  clrRed
#property indicator_width2  2
double BuyBuffer[];
double SellBuffer[];
//--- Inputs
input int MA_Period = 50;
input ENUM_MA_METHOD MA_Type = MODE_EMA;
input ENUM_APPLIED_PRICE Price_Type = PRICE_CLOSE;
//--- Handle
int maHandle;
//+------------------------------------------------------------------+
int OnInit()
{
   SetIndexBuffer(0, BuyBuffer, INDICATOR_DATA);
   SetIndexBuffer(1, SellBuffer, INDICATOR_DATA);
   PlotIndexSetInteger(0, PLOT_ARROW, 233);
   PlotIndexSetInteger(1, PLOT_ARROW, 234);
   PlotIndexSetDouble(0, PLOT_EMPTY_VALUE, EMPTY_VALUE);
   PlotIndexSetDouble(1, PLOT_EMPTY_VALUE, EMPTY_VALUE);
   maHandle = iMA(_Symbol, _Period, MA_Period, 0, MA_Type, Price_Type);
   if(maHandle == INVALID_HANDLE)
      return(INIT_FAILED);
   return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
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 < MA_Period + 2)
      return(0);

   double ma[];
   ArraySetAsSeries(ma, false); // ← JAVÍTÁS: false, nem true!

   if(CopyBuffer(maHandle, 0, 0, rates_total, ma) <= 0)
      return(0);

   int start = prev_calculated > 1 ? prev_calculated - 1 : 1;

   for(int i = start; i < rates_total; i++)
   {
      BuyBuffer[i]  = EMPTY_VALUE;
      SellBuffer[i] = EMPTY_VALUE;

      //--- TOUCH: gyertya range magában foglalja az MA-t
      bool touch = (low[i] <= ma[i] && high[i] >= ma[i]);

      //--- BUY: bullish reakció érintés után
      if(touch && close[i] > ma[i] && close[i] > open[i])
      {
         BuyBuffer[i] = low[i] - (10 * _Point);
      }
      //--- SELL: bearish reakció érintés után
      if(touch && close[i] < ma[i] && close[i] < open[i])
      {
         SellBuffer[i] = high[i] + (10 * _Point);
      }
   }

   return(rates_total);
}
//+------------------------------------------------------------------+