mirror of
https://github.com/softwaredevelop/mql5.git
synced 2026-08-21 00:08:07 +00:00
refactor: Corrected Mean line drawing logic
This commit is contained in:
@@ -4,8 +4,8 @@
|
||||
//| |
|
||||
//+------------------------------------------------------------------+
|
||||
#property copyright "Copyright 2025, xxxxxxxx"
|
||||
#property version "1.40" // Added Mean line calculation
|
||||
#property description "Draws boxes, VWAP, and Mean lines for user-defined trading sessions."
|
||||
#property version "1.61" // Corrected Mean line drawing logic
|
||||
#property description "Draws boxes, VWAP, Mean, and LinReg lines for user-defined trading sessions."
|
||||
#property description "Times are based on broker's server time."
|
||||
#property indicator_chart_window
|
||||
#property indicator_plots 0
|
||||
@@ -24,13 +24,14 @@ private:
|
||||
bool m_enabled;
|
||||
bool m_fill_box;
|
||||
bool m_show_vwap;
|
||||
bool m_show_mean; // New member for mean line
|
||||
bool m_show_mean;
|
||||
bool m_show_linreg;
|
||||
ENUM_APPLIED_VOLUME m_volume_type;
|
||||
|
||||
bool IsTimeInSession(const MqlDateTime &dt);
|
||||
|
||||
public:
|
||||
void Init(bool enabled, string start_time, string end_time, color box_color, bool fill_box, bool show_vwap, bool show_mean, ENUM_APPLIED_VOLUME vol_type, string prefix);
|
||||
void Init(bool enabled, string start_time, string end_time, color box_color, bool fill_box, bool show_vwap, bool show_mean, bool show_linreg, ENUM_APPLIED_VOLUME vol_type, string prefix);
|
||||
void Update(const int rates_total, const datetime &time[], const double &high[], const double &low[], const double &close[], const long &tick_volume[], const long &volume[]);
|
||||
void Cleanup(void);
|
||||
};
|
||||
@@ -38,7 +39,7 @@ public:
|
||||
//+------------------------------------------------------------------+
|
||||
//| CSessionAnalyzer: Initialization |
|
||||
//+------------------------------------------------------------------+
|
||||
void CSessionAnalyzer::Init(bool enabled, string start_time, string end_time, color box_color, bool fill_box, bool show_vwap, bool show_mean, ENUM_APPLIED_VOLUME vol_type, string prefix)
|
||||
void CSessionAnalyzer::Init(bool enabled, string start_time, string end_time, color box_color, bool fill_box, bool show_vwap, bool show_mean, bool show_linreg, ENUM_APPLIED_VOLUME vol_type, string prefix)
|
||||
{
|
||||
m_enabled = enabled;
|
||||
m_prefix = prefix;
|
||||
@@ -46,6 +47,7 @@ void CSessionAnalyzer::Init(bool enabled, string start_time, string end_time, co
|
||||
m_fill_box = fill_box;
|
||||
m_show_vwap = show_vwap;
|
||||
m_show_mean = show_mean;
|
||||
m_show_linreg = show_linreg;
|
||||
m_volume_type = vol_type;
|
||||
|
||||
string parts[];
|
||||
@@ -102,13 +104,13 @@ void CSessionAnalyzer::Update(const int rates_total, const datetime &time[], con
|
||||
|
||||
bool in_session = false;
|
||||
int session_start_bar = -1;
|
||||
double session_high = 0;
|
||||
double session_low = 0;
|
||||
double session_high = 0, session_low = 0;
|
||||
long session_id = 0;
|
||||
|
||||
double cumulative_tpv = 0, cumulative_vol = 0, prev_vwap = 0;
|
||||
double cumulative_price = 0;
|
||||
int bar_count = 0;
|
||||
double sum_x = 0, sum_y = 0, sum_xy = 0, sum_x2 = 0;
|
||||
|
||||
for(int i = 1; i < rates_total; i++)
|
||||
{
|
||||
@@ -129,29 +131,16 @@ void CSessionAnalyzer::Update(const int rates_total, const datetime &time[], con
|
||||
prev_vwap = 0;
|
||||
cumulative_price = 0;
|
||||
bar_count = 0;
|
||||
sum_x = 0;
|
||||
sum_y = 0;
|
||||
sum_xy = 0;
|
||||
sum_x2 = 0;
|
||||
}
|
||||
else
|
||||
if(!is_in_current_session && in_session)
|
||||
{
|
||||
in_session = false;
|
||||
if(session_start_bar != -1 && i > session_start_bar)
|
||||
{
|
||||
string box_name = m_prefix + "Box_" + (string)session_id;
|
||||
ObjectCreate(0, box_name, OBJ_RECTANGLE, 0, time[session_start_bar], session_high, time[i-1], session_low);
|
||||
ObjectSetInteger(0, box_name, OBJPROP_COLOR, m_color);
|
||||
ObjectSetInteger(0, box_name, OBJPROP_STYLE, STYLE_SOLID);
|
||||
ObjectSetInteger(0, box_name, OBJPROP_BACK, true);
|
||||
ObjectSetInteger(0, box_name, OBJPROP_FILL, m_fill_box);
|
||||
|
||||
if(m_show_mean && bar_count > 0)
|
||||
{
|
||||
double mean_price = cumulative_price / bar_count;
|
||||
string mean_line_name = m_prefix + "Mean_" + (string)session_id;
|
||||
ObjectCreate(0, mean_line_name, OBJ_TREND, 0, time[session_start_bar], mean_price, time[i-1], mean_price);
|
||||
ObjectSetInteger(0, mean_line_name, OBJPROP_COLOR, m_color);
|
||||
ObjectSetInteger(0, mean_line_name, OBJPROP_STYLE, STYLE_DOT);
|
||||
}
|
||||
}
|
||||
// Final drawing is now handled by the real-time update logic below
|
||||
}
|
||||
|
||||
if(in_session)
|
||||
@@ -161,19 +150,15 @@ void CSessionAnalyzer::Update(const int rates_total, const datetime &time[], con
|
||||
if(low[i] < session_low)
|
||||
session_low = low[i];
|
||||
|
||||
// --- VWAP Calculation ---
|
||||
if(m_show_vwap)
|
||||
{
|
||||
double typical_price = (high[i] + low[i] + close[i]) / 3.0;
|
||||
long current_volume = (m_volume_type == VOLUME_TICK) ? tick_volume[i] : volume[i];
|
||||
if(current_volume < 1)
|
||||
current_volume = 1;
|
||||
|
||||
cumulative_tpv += typical_price * (double)current_volume;
|
||||
cumulative_vol += (double)current_volume;
|
||||
|
||||
double current_vwap = (cumulative_vol > 0) ? cumulative_tpv / cumulative_vol : 0;
|
||||
|
||||
if(prev_vwap > 0)
|
||||
{
|
||||
string vwap_line_name = m_prefix + "VWAP_" + (string)time[i];
|
||||
@@ -184,31 +169,69 @@ void CSessionAnalyzer::Update(const int rates_total, const datetime &time[], con
|
||||
prev_vwap = current_vwap;
|
||||
}
|
||||
|
||||
// --- Mean Calculation ---
|
||||
if(m_show_mean)
|
||||
if(m_show_mean || m_show_linreg)
|
||||
{
|
||||
cumulative_price += close[i];
|
||||
double x = bar_count;
|
||||
double y = close[i];
|
||||
sum_x += x;
|
||||
sum_y += y;
|
||||
sum_xy += x * y;
|
||||
sum_x2 += x * x;
|
||||
bar_count++;
|
||||
}
|
||||
|
||||
// Update the box for the current, active session
|
||||
if(i == rates_total - 1)
|
||||
// --- Real-time drawing of all components for the current session ---
|
||||
string box_name = m_prefix + "Box_" + (string)session_id;
|
||||
if(ObjectFind(0, box_name) < 0)
|
||||
{
|
||||
string obj_name = m_prefix + "Box_" + (string)session_id;
|
||||
if(ObjectFind(0, obj_name) < 0)
|
||||
{
|
||||
ObjectCreate(0, obj_name, OBJ_RECTANGLE, 0, time[session_start_bar], session_high, time[i], session_low);
|
||||
ObjectSetInteger(0, obj_name, OBJPROP_COLOR, m_color);
|
||||
ObjectSetInteger(0, obj_name, OBJPROP_STYLE, STYLE_SOLID);
|
||||
ObjectSetInteger(0, obj_name, OBJPROP_BACK, true);
|
||||
ObjectSetInteger(0, obj_name, OBJPROP_FILL, m_fill_box);
|
||||
}
|
||||
ObjectCreate(0, box_name, OBJ_RECTANGLE, 0, time[session_start_bar], session_high, time[i], session_low);
|
||||
ObjectSetInteger(0, box_name, OBJPROP_COLOR, m_color);
|
||||
ObjectSetInteger(0, box_name, OBJPROP_STYLE, STYLE_SOLID);
|
||||
ObjectSetInteger(0, box_name, OBJPROP_BACK, true);
|
||||
ObjectSetInteger(0, box_name, OBJPROP_FILL, m_fill_box);
|
||||
}
|
||||
else
|
||||
{
|
||||
ObjectSetDouble(0, box_name, OBJPROP_PRICE, 0, session_high);
|
||||
ObjectSetDouble(0, box_name, OBJPROP_PRICE, 1, session_low);
|
||||
ObjectSetInteger(0, box_name, OBJPROP_TIME, 1, time[i]);
|
||||
}
|
||||
|
||||
if(m_show_mean && bar_count > 0)
|
||||
{
|
||||
double mean_price = cumulative_price / bar_count;
|
||||
string mean_line_name = m_prefix + "Mean_" + (string)session_id;
|
||||
if(ObjectFind(0, mean_line_name) < 0)
|
||||
ObjectCreate(0, mean_line_name, OBJ_TREND, 0, time[session_start_bar], mean_price, time[i], mean_price);
|
||||
else
|
||||
{
|
||||
ObjectSetDouble(0, obj_name, OBJPROP_PRICE, 0, session_high);
|
||||
ObjectSetDouble(0, obj_name, OBJPROP_PRICE, 1, session_low);
|
||||
ObjectSetInteger(0, obj_name, OBJPROP_TIME, 1, time[i]);
|
||||
// CORRECTED: Update both price points to keep the line horizontal
|
||||
ObjectSetDouble(0, mean_line_name, OBJPROP_PRICE, 0, mean_price);
|
||||
ObjectSetDouble(0, mean_line_name, OBJPROP_PRICE, 1, mean_price);
|
||||
ObjectSetInteger(0, mean_line_name, OBJPROP_TIME, 1, time[i]);
|
||||
}
|
||||
ObjectSetInteger(0, mean_line_name, OBJPROP_COLOR, m_color);
|
||||
ObjectSetInteger(0, mean_line_name, OBJPROP_STYLE, STYLE_DOT);
|
||||
}
|
||||
|
||||
if(m_show_linreg && bar_count > 1)
|
||||
{
|
||||
double b = (bar_count * sum_xy - sum_x * sum_y) / (bar_count * sum_x2 - sum_x * sum_x);
|
||||
double a = (sum_y - b * sum_x) / bar_count;
|
||||
double start_price = a;
|
||||
double end_price = a + b * (bar_count - 1);
|
||||
string lr_line_name = m_prefix + "LinReg_" + (string)session_id;
|
||||
if(ObjectFind(0, lr_line_name) < 0)
|
||||
ObjectCreate(0, lr_line_name, OBJ_TREND, 0, time[session_start_bar], start_price, time[i], end_price);
|
||||
else
|
||||
{
|
||||
ObjectMove(0, lr_line_name, 0, time[session_start_bar], start_price);
|
||||
ObjectMove(0, lr_line_name, 1, time[i], end_price);
|
||||
}
|
||||
ObjectSetInteger(0, lr_line_name, OBJPROP_COLOR, m_color);
|
||||
ObjectSetInteger(0, lr_line_name, OBJPROP_STYLE, STYLE_DASHDOT);
|
||||
ObjectSetInteger(0, lr_line_name, OBJPROP_WIDTH, 2);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -229,6 +252,7 @@ input string InpPreMarket_End = "09:30";
|
||||
input color InpPreMarket_Color = C'33,150,243';
|
||||
input bool InpPreMarket_VWAP = true;
|
||||
input bool InpPreMarket_Mean = true;
|
||||
input bool InpPreMarket_LinReg = true;
|
||||
|
||||
//+------------------------------------------------------------------+
|
||||
//| |
|
||||
@@ -240,6 +264,7 @@ input string InpCore_End = "16:00";
|
||||
input color InpCore_Color = C'255,87,34';
|
||||
input bool InpCore_VWAP = true;
|
||||
input bool InpCore_Mean = true;
|
||||
input bool InpCore_LinReg = true;
|
||||
|
||||
//+------------------------------------------------------------------+
|
||||
//| |
|
||||
@@ -251,6 +276,7 @@ input string InpPostMarket_End = "20:00";
|
||||
input color InpPostMarket_Color = C'103,58,183';
|
||||
input bool InpPostMarket_VWAP = true;
|
||||
input bool InpPostMarket_Mean = true;
|
||||
input bool InpPostMarket_LinReg = true;
|
||||
|
||||
//--- Global Variables ---
|
||||
CSessionAnalyzer *g_pre_market_analyzer;
|
||||
@@ -268,17 +294,17 @@ int OnInit()
|
||||
g_pre_market_analyzer = new CSessionAnalyzer();
|
||||
if(CheckPointer(g_pre_market_analyzer) == POINTER_INVALID)
|
||||
return INIT_FAILED;
|
||||
g_pre_market_analyzer.Init(InpPreMarket_Enable, InpPreMarket_Start, InpPreMarket_End, InpPreMarket_Color, InpFillBoxes, InpPreMarket_VWAP, InpPreMarket_Mean, InpVolumeType, "PreMarket_");
|
||||
g_pre_market_analyzer.Init(InpPreMarket_Enable, InpPreMarket_Start, InpPreMarket_End, InpPreMarket_Color, InpFillBoxes, InpPreMarket_VWAP, InpPreMarket_Mean, InpPreMarket_LinReg, InpVolumeType, "PreMarket_");
|
||||
|
||||
g_core_market_analyzer = new CSessionAnalyzer();
|
||||
if(CheckPointer(g_core_market_analyzer) == POINTER_INVALID)
|
||||
return INIT_FAILED;
|
||||
g_core_market_analyzer.Init(InpCore_Enable, InpCore_Start, InpCore_End, InpCore_Color, InpFillBoxes, InpCore_VWAP, InpCore_Mean, InpVolumeType, "CoreMarket_");
|
||||
g_core_market_analyzer.Init(InpCore_Enable, InpCore_Start, InpCore_End, InpCore_Color, InpFillBoxes, InpCore_VWAP, InpCore_Mean, InpCore_LinReg, InpVolumeType, "CoreMarket_");
|
||||
|
||||
g_post_market_analyzer = new CSessionAnalyzer();
|
||||
if(CheckPointer(g_post_market_analyzer) == POINTER_INVALID)
|
||||
return INIT_FAILED;
|
||||
g_post_market_analyzer.Init(InpPostMarket_Enable, InpPostMarket_Start, InpPostMarket_End, InpPostMarket_Color, InpFillBoxes, InpPostMarket_VWAP, InpPostMarket_Mean, InpVolumeType, "PostMarket_");
|
||||
g_post_market_analyzer.Init(InpPostMarket_Enable, InpPostMarket_Start, InpPostMarket_End, InpPostMarket_Color, InpFillBoxes, InpPostMarket_VWAP, InpPostMarket_Mean, InpPostMarket_LinReg, InpVolumeType, "PostMarket_");
|
||||
|
||||
IndicatorSetString(INDICATOR_SHORTNAME, "Session Analysis");
|
||||
return(INIT_SUCCEEDED);
|
||||
@@ -311,9 +337,10 @@ void OnDeinit(const int reason)
|
||||
//+------------------------------------------------------------------+
|
||||
int OnCalculate(const int rates_total, const int, 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(time[rates_total - 1] == g_last_bar_time && rates_total > 1)
|
||||
if(rates_total > 0 && time[rates_total - 1] == g_last_bar_time)
|
||||
return(rates_total);
|
||||
g_last_bar_time = time[rates_total - 1];
|
||||
if(rates_total > 0)
|
||||
g_last_bar_time = time[rates_total - 1];
|
||||
|
||||
if(CheckPointer(g_pre_market_analyzer) != POINTER_INVALID)
|
||||
g_pre_market_analyzer.Update(rates_total, time, high, low, close, tick_volume, volume);
|
||||
|
||||
Reference in New Issue
Block a user