Class BarSeries
- Namespace
- Balsam
- Assembly
- Balsam.Backtester.dll
Encapsulates a collection of bars and returns timeseries of open, high, low, close, volume, open interest and unadjusted close data.
public class BarSeries : SeriesBase<Bar>, ISeries, IEnumerable<Bar>, IEnumerable, ICloneable
- Inheritance
-
BarSeries
- Implements
- Derived
- Inherited Members
- Extension Methods
Constructors
BarSeries()
Creates an empty BarSeries.
public BarSeries()
BarSeries(IEnumerable<Bar>)
Creates a new BarSeries using the specified bars.
public BarSeries(IEnumerable<Bar> bars)
Parameters
bars
IEnumerable<Bar>
BarSeries(int)
Creates an emtpy BarSeries with the specified capacity.
public BarSeries(int capacity)
Parameters
capacity
int
Properties
Close
Gets the timeseries of closing prices.
public TimeSeries Close { get; }
Property Value
High
Gets the timeseries of high prices.
public TimeSeries High { get; }
Property Value
Instrument
Gets/sets the instrument associated with this BarSeries.
public Instrument Instrument { get; set; }
Property Value
Low
Gets the timeseries of low prices.
public TimeSeries Low { get; }
Property Value
Open
Gets the timeseries of opening prices.
public TimeSeries Open { get; }
Property Value
OpenInterest
Gets the timeseries of open interest.
public TimeSeries OpenInterest { get; }
Property Value
UnadjustedClose
Gets the timeseries of unadjusted closing prices.
public TimeSeries UnadjustedClose { get; }
Property Value
Volume
Gets the timeseries of volume.
public TimeSeries Volume { get; }
Property Value
Methods
Add(Bar)
Adds a bar to the end of the collection.
public void Add(Bar bar)
Parameters
bar
Bar
Add(BarSeries, double, bool)
Adds the specfied value to a BarSeries.
public static BarSeries Add(BarSeries series, double value, bool modifyUnadjustedClose = false)
Parameters
Returns
Adjust(MathematicalOperation, double, int, bool)
Adjusts opens, highs, lows, closes and (optionally) unadjusted closes by a specified value. Useful for back-adjusting.
public void Adjust(MathematicalOperation operation, double value, int digitsToRound = -1, bool modifyUnadjustedClose = false)
Parameters
operation
MathematicalOperationvalue
doubledigitsToRound
intNumber of digits to round. -1 disables rounding.
modifyUnadjustedClose
bool
Clone(bool)
Returns a copy of this BarSeries.
public BarSeries Clone(bool deepCopy = false)
Parameters
deepCopy
boolSet to true to return totally independent objects. Generally not necessary since Bar object is read-only.
Returns
Compare(BarSeries, BarSeries, bool, int, int)
Compares two BarSeries and reports any discrepancies to the console.
public static void Compare(BarSeries left, BarSeries right, bool includeVolumeAndOpenInt = true, int maxDisrepancies = 100, int digitsToRound = 15)
Parameters
left
BarSeriesright
BarSeriesincludeVolumeAndOpenInt
boolmaxDisrepancies
intdigitsToRound
intUse -1 to force strict comparison.
Compress(ICompressionProvider)
Compresses the series using the specified compression provider.
public BarSeries Compress(ICompressionProvider compressionProvider)
Parameters
compressionProvider
ICompressionProvider
Returns
Divide(BarSeries, double, bool)
Divides a BarSeries by the specified value.
public static BarSeries Divide(BarSeries series, double denominator, bool modifyUnadjustedClose = false)
Parameters
Returns
Divide(double, BarSeries, bool)
Divides a value by a BarSeries returning a new BarSeries.
public static BarSeries Divide(double numerator, BarSeries series, bool modifyUnadjustedClose = false)
Parameters
Returns
Equals(BarSeries)
Returns true if all observations of the specified BarSeries are identical to this BarSeries.
public bool Equals(BarSeries series)
Parameters
series
BarSeries
Returns
Equals(BarSeries, BarSeries)
Returns true if the specified BarSeries are identical across all bars.
public static bool Equals(BarSeries left, BarSeries right)
Parameters
Returns
ExportToFileCore(StreamWriter, string, bool)
The core file export code.
protected override void ExportToFileCore(StreamWriter sw, string delimiter = ",", bool includeHeader = false)
Parameters
sw
StreamWriterdelimiter
stringincludeHeader
bool
GetValue(int)
Returns the bar at the specified index regardless of indexing strategy.
public Bar GetValue(int index)
Parameters
index
int
Returns
Insert(Bar)
Inserts the specified bar into the collection.
public void Insert(Bar bar)
Parameters
bar
Bar
Load(Stream, PersistenceFormat)
Loads a BarSeries from a stream using the specified format.
public static BarSeries Load(Stream stream, PersistenceFormat format)
Parameters
stream
Streamformat
PersistenceFormat
Returns
Load(string)
Loads a BarSeries from a file. Format is determined by the file extension.
public static BarSeries Load(string fileName)
Parameters
fileName
string
Returns
Load(string, PersistenceFormat)
Loads a BarSeries from a file using the specified format.
public static BarSeries Load(string fileName, PersistenceFormat format)
Parameters
fileName
stringformat
PersistenceFormat
Returns
Merge(BarSeries, bool)
Merges the specified BarSeries into the existing one.
public void Merge(BarSeries series, bool overwrite = false)
Parameters
Multiply(BarSeries, double, bool)
Multiplies a BarSeries by the specified value.
public static BarSeries Multiply(BarSeries series, double value, bool modifyUnadjustedClose = false)
Parameters
Returns
OnAdd(DateTime, Bar)
Adds the specified bar to the end of the collection. If the date of the bar does not equal the specified date, a new bar is created with the specified date.
protected override void OnAdd(DateTime date, Bar value)
Parameters
OnAdjust(MathematicalOperation, double, int, bool)
Adjusts the entire barseries using the specified mathematical operation.
protected virtual void OnAdjust(MathematicalOperation operation, double value, int digitsToRound, bool modifyUnadjustedClose)
Parameters
operation
MathematicalOperationvalue
doubledigitsToRound
intmodifyUnadjustedClose
bool
OnClear()
Clears the BarSeries.
protected override void OnClear()
OnCompress(ICompressionProvider)
Calls the specified compression provider and converts to a BarSeries as necessary.
protected virtual BarSeries OnCompress(ICompressionProvider compressionProvider)
Parameters
compressionProvider
ICompressionProvider
Returns
OnDatesChanged()
Recreates internal list of bars with the updated dates and nulls out TimeSeries properties forcing them to be recreated on the next access.
protected override void OnDatesChanged()
OnInsert(int, DateTime, Bar)
Inserts the specified item into the collection.
protected override void OnInsert(int index, DateTime date, Bar item)
Parameters
OnRemove(int)
Removes a bar at the specified index.
protected override void OnRemove(int index)
Parameters
index
int
OnSetIndex(int)
Ensures that the OHLCVIU timeseries stay in sync with the parent.
protected override void OnSetIndex(int index)
Parameters
index
int
OnSetValue(int, Bar)
Sets the specified bar at the specified index.
protected override void OnSetValue(int index, Bar value)
Parameters
OnTrim()
Trims the BarSeries
protected override void OnTrim()
ParseDouble(string)
Converts string to double with support for N/A values.
protected static double ParseDouble(string text)
Parameters
text
string
Returns
RaiseNegative(BarSeries)
Returns a new BarSeries where prices are adjusted so that the lowest low is one tick above zero. The unadjusted close is not modified.
public static BarSeries RaiseNegative(BarSeries series)
Parameters
series
BarSeries
Returns
RaiseNegative(BarSeries, double, bool)
Returns a new BarSeries where if the lowest low is negative, the whole series is adjusted so that the low of the series is equal to the specified minimum.
public static BarSeries RaiseNegative(BarSeries series, double minimum, bool modifyUnadjustedClose)
Parameters
Returns
Save(Stream, PersistenceFormat)
Saves the BarSeries to the specified stream in the specified format.
public void Save(Stream stream, PersistenceFormat format)
Parameters
stream
Streamformat
PersistenceFormat
Save(string, PersistenceFormat)
Saves the barseries using the specified format.
public void Save(string path, PersistenceFormat format)
Parameters
path
stringPath can be a filename or directory. Use a trailing backslash to specify a directory; it will then use the symbol + default extension as the filename.
format
PersistenceFormat
Save(string, PersistenceFormat, bool)
Save a BarSeries using the specified parameters.
public void Save(string fileName, PersistenceFormat format, bool compress)
Parameters
fileName
stringformat
PersistenceFormatcompress
bool
Subset(DateTime)
Returns a new BarSeries starting at the specified start date.
public BarSeries Subset(DateTime startDate)
Parameters
startDate
DateTime
Returns
Subset(DateTime, DateTime)
Returns a new BarSeries containing only the range of dates specified by the startDate and endDate parameters.
public BarSeries Subset(DateTime startDate, DateTime endDate)
Parameters
Returns
Subset(int, int)
Returns a subset of this BarSeries starting at the specified index with the specified count.
public BarSeries Subset(int index, int count)
Parameters
index
intThe starting index.
count
intThe number of observations to return. Automatically adjusted to prevent out-of-bounds errors.
Returns
Subtract(BarSeries, double, bool)
Subtracts the specfied value from a BarSeries.
public static BarSeries Subtract(BarSeries series, double value, bool modifyUnadjustedClose = false)
Parameters
Returns
Subtract(double, BarSeries, bool)
Subtracts a BarSeries from a value returning a new BarSeries.
public static BarSeries Subtract(double value, BarSeries series, bool modifyUnadjustedClose = false)
Parameters
Returns
Sync(ISeries)
Returns a new BarSeries synced to an outside series.
public BarSeries Sync(ISeries series)
Parameters
series
ISeries
Returns
Sync(ISeries, SyncOption)
Returns a new BarSeries synced to an outside series using the specified sync option to handle missing values.
public BarSeries Sync(ISeries series, SyncOption option)
Parameters
series
ISeriesoption
SyncOption
Returns
Sync(IEnumerable<DateTime>)
Returns a new series synced to the specified enumerable of dates.
public BarSeries Sync(IEnumerable<DateTime> dates)
Parameters
dates
IEnumerable<DateTime>
Returns
Sync(IEnumerable<DateTime>, SyncOption)
Returns a new series synced to the specified enumerable of dates using the specified sync option to handle missing values.
public BarSeries Sync(IEnumerable<DateTime> dates, SyncOption option)
Parameters
dates
IEnumerable<DateTime>option
SyncOption
Returns
SyncCore(IEnumerable<DateTime>, SyncOption)
Core synchronization code.
protected override SeriesBase<Bar> SyncCore(IEnumerable<DateTime> dates, SyncOption option)
Parameters
dates
IEnumerable<DateTime>option
SyncOption
Returns
ToDaily(TimeSpan)
Compresses an intraday series to daily using the specified session end time.
public BarSeries ToDaily(TimeSpan sessionEndTime)
Parameters
sessionEndTime
TimeSpan
Returns
ToIntraday(int)
Compresses an intraday series to the specified number of minutes.
public BarSeries ToIntraday(int minutes)
Parameters
minutes
int
Returns
ToMonthly()
Compresses to monthly using the default options.
public BarSeries ToMonthly()
Returns
ToWeekly()
Compresses to weekly using the default options.
public BarSeries ToWeekly()
Returns
Operators
operator +(BarSeries, double)
Adds the specified value to the BarSeries, leaving volume and open interest unchanged.
public static BarSeries operator +(BarSeries left, double right)
Parameters
Returns
operator +(double, BarSeries)
Adds the specified value to the BarSeries, leaving volume and open interest unchanged.
public static BarSeries operator +(double left, BarSeries right)
Parameters
Returns
operator /(BarSeries, TimeSeries)
Divides the BarSeries by the specified TimeSeries.
public static BarSeries operator /(BarSeries left, TimeSeries right)
Parameters
left
BarSeriesright
TimeSeries
Returns
operator /(BarSeries, double)
Divides the BarSeries by the specified value, leaving volume and open interest unchanged.
public static BarSeries operator /(BarSeries left, double right)
Parameters
Returns
operator /(double, BarSeries)
Divides the BarSeries by the specified value, leaving volume and open interest unchanged.
public static BarSeries operator /(double left, BarSeries right)
Parameters
Returns
operator *(BarSeries, TimeSeries)
Multiplies the BarSeries by the specified TimeSeries, leaving volume, open interest and unadjusted close unchanged.
public static BarSeries operator *(BarSeries left, TimeSeries right)
Parameters
left
BarSeriesright
TimeSeries
Returns
operator *(BarSeries, double)
Multiplies the BarSeries by the specified value, leaving volume and open interest unchanged.
public static BarSeries operator *(BarSeries left, double right)
Parameters
Returns
operator *(double, BarSeries)
Multiplies the BarSeries by the specified value, leaving volume and open interest unchanged.
public static BarSeries operator *(double left, BarSeries right)
Parameters
Returns
operator -(BarSeries, double)
Subtracts the specified value from the BarSeries, leaving volume and open interest unchanged.
public static BarSeries operator -(BarSeries left, double right)
Parameters
Returns
operator -(double, BarSeries)
Subtracts a BarSeries from a value, leaving volume and open interest unchanged.
public static BarSeries operator -(double left, BarSeries right)