public void VolumeCandlePart() { List <SmaResult> results = quotes.GetSma(20, CandlePart.Volume) .ToList(); // assertions // proper quantities // should always be the same number of results as there is quotes Assert.AreEqual(502, results.Count); Assert.AreEqual(483, results.Where(x => x.Sma != null).Count()); // sample values SmaResult r24 = results[24]; Assert.AreEqual(77293768.2m, r24.Sma); SmaResult r290 = results[290]; Assert.AreEqual(157958070.8m, r290.Sma); SmaResult r501 = results[501]; Assert.AreEqual(DateTime.ParseExact("12/31/2018", "MM/dd/yyyy", EnglishCulture), r501.Date); Assert.AreEqual(163695200m, r501.Sma); }
public static void Main() { // fetch historical quotes from data provider IEnumerable <Quote> quotes = GetHistoryFromFeed(); // calculate 10-period SMA IEnumerable <SmaResult> results = quotes.GetSma(10); // show results Console.WriteLine("SMA Results ---------------------------"); foreach (SmaResult r in results.TakeLast(10)) // only showing last 10 records for brevity { Console.WriteLine($"SMA on {r.Date:u} was ${r.Sma:N3}"); } // optionally, you can lookup individual values by date DateTime lookupDate = DateTime .Parse("2021-08-12T17:08:17.9746795+02:00", CultureInfo.InvariantCulture); decimal?specificSma = results.Find(lookupDate).Sma; Console.WriteLine(); Console.WriteLine("SMA on Specific Date ------------------"); Console.WriteLine($"SMA on {lookupDate:u} was ${specificSma:N3}"); // analyze results (compare to quote values) Console.WriteLine(); Console.WriteLine("SMA Analysis --------------------------"); /************************************************************ * Results are usually returned with the same number of * elements as the provided quotes; see individual indicator * docs for more information. * * As such, converting to List means they can be indexed * with the same ordinal position. ************************************************************/ List <Quote> quotesList = quotes .ToList(); List <SmaResult> resultsList = results .ToList(); for (int i = quotesList.Count - 25; i < quotesList.Count; i++) { // only showing ~25 records for brevity Quote q = quotesList[i]; SmaResult r = resultsList[i]; bool isBullish = q.Close > r.Sma; Console.WriteLine($"SMA on {r.Date:u} was ${r.Sma:N3}" + $" and Bullishness is {isBullish}"); } }
public void GetSmaTest() { int lookbackPeriod = 20; IEnumerable <SmaResult> results = Indicator.GetSma(history, lookbackPeriod); // assertions // proper quantities // should always be the same number of results as there is history Assert.AreEqual(502, results.Count()); Assert.AreEqual(502 - lookbackPeriod + 1, results.Where(x => x.Sma != null).Count()); // sample value SmaResult sma = results.Where(x => x.Date == DateTime.Parse("12/31/2018")).FirstOrDefault(); Assert.AreEqual((decimal)251.86, sma.Sma); }
public void Standard() { int lookbackPeriod = 20; List <SmaResult> results = Indicator.GetSma(history, lookbackPeriod) .ToList(); // assertions // proper quantities // should always be the same number of results as there is history Assert.AreEqual(502, results.Count); Assert.AreEqual(502 - lookbackPeriod + 1, results.Where(x => x.Sma != null).Count()); // sample value SmaResult r = results[501]; Assert.AreEqual(251.86m, r.Sma); }
public void GetSmaTest() { int lookbackPeriod = 20; IEnumerable <SmaResult> results = Indicator.GetSma(history, lookbackPeriod, true); // assertions // proper quantities // should always be the same number of results as there is history Assert.AreEqual(502, results.Count()); Assert.AreEqual(502 - lookbackPeriod + 1, results.Where(x => x.Sma != null).Count()); // sample value SmaResult r = results.Where(x => x.Index == 502).FirstOrDefault(); Assert.AreEqual(251.86m, r.Sma); Assert.AreEqual(9.45m, r.Mad); Assert.AreEqual(119.2510m, Math.Round((decimal)r.Mse, 4)); Assert.AreEqual(0.037637m, Math.Round((decimal)r.Mape, 6)); }
public void GetSmaTest() { int lookbackPeriod = 20; IEnumerable <SmaResult> results = Indicator.GetSma(history, lookbackPeriod); // assertions // proper quantities // should always be the same number of results as there is history Assert.AreEqual(502, results.Count()); Assert.AreEqual(502 - lookbackPeriod + 1, results.Where(x => x.Sma != null).Count()); // sample value SmaResult r = results.Where(x => x.Date == DateTime.ParseExact("12/31/2018", "MM/dd/yyyy", null)).FirstOrDefault(); Assert.AreEqual((decimal)251.86, r.Sma); Assert.AreEqual((decimal)9.45, r.Mad); Assert.AreEqual((double)119.2510, Math.Round((double)r.Mse, 4)); Assert.AreEqual((double)0.037637, Math.Round((double)r.Mape, 6)); }
// DETRENDED PRICE OSCILLATOR (DPO) /// <include file='./info.xml' path='indicator/*' /> /// public static IEnumerable <DpoResult> GetDpo <TQuote>( this IEnumerable <TQuote> quotes, int lookbackPeriods) where TQuote : IQuote { // sort quotes List <TQuote> quotesList = quotes.SortToList(); // check parameter arguments ValidateDpo(lookbackPeriods); // initialize int length = quotesList.Count; int offset = (lookbackPeriods / 2) + 1; List <SmaResult> sma = quotes.GetSma(lookbackPeriods).ToList(); List <DpoResult> results = new(length); // roll through quotes for (int i = 0; i < length; i++) { TQuote q = quotesList[i]; DpoResult r = new() { Date = q.Date }; results.Add(r); if (i >= lookbackPeriods - offset - 1 && i < length - offset) { SmaResult s = sma[i + offset]; r.Sma = s.Sma; r.Dpo = q.Close - s.Sma; } } return(results); }