//+------------------------------------------------------------------+
//| TrendColor.mq5|
//| Copyright 2024, MetaQuotes Software Corp. |
//| [Link] |
//+------------------------------------------------------------------+
#property strict
// Define input parameters
input int FastMA = 12;
input int SlowMA = 26;
// Declare global variables
int FastMAHandle;
int SlowMAHandle;
//+------------------------------------------------------------------+
//| Expert initialization function |
//+------------------------------------------------------------------+
int OnInit()
{
// Create moving averages
FastMAHandle = iMA(_Symbol, _Period, FastMA, 0, MODE_SMA, PRICE_CLOSE);
SlowMAHandle = iMA(_Symbol, _Period, SlowMA, 0, MODE_SMA, PRICE_CLOSE);
if (FastMAHandle == INVALID_HANDLE || SlowMAHandle == INVALID_HANDLE)
{
Print("Error creating Moving Averages");
return(INIT_FAILED);
}
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Expert deinitialization function |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
// Release moving averages
if (FastMAHandle != INVALID_HANDLE)
IndicatorRelease(FastMAHandle);
if (SlowMAHandle != INVALID_HANDLE)
IndicatorRelease(SlowMAHandle);
}
//+------------------------------------------------------------------+
//| Expert tick function |
//+------------------------------------------------------------------+
void OnTick()
{
double FastMAValue[], SlowMAValue[];
// Get the last values of the moving averages
if (CopyBuffer(FastMAHandle, 0, 0, 1, FastMAValue) <= 0 ||
CopyBuffer(SlowMAHandle, 0, 0, 1, SlowMAValue) <= 0)
{
Print("Error retrieving Moving Average values");
return;
}
// Determine the market tendency
if (FastMAValue[0] > SlowMAValue[0])
{
// Uptrend
ColorBackground(clrGreen);
}
else if (FastMAValue[0] < SlowMAValue[0])
{
// Downtrend
ColorBackground(clrRed);
}
else
{
// Sideways
ColorBackground(clrYellow);
}
}
//+------------------------------------------------------------------+
//| Color the background |
//+------------------------------------------------------------------+
void ColorBackground(color clr)
{
ChartSetInteger(0, CHART_COLOR_BACKGROUND, clr);
}