diff --git a/src/Numerics/Financial/Bar.cs b/src/Numerics/Financial/Bar.cs index 6b7f3312..43b075a7 100644 --- a/src/Numerics/Financial/Bar.cs +++ b/src/Numerics/Financial/Bar.cs @@ -1,19 +1,44 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; +// +// Math.NET Numerics, part of the Math.NET Project +// http://numerics.mathdotnet.com +// http://github.com/mathnet/mathnet-numerics +// http://mathnetnumerics.codeplex.com +// +// Copyright (c) 2009-2015 Math.NET +// +// Permission is hereby granted, free of charge, to any person +// obtaining a copy of this software and associated documentation +// files (the "Software"), to deal in the Software without +// restriction, including without limitation the rights to use, +// copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the +// Software is furnished to do so, subject to the following +// conditions: +// +// The above copyright notice and this permission notice shall be +// included in all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES +// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT +// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, +// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR +// OTHER DEALINGS IN THE SOFTWARE. +// namespace MathNet.Numerics.Financial { /// - /// Repressentation of a stock bar + /// Representation of a stock bar /// public struct Bar { - private readonly double _high; - private readonly double _low; - private readonly double _open; - private readonly double _close; + readonly double _high; + readonly double _low; + readonly double _open; + readonly double _close; /// /// High of the bar @@ -46,14 +71,12 @@ namespace MathNet.Numerics.Financial { get { return _close; } } - /// - /// ctor - /// + /// High of the bar /// Low of the bar /// Open of the bar /// Close of the bar - public Bar(double high,double low,double open,double close) + public Bar(double high, double low, double open, double close) { _high = high; _low = low; diff --git a/src/Numerics/Financial/Indicators.cs b/src/Numerics/Financial/Indicators.cs index 2635459e..093ce3d8 100644 --- a/src/Numerics/Financial/Indicators.cs +++ b/src/Numerics/Financial/Indicators.cs @@ -27,10 +27,10 @@ // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR // OTHER DEALINGS IN THE SOFTWARE. // + using System; using System.Collections.Generic; using System.Linq; -using System.Text; using MathNet.Numerics.Statistics; namespace MathNet.Numerics.Financial @@ -41,29 +41,27 @@ namespace MathNet.Numerics.Financial public static class Indicators { /// - /// Evaluates the sample mean over a moving window, for each samples. - /// Returns NaN if no data is empty or if any entry is NaN. + /// Calculate the Simple Moving Average (SMA). /// - /// The sample stream to calculate the mean of. - /// The number of last samples to consider. - /// The SMA for samples + /// Input samples + /// Period of calculation public static IEnumerable SMA(this IEnumerable samples, int period) { return samples.MovingAverage(period); } + /// - /// Calculate the Average True Range (ATR) + /// Calculate the Average True Range (ATR). /// /// Input samples /// Period of calculation - /// public static IEnumerable ATR(this IEnumerable samples, int period) { - if (period<=0) - throw new ArgumentException("period should be greater than 0","period"); - if(samples==null) + if (period <= 0) + throw new ArgumentException("period should be greater than 0", "period"); + if (samples == null) throw new ArgumentNullException("samples", "samples should not be null"); - if (period > (samples.Count() +1)) + if (period > (samples.Count())) throw new ArgumentException("samples", "samples should be greater than period"); var trList = new List(); @@ -77,11 +75,11 @@ namespace MathNet.Numerics.Financial { var currentBar = enumerator.Current; - var hl = Math.Round(currentBar.High - currentBar.Low,10); + var hl = Math.Round(currentBar.High - currentBar.Low, 10); hl = Math.Abs(hl); - var pdch = Math.Round(lastBar.Close - currentBar.High,10); + var pdch = Math.Round(lastBar.Close - currentBar.High, 10); pdch = Math.Abs(pdch); - var pdcl = Math.Round(lastBar.Close - currentBar.Low,10); + var pdcl = Math.Round(lastBar.Close - currentBar.Low, 10); pdcl = Math.Abs(pdcl); double tr = Math.Max(hl, Math.Max(pdch, pdcl)); @@ -98,7 +96,7 @@ namespace MathNet.Numerics.Financial //remove first tr, this is not valid trList.RemoveAt(0); - while (trList.Count>=period) + while (trList.Count >= period) { var mean = trList.Take(period).Mean(); var meanRounded = Math.Round(mean, 2); @@ -108,6 +106,5 @@ namespace MathNet.Numerics.Financial return atrList; } - } } diff --git a/src/UnitTests/FinancialTests/IndicatorsTests.cs b/src/UnitTests/FinancialTests/IndicatorsTests.cs index eb10a02a..ce506e8c 100644 --- a/src/UnitTests/FinancialTests/IndicatorsTests.cs +++ b/src/UnitTests/FinancialTests/IndicatorsTests.cs @@ -27,12 +27,10 @@ // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR // OTHER DEALINGS IN THE SOFTWARE. // + using System; using System.Collections.Generic; -using System.Diagnostics; using System.Linq; -using System.Text; -using System.Threading.Tasks; using MathNet.Numerics.Financial; using NUnit.Framework; @@ -57,7 +55,7 @@ namespace MathNet.Numerics.UnitTests.FinancialTests var expectedValueAtBar20 = 132.33; var actual = atr.ElementAt(20); Assert.AreEqual(expectedValueAtBar20, actual); - + } [Test] @@ -79,12 +77,11 @@ namespace MathNet.Numerics.UnitTests.FinancialTests Assert.That(() => inputBars.ATR(period), Throws.Exception.TypeOf()); } - private IEnumerable GenerateValidBars() { StockDataReader reader = new StockDataReader(); var data = reader.ReadFile("./data/Finance/DaxHistoricalData.dat"); - + List inputBars = new List(); foreach (var stockData in data) @@ -95,5 +92,4 @@ namespace MathNet.Numerics.UnitTests.FinancialTests return inputBars; } } - } diff --git a/src/UnitTests/FinancialTests/StockDataReader.cs b/src/UnitTests/FinancialTests/StockDataReader.cs index 3a6b2e36..d57ccd3a 100644 --- a/src/UnitTests/FinancialTests/StockDataReader.cs +++ b/src/UnitTests/FinancialTests/StockDataReader.cs @@ -2,26 +2,26 @@ using System.Collections.Generic; using System.Globalization; using System.IO; -using System.Linq; -using System.Text; -using System.Threading.Tasks; namespace MathNet.Numerics.UnitTests.FinancialTests { /// - /// Class reads a file with stockdata + /// Class reads a file with stock data /// internal class StockDataReader { /// - /// Reads a file with stockquotes + /// Reads a file with stock quotes /// /// Path of the file - /// Stockdata + /// StockData public IEnumerable ReadFile(string filePath) { List resultList = new List(); + var dateFormat = new CultureInfo("de-DE", false).DateTimeFormat; + var numberFormat = CultureInfo.InvariantCulture.NumberFormat; + using (var reader = new StreamReader(filePath)) { var firstLine = reader.ReadLine(); @@ -40,13 +40,12 @@ namespace MathNet.Numerics.UnitTests.FinancialTests var stringValues = line.Split(';'); - //first Datetime: - DateTime date = DateTime.Parse(stringValues[0]); - double open = double.Parse(stringValues[1], CultureInfo.InvariantCulture); - double high = double.Parse(stringValues[2], CultureInfo.InvariantCulture); - double low = double.Parse(stringValues[3], CultureInfo.InvariantCulture); - double close = double.Parse(stringValues[4], CultureInfo.InvariantCulture); - int volume = int.Parse(stringValues[5], CultureInfo.InvariantCulture); + DateTime date = DateTime.Parse(stringValues[0], dateFormat); + double open = double.Parse(stringValues[1], numberFormat); + double high = double.Parse(stringValues[2], numberFormat); + double low = double.Parse(stringValues[3], numberFormat); + double close = double.Parse(stringValues[4], numberFormat); + int volume = int.Parse(stringValues[5], numberFormat); StockData stockData = new StockData(date, open, close, high, low, volume); resultList.Add(stockData); @@ -57,13 +56,10 @@ namespace MathNet.Numerics.UnitTests.FinancialTests } } /// - /// Entity class for holding stockdata + /// Entity class for holding stock data /// internal class StockData { - /// - /// Ctor - /// /// Date /// Open quote /// Open quote @@ -104,7 +100,4 @@ namespace MathNet.Numerics.UnitTests.FinancialTests /// public int Volume { get; private set; } } - - } -