Series style indicators for batch processing
Series style calculates an indicator over a full collection of bars in one call. It is the simplest and fastest style, and the one to start with.
When to use batch style
Use Series style when you have the bars you need up front, as in:
- Backtesting and historical research
- Screening many symbols
- Charts and reports
When bars keep arriving, don't re-run a Series method for every new bar; its cost grows with the history it recalculates. Use Buffer lists or Stream hubs instead, which add each bar at a flat cost. See Bars that arrive one at a time for a comparison.
Basic usage
Call an indicator method on your bars to get a result for every bar:
using FacioQuo.Stock.Indicators;
// your own method that gets historical bars from your data source
IReadOnlyList<Bar> bars = GetBarsFromFeed("MSFT");
// calculate a 20-period SMA
IReadOnlyList<SmaResult> results = bars.ToSma(20);
foreach (SmaResult r in results)
{
Console.WriteLine($"SMA on {r.Timestamp:d} was {r.Sma:N4}");
}The results hold one result per bar, in the same order as the bars. Values are null until the indicator has enough bars to warm up; each indicator's page lists its warmup period. For how much history to provide, see How much historical bar data do I need?
Bar order
Series methods expect bars in time order, oldest first, with no duplicate timestamps. They don't sort or check your bars.
🚩 Silent errors from unsorted bars
Bars that are out of order, or that repeat a timestamp, produce incorrect results without any error. If your data source doesn't guarantee the order, sort the bars first with ToSortedList(). To check for duplicates and order problems, use Validate().
Chaining indicators
Call one indicator method on another's results to calculate an indicator from that indicator. For the broader concept, see Chaining indicators.
// RSI of On-Balance Volume
IReadOnlyList<RsiResult> rsiOfObv = bars
.ToObv()
.ToRsi(14);
// EMA of the (High + Low) / 2 price instead of Close
IReadOnlyList<EmaResult> emaOfHl2 = bars
.Use(CandlePart.HL2)
.ToEma(20);Working with results
Result utilities help with common follow-up steps:
IReadOnlyList<AdxResult> results = bars.ToAdx(14);
// find the result for a timestamp
AdxResult? result = results.Find(new DateTime(2024, 1, 15));
// remove the warmup period's results
IReadOnlyList<AdxResult> warmedUp = results.RemoveWarmupPeriods();
// keep only the bars where a candlestick pattern matched
IReadOnlyList<CandleResult> signals = bars
.ToMarubozu()
.Condense();Results are lists of immutable records, so you can use LINQ on them and share them across threads. A Series method doesn't change the bars you pass, so you can also calculate several indicators from the same bars on different threads at once.
See also
- Getting started for installation and supplying bar data
- Result utilities for finding, trimming, and condensing results
- Buffer lists and Stream hubs for bars that arrive one at a time
- Indicators for available indicators