refactor: Market 1, 2, 3 Settings

This commit is contained in:
Toh4iem9
2025-10-16 09:07:19 +02:00
parent 047a87e072
commit 995cbc4e36
+142 -103
View File
@@ -3,8 +3,8 @@
//| Copyright 2025, xxxxxxxx| //| Copyright 2025, xxxxxxxx|
//+------------------------------------------------------------------+ //+------------------------------------------------------------------+
#property copyright "Copyright 2025, xxxxxxxx" #property copyright "Copyright 2025, xxxxxxxx"
#property version "3.04" // Using Time+Rand for a truly unique instance ID #property version "4.00" // Scaled to support 3 independent markets
#property description "Draws boxes, VWAP, Mean, and LinReg lines for user-defined trading sessions." #property description "Draws boxes and analytics for up to 3 independent markets, each with Pre, Core, and Post sessions."
#property description "Supports Standard and Heikin Ashi price sources. Times are based on broker's server time." #property description "Supports Standard and Heikin Ashi price sources. Times are based on broker's server time."
#property indicator_chart_window #property indicator_chart_window
#property indicator_plots 0 #property indicator_plots 0
@@ -15,69 +15,107 @@
enum ENUM_APPLIED_PRICE_HA_ALL enum ENUM_APPLIED_PRICE_HA_ALL
{ {
//--- Heikin Ashi Prices (negative values for easy identification) //--- Heikin Ashi Prices (negative values for easy identification)
PRICE_HA_CLOSE = -1, PRICE_HA_CLOSE = -1, PRICE_HA_OPEN = -2, PRICE_HA_HIGH = -3, PRICE_HA_LOW = -4,
PRICE_HA_OPEN = -2, PRICE_HA_MEDIAN = -5, PRICE_HA_TYPICAL = -6, PRICE_HA_WEIGHTED = -7,
PRICE_HA_HIGH = -3,
PRICE_HA_LOW = -4,
PRICE_HA_MEDIAN = -5,
PRICE_HA_TYPICAL = -6,
PRICE_HA_WEIGHTED = -7,
//--- Standard Prices (using built-in ENUM_APPLIED_PRICE values) //--- Standard Prices (using built-in ENUM_APPLIED_PRICE values)
PRICE_CLOSE_STD = PRICE_CLOSE, PRICE_CLOSE_STD = PRICE_CLOSE, PRICE_OPEN_STD = PRICE_OPEN, PRICE_HIGH_STD = PRICE_HIGH,
PRICE_OPEN_STD = PRICE_OPEN, PRICE_LOW_STD = PRICE_LOW, PRICE_MEDIAN_STD = PRICE_MEDIAN, PRICE_TYPICAL_STD = PRICE_TYPICAL,
PRICE_HIGH_STD = PRICE_HIGH,
PRICE_LOW_STD = PRICE_LOW,
PRICE_MEDIAN_STD = PRICE_MEDIAN,
PRICE_TYPICAL_STD = PRICE_TYPICAL,
PRICE_WEIGHTED_STD= PRICE_WEIGHTED PRICE_WEIGHTED_STD= PRICE_WEIGHTED
}; };
//--- Input Parameters --- //--- Input Parameters ---
input group "Display Settings" input group "Global Settings"
input bool InpFillBoxes = false; input bool InpFillBoxes = false;
input ENUM_APPLIED_VOLUME InpVolumeType = VOLUME_TICK; input ENUM_APPLIED_VOLUME InpVolumeType = VOLUME_TICK;
input ENUM_APPLIED_PRICE_HA_ALL InpSourcePrice = PRICE_CLOSE_STD; // Price for Mean and LinReg input ENUM_APPLIED_PRICE_HA_ALL InpSourcePrice = PRICE_CLOSE_STD; // Price for Mean and LinReg
//+------------------------------------------------------------------+ //--- Market 1 Settings ---
//| | input group "Market 1 Settings (e.g., NYSE)"
//+------------------------------------------------------------------+ input bool InpM1_Enable = true;
input group "Pre-Market Session (Broker Time)" input group "M1 Pre-Market Session"
input bool InpPreMarket_Enable = true; input bool InpM1_PreMarket_Enable = true;
input string InpPreMarket_Start = "06:30"; input string InpM1_PreMarket_Start = "06:30";
input string InpPreMarket_End = "09:30"; input string InpM1_PreMarket_End = "09:30";
input color InpPreMarket_Color = clrSlateBlue; input color InpM1_PreMarket_Color = clrSlateBlue;
input bool InpPreMarket_VWAP = true; input bool InpM1_PreMarket_VWAP = true;
input bool InpPreMarket_Mean = true; input bool InpM1_PreMarket_Mean = true;
input bool InpPreMarket_LinReg = true; input bool InpM1_PreMarket_LinReg = true;
input group "M1 Core Trading Session"
input bool InpM1_Core_Enable = true;
input string InpM1_Core_Start = "09:30";
input string InpM1_Core_End = "16:00";
input color InpM1_Core_Color = clrSlateBlue;
input bool InpM1_Core_VWAP = true;
input bool InpM1_Core_Mean = true;
input bool InpM1_Core_LinReg = true;
input group "M1 Post-Market Session"
input bool InpM1_PostMarket_Enable = true;
input string InpM1_PostMarket_Start = "16:00";
input string InpM1_PostMarket_End = "20:00";
input color InpM1_PostMarket_Color = clrSlateBlue;
input bool InpM1_PostMarket_VWAP = true;
input bool InpM1_PostMarket_Mean = true;
input bool InpM1_PostMarket_LinReg = true;
//+------------------------------------------------------------------+ //--- Market 2 Settings ---
//| | input group "Market 2 Settings (e.g., LSE)"
//+------------------------------------------------------------------+ input bool InpM2_Enable = true;
input group "Core Trading Session (Broker Time)" input group "M2 Pre-Market Session"
input bool InpCore_Enable = true; input bool InpM2_PreMarket_Enable = true;
input string InpCore_Start = "09:30"; input string InpM2_PreMarket_Start = "04:00";
input string InpCore_End = "16:00"; input string InpM2_PreMarket_End = "07:00";
input color InpCore_Color = clrSlateBlue; input color InpM2_PreMarket_Color = clrIndianRed;
input bool InpCore_VWAP = true; input bool InpM2_PreMarket_VWAP = true;
input bool InpCore_Mean = true; input bool InpM2_PreMarket_Mean = true;
input bool InpCore_LinReg = true; input bool InpM2_PreMarket_LinReg = true;
input group "M2 Core Trading Session"
input bool InpM2_Core_Enable = true;
input string InpM2_Core_Start = "07:00";
input string InpM2_Core_End = "15:30";
input color InpM2_Core_Color = clrIndianRed;
input bool InpM2_Core_VWAP = true;
input bool InpM2_Core_Mean = true;
input bool InpM2_Core_LinReg = true;
input group "M2 Post-Market Session"
input bool InpM2_PostMarket_Enable = true;
input string InpM2_PostMarket_Start = "15:30";
input string InpM2_PostMarket_End = "16:15";
input color InpM2_PostMarket_Color = clrIndianRed;
input bool InpM2_PostMarket_VWAP = true;
input bool InpM2_PostMarket_Mean = true;
input bool InpM2_PostMarket_LinReg = true;
//+------------------------------------------------------------------+ //--- Market 3 Settings ---
//| | input group "Market 3 Settings (e.g., TSE)"
//+------------------------------------------------------------------+ input bool InpM3_Enable = true;
input group "Post-Market Session (Broker Time)" input group "M3 Pre-Market Session"
input bool InpPostMarket_Enable = true; input bool InpM3_PreMarket_Enable = true;
input string InpPostMarket_Start = "16:00"; input string InpM3_PreMarket_Start = "08:00";
input string InpPostMarket_End = "20:00"; input string InpM3_PreMarket_End = "09:00";
input color InpPostMarket_Color = clrSlateBlue; input color InpM3_PreMarket_Color = clrSeaGreen;
input bool InpPostMarket_VWAP = true; input bool InpM3_PreMarket_VWAP = true;
input bool InpPostMarket_Mean = true; input bool InpM3_PreMarket_Mean = true;
input bool InpPostMarket_LinReg = true; input bool InpM3_PreMarket_LinReg = true;
input group "M3 Core Trading Session"
input bool InpM3_Core_Enable = true;
input string InpM3_Core_Start = "09:00";
input string InpM3_Core_End = "11:30";
input color InpM3_Core_Color = clrSeaGreen;
input bool InpM3_Core_VWAP = true;
input bool InpM3_Core_Mean = true;
input bool InpM3_Core_LinReg = true;
input group "M3 Post-Market Session"
input bool InpM3_PostMarket_Enable = true;
input string InpM3_PostMarket_Start = "12:30";
input string InpM3_PostMarket_End = "15:30";
input color InpM3_PostMarket_Color = clrSeaGreen;
input bool InpM3_PostMarket_VWAP = true;
input bool InpM3_PostMarket_Mean = true;
input bool InpM3_PostMarket_LinReg = true;
//--- Global Variables --- //--- Global Variables ---
CSessionAnalyzer *g_pre_market_analyzer; #define TOTAL_ANALYZERS 9
CSessionAnalyzer *g_core_market_analyzer; CSessionAnalyzer *g_analyzers[TOTAL_ANALYZERS];
CSessionAnalyzer *g_post_market_analyzer;
datetime g_last_bar_time; datetime g_last_bar_time;
//+------------------------------------------------------------------+ //+------------------------------------------------------------------+
@@ -86,41 +124,52 @@ datetime g_last_bar_time;
int OnInit() int OnInit()
{ {
g_last_bar_time = 0; g_last_bar_time = 0;
for(int i = 0; i < TOTAL_ANALYZERS; i++)
g_analyzers[i] = NULL;
//--- *** KEY CHANGE: Create a truly unique prefix using Time + Random value *** //--- Create a stable, unique prefix for this indicator instance
//--- Seed the random number generator to ensure it's different on each terminal launch MathSrand((int)TimeCurrent() + (int)ChartID());
MathSrand((int)TimeCurrent()); string temp_short_name = StringFormat("SessPro_TempID_%d_%d", TimeCurrent(), MathRand());
string unique_prefix = StringFormat("_ID_%d_%d_", TimeCurrent(), MathRand()); IndicatorSetString(INDICATOR_SHORTNAME, temp_short_name);
ChartRedraw();
int window_index = ChartWindowFind(0, temp_short_name);
if(window_index < 0)
window_index = 0;
string unique_prefix = StringFormat("SessPro_%d_%d_", ChartID(), window_index);
bool is_ha = (InpSourcePrice <= PRICE_HA_CLOSE);
string ha_suffix = ""; //--- Instantiate all 9 analyzers ---
if(InpSourcePrice <= PRICE_HA_CLOSE) // Market 1
g_analyzers[0] = is_ha ? new CSessionAnalyzer_HA() : new CSessionAnalyzer();
g_analyzers[0].Init(InpM1_Enable && InpM1_PreMarket_Enable, InpM1_PreMarket_Start, InpM1_PreMarket_End, InpM1_PreMarket_Color, InpFillBoxes, InpM1_PreMarket_VWAP, InpM1_PreMarket_Mean, InpM1_PreMarket_LinReg, InpVolumeType, unique_prefix + "M1_Pre_");
g_analyzers[1] = is_ha ? new CSessionAnalyzer_HA() : new CSessionAnalyzer();
g_analyzers[1].Init(InpM1_Enable && InpM1_Core_Enable, InpM1_Core_Start, InpM1_Core_End, InpM1_Core_Color, InpFillBoxes, InpM1_Core_VWAP, InpM1_Core_Mean, InpM1_Core_LinReg, InpVolumeType, unique_prefix + "M1_Core_");
g_analyzers[2] = is_ha ? new CSessionAnalyzer_HA() : new CSessionAnalyzer();
g_analyzers[2].Init(InpM1_Enable && InpM1_PostMarket_Enable, InpM1_PostMarket_Start, InpM1_PostMarket_End, InpM1_PostMarket_Color, InpFillBoxes, InpM1_PostMarket_VWAP, InpM1_PostMarket_Mean, InpM1_PostMarket_LinReg, InpVolumeType, unique_prefix + "M1_Post_");
// Market 2
g_analyzers[3] = is_ha ? new CSessionAnalyzer_HA() : new CSessionAnalyzer();
g_analyzers[3].Init(InpM2_Enable && InpM2_PreMarket_Enable, InpM2_PreMarket_Start, InpM2_PreMarket_End, InpM2_PreMarket_Color, InpFillBoxes, InpM2_PreMarket_VWAP, InpM2_PreMarket_Mean, InpM2_PreMarket_LinReg, InpVolumeType, unique_prefix + "M2_Pre_");
g_analyzers[4] = is_ha ? new CSessionAnalyzer_HA() : new CSessionAnalyzer();
g_analyzers[4].Init(InpM2_Enable && InpM2_Core_Enable, InpM2_Core_Start, InpM2_Core_End, InpM2_Core_Color, InpFillBoxes, InpM2_Core_VWAP, InpM2_Core_Mean, InpM2_Core_LinReg, InpVolumeType, unique_prefix + "M2_Core_");
g_analyzers[5] = is_ha ? new CSessionAnalyzer_HA() : new CSessionAnalyzer();
g_analyzers[5].Init(InpM2_Enable && InpM2_PostMarket_Enable, InpM2_PostMarket_Start, InpM2_PostMarket_End, InpM2_PostMarket_Color, InpFillBoxes, InpM2_PostMarket_VWAP, InpM2_PostMarket_Mean, InpM2_PostMarket_LinReg, InpVolumeType, unique_prefix + "M2_Post_");
// Market 3
g_analyzers[6] = is_ha ? new CSessionAnalyzer_HA() : new CSessionAnalyzer();
g_analyzers[6].Init(InpM3_Enable && InpM3_PreMarket_Enable, InpM3_PreMarket_Start, InpM3_PreMarket_End, InpM3_PreMarket_Color, InpFillBoxes, InpM3_PreMarket_VWAP, InpM3_PreMarket_Mean, InpM3_PreMarket_LinReg, InpVolumeType, unique_prefix + "M3_Pre_");
g_analyzers[7] = is_ha ? new CSessionAnalyzer_HA() : new CSessionAnalyzer();
g_analyzers[7].Init(InpM3_Enable && InpM3_Core_Enable, InpM3_Core_Start, InpM3_Core_End, InpM3_Core_Color, InpFillBoxes, InpM3_Core_VWAP, InpM3_Core_Mean, InpM3_Core_LinReg, InpVolumeType, unique_prefix + "M3_Core_");
g_analyzers[8] = is_ha ? new CSessionAnalyzer_HA() : new CSessionAnalyzer();
g_analyzers[8].Init(InpM3_Enable && InpM3_PostMarket_Enable, InpM3_PostMarket_Start, InpM3_PostMarket_End, InpM3_PostMarket_Color, InpFillBoxes, InpM3_PostMarket_VWAP, InpM3_PostMarket_Mean, InpM3_PostMarket_LinReg, InpVolumeType, unique_prefix + "M3_Post_");
//--- Clean up any old objects before drawing ---
for(int i = 0; i < TOTAL_ANALYZERS; i++)
{ {
g_pre_market_analyzer = new CSessionAnalyzer_HA(); if(CheckPointer(g_analyzers[i]) != POINTER_INVALID)
g_core_market_analyzer = new CSessionAnalyzer_HA(); g_analyzers[i].Cleanup();
g_post_market_analyzer = new CSessionAnalyzer_HA();
ha_suffix = " HA";
}
else
{
g_pre_market_analyzer = new CSessionAnalyzer();
g_core_market_analyzer = new CSessionAnalyzer();
g_post_market_analyzer = new CSessionAnalyzer();
} }
if(CheckPointer(g_pre_market_analyzer) == POINTER_INVALID) IndicatorSetString(INDICATOR_SHORTNAME, "Session Analysis" + (is_ha ? " HA" : ""));
return INIT_FAILED;
g_pre_market_analyzer.Init(InpPreMarket_Enable, InpPreMarket_Start, InpPreMarket_End, InpPreMarket_Color, InpFillBoxes, InpPreMarket_VWAP, InpPreMarket_Mean, InpPreMarket_LinReg, InpVolumeType, unique_prefix + "PreMarket_");
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, InpCore_LinReg, InpVolumeType, unique_prefix + "CoreMarket_");
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, InpPostMarket_LinReg, InpVolumeType, unique_prefix + "PostMarket_");
IndicatorSetString(INDICATOR_SHORTNAME, "Session Analysis" + ha_suffix);
return(INIT_SUCCEEDED); return(INIT_SUCCEEDED);
} }
@@ -129,20 +178,13 @@ int OnInit()
//+------------------------------------------------------------------+ //+------------------------------------------------------------------+
void OnDeinit(const int reason) void OnDeinit(const int reason)
{ {
if(CheckPointer(g_pre_market_analyzer) != POINTER_INVALID) for(int i = 0; i < TOTAL_ANALYZERS; i++)
{ {
g_pre_market_analyzer.Cleanup(); if(CheckPointer(g_analyzers[i]) != POINTER_INVALID)
delete g_pre_market_analyzer; {
} g_analyzers[i].Cleanup();
if(CheckPointer(g_core_market_analyzer) != POINTER_INVALID) delete g_analyzers[i];
{ }
g_core_market_analyzer.Cleanup();
delete g_core_market_analyzer;
}
if(CheckPointer(g_post_market_analyzer) != POINTER_INVALID)
{
g_post_market_analyzer.Cleanup();
delete g_post_market_analyzer;
} }
} }
@@ -151,7 +193,7 @@ 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[]) 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(rates_total > 0 && time[rates_total - 1] == g_last_bar_time) if(rates_total > 0 && time[rates_total - 1] == g_last_bar_time && Bars(_Symbol, _Period) == rates_total)
return(rates_total); return(rates_total);
if(rates_total > 0) if(rates_total > 0)
g_last_bar_time = time[rates_total - 1]; g_last_bar_time = time[rates_total - 1];
@@ -162,14 +204,11 @@ int OnCalculate(const int rates_total, const int, const datetime& time[], const
else else
price_type = (ENUM_APPLIED_PRICE)InpSourcePrice; price_type = (ENUM_APPLIED_PRICE)InpSourcePrice;
if(CheckPointer(g_pre_market_analyzer) != POINTER_INVALID) for(int i = 0; i < TOTAL_ANALYZERS; i++)
g_pre_market_analyzer.Update(rates_total, time, open, high, low, close, tick_volume, volume, price_type); {
if(CheckPointer(g_analyzers[i]) != POINTER_INVALID)
if(CheckPointer(g_core_market_analyzer) != POINTER_INVALID) g_analyzers[i].Update(rates_total, time, open, high, low, close, tick_volume, volume, price_type);
g_core_market_analyzer.Update(rates_total, time, open, high, low, close, tick_volume, volume, price_type); }
if(CheckPointer(g_post_market_analyzer) != POINTER_INVALID)
g_post_market_analyzer.Update(rates_total, time, open, high, low, close, tick_volume, volume, price_type);
ChartRedraw(); ChartRedraw();
return(rates_total); return(rates_total);