Prior Week OHLC (Update Feb.17.2025 for 2.0.0.50-RC.419)

Prior Week OHLC is based on the prior daily OHLC data from Tickblaze.

To display weekly OHLC levels on any chart, at least two weeks of data must be loaded for the indicator to calculate the levels properly.

namespace CC.Indicators.PriorWeekOHLC;

/// <summary>
/// Prior Week OHLC [CC.PWOHLC]
/// </summary>
/// 
public partial class cPriorWeekOHLC : Indicator
{
    [Plot("Open")]
    public PlotSeries Open { get; set; } = new(Color.Orange, LineStyle.Dash);

    [Plot("High")]
    public PlotSeries High { get; set; } = new(Color.Red, LineStyle.Dash);

    [Plot("Low")]
    public PlotSeries Low { get; set; } = new(Color.Blue, LineStyle.Dash);

    [Plot("Close")]
    public PlotSeries Close { get; set; } = new(Color.Gray, LineStyle.Dash);

    private double _open, _high, _low, _close;
    private DayOfWeek _lastDay;
    // Bar series for different timeframes

    public cPriorWeekOHLC()
    {
        Name = "Prior Week OHLC";
        ShortName = "CC.PWOHLC";
        IsOverlay = true;
    }

    protected override void Calculate(int index)
    {
        var bar = Bars[index];
        var currentDay = bar.Time.DayOfWeek;
        var isNewSession = false;
        
        if (index > 0 && double.IsNaN(Open[index]))
        {
            Open[index] = Open[index - 1];
            High[index] = High[index - 1];
            Low[index] = Low[index - 1];
            Close[index] = Close[index - 1];
        }
        else
        if (index == 0)
        {
            _open = bar.Open;
            _high = bar.High;
            _low = bar.Low;
            _close = bar.Close;
        }

        if (currentDay != _lastDay)
        {
            if (currentDay == DayOfWeek.Sunday) isNewSession = true;
            _lastDay = currentDay;
        }

        if (isNewSession)
        {
            Open[index] = _open;
            High[index] = _high;
            Low[index] = _low;
            Close[index] = _close;

            _open = bar.Open;
            _high = bar.High;
            _low = bar.Low;
            _close = bar.Close;
        }
        else
        {
            _high = Math.Max(_high, bar.High);
            _low = Math.Min(_low, bar.Low);
            _close = bar.Close;
        }
    }
}

CC.Indicators.PriorWeekOHLC.zip
108.53KB
5
16 replies