I accumulate volume and price in OnMarketData:
protected override void OnMarketData(MarketDataEventArgs marketDataUpdate)
{
// Reset for every session
if (sessionIterator.IsNewSession(marketDataUpdate.Time, false))
{
Print("NewSession: " + marketDataUpdate.Time.ToString());
totalShares = 0;
totalDollars = 0;
sessionIterator.GetNextSession(marketDataUpdate.Time, false);
}
// Check session time
if (!sessionIterator.IsInSession(marketDataUpdate.Time, false, true))
{
return;
}
//Print(marketDataUpdate.Time.ToString() + totalShares.ToString());
// TickReplay events only occur on the "Last" market data type
if (marketDataUpdate.MarketDataType == MarketDataType.Last)
{
totalShares += marketDataUpdate.Volume;
totalDollars += marketDataUpdate.Volume * marketDataUpdate.Price;
Vwap[0] = totalDollars / totalShares;
}
}
However, when I attempt to use market replay, the indicator receives the full session's replay data before I press the "play" button in the market replay window.
Once I press the play button, the indicator receives the replay data again without an IsNewSession reset, and this causes the replay days vwap data to be double-counted.
So here is the setup:
Nt 8 version: 8.0.19.0
Chart Settings
Instrument: Es 06-19 (Downloaded from Ninjatrader market replay server)
Tick Replay: Enabled
Load Data based on: Days
Days To Load: 5
End date: 06-03-2019
Trading Hours: CME US Index Futures RTH
5 min candlestick chart based on last price.
Chart is as expected. Vwap is drawing, resetting on each new session.
At this point I connect to "Playback Connection" select start date of 06-04-2019. I drag the playback control to start the playback just before the 9:30 open, usually something like 9:29:04.
I have NOT pressed the play button, but my indicator OnMarketData has hit the sessionIterator.IsNewSession()(for 06-04-2019 session) condition(perhaps to be expected, not sure, but either way not a problem) and OnMarketData receives all the trades for the entire replay session from 06-04-2019 9:30:00 am until 06-04-2019 16:15:00(not expected). I'll repeat the above, the replay start time is like 9:29:04, I have not started the replay, but OnMarketData receives all the replay's session data. None of the 06-04-2019 candle sticks or vwap indicators are rendered.
Now I press play, I never get a IsNewSession() reset, and OnMarketData gets the 06-04-2019 replay data AGAIN, and the vwap is off because it's not starting out with zeroed variables once the replay is started like it should.
Why am I getting the replay data before the replay is started and then getting same data after the replay is started ?
Just hit me, perhaps the data is not coming from the replay file but it's coming from my tick history data ? I'll try to figure that out.
Thanks.
Full code:
#region Using declarations
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.IO;
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
{
public class DailyVwap2 : Indicator
{
protected double totalDollars = 0;
protected double totalShares = 0;
protected SessionIterator sessionIterator;
private StreamWriter sw;
protected override void OnStateChange()
{
if (State == State.SetDefaults)
{
BarsRequiredToPlot = 0;
Description = @"Enter the description for your new custom Indicator here.";
Name = "DailyVwap2";
Calculate = Calculate.OnEachTick;
IsOverlay = true;
DisplayInDataBox = true;
DrawOnPricePanel = true;
DrawHorizontalGridLines = true;
DrawVerticalGridLines = true;
PaintPriceMarkers = false;
ScaleJustification = NinjaTrader.Gui.Chart.ScaleJustification.Right;
//Disable this property if your indicator requires custom values that cumulate with each new market data event.
//See Help Guide for additional information.
IsSuspendedWhileInactive = false;
AddPlot(Brushes.DarkTurquoise, "Vwap");
}
else if (State == State.DataLoaded)
{
sessionIterator = new SessionIterator(Bars);
}
else if (State == State.Configure)
{
}
}
protected override void OnBarUpdate()
{
}
protected override void OnMarketData(MarketDataEventArgs marketDataUpdate)
{
// Resset for every session
if (sessionIterator.IsNewSession(marketDataUpdate.Time, false))
{
Print("NewSession: " + marketDataUpdate.Time.ToString());
totalShares = 0;
totalDollars = 0;
sessionIterator.GetNextSession(marketDataUpdate.Time, false);
}
// Check session time
if (!sessionIterator.IsInSession(marketDataUpdate.Time, false, true))
{
return;
}
//Print(marketDataUpdate.Time.ToString() + totalShares.ToString());
// TickReplay events only occur on the "Last" market data type
if (marketDataUpdate.MarketDataType == MarketDataType.Last)
{
totalShares += marketDataUpdate.Volume;
totalDollars += marketDataUpdate.Volume * marketDataUpdate.Price;
Vwap[0] = totalDollars / totalShares;
}
}
#region Properties
[Browsable(false)]
[XmlIgnore]
public Series<double> Vwap
{
get { return Values[0]; }
}
#endregion
}
}
#region NinjaScript generated code. Neither change nor remove.
namespace NinjaTrader.NinjaScript.Indicators
{
public partial class Indicator : NinjaTrader.Gui.NinjaScript.IndicatorRenderBase
{
private DailyVwap2[] cacheDailyVwap2;
public DailyVwap2 DailyVwap2()
{
return DailyVwap2(Input);
}
public DailyVwap2 DailyVwap2(ISeries<double> input)
{
if (cacheDailyVwap2 != null)
for (int idx = 0; idx < cacheDailyVwap2.Length; idx++)
if (cacheDailyVwap2[idx] != null && cacheDailyVwap2[idx].EqualsInput(input))
return cacheDailyVwap2[idx];
return CacheIndicator<DailyVwap2>(new DailyVwap2(), input, ref cacheDailyVwap2);
}
}
}
namespace NinjaTrader.NinjaScript.MarketAnalyzerColumns
{
public partial class MarketAnalyzerColumn : MarketAnalyzerColumnBase
{
public Indicators.DailyVwap2 DailyVwap2()
{
return indicator.DailyVwap2(Input);
}
public Indicators.DailyVwap2 DailyVwap2(ISeries<double> input )
{
return indicator.DailyVwap2(input);
}
}
}
namespace NinjaTrader.NinjaScript.Strategies
{
public partial class Strategy : NinjaTrader.Gui.NinjaScript.StrategyRenderBase
{
public Indicators.DailyVwap2 DailyVwap2()
{
return indicator.DailyVwap2(Input);
}
public Indicators.DailyVwap2 DailyVwap2(ISeries<double> input )
{
return indicator.DailyVwap2(input);
}
}
}
#endregion

Comment