Series transformer snippet

jarym
Posts: 58
Joined: 16 Feb 2015
Has thanked: 14 times
Been thanked: 6 times

Series transformer snippet

Postby jarym » 18 Jul 2020

Coders, ever find yourself needing to quickly apply a transformation to an ISeries Object, well I did. I created a quick transformer class.

Code: Select all

public class ISeriesTransformer<TSourceType, TDestinationType> : ISeries<TDestinationType> { private ISeries<TSourceType> _source; private Converter<TSourceType, TDestinationType> _converter; public ISeriesTransformer(ISeries<TSourceType> source, Converter<TSourceType, TDestinationType> converter) { _source = source; _converter = converter; } public TDestinationType this[int barsAgo] { get { return _converter(_source[barsAgo]); } } public TDestinationType Value { get { return _converter(_source.Value); } } }
and an extension method:

Code: Select all

public static ISeries<TDestinationType> Transform<TSourceType, TDestinationType>(this ISeries<TSourceType> series, Converter<TSourceType, TDestinationType> converter) { new ISeriesTransformer<TSourceType,TDestinationType>(series, converter); }
so now I can do something like:

Code: Select all

ISeries<double> closePriceMinus10 = Bars.Close.Transform(closePrice => closePrice - 10);
Ok, contrived example but its allowed me to chain together ISeries transformations and get a more functional code-flow going. Since it works with any ISeries you can use it with series functions too.

Hope this helps others.

Emmanuel
Posts: 355
Joined: 21 May 2009
Has thanked: 109 times
Been thanked: 28 times

Re: Series transformer snippet

Postby Emmanuel » 24 Jul 2020

Absolutely, this can be useful !

Thank you very much

User avatar
dataheck
Posts: 30
Joined: 19 Nov 2019
Location: Amherst, Nova Scotia
Been thanked: 5 times
Contact:

Re: Series transformer snippet

Postby dataheck » 31 Jul 2020

Thank you for sharing! This is pretty neat, and would have saved me from making a few trivial functions.


Return to “User Contributed Studies”