It's anchored VWAP with manual input of anchoring location (date and time).
Grok3 converted this for NT8 but I can't compile.
Can someone make it error free?
region Using declarations
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Input;
using System.Windows.Media;
using System.Xml.Serialization;
using NinjaTrader.Cbi;
using NinjaTrader.Gui;
using NinjaTrader.Gui.Chart;
using NinjaTrader.Gui.SuperDom;
using NinjaTrader.Gui.Tools;
using NinjaTrader.Data;
using NinjaTrader.NinjaScript;
using NinjaTrader.Core.FloatingPoint;
using NinjaTrader.NinjaScript.DrawingTools;
#endregion
// This namespace holds Indicators in this folder and is required. Do not change it.
namespace NinjaTrader.NinjaScript.Indicators
{
[NinjaScriptType]
public class MyCustomIndicator : Indicator // Changed class name to match file
{
private double cumulativeValue; // Sum of (price * volume)
private double cumulativeVolume; // Sum of volume
protected override void OnStateChange()
{
if (State == State.SetDefaults)
{
Description = "Calculates an Anchored VWAP starting from a specific date and time.";
Name = "MyCustomIndicator"; // Updated to match class name
Calculate = Calculate.OnBarClose;
IsOverlay = true;
DisplayInDataBox = true;
DrawOnPricePanel = true;
PaintPriceMarkers = true;
// Initialize default anchor date and time (July 1, 2019, 9:30 AM)
AnchorDate = new DateTime(2019, 7, 1);
AnchorTime = new TimeSpan(9, 30, 0);
AddPlot(new Stroke(Brushes.Cyan, 3), PlotStyle.Line, "VWAP");
}
else if (State == State.DataLoaded)
{
cumulativeValue = 0;
cumulativeVolume = 0;
}
}
protected override void OnBarUpdate()
{
// Combine date and time of the current bar into a single DateTime
DateTime barDateTime = Time[0];
// Combine anchor date and time into a single DateTime
DateTime anchorDateTime = AnchorDate.Date.Add(AnchorTime);
// Check if the current bar is on or after the anchor date and time
if (barDateTime >= anchorDateTime)
{
// Calculate typical price as (high + low + close) / 3
double typicalPrice = (High[0] + Low[0] + Close[0]) / 3.0;
// Add to cumulative sums
cumulativeValue += typicalPrice * Volume[0];
cumulativeVolume += Volume[0];
}
// Calculate and plot VWAP if there's volume, otherwise set to double.NaN
if (cumulativeVolume > 0)
{
Values[0][0] = cumulativeValue / cumulativeVolume;
}
else
{
Values[0][0] = double.NaN;
}
}
region Properties
[NinjaScriptProperty]
[Display(Name = "Anchor Date", Description = "Date to start VWAP calculation", Order = 1, GroupName = "Parameters")]
[PropertyEditor("NinjaTrader.Gui.Tools.DateEditor")]
public DateTime AnchorDate { get; set; }
[NinjaScriptProperty]
[Display(Name = "Anchor Time", Description = "Time to start VWAP calculation", Order = 2, GroupName = "Parameters")]
[PropertyEditor("NinjaTrader.Gui.Tools.TimeEditor")]
public TimeSpan AnchorTime { get; set; }
#endregion
}
}

Comment