refactor: added selectable ray extensions

This commit is contained in:
Toh4iem9
2025-08-30 11:13:01 +02:00
parent 7223bdfef9
commit a9ac01a886
@@ -5,22 +5,24 @@
//+------------------------------------------------------------------+ //+------------------------------------------------------------------+
#property copyright "Copyright 2025, xxxxxxxx" #property copyright "Copyright 2025, xxxxxxxx"
#property link "" #property link ""
#property version "3.00" // Final version using OBJ_REGRESSION with smart update #property version "2.02" // Added selectable ray extensions
#property description "Draws a clean Linear Regression Channel, updated on new bars." #property description "Draws a Linear Regression Channel with optional extensions."
#property indicator_chart_window #property indicator_chart_window
#property indicator_buffers 0 // No buffers needed for plotting #property indicator_buffers 0
#property indicator_plots 0 // No plots needed #property indicator_plots 0
//--- Input Parameters --- //--- Input Parameters ---
input int InpRegressionPeriod = 100; input int InpRegressionPeriod = 100;
input double InpDeviations = 2.0; input double InpDeviations = 2.0;
// Note: OBJ_REGRESSION always uses PRICE_CLOSE, so InpAppliedPrice is not needed. input group "Channel Extensions"
input bool InpRayRight = false; // Extend channel to the right
input bool InpRayLeft = false; // Extend channel to the left
//--- Global Variables --- //--- Global Variables ---
int g_ExtPeriod; int g_ExtPeriod;
double g_ExtDeviations; double g_ExtDeviations;
string g_channel_name; string g_channel_name;
datetime g_last_update_time; // To track the time of the last update datetime g_last_update_time;
//+------------------------------------------------------------------+ //+------------------------------------------------------------------+
//| Custom indicator initialization function. | //| Custom indicator initialization function. |
@@ -35,6 +37,8 @@ int OnInit()
IndicatorSetString(INDICATOR_SHORTNAME, StringFormat("LinReg(%d, %.1f)", g_ExtPeriod, g_ExtDeviations)); IndicatorSetString(INDICATOR_SHORTNAME, StringFormat("LinReg(%d, %.1f)", g_ExtPeriod, g_ExtDeviations));
EventSetTimer(1);
return(INIT_SUCCEEDED); return(INIT_SUCCEEDED);
} }
@@ -43,11 +47,19 @@ int OnInit()
//+------------------------------------------------------------------+ //+------------------------------------------------------------------+
void OnDeinit(const int reason) void OnDeinit(const int reason)
{ {
// Clean up the chart object EventKillTimer();
ObjectDelete(0, g_channel_name); ObjectDelete(0, g_channel_name);
ChartRedraw(); ChartRedraw();
} }
//+------------------------------------------------------------------+
//| Timer event handler. |
//+------------------------------------------------------------------+
void OnTimer()
{
UpdateRegressionChannel();
}
//+------------------------------------------------------------------+ //+------------------------------------------------------------------+
//| Main calculation function. | //| Main calculation function. |
//+------------------------------------------------------------------+ //+------------------------------------------------------------------+
@@ -62,14 +74,15 @@ int OnCalculate(const int rates_total,
const long &volume[], const long &volume[],
const int &spread[]) const int &spread[])
{ {
if(rates_total < g_ExtPeriod) if(prev_calculated == 0)
return(0); {
UpdateRegressionChannel();
}
//--- Check if a new bar has appeared ---
datetime last_bar_time = time[rates_total - 1]; datetime last_bar_time = time[rates_total - 1];
if(last_bar_time > g_last_update_time) if(last_bar_time > g_last_update_time)
{ {
UpdateRegressionChannel(rates_total, time); UpdateRegressionChannel();
g_last_update_time = last_bar_time; g_last_update_time = last_bar_time;
} }
@@ -79,13 +92,14 @@ int OnCalculate(const int rates_total,
//+------------------------------------------------------------------+ //+------------------------------------------------------------------+
//| Updates the position and properties of the regression channel. | //| Updates the position and properties of the regression channel. |
//+------------------------------------------------------------------+ //+------------------------------------------------------------------+
void UpdateRegressionChannel(int rates_total, const datetime &time[]) void UpdateRegressionChannel()
{ {
// Define the start and end time for the channel if(Bars(_Symbol, _Period) < g_ExtPeriod)
datetime time1 = time[rates_total - g_ExtPeriod]; return;
datetime time2 = time[rates_total - 1];
datetime time1 = iTime(_Symbol, _Period, g_ExtPeriod - 1);
datetime time2 = iTime(_Symbol, _Period, 0);
// Check if the object exists. If not, create it.
if(ObjectFind(0, g_channel_name) < 0) if(ObjectFind(0, g_channel_name) < 0)
{ {
if(!ObjectCreate(0, g_channel_name, OBJ_REGRESSION, 0, time1, 0, time2, 0)) if(!ObjectCreate(0, g_channel_name, OBJ_REGRESSION, 0, time1, 0, time2, 0))
@@ -94,18 +108,24 @@ void UpdateRegressionChannel(int rates_total, const datetime &time[])
return; return;
} }
// Set visual properties only once on creation
ObjectSetInteger(0, g_channel_name, OBJPROP_COLOR, clrRed); ObjectSetInteger(0, g_channel_name, OBJPROP_COLOR, clrRed);
ObjectSetInteger(0, g_channel_name, OBJPROP_STYLE, STYLE_SOLID); ObjectSetInteger(0, g_channel_name, OBJPROP_STYLE, STYLE_SOLID);
ObjectSetDouble(0, g_channel_name, OBJPROP_DEVIATION, g_ExtDeviations); ObjectSetDouble(0, g_channel_name, OBJPROP_DEVIATION, g_ExtDeviations);
ObjectSetInteger(0, g_channel_name, OBJPROP_FILL, false); ObjectSetInteger(0, g_channel_name, OBJPROP_FILL, false);
ObjectSetInteger(0, g_channel_name, OBJPROP_RAY_RIGHT, false); // Do not extend to the right
ObjectSetInteger(0, g_channel_name, OBJPROP_SELECTABLE, false); ObjectSetInteger(0, g_channel_name, OBJPROP_SELECTABLE, false);
// --- FIX: Set ray properties on creation based on inputs ---
ObjectSetInteger(0, g_channel_name, OBJPROP_RAY_RIGHT, InpRayRight);
ObjectSetInteger(0, g_channel_name, OBJPROP_RAY_LEFT, InpRayLeft);
} }
else // If it exists, just move its time coordinates else
{ {
ObjectSetInteger(0, g_channel_name, OBJPROP_TIME, 0, time1); ObjectSetInteger(0, g_channel_name, OBJPROP_TIME, 0, time1);
ObjectSetInteger(0, g_channel_name, OBJPROP_TIME, 1, time2); ObjectSetInteger(0, g_channel_name, OBJPROP_TIME, 1, time2);
// --- FIX: Also update ray properties on subsequent updates ---
// This allows the user to change them in the indicator properties window
ObjectSetInteger(0, g_channel_name, OBJPROP_RAY_RIGHT, InpRayRight);
ObjectSetInteger(0, g_channel_name, OBJPROP_RAY_LEFT, InpRayLeft);
} }
ChartRedraw(); ChartRedraw();