jeudi 17 mai 2018

Design pattern for calculating dynamic data

I am writing a project that calculates a variety of stock price indicators, such as Simple/Exponential Moving Averages etc. The data I am working with is real-time, and most of these indicators are iterative and use previous values to determine the current value.

Take the Exponential Moving Average, for instance. If I were to calculate an EMA based on 10 periods (EMA(10)):

Initial: SMA(10) = [Sum of previous 10 periods]/10
Multiplier: ( 2 / ( # Periods + 1 ) = ( 2 / 11 ) = 0.1818
EMA: ( Current Value - EMA(Previous) ) * Multiplier + EMA(Previous)

When the value for the current time period changes, the EMA must be recalculated with the new value. If the entire span of values is recalculated every single time, there will be a noticeable performance hit, so caching the past values and only recalculating the current value is important.

I am wondering if there is a certain design pattern that allows for this type of functionality. Ideally, I would like to have each indicator inherit from a base pattern class to keep everything uniform.

Aucun commentaire:

Enregistrer un commentaire