// convert to basic double
    internal static List <BasicD> ConvertToBasic <TQuote>(
        this IEnumerable <TQuote> quotes, CandlePart element = CandlePart.Close)
        where TQuote : IQuote
    {
        // elements represents the targeted OHLCV parts, so use "O" to return <Open> as base data, etc.
        // convert to basic double precision format
        IEnumerable <BasicD> basicDouble = element switch
        {
            CandlePart.Open => quotes.Select(x => new BasicD {
                Date = x.Date, Value = (double)x.Open
            }),
            CandlePart.High => quotes.Select(x => new BasicD {
                Date = x.Date, Value = (double)x.High
            }),
            CandlePart.Low => quotes.Select(x => new BasicD {
                Date = x.Date, Value = (double)x.Low
            }),
            CandlePart.Close => quotes.Select(x => new BasicD {
                Date = x.Date, Value = (double)x.Close
            }),
            CandlePart.Volume => quotes.Select(x => new BasicD {
                Date = x.Date, Value = (double)x.Volume
            }),
            CandlePart.HL2 => quotes.Select(x => new BasicD {
                Date = x.Date, Value = (double)(x.High + x.Low) / 2
            }),
            _ => new List <BasicD>(),
        };

        return(basicDouble.OrderBy(x => x.Date).ToList());
    }
Exemple #2
0
    // EXPONENTIAL MOVING AVERAGE
    /// <include file='./info.xml' path='indicator/*' />
    ///
    public static IEnumerable <EmaResult> GetEma <TQuote>(
        this IEnumerable <TQuote> quotes,
        int lookbackPeriods,
        CandlePart candlePart = CandlePart.Close)
        where TQuote : IQuote
    {
        // convert quotes
        List <BasicD> bdList = quotes.ConvertToBasic(candlePart);

        // calculate
        return(bdList.CalcEma(lookbackPeriods));
    }
Exemple #3
0
    // SIMPLE MOVING AVERAGE
    /// <include file='./info.xml' path='indicator/type[@name="Main"]/*' />
    ///
    public static IEnumerable <SmaResult> GetSma <TQuote>(
        this IEnumerable <TQuote> quotes,
        int lookbackPeriods,
        CandlePart candlePart = CandlePart.Close)
        where TQuote : IQuote
    {
        // check parameter arguments
        ValidateSma(lookbackPeriods);

        // initialize
        List <BasicD> bdList = quotes.ConvertToBasic(candlePart);

        // calculate
        return(bdList.CalcSma(lookbackPeriods));
    }
Exemple #4
0
    // WEIGHTED MOVING AVERAGE
    /// <include file='./info.xml' path='indicator/*' />
    ///
    public static IEnumerable <WmaResult> GetWma <TQuote>(
        this IEnumerable <TQuote> quotes,
        int lookbackPeriods,
        CandlePart candlePart = CandlePart.Close)
        where TQuote : IQuote
    {
        // convert quotes
        List <BasicD> bdList = quotes.ConvertToBasic(candlePart);

        // check parameter arguments
        ValidateWma(lookbackPeriods);

        // initialize
        List <WmaResult> results = new(bdList.Count);
        double           divisor = (double)lookbackPeriods * (lookbackPeriods + 1) / 2d;

        // roll through quotes
        for (int i = 0; i < bdList.Count; i++)
        {
            BasicD q     = bdList[i];
            int    index = i + 1;

            WmaResult result = new()
            {
                Date = q.Date
            };

            if (index >= lookbackPeriods)
            {
                double wma = 0;
                for (int p = index - lookbackPeriods; p < index; p++)
                {
                    BasicD d = bdList[p];
                    wma += (double)d.Value * (lookbackPeriods - (index - p - 1)) / divisor;
                }

                result.Wma = (decimal)wma;
            }

            results.Add(result);
        }

        return(results);
    }