Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 36 additions & 2 deletions Technical/VWAP.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ public enum VWAPPeriodType
Daily,
Weekly,
Monthly,
Quarterly,
Yearly,
All,
Custom
}
Expand Down Expand Up @@ -619,7 +621,7 @@ protected override void OnCalculate(int bar, decimal value)
}

if (!ShowFirstPeriod && !AllowCustomStartPoint && !_calcStarted
&& Type is VWAPPeriodType.Weekly or VWAPPeriodType.Monthly or VWAPPeriodType.Custom)
&& Type is VWAPPeriodType.Weekly or VWAPPeriodType.Monthly or VWAPPeriodType.Quarterly or VWAPPeriodType.Yearly or VWAPPeriodType.Custom)
{
if (bar == 0 && Type is not VWAPPeriodType.Custom)
return;
Expand All @@ -632,6 +634,12 @@ protected override void OnCalculate(int bar, decimal value)
case VWAPPeriodType.Monthly:
_calcStarted = IsNewMonth(bar);
break;
case VWAPPeriodType.Quarterly:
_calcStarted = IsNewQuarter(bar);
break;
case VWAPPeriodType.Yearly:
_calcStarted = IsNewYear(bar);
break;
case VWAPPeriodType.Custom:
_calcStarted = IsNewCustomSession(bar);
break;
Expand Down Expand Up @@ -685,6 +693,8 @@ protected override void OnCalculate(int bar, decimal value)
case VWAPPeriodType.Daily when IsNewSession(bar):
case VWAPPeriodType.Weekly when IsNewWeek(bar):
case VWAPPeriodType.Monthly when IsNewMonth(bar):
case VWAPPeriodType.Quarterly when IsNewQuarter(bar):
case VWAPPeriodType.Yearly when IsNewYear(bar):
case VWAPPeriodType.Custom when IsNewCustomSession(bar):
needReset = true;
break;
Expand Down Expand Up @@ -945,6 +955,30 @@ private int BarFromDate(DateTime date)
return bar;
}

private bool IsNewQuarter(int bar)
{
if (bar == 0)
return false;

var prevTime = GetCandle(bar - 1).Time;
var currTime = GetCandle(bar).Time;

int GetQuarter(DateTime dt) => (dt.Month - 1) / 3 + 1;

return prevTime.Year != currTime.Year || GetQuarter(currTime) != GetQuarter(prevTime);
}

private bool IsNewYear(int bar)
{
if (bar == 0)
return false;

var prevTime = GetCandle(bar - 1).Time;
var currTime = GetCandle(bar).Time;

return prevTime.Year != currTime.Year;
}

private bool IsNewCustomSession(int bar)
{
var currentBar = GetCandle(bar);
Expand Down Expand Up @@ -1022,4 +1056,4 @@ private static CrossColor GetColorFromHex(string hexString)
}

#endregion
}
}