refactor: vwap

This commit is contained in:
Toh4iem9
2025-10-12 23:00:02 +02:00
parent 12f119d64f
commit eae0579702
+98 -66
View File
@@ -4,17 +4,17 @@
//| |
//+------------------------------------------------------------------+
#property copyright "Copyright 2025, xxxxxxxx"
#property version "1.20" // Added option to fill boxes
#property description "Draws boxes around Pre-Market, Core, and Post-Market sessions."
#property version "1.30" // Added VWAP calculation
#property description "Draws boxes and calculates VWAP for user-defined trading sessions."
#property description "Times are based on broker's server time."
#property indicator_chart_window
#property indicator_plots 0
//+------------------------------------------------------------------+
//| CLASS: CSessionBoxer |
//| Manages the drawing and updating of a single session box. |
//| CLASS: CSessionAnalyzer |
//| Manages the drawing and analysis of a single session. |
//+------------------------------------------------------------------+
class CSessionBoxer
class CSessionAnalyzer
{
private:
int m_start_hour, m_start_min;
@@ -22,25 +22,29 @@ private:
color m_color;
string m_prefix;
bool m_enabled;
bool m_fill_box; // New member for fill property
bool m_fill_box;
bool m_show_vwap;
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, string prefix);
void Update(const int rates_total, const datetime &time[], const double &high[], const double &low[]);
void Init(bool enabled, string start_time, string end_time, color box_color, bool fill_box, bool show_vwap, 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);
};
//+------------------------------------------------------------------+
//| CSessionBoxer: Initialization |
//| CSessionAnalyzer: Initialization |
//+------------------------------------------------------------------+
void CSessionBoxer::Init(bool enabled, string start_time, string end_time, color box_color, bool fill_box, string prefix)
void CSessionAnalyzer::Init(bool enabled, string start_time, string end_time, color box_color, bool fill_box, bool show_vwap, ENUM_APPLIED_VOLUME vol_type, string prefix)
{
m_enabled = enabled;
m_prefix = prefix;
m_color = box_color;
m_fill_box = fill_box;
m_enabled = enabled;
m_prefix = prefix;
m_color = box_color;
m_fill_box = fill_box;
m_show_vwap = show_vwap;
m_volume_type = vol_type;
string parts[];
if(StringSplit(start_time, ':', parts) == 2)
@@ -56,9 +60,9 @@ void CSessionBoxer::Init(bool enabled, string start_time, string end_time, color
}
//+------------------------------------------------------------------+
//| CSessionBoxer: Checks if a given time is within the session. |
//| CSessionAnalyzer: Checks if a given time is within the session. |
//+------------------------------------------------------------------+
bool CSessionBoxer::IsTimeInSession(const MqlDateTime &dt)
bool CSessionAnalyzer::IsTimeInSession(const MqlDateTime &dt)
{
int current_time_in_minutes = dt.hour * 60 + dt.min;
int start_time_in_minutes = m_start_hour * 60 + m_start_min;
@@ -75,26 +79,19 @@ bool CSessionBoxer::IsTimeInSession(const MqlDateTime &dt)
}
//+------------------------------------------------------------------+
//| CSessionBoxer: Deletes all objects created by this instance |
//| CSessionAnalyzer: Deletes all objects created by this instance |
//+------------------------------------------------------------------+
void CSessionBoxer::Cleanup(void)
void CSessionAnalyzer::Cleanup(void)
{
if(!m_enabled)
return;
for(int i = ObjectsTotal(0, -1, OBJ_RECTANGLE) - 1; i >= 0; i--)
{
string name = ObjectName(0, i, -1, OBJ_RECTANGLE);
if(StringFind(name, m_prefix) == 0)
{
ObjectDelete(0, name);
}
}
ObjectsDeleteAll(0, m_prefix);
}
//+------------------------------------------------------------------+
//| CSessionBoxer: Main update logic |
//| CSessionAnalyzer: Main update logic |
//+------------------------------------------------------------------+
void CSessionBoxer::Update(const int rates_total, const datetime &time[], const double &high[], const double &low[])
void CSessionAnalyzer::Update(const int rates_total, const datetime &time[], const double &high[], const double &low[], const double &close[], const long &tick_volume[], const long &volume[])
{
if(!m_enabled)
return;
@@ -107,6 +104,10 @@ void CSessionBoxer::Update(const int rates_total, const datetime &time[], const
double session_low = 0;
long session_id = 0;
double cumulative_tpv = 0;
double cumulative_vol = 0;
double prev_vwap = 0;
for(int i = 1; i < rates_total; i++)
{
MqlDateTime dt;
@@ -120,7 +121,10 @@ void CSessionBoxer::Update(const int rates_total, const datetime &time[], const
session_start_bar = i;
session_high = high[i];
session_low = low[i];
session_id = (long)time[i] - (dt.hour*3600 + dt.min*60 + dt.sec); // Use day start as ID
session_id = (long)time[i] - (dt.hour*3600 + dt.min*60 + dt.sec);
cumulative_tpv = 0;
cumulative_vol = 0;
prev_vwap = 0;
}
else
if(!is_in_current_session && in_session)
@@ -128,12 +132,12 @@ void CSessionBoxer::Update(const int rates_total, const datetime &time[], const
in_session = false;
if(session_start_bar != -1 && i > session_start_bar)
{
string obj_name = m_prefix + (string)session_id;
string obj_name = m_prefix + "Box_" + (string)session_id;
ObjectCreate(0, obj_name, OBJ_RECTANGLE, 0, time[session_start_bar], session_high, time[i-1], 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); // Use input for fill
ObjectSetInteger(0, obj_name, OBJPROP_FILL, m_fill_box);
}
}
@@ -144,16 +148,40 @@ void CSessionBoxer::Update(const int rates_total, const datetime &time[], const
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) // Don't draw the first point, only from the second
{
string vwap_line_name = m_prefix + "VWAP_" + (string)time[i];
ObjectCreate(0, vwap_line_name, OBJ_TREND, 0, time[i-1], prev_vwap, time[i], current_vwap);
ObjectSetInteger(0, vwap_line_name, OBJPROP_COLOR, m_color);
ObjectSetInteger(0, vwap_line_name, OBJPROP_WIDTH, 2);
}
prev_vwap = current_vwap;
}
// Update the box for the current, active session
if(i == rates_total - 1)
{
string obj_name = m_prefix + (string)session_id;
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); // Use input for fill
ObjectSetInteger(0, obj_name, OBJPROP_FILL, m_fill_box);
}
else
{
@@ -168,7 +196,8 @@ void CSessionBoxer::Update(const int rates_total, const datetime &time[], const
//--- Input Parameters ---
input group "Display Settings"
input bool InpFillBoxes = false; // Fill Session Boxes
input bool InpFillBoxes = false;
input ENUM_APPLIED_VOLUME InpVolumeType = VOLUME_TICK;
//+------------------------------------------------------------------+
//| |
@@ -177,7 +206,8 @@ input group "Pre-Market Session (Broker Time)"
input bool InpPreMarket_Enable = true;
input string InpPreMarket_Start = "08:00";
input string InpPreMarket_End = "09:30";
input color InpPreMarket_Color = C'33,150,243'; // Blue
input color InpPreMarket_Color = C'33,150,243';
input bool InpPreMarket_VWAP = true;
//+------------------------------------------------------------------+
//| |
@@ -186,7 +216,8 @@ input group "Core Trading Session (Broker Time)"
input bool InpCore_Enable = true;
input string InpCore_Start = "09:30";
input string InpCore_End = "16:00";
input color InpCore_Color = C'255,87,34'; // Deep Orange
input color InpCore_Color = C'255,87,34';
input bool InpCore_VWAP = true;
//+------------------------------------------------------------------+
//| |
@@ -195,12 +226,13 @@ input group "Post-Market Session (Broker Time)"
input bool InpPostMarket_Enable = true;
input string InpPostMarket_Start = "16:00";
input string InpPostMarket_End = "20:00";
input color InpPostMarket_Color = C'103,58,183'; // Deep Purple
input color InpPostMarket_Color = C'103,58,183';
input bool InpPostMarket_VWAP = true;
//--- Global Variables ---
CSessionBoxer *g_pre_market_boxer;
CSessionBoxer *g_core_market_boxer;
CSessionBoxer *g_post_market_boxer;
CSessionAnalyzer *g_pre_market_analyzer;
CSessionAnalyzer *g_core_market_analyzer;
CSessionAnalyzer *g_post_market_analyzer;
datetime g_last_bar_time;
//+------------------------------------------------------------------+
@@ -210,20 +242,20 @@ int OnInit()
{
g_last_bar_time = 0;
g_pre_market_boxer = new CSessionBoxer();
if(CheckPointer(g_pre_market_boxer) == POINTER_INVALID)
g_pre_market_analyzer = new CSessionAnalyzer();
if(CheckPointer(g_pre_market_analyzer) == POINTER_INVALID)
return INIT_FAILED;
g_pre_market_boxer.Init(InpPreMarket_Enable, InpPreMarket_Start, InpPreMarket_End, InpPreMarket_Color, InpFillBoxes, "PreMarketBox_");
g_pre_market_analyzer.Init(InpPreMarket_Enable, InpPreMarket_Start, InpPreMarket_End, InpPreMarket_Color, InpFillBoxes, InpPreMarket_VWAP, InpVolumeType, "PreMarket_");
g_core_market_boxer = new CSessionBoxer();
if(CheckPointer(g_core_market_boxer) == POINTER_INVALID)
g_core_market_analyzer = new CSessionAnalyzer();
if(CheckPointer(g_core_market_analyzer) == POINTER_INVALID)
return INIT_FAILED;
g_core_market_boxer.Init(InpCore_Enable, InpCore_Start, InpCore_End, InpCore_Color, InpFillBoxes, "CoreMarketBox_");
g_core_market_analyzer.Init(InpCore_Enable, InpCore_Start, InpCore_End, InpCore_Color, InpFillBoxes, InpCore_VWAP, InpVolumeType, "CoreMarket_");
g_post_market_boxer = new CSessionBoxer();
if(CheckPointer(g_post_market_boxer) == POINTER_INVALID)
g_post_market_analyzer = new CSessionAnalyzer();
if(CheckPointer(g_post_market_analyzer) == POINTER_INVALID)
return INIT_FAILED;
g_post_market_boxer.Init(InpPostMarket_Enable, InpPostMarket_Start, InpPostMarket_End, InpPostMarket_Color, InpFillBoxes, "PostMarketBox_");
g_post_market_analyzer.Init(InpPostMarket_Enable, InpPostMarket_Start, InpPostMarket_End, InpPostMarket_Color, InpFillBoxes, InpPostMarket_VWAP, InpVolumeType, "PostMarket_");
IndicatorSetString(INDICATOR_SHORTNAME, "Session Analysis");
return(INIT_SUCCEEDED);
@@ -234,43 +266,43 @@ int OnInit()
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
if(CheckPointer(g_pre_market_boxer) != POINTER_INVALID)
if(CheckPointer(g_pre_market_analyzer) != POINTER_INVALID)
{
g_pre_market_boxer.Cleanup();
delete g_pre_market_boxer;
g_pre_market_analyzer.Cleanup();
delete g_pre_market_analyzer;
}
if(CheckPointer(g_core_market_boxer) != POINTER_INVALID)
if(CheckPointer(g_core_market_analyzer) != POINTER_INVALID)
{
g_core_market_boxer.Cleanup();
delete g_core_market_boxer;
g_core_market_analyzer.Cleanup();
delete g_core_market_analyzer;
}
if(CheckPointer(g_post_market_boxer) != POINTER_INVALID)
if(CheckPointer(g_post_market_analyzer) != POINTER_INVALID)
{
g_post_market_boxer.Cleanup();
delete g_post_market_boxer;
g_post_market_analyzer.Cleanup();
delete g_post_market_analyzer;
}
}
//+------------------------------------------------------------------+
//| Custom indicator iteration function. |
//+------------------------------------------------------------------+
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&[], const long&[], const int&[])
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)
return(rates_total);
g_last_bar_time = time[rates_total - 1];
if(CheckPointer(g_pre_market_boxer) != POINTER_INVALID)
g_pre_market_boxer.Update(rates_total, time, high, low);
if(CheckPointer(g_pre_market_analyzer) != POINTER_INVALID)
g_pre_market_analyzer.Update(rates_total, time, high, low, close, tick_volume, volume);
if(CheckPointer(g_core_market_boxer) != POINTER_INVALID)
g_core_market_boxer.Update(rates_total, time, high, low);
if(CheckPointer(g_core_market_analyzer) != POINTER_INVALID)
g_core_market_analyzer.Update(rates_total, time, high, low, close, tick_volume, volume);
if(CheckPointer(g_post_market_boxer) != POINTER_INVALID)
g_post_market_boxer.Update(rates_total, time, high, low);
if(CheckPointer(g_post_market_analyzer) != POINTER_INVALID)
g_post_market_analyzer.Update(rates_total, time, high, low, close, tick_volume, volume);
ChartRedraw();
return(rates_total);
}
//+------------------------------------------------------------------+
//+------------------------------------------------------------------+```
//+------------------------------------------------------------------+