Files
mql5/Indicators/MyIndicators/LinearRegressionChannel.mq5
T

125 lines
4.6 KiB
Plaintext
Raw Normal View History

2025-08-30 00:00:16 +02:00
//+------------------------------------------------------------------+
//| LinearRegressionChannel.mq5 |
//| Copyright 2025, xxxxxxxx |
//| |
//+------------------------------------------------------------------+
#property copyright "Copyright 2025, xxxxxxxx"
#property link ""
2025-08-30 13:48:51 +02:00
#property version "1.01" // Added color selection
#property description "Draws a Linear Regression Channel where width is based on max deviation."
2025-08-30 00:00:16 +02:00
#property indicator_chart_window
2025-08-30 11:13:01 +02:00
#property indicator_buffers 0
#property indicator_plots 0
2025-08-30 00:00:16 +02:00
//--- Input Parameters ---
2025-08-30 13:48:51 +02:00
input int InpRegressionPeriod = 100; // Period for the regression calculation
input color InpChannelColor = clrRed; // Channel color
input group "Channel Extensions"
2025-08-30 13:48:51 +02:00
input bool InpRayRight = false; // Extend channel to the right
input bool InpRayLeft = false; // Extend channel to the left
2025-08-30 00:00:16 +02:00
//--- Global Variables ---
int g_ExtPeriod;
string g_channel_name;
2025-08-30 11:13:01 +02:00
datetime g_last_update_time;
2025-08-30 00:00:16 +02:00
//+------------------------------------------------------------------+
//| Custom indicator initialization function. |
//+------------------------------------------------------------------+
int OnInit()
{
g_ExtPeriod = (InpRegressionPeriod < 2) ? 2 : InpRegressionPeriod;
g_channel_name = "LinRegChannel_" + IntegerToString(ChartID()) + "_" + IntegerToString(GetTickCount());
g_last_update_time = 0;
IndicatorSetString(INDICATOR_SHORTNAME, StringFormat("LinReg(%d)", g_ExtPeriod));
2025-08-30 00:00:16 +02:00
2025-08-30 11:13:01 +02:00
EventSetTimer(1);
2025-08-30 00:00:16 +02:00
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function. |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
2025-08-30 11:13:01 +02:00
EventKillTimer();
2025-08-30 00:00:16 +02:00
ObjectDelete(0, g_channel_name);
ChartRedraw();
}
2025-08-30 11:13:01 +02:00
//+------------------------------------------------------------------+
//| Timer event handler. |
//+------------------------------------------------------------------+
void OnTimer()
{
UpdateChannel();
2025-08-30 11:13:01 +02:00
}
2025-08-30 00:00:16 +02:00
//+------------------------------------------------------------------+
//| 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[])
{
if(rates_total < g_ExtPeriod)
return(0);
2025-08-30 00:00:16 +02:00
datetime last_bar_time = time[rates_total - 1];
if(last_bar_time > g_last_update_time)
{
UpdateChannel();
2025-08-30 00:00:16 +02:00
g_last_update_time = last_bar_time;
}
return(rates_total);
}
//+------------------------------------------------------------------+
//| Updates the position and properties of the regression channel. |
//+------------------------------------------------------------------+
void UpdateChannel()
2025-08-30 00:00:16 +02:00
{
2025-08-30 11:13:01 +02:00
if(Bars(_Symbol, _Period) < g_ExtPeriod)
return;
datetime time1 = iTime(_Symbol, _Period, g_ExtPeriod - 1);
datetime time2 = iTime(_Symbol, _Period, 0);
2025-08-30 00:00:16 +02:00
if(ObjectFind(0, g_channel_name) < 0)
{
if(!ObjectCreate(0, g_channel_name, OBJ_REGRESSION, 0, time1, 0, time2, 0))
{
Print("Error creating regression channel object: ", GetLastError());
return;
}
// Set visual properties only once on creation
2025-08-30 00:00:16 +02:00
ObjectSetInteger(0, g_channel_name, OBJPROP_STYLE, STYLE_SOLID);
ObjectSetInteger(0, g_channel_name, OBJPROP_FILL, false);
ObjectSetInteger(0, g_channel_name, OBJPROP_SELECTABLE, false);
}
2025-08-30 13:48:51 +02:00
// Update properties on every call to allow for dynamic changes
ObjectSetInteger(0, g_channel_name, OBJPROP_TIME, 0, time1);
ObjectSetInteger(0, g_channel_name, OBJPROP_TIME, 1, time2);
ObjectSetInteger(0, g_channel_name, OBJPROP_RAY_RIGHT, InpRayRight);
ObjectSetInteger(0, g_channel_name, OBJPROP_RAY_LEFT, InpRayLeft);
2025-08-30 13:48:51 +02:00
// --- FIX: Set color based on input ---
ObjectSetInteger(0, g_channel_name, OBJPROP_COLOR, InpChannelColor);
2025-08-30 00:00:16 +02:00
ChartRedraw();
}
//+------------------------------------------------------------------+
//+------------------------------------------------------------------+