For example if I set it up for 100 seconds you would expect the bar to complete every 100 seconds but instead a new one is created on every price change.
For simplicity sake I have copied the original code for the SecondBarsType with my 2 changes necessary for just changing the name etc.
#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;
#endregion
//This namespace holds Bars types in this folder and is required. Do not change it.
namespace NinjaTrader.NinjaScript.BarsTypes
{
public class FXR_SecondFilter : BarsType
{
public override void ApplyDefaultBasePeriodValue(BarsPeriod period)
{
}
public override void ApplyDefaultValue(BarsPeriod period)
{
period.BarsPeriodTypeName = Custom.Resource.BarsPeriodTypeNameSecond;
period.Value = 30;
}
public override string ChartLabel(DateTime time)
{
return time.ToString("HH:mm:ss");
}
public override int GetInitialLookBackDays(BarsPeriod period, TradingHours tradingHours, int barsBack)
{
return (int) Math.Max(1, Math.Ceiling(barsBack / Math.Max(1, 8.0 * 60 * 60 / period.Value)) * 7.0 / 5.0);
}
public override double GetPercentComplete(Bars bars, DateTime now)
{
return now <= bars.LastBarTime ? 1.0 - (bars.LastBarTime.Subtract(now).TotalSeconds / bars.BarsPeriod.Value) : 1;
}
protected override void OnDataPoint(Bars bars, double open, double high, double low, double close, DateTime time, long volume, bool isBar, double bid, double ask)
{
if (SessionIterator == null)
SessionIterator = new SessionIterator(bars);
if (bars.Count == 0 || time >= bars.LastBarTime)
{
DateTime barTime = TimeToBarTime(bars, time, isBar);
AddBar(bars, open, high, low, close, barTime, volume);
}
else
UpdateBar(bars, high, low, close, bars.LastBarTime, volume);
}
protected override void OnStateChange()
{
if (State == State.SetDefaults)
{
// ONLY CHANGES - NEXT 2 LINES - ORIGINAL COMMENTED OUT
Name = "FXR Second Filter"; //Custom.Resource.NinjaScriptBarsTypeSecond;
BarsPeriod = new BarsPeriod{BarsPeriodType = (BarsPeriodType)18,BarsPeriodTypeName = Name};//new BarsPeriod { BarsPeriodType = BarsPeriodType.Second };
BuiltFrom = BarsPeriodType.Tick;
DaysToLoad = 3;
IsIntraday = true;
}
else if (State == State.Configure)
{
Name = string.Format("{0}{1}", string.Format(Core.Globals.GeneralOptions.CurrentCulture, Custom.Resource.DataBarsTypeSecond, BarsPeriod.Value), (BarsPeriod.MarketDataType != MarketDataType.Last ? string.Format(" - {0}", Core.Globals.ToLocalizedObject(BarsPeriod.MarketDataType, Core.Globals.GeneralOptions.CurrentUICulture)) : string.Empty));
Properties.Remove(Properties.Find("BaseBarsPeriodType", true));
Properties.Remove(Properties.Find("BaseBarsPeriodValue", true));
Properties.Remove(Properties.Find("PointAndFigurePriceType", true));
Properties.Remove(Properties.Find("ReversalType", true));
Properties.Remove(Properties.Find("Value2", true));
}
}
private DateTime TimeToBarTime(Bars bars, DateTime time, bool isBar)
{
if (SessionIterator.IsNewSession(time, isBar))
SessionIterator.GetNextSession(time, isBar);
if (bars.IsResetOnNewTradingDay || !bars.IsResetOnNewTradingDay && bars.Count == 0)
{
DateTime barTimeStamp = isBar
? SessionIterator.ActualSessionBegin.AddSeconds(Math.Ceiling(Math.Ceiling(Math.Max(0, time.Subtract(SessionIterator.ActualSessionBegin).TotalSeconds)) / bars.BarsPeriod.Value) * bars.BarsPeriod.Value)
: SessionIterator.ActualSessionBegin.AddSeconds(bars.BarsPeriod.Value + Math.Floor(Math.Floor(Math.Max(0, time.Subtract(SessionIterator.ActualSessionBegin).TotalSeconds)) / bars.BarsPeriod.Value) * bars.BarsPeriod.Value);
if (bars.TradingHours.Sessions.Count > 0 && barTimeStamp > SessionIterator.ActualSessionEnd) // Cut last bar in session down to session end on odd session end time
barTimeStamp = SessionIterator.ActualSessionEnd;
return barTimeStamp;
}
else
{
DateTime lastBarTime = bars.GetTime(bars.Count - 1);
DateTime barTimeStamp = isBar
? lastBarTime.AddSeconds(Math.Ceiling(Math.Ceiling(Math.Max(0, time.Subtract(lastBarTime).TotalSeconds)) / bars.BarsPeriod.Value) * bars.BarsPeriod.Value)
: lastBarTime.AddSeconds(bars.BarsPeriod.Value + Math.Floor(Math.Floor(Math.Max(0, time.Subtract(lastBarTime).TotalSeconds)) / bars.BarsPeriod.Value) * bars.BarsPeriod.Value);
if (bars.TradingHours.Sessions.Count > 0 && barTimeStamp > SessionIterator.ActualSessionEnd)
{
DateTime saveActualSessionEnd = SessionIterator.ActualSessionEnd;
SessionIterator.GetNextSession(SessionIterator.ActualSessionEnd.AddSeconds(1), isBar);
barTimeStamp = SessionIterator.ActualSessionBegin.AddSeconds((int) barTimeStamp.Subtract(saveActualSessionEnd).TotalSeconds);
}
return barTimeStamp;
}
}
}
}

Comment