//+------------------------------------------------------------------+
//|                                ChartBackgroundColorByMA_BTEv.mq4 |
//|                                          Copyright 2022 mql5.com |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2022 mql5.com"
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict
#property indicator_chart_window

input color              BullishColor       = clrDarkGreen;
input color              BearishColor       = clrMaroon; 
input uint               MaPeriod           = 9;
input ENUM_MA_METHOD     MaMethod           = MODE_SMA;
input ENUM_APPLIED_PRICE MaPrice            = PRICE_CLOSE; 
input uint               MaBar              = 1;      

double ma;
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- indicator buffers mapping
   
//---
   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[])
  {
//---
                ma=iMA(_Symbol,_Period,MaPeriod,0,MaMethod,MaPrice,MaBar);
                if(Bid>ma)
                ChartSetInteger(0,CHART_COLOR_BACKGROUND,BullishColor);
                if(Bid<=ma)
                ChartSetInteger(0,CHART_COLOR_BACKGROUND,BearishColor);
   
//--- return value of prev_calculated for next call
   return(rates_total);
  }
//+------------------------------------------------------------------+
