Advanced SessionVWAP Crossover Stepped by Naya
441 downloads / 255 views / Created: 09.06.2025




Average Rating: 0
Indicator Description
This indicator calculates two VWAP lines (Fast and Slow) that reset at customizable times, then applies trading logic between them.
Key Features:
- Uses both Fast and Slow VWAP with adjustable anchor periods
- Supports Daily, Weekly, Monthly or Session-based anchoring
- Reset intervals can be set from 30 to 1440 minutes in 30-minute steps
- Precise anchor times for session mode (specific hour/minute)
- Offers 8 different logic options for generating signals
- Works as both entry and exit filter in strategies
Parameters:
1. Logic selection (8 options including crossovers and trend combinations)
2. Fast and Slow anchor period types
3. Fast and Slow reset periods (30-1440 mins)
4. Session start times for both VWAPs
Use Case:
Best for intraday strategies using session-based VWAP dynamics or multi-timeframe VWAP analysis.
Key Features:
- Uses both Fast and Slow VWAP with adjustable anchor periods
- Supports Daily, Weekly, Monthly or Session-based anchoring
- Reset intervals can be set from 30 to 1440 minutes in 30-minute steps
- Precise anchor times for session mode (specific hour/minute)
- Offers 8 different logic options for generating signals
- Works as both entry and exit filter in strategies
Parameters:
1. Logic selection (8 options including crossovers and trend combinations)
2. Fast and Slow anchor period types
3. Fast and Slow reset periods (30-1440 mins)
4. Session start times for both VWAPs
Use Case:
Best for intraday strategies using session-based VWAP dynamics or multi-timeframe VWAP analysis.
Comments
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443
using System;
using System.Collections.Generic;
using System.Drawing;
using ForexStrategyBuilder.Infrastructure.Entities;
using ForexStrategyBuilder.Infrastructure.Enums;
using ForexStrategyBuilder.Infrastructure.Interfaces;
namespace ForexStrategyBuilder.Indicators.Custom
{
public class AdvancedSessionVWAPCrossoverStepped : Indicator
{
public AdvancedSessionVWAPCrossoverStepped()
{
IndicatorName = "AdvancedSessionVWAPCrossoverStepped";
PossibleSlots = SlotTypes.OpenFilter | SlotTypes.CloseFilter;
IndicatorAuthor = "NAYA,+237674724684";
IndicatorVersion = "1.0";
IndicatorDescription = "Crossover between two SessionVWAP lines (Fast vs. Slow) with stepped parameters.";
}
public override void Initialize(SlotTypes slotType)
{
SlotType = slotType;
// Logic selection
IndParam.ListParam[0].Caption = "Logic";
IndParam.ListParam[0].ItemList = new[]
{
"Fast SessionVWAP crosses Slow SessionVWAP upward",
"Fast SessionVWAP crosses Slow SessionVWAP downward",
"Fast SessionVWAP is higher than Slow SessionVWAP",
"Fast SessionVWAP is lower than Slow SessionVWAP",
"Fast SessionVWAP rises, Slow SessionVWAP falls",
"Fast SessionVWAP falls, Slow SessionVWAP rises",
"Fast SessionVWAP rises, Slow SessionVWAP rises",
"Fast SessionVWAP falls, Slow SessionVWAP falls"
};
IndParam.ListParam[0].Index = 0;
IndParam.ListParam[0].Text = IndParam.ListParam[0].ItemList[0];
IndParam.ListParam[0].Enabled = true;
IndParam.ListParam[0].ToolTip = "Logic of application of the indicator.";
// Fast Anchor Period
IndParam.ListParam[1].Caption = "Fast Anchor Period";
IndParam.ListParam[1].ItemList = new[] { "Daily", "Weekly", "Monthly", "Session" };
IndParam.ListParam[1].Index = 3;
IndParam.ListParam[1].Text = "Session";
IndParam.ListParam[1].Enabled = true;
IndParam.ListParam[1].ToolTip = "Select the anchor period for Fast VWAP calculation.";
// Fast Reset Period
IndParam.ListParam[2].Caption = "Fast Reset Period (minutes)";
var fastResetPeriods = new List();
for (int i = 60; i <= 1440; i += 60)
fastResetPeriods.Add(i.ToString());
IndParam.ListParam[2].ItemList = fastResetPeriods.ToArray();
IndParam.ListParam[2].Index = 0;
IndParam.ListParam[2].Text = IndParam.ListParam[2].ItemList[0];
IndParam.ListParam[2].Enabled = true;
IndParam.ListParam[2].ToolTip = "Fast VWAP reset period in minutes (60-1440 in steps of 60).";
// Slow Anchor Period
IndParam.ListParam[3].Caption = "Slow Anchor Period";
IndParam.ListParam[3].ItemList = new[] { "Daily", "Weekly", "Monthly", "Session" };
IndParam.ListParam[3].Index = 3;
IndParam.ListParam[3].Text = "Session";
IndParam.ListParam[3].Enabled = true;
IndParam.ListParam[3].ToolTip = "Select the anchor period for Slow VWAP calculation.";
// Slow Reset Period
IndParam.ListParam[4].Caption = "Slow Reset Period (minutes)";
var slowResetPeriods = new List();
for (int i = 60; i <= 1440; i += 60)
slowResetPeriods.Add(i.ToString());
IndParam.ListParam[4].ItemList = slowResetPeriods.ToArray();
IndParam.ListParam[4].Index = 3;
IndParam.ListParam[4].Text = IndParam.ListParam[4].ItemList[3];
IndParam.ListParam[4].Enabled = true;
IndParam.ListParam[4].ToolTip = "Slow VWAP reset period in minutes (60-1440 in steps of 60).";
// Fast Anchor Time
IndParam.NumParam[0].Caption = "Fast Anchor Hour";
IndParam.NumParam[0].Value = 0;
IndParam.NumParam[0].Min = 0;
IndParam.NumParam[0].Max = 23;
IndParam.NumParam[0].Enabled = true;
IndParam.NumParam[0].ToolTip = "Hour (0-23) for Fast VWAP anchor (Session only).";
IndParam.NumParam[1].Caption = "Fast Anchor Minute";
IndParam.NumParam[1].Value = 0;
IndParam.NumParam[1].Min = 0;
IndParam.NumParam[1].Max = 59;
IndParam.NumParam[1].Enabled = true;
IndParam.NumParam[1].ToolTip = "Minute (0-59) for Fast VWAP anchor (Session only).";
// Slow Anchor Time
IndParam.NumParam[2].Caption = "Slow Anchor Hour";
IndParam.NumParam[2].Value = 0;
IndParam.NumParam[2].Min = 0;
IndParam.NumParam[2].Max = 23;
IndParam.NumParam[2].Enabled = true;
IndParam.NumParam[2].ToolTip = "Hour (0-23) for Slow VWAP anchor (Session only).";
IndParam.NumParam[3].Caption = "Slow Anchor Minute";
IndParam.NumParam[3].Value = 0;
IndParam.NumParam[3].Min = 0;
IndParam.NumParam[3].Max = 59;
IndParam.NumParam[3].Enabled = true;
IndParam.NumParam[3].ToolTip = "Minute (0-59) for Slow VWAP anchor (Session only).";
// Use previous bar value
IndParam.CheckParam[0].Caption = "Use previous bar value";
IndParam.CheckParam[0].Enabled = true;
IndParam.CheckParam[0].ToolTip = "If checked, uses the previous bar's value for signals.";
}
private double[] CalculateAdvancedVWAP(int anchorPeriodIndex, int anchorHour, int anchorMinute, int resetPeriod)
{
int bars = Bars;
double[] vwapBuffer = new double[bars];
double cumPV = 0.0;
double cumV = 0.0;
DateTime currentPeriodTime = DateTime.MinValue;
for (int iBar = 3; iBar < bars; iBar++)
{
DateTime barTime = Time[iBar];
DateTime barEndTime = barTime.AddMinutes((int)Period); // Bar end time
DateTime periodStart;
if (anchorPeriodIndex < 3) // Daily, Weekly, Monthly
{
periodStart = GetAnchorStartTime(barEndTime, anchorPeriodIndex + 1);
}
else // Session
{
periodStart = GetLastResetTimeStepped(barEndTime, anchorHour, anchorMinute, resetPeriod);
}
if (periodStart != currentPeriodTime)
{
cumPV = 0.0;
cumV = 0.0;
currentPeriodTime = periodStart;
}
double typicalPrice = (Open[iBar] + High[iBar] + Low[iBar] + Close[iBar]) / 4.0;
double volume = Volume[iBar];
cumPV += typicalPrice * volume;
cumV += volume;
vwapBuffer[iBar] = (cumV > 0.0) ? (cumPV / cumV) : 0.0;
}
return vwapBuffer;
}
private DateTime GetAnchorStartTime(DateTime currentTime, int anchorType)
{
switch (anchorType)
{
case 1: return currentTime.Date;
case 2:
DateTime monday = currentTime.Date;
while (monday.DayOfWeek != DayOfWeek.Monday)
monday = monday.AddDays(-1);
return monday;
case 3: return new DateTime(currentTime.Year, currentTime.Month, 1);
default: return currentTime.Date;
}
}
private DateTime GetLastResetTimeStepped(DateTime currentTime, int anchorHour, int anchorMinute, int resetPeriod)
{
DateTime anchorToday = new DateTime(
currentTime.Year, currentTime.Month, currentTime.Day,
anchorHour, anchorMinute, 0);
if (currentTime >= anchorToday)
{
TimeSpan timeSinceAnchor = currentTime - anchorToday;
int periods = (int)(timeSinceAnchor.TotalMinutes / resetPeriod);
return anchorToday.AddMinutes(periods * resetPeriod);
}
else
{
DateTime anchorYesterday = anchorToday.AddDays(-1);
TimeSpan timeSinceAnchor = currentTime - anchorYesterday;
int periods = (int)(timeSinceAnchor.TotalMinutes / resetPeriod);
return anchorYesterday.AddMinutes(periods * resetPeriod);
}
}
public override void Calculate(IDataSet dataSet)
{
DataSet = dataSet;
int fastAnchorPeriodIndex = IndParam.ListParam[1].Index;
int fastResetPeriod = int.Parse(IndParam.ListParam[2].Text);
int slowAnchorPeriodIndex = IndParam.ListParam[3].Index;
int slowResetPeriod = int.Parse(IndParam.ListParam[4].Text);
int fastAnchorHour = (int)IndParam.NumParam[0].Value;
int fastAnchorMinute = (int)IndParam.NumParam[1].Value;
int slowAnchorHour = (int)IndParam.NumParam[2].Value;
int slowAnchorMinute = (int)IndParam.NumParam[3].Value;
int usePrev = IndParam.CheckParam[0].Checked ? 1 : 0;
double[] fastVWAP = CalculateAdvancedVWAP(fastAnchorPeriodIndex, fastAnchorHour, fastAnchorMinute, fastResetPeriod);
double[] slowVWAP = CalculateAdvancedVWAP(slowAnchorPeriodIndex, slowAnchorHour, slowAnchorMinute, slowResetPeriod);
int firstBar = 3 + usePrev + 2;
double[] oscillator = new double[Bars];
for (int bar = firstBar; bar < Bars; bar++)
{
oscillator[bar] = fastVWAP[bar] - slowVWAP[bar];
}
Component = new IndicatorComp[4];
Component[0] = new IndicatorComp
{
CompName = "Fast VWAP",
DataType = IndComponentType.IndicatorValue,
ChartType = IndChartType.Line,
ChartColor = Color.Gold,
FirstBar = firstBar,
Value = fastVWAP
};
Component[1] = new IndicatorComp
{
CompName = "Slow VWAP",
DataType = IndComponentType.IndicatorValue,
ChartType = IndChartType.Line,
ChartColor = Color.DarkBlue,
FirstBar = firstBar,
Value = slowVWAP
};
Component[2] = new IndicatorComp
{
ChartType = IndChartType.NoChart,
FirstBar = firstBar,
Value = new double[Bars]
};
Component[3] = new IndicatorComp
{
ChartType = IndChartType.NoChart,
FirstBar = firstBar,
Value = new double[Bars]
};
if (SlotType == SlotTypes.OpenFilter)
{
Component[2].DataType = IndComponentType.AllowOpenLong;
Component[2].CompName = "Is long entry allowed";
Component[3].DataType = IndComponentType.AllowOpenShort;
Component[3].CompName = "Is short entry allowed";
}
else if (SlotType == SlotTypes.CloseFilter)
{
Component[2].DataType = IndComponentType.ForceCloseLong;
Component[2].CompName = "Close out long position";
Component[3].DataType = IndComponentType.ForceCloseShort;
Component[3].CompName = "Close out short position";
}
string logicText = IndParam.ListParam[0].Text;
for (int bar = firstBar + usePrev; bar < Bars; bar++)
{
int prevBar = bar - usePrev;
int prevPrevBar = prevBar - 1;
// Ensure we don't go out of bounds
if (prevPrevBar < 0) continue;
double fastCurrent = fastVWAP[prevBar];
double fastPrevious = fastVWAP[prevPrevBar];
double slowCurrent = slowVWAP[prevBar];
double slowPrevious = slowVWAP[prevPrevBar];
double oscCurrent = oscillator[prevBar];
double oscPrevious = oscillator[prevPrevBar];
switch (logicText)
{
case "Fast SessionVWAP crosses Slow SessionVWAP upward":
Component[2].Value[bar] = (oscCurrent > 0 && oscPrevious <= 0) ? 1 : 0;
Component[3].Value[bar] = (oscCurrent < 0 && oscPrevious >= 0) ? 1 : 0;
break;
case "Fast SessionVWAP crosses Slow SessionVWAP downward":
Component[2].Value[bar] = (oscCurrent < 0 && oscPrevious >= 0) ? 1 : 0;
Component[3].Value[bar] = (oscCurrent > 0 && oscPrevious <= 0) ? 1 : 0;
break;
case "Fast SessionVWAP is higher than Slow SessionVWAP":
Component[2].Value[bar] = oscCurrent > 0 ? 1 : 0;
Component[3].Value[bar] = oscCurrent < 0 ? 1 : 0;
break;
case "Fast SessionVWAP is lower than Slow SessionVWAP":
Component[2].Value[bar] = oscCurrent < 0 ? 1 : 0;
Component[3].Value[bar] = oscCurrent > 0 ? 1 : 0;
break;
case "Fast SessionVWAP rises, Slow SessionVWAP falls":
Component[2].Value[bar] = (fastCurrent > fastPrevious + Sigma() &&
slowCurrent < slowPrevious - Sigma()) ? 1 : 0;
Component[3].Value[bar] = (fastCurrent < fastPrevious - Sigma() &&
slowCurrent > slowPrevious + Sigma()) ? 1 : 0;
break;
case "Fast SessionVWAP falls, Slow SessionVWAP rises":
Component[2].Value[bar] = (fastCurrent < fastPrevious - Sigma() &&
slowCurrent > slowPrevious + Sigma()) ? 1 : 0;
Component[3].Value[bar] = (fastCurrent > fastPrevious + Sigma() &&
slowCurrent < slowPrevious - Sigma()) ? 1 : 0;
break;
case "Fast SessionVWAP rises, Slow SessionVWAP rises":
Component[2].Value[bar] = (fastCurrent > fastPrevious + Sigma() &&
slowCurrent > slowPrevious + Sigma()) ? 1 : 0;
Component[3].Value[bar] = (fastCurrent < fastPrevious - Sigma() &&
slowCurrent < slowPrevious - Sigma()) ? 1 : 0;
break;
case "Fast SessionVWAP falls, Slow SessionVWAP falls":
Component[2].Value[bar] = (fastCurrent < fastPrevious - Sigma() &&
slowCurrent < slowPrevious - Sigma()) ? 1 : 0;
Component[3].Value[bar] = (fastCurrent > fastPrevious + Sigma() &&
slowCurrent > slowPrevious + Sigma()) ? 1 : 0;
break;
}
}
}
public override void SetDescription()
{
string baseString = IndicatorName + (IndParam.CheckParam[0].Checked ? "* (" : " (");
string fastPeriodText = IndParam.ListParam[1].Text;
string slowPeriodText = IndParam.ListParam[3].Text;
string fastParamString = fastPeriodText == "Session"
? "Fast: Session {(int)IndParam.NumParam[0].Value:00}:{(int)IndParam.NumParam[1].Value:00}, {IndParam.ListParam[2].Text} min"
: "Fast: {fastPeriodText}, {IndParam.ListParam[2].Text} min";
string slowParamString = slowPeriodText == "Session"
? "Slow: Session {(int)IndParam.NumParam[2].Value:00}:{(int)IndParam.NumParam[3].Value:00}, {IndParam.ListParam[4].Text} min"
: "Slow: {slowPeriodText}, {IndParam.ListParam[4].Text} min";
string paramString = fastParamString + " | " + slowParamString + ")";
EntryFilterLongDescription = baseString + paramString + "; Fast VWAP ";
EntryFilterShortDescription = baseString + paramString + "; Fast VWAP ";
ExitFilterLongDescription = baseString + paramString + "; Fast VWAP ";
ExitFilterShortDescription = baseString + paramString + "; Fast VWAP ";
string logic = IndParam.ListParam[0].Text;
switch (logic)
{
case "Fast SessionVWAP crosses Slow SessionVWAP upward":
EntryFilterLongDescription += "crosses Slow VWAP upward";
EntryFilterShortDescription += "crosses Slow VWAP downward";
ExitFilterLongDescription += "crosses Slow VWAP upward";
ExitFilterShortDescription += "crosses Slow VWAP downward";
break;
case "Fast SessionVWAP crosses Slow SessionVWAP downward":
EntryFilterLongDescription += "crosses Slow VWAP downward";
EntryFilterShortDescription += "crosses Slow VWAP upward";
ExitFilterLongDescription += "crosses Slow VWAP downward";
ExitFilterShortDescription += "crosses Slow VWAP upward";
break;
case "Fast SessionVWAP is higher than Slow SessionVWAP":
EntryFilterLongDescription += "is higher than Slow VWAP";
EntryFilterShortDescription += "is lower than Slow VWAP";
ExitFilterLongDescription += "is higher than Slow VWAP";
ExitFilterShortDescription += "is lower than Slow VWAP";
break;
case "Fast SessionVWAP is lower than Slow SessionVWAP":
EntryFilterLongDescription += "is lower than Slow VWAP";
EntryFilterShortDescription += "is higher than Slow VWAP";
ExitFilterLongDescription += "is lower than Slow VWAP";
ExitFilterShortDescription += "is higher than Slow VWAP";
break;
case "Fast SessionVWAP rises, Slow SessionVWAP falls":
EntryFilterLongDescription += "rises while Slow VWAP falls";
EntryFilterShortDescription += "falls while Slow VWAP rises";
ExitFilterLongDescription += "rises while Slow VWAP falls";
ExitFilterShortDescription += "falls while Slow VWAP rises";
break;
case "Fast SessionVWAP falls, Slow SessionVWAP rises":
EntryFilterLongDescription += "falls while Slow VWAP rises";
EntryFilterShortDescription += "rises while Slow VWAP falls";
ExitFilterLongDescription += "falls while Slow VWAP rises";
ExitFilterShortDescription += "rises while Slow VWAP falls";
break;
case "Fast SessionVWAP rises, Slow SessionVWAP rises":
EntryFilterLongDescription += "and Slow VWAP are both rising";
EntryFilterShortDescription += "and Slow VWAP are both falling";
ExitFilterLongDescription += "and Slow VWAP are both rising";
ExitFilterShortDescription += "and Slow VWAP are both falling";
break;
case "Fast SessionVWAP falls, Slow SessionVWAP falls":
EntryFilterLongDescription += "and Slow VWAP are both falling";
EntryFilterShortDescription += "and Slow VWAP are both rising";
ExitFilterLongDescription += "and Slow VWAP are both falling";
ExitFilterShortDescription += "and Slow VWAP are both rising";
break;
}
}
public override string ToString()
{
string prefix = IndicatorName + (IndParam.CheckParam[0].Checked ? "* (" : " (");
string fastPeriodText = IndParam.ListParam[1].Text;
string slowPeriodText = IndParam.ListParam[3].Text;
string fastString = fastPeriodText == "Session"
? "{(int)IndParam.NumParam[0].Value:00}:{(int)IndParam.NumParam[1].Value:00}, {IndParam.ListParam[2].Text}"
: "{fastPeriodText}, {IndParam.ListParam[2].Text}";
string slowString = slowPeriodText == "Session"
? "{(int)IndParam.NumParam[2].Value:00}:{(int)IndParam.NumParam[3].Value:00}, {IndParam.ListParam[4].Text}"
: "{slowPeriodText}, {IndParam.ListParam[4].Text}";
return prefix + "Fast: " + fastString + " | Slow: " + slowString + ")";
}
}
}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284#property copyright "Copyright (C) 2025 NAYA +237674724684" #property link "NAYA +237674724684" #property version "1.0" #property strict #include <Forexsb.com/Indicator.mqh> #include <Forexsb.com/Enumerations.mqh> class AdvancedSessionVWAPCrossoverStepped : public Indicator { public: AdvancedSessionVWAPCrossoverStepped(SlotTypes slotType); virtual void Calculate(DataSet &dataSet); private: void CalculateAdvancedVWAP(int anchorPeriodIndex, int anchorHour, int anchorMinute, int resetPeriod, double &vwap[]); datetime GetAnchorStartTime(datetime currentTime, int anchorType); datetime GetLastResetTimeStepped(datetime currentTime, int anchorHour, int anchorMinute, int resetPeriod); int TimeDayCount(const MqlDateTime &dt); }; AdvancedSessionVWAPCrossoverStepped::AdvancedSessionVWAPCrossoverStepped(SlotTypes slotType) { SlotType = slotType; IndicatorName = "AdvancedSessionVWAPCrossoverStepped"; WarningMessage = "Crossover between two SessionVWAP lines (Fast vs. Slow) with stepped parameters."; IsAllowLTF = true; ExecTime = ExecutionTime_DuringTheBar; IsSeparateChart = false; IsDiscreteValues = false; IsDefaultGroupAll = false; } void AdvancedSessionVWAPCrossoverStepped::Calculate(DataSet &dataSet) { Data = GetPointer(dataSet); // Read parameters int fastAnchorPeriodIndex = (int)ListParam[1].Index; int fastResetPeriod = (int)StringToInteger(ListParam[2].Text); int slowAnchorPeriodIndex = (int)ListParam[3].Index; int slowResetPeriod = (int)StringToInteger(ListParam[4].Text); int fastAnchorHour = (int)NumParam[0].Value; int fastAnchorMinute = (int)NumParam[1].Value; int slowAnchorHour = (int)NumParam[2].Value; int slowAnchorMinute = (int)NumParam[3].Value; int previous = CheckParam[0].Checked ? 1 : 0; // Compute VWAPs double fastVWAP[]; CalculateAdvancedVWAP(fastAnchorPeriodIndex, fastAnchorHour, fastAnchorMinute, fastResetPeriod, fastVWAP); double slowVWAP[]; CalculateAdvancedVWAP(slowAnchorPeriodIndex, slowAnchorHour, slowAnchorMinute, slowResetPeriod, slowVWAP); // Build oscillator int firstBar = 3 + previous + 2; double oscillator[]; ArrayResize(oscillator, Data.Bars); ArrayInitialize(oscillator, 0.0); for(int bar = firstBar; bar < Data.Bars; bar++) { oscillator[bar] = fastVWAP[bar] - slowVWAP[bar]; } // Create components Component[0].CompName = "Fast VWAP"; Component[0].DataType = IndComponentType_IndicatorValue; Component[0].FirstBar = firstBar; ArrayResize(Component[0].Value, Data.Bars); ArrayCopy(Component[0].Value, fastVWAP); Component[1].CompName = "Slow VWAP"; Component[1].DataType = IndComponentType_IndicatorValue; Component[1].FirstBar = firstBar; ArrayResize(Component[1].Value, Data.Bars); ArrayCopy(Component[1].Value, slowVWAP); ArrayResize(Component[2].Value, Data.Bars); ArrayInitialize(Component[2].Value, 0.0); Component[2].FirstBar = firstBar; ArrayResize(Component[3].Value, Data.Bars); ArrayInitialize(Component[3].Value, 0.0); Component[3].FirstBar = firstBar; // Set component types based on slot if(SlotType == SlotTypes_OpenFilter) { Component[2].DataType = IndComponentType_AllowOpenLong; Component[2].CompName = "Is long entry allowed"; Component[3].DataType = IndComponentType_AllowOpenShort; Component[3].CompName = "Is short entry allowed"; } else if(SlotType == SlotTypes_CloseFilter) { Component[2].DataType = IndComponentType_ForceCloseLong; Component[2].CompName = "Close out long position"; Component[3].DataType = IndComponentType_ForceCloseShort; Component[3].CompName = "Close out short position"; } // Apply logic string logicText = ListParam[0].Text; for(int bar = firstBar + previous; bar < Data.Bars; bar++) { double fastCurrent = fastVWAP[bar - previous]; double fastPrevious = fastVWAP[bar - previous - 1]; double slowCurrent = slowVWAP[bar - previous]; double slowPrevious = slowVWAP[bar - previous - 1]; double oscCurrent = oscillator[bar - previous]; double oscPrevious = oscillator[bar - previous - 1]; if(logicText == "Fast SessionVWAP crosses Slow SessionVWAP upward") { Component[2].Value[bar] = (oscCurrent > 0 && oscPrevious <= 0) ? 1.0 : 0.0; Component[3].Value[bar] = (oscCurrent < 0 && oscPrevious >= 0) ? 1.0 : 0.0; } else if(logicText == "Fast SessionVWAP crosses Slow SessionVWAP downward") { Component[2].Value[bar] = (oscCurrent < 0 && oscPrevious >= 0) ? 1.0 : 0.0; Component[3].Value[bar] = (oscCurrent > 0 && oscPrevious <= 0) ? 1.0 : 0.0; } else if(logicText == "Fast SessionVWAP is higher than Slow SessionVWAP") { Component[2].Value[bar] = (oscCurrent > 0) ? 1.0 : 0.0; Component[3].Value[bar] = (oscCurrent < 0) ? 1.0 : 0.0; } else if(logicText == "Fast SessionVWAP is lower than Slow SessionVWAP") { Component[2].Value[bar] = (oscCurrent < 0) ? 1.0 : 0.0; Component[3].Value[bar] = (oscCurrent > 0) ? 1.0 : 0.0; } else if(logicText == "Fast SessionVWAP rises, Slow SessionVWAP falls") { Component[2].Value[bar] = (fastCurrent > fastPrevious + Sigma() && slowCurrent < slowPrevious - Sigma()) ? 1.0 : 0.0; Component[3].Value[bar] = (fastCurrent < fastPrevious - Sigma() && slowCurrent > slowPrevious + Sigma()) ? 1.0 : 0.0; } else if(logicText == "Fast SessionVWAP falls, Slow SessionVWAP rises") { Component[2].Value[bar] = (fastCurrent < fastPrevious - Sigma() && slowCurrent > slowPrevious + Sigma()) ? 1.0 : 0.0; Component[3].Value[bar] = (fastCurrent > fastPrevious + Sigma() && slowCurrent < slowPrevious - Sigma()) ? 1.0 : 0.0; } else if(logicText == "Fast SessionVWAP rises, Slow SessionVWAP rises") { Component[2].Value[bar] = (fastCurrent > fastPrevious + Sigma() && slowCurrent > slowPrevious + Sigma()) ? 1.0 : 0.0; Component[3].Value[bar] = (fastCurrent < fastPrevious - Sigma() && slowCurrent < slowPrevious - Sigma()) ? 1.0 : 0.0; } else if(logicText == "Fast SessionVWAP falls, Slow SessionVWAP falls") { Component[2].Value[bar] = (fastCurrent < fastPrevious - Sigma() && slowCurrent < slowPrevious - Sigma()) ? 1.0 : 0.0; Component[3].Value[bar] = (fastCurrent > fastPrevious + Sigma() && slowCurrent > slowPrevious + Sigma()) ? 1.0 : 0.0; } } } void AdvancedSessionVWAPCrossoverStepped::CalculateAdvancedVWAP(int anchorPeriodIndex, int anchorHour, int anchorMinute, int resetPeriod, double &vwap[]) { int bars = Data.Bars; ArrayResize(vwap, bars); ArrayInitialize(vwap, 0.0); double cumPV = 0.0; double cumV = 0.0; datetime currentPeriodTime = 0; for(int iBar = 3; iBar < bars; iBar++) { datetime barTime = Data.Time[iBar]; datetime barEndTime = barTime + (Period() * 60); // Bar end time datetime periodStart; if(anchorPeriodIndex < 3) // Daily, Weekly, Monthly { periodStart = GetAnchorStartTime(barEndTime, anchorPeriodIndex + 1); } else // Session { periodStart = GetLastResetTimeStepped(barEndTime, anchorHour, anchorMinute, resetPeriod); } if(periodStart != currentPeriodTime) { cumPV = 0.0; cumV = 0.0; currentPeriodTime = periodStart; } double typicalPrice = (Data.Open[iBar] + Data.High[iBar] + Data.Low[iBar] + Data.Close[iBar]) / 4.0; double volume = Data.Volume[iBar]; cumPV += typicalPrice * volume; cumV += volume; vwap[iBar] = (cumV > 0.0) ? (cumPV / cumV) : 0.0; } } datetime AdvancedSessionVWAPCrossoverStepped::GetAnchorStartTime(datetime currentTime, int anchorType) { MqlDateTime s; TimeToStruct(currentTime, s); switch(anchorType) { case 1: // Daily s.hour = s.min = s.sec = 0; break; case 2: // Weekly (Monday) while(s.day_of_week != 1) // Monday==1 { currentTime -= 86400; TimeToStruct(currentTime, s); } s.hour = s.min = s.sec = 0; break; case 3: // Monthly s.day = 1; s.hour = s.min = s.sec = 0; break; default: s.hour = s.min = s.sec = 0; break; } return StructToTime(s); } datetime AdvancedSessionVWAPCrossoverStepped::GetLastResetTimeStepped(datetime currentTime, int anchorHour, int anchorMinute, int resetPeriod) { MqlDateTime currentMqlTime; TimeToStruct(currentTime, currentMqlTime); // Create today's anchor time MqlDateTime anchorToday = currentMqlTime; anchorToday.hour = anchorHour; anchorToday.min = anchorMinute; anchorToday.sec = 0; datetime anchorTimeToday = StructToTime(anchorToday); if(currentTime >= anchorTimeToday) { // Calculate how many full reset periods have passed int minutesSinceAnchor = (int)((currentTime - anchorTimeToday) / 60); int periods = minutesSinceAnchor / resetPeriod; return anchorTimeToday + (periods * resetPeriod * 60); } else { // Use yesterday's anchor time datetime anchorYesterday = anchorTimeToday - 86400; int minutesSinceAnchor = (int)((currentTime - anchorYesterday) / 60); int periods = minutesSinceAnchor / resetPeriod; return anchorYesterday + (periods * resetPeriod * 60); } } int AdvancedSessionVWAPCrossoverStepped::TimeDayCount(const MqlDateTime &dt) { if(dt.mon == 2) { // February if((dt.year % 4 == 0 && dt.year % 100 != 0) || dt.year % 400 == 0) return 29; // Leap year else return 28; } else if(dt.mon == 4 || dt.mon == 6 || dt.mon == 9 || dt.mon == 11) { return 30; } return 31; }
Risk warning: Forex, spread bets and CFD are leveraged products. They may not be suitable for you as they carry a high degree of risk to your capital and you can lose more than your initial investment. You should ensure you understand all of the risks.
Copyright © 2006 - 2025, Forex Software Ltd.;
Copyright © 2006 - 2025, Forex Software Ltd.;