mathnet-numerics icon indicating copy to clipboard operation
mathnet-numerics copied to clipboard

Added financial technical indicator atr

Open feisel opened this issue 10 years ago • 4 comments

i think it should be great if mathnet has more financial Methods so i added financial technical indicator Average True Range (ATR).

It would be great if you merges this in mathnet.

Greatings Felix

feisel avatar Jun 19 '15 10:06 feisel

Thanks!

It seems some of the tests actually fail on the build server. Could you have a look?

1) Test Error : MathNet.Numerics.UnitTests.FinancialTests.IndicatorsTests.Atr_ShouldRaiseException_IfPeriodIsGreaterThanBarCount
   System.FormatException : String was not recognized as a valid DateTime.
   at System.DateTimeParse.Parse(String s, DateTimeFormatInfo dtfi, DateTimeStyles styles)
   at MathNet.Numerics.UnitTests.FinancialTests.StockDataReader.ReadFile(String filePath) in c:\projects\mathnet-numerics\src\UnitTests\FinancialTests\StockDataReader.cs:line 45
   at MathNet.Numerics.UnitTests.FinancialTests.IndicatorsTests.GenerateValidBars() in c:\projects\mathnet-numerics\src\UnitTests\FinancialTests\IndicatorsTests.cs:line 86
   at MathNet.Numerics.UnitTests.FinancialTests.IndicatorsTests.Atr_ShouldRaiseException_IfPeriodIsGreaterThanBarCount() in c:\projects\mathnet-numerics\src\UnitTests\FinancialTests\IndicatorsTests.cs:line 75

2) Test Error : MathNet.Numerics.UnitTests.FinancialTests.IndicatorsTests.Atr_ShouldRaiseException_IfPeriodIsZero
   System.FormatException : String was not recognized as a valid DateTime.
   at System.DateTimeParse.Parse(String s, DateTimeFormatInfo dtfi, DateTimeStyles styles)
   at MathNet.Numerics.UnitTests.FinancialTests.StockDataReader.ReadFile(String filePath) in c:\projects\mathnet-numerics\src\UnitTests\FinancialTests\StockDataReader.cs:line 45
   at MathNet.Numerics.UnitTests.FinancialTests.IndicatorsTests.GenerateValidBars() in c:\projects\mathnet-numerics\src\UnitTests\FinancialTests\IndicatorsTests.cs:line 86
   at MathNet.Numerics.UnitTests.FinancialTests.IndicatorsTests.Atr_ShouldRaiseException_IfPeriodIsZero() in c:\projects\mathnet-numerics\src\UnitTests\FinancialTests\IndicatorsTests.cs:line 66

3) Test Error : MathNet.Numerics.UnitTests.FinancialTests.IndicatorsTests.Atr_ShouldReturnValue
   System.FormatException : String was not recognized as a valid DateTime.
   at System.DateTimeParse.Parse(String s, DateTimeFormatInfo dtfi, DateTimeStyles styles)
   at MathNet.Numerics.UnitTests.FinancialTests.StockDataReader.ReadFile(String filePath) in c:\projects\mathnet-numerics\src\UnitTests\FinancialTests\StockDataReader.cs:line 45
   at MathNet.Numerics.UnitTests.FinancialTests.IndicatorsTests.GenerateValidBars() in c:\projects\mathnet-numerics\src\UnitTests\FinancialTests\IndicatorsTests.cs:line 86
   at MathNet.Numerics.UnitTests.FinancialTests.IndicatorsTests.Atr_ShouldReturnValue() in c:\projects\mathnet-numerics\src\UnitTests\FinancialTests\IndicatorsTests.cs:line 55

4) Test Error : MathNet.Numerics.UnitTests.FinancialTests.IndicatorsTests.Atr_ShouldReturnValuesWithoutException
   System.FormatException : String was not recognized as a valid DateTime.
   at System.DateTimeParse.Parse(String s, DateTimeFormatInfo dtfi, DateTimeStyles styles)
   at MathNet.Numerics.UnitTests.FinancialTests.StockDataReader.ReadFile(String filePath) in c:\projects\mathnet-numerics\src\UnitTests\FinancialTests\StockDataReader.cs:line 45
   at MathNet.Numerics.UnitTests.FinancialTests.IndicatorsTests.GenerateValidBars() in c:\projects\mathnet-numerics\src\UnitTests\FinancialTests\IndicatorsTests.cs:line 86
   at MathNet.Numerics.UnitTests.FinancialTests.IndicatorsTests.Atr_ShouldReturnValuesWithoutException() in c:\projects\mathnet-numerics\src\UnitTests\FinancialTests\IndicatorsTests.cs:line 48

cdrnet avatar Jun 22 '15 16:06 cdrnet

Seems like the dates in the test file are in a culture-local format (German?), without explicitly specifying de-DE these tests will fail when run in other cultures.

cdrnet avatar Jun 22 '15 16:06 cdrnet

Hi fixed the errors.

feisel avatar Jun 23 '15 10:06 feisel

Thanks for the fix. I've pulled your changes into the atr-indicator branch of this repo.

I have a suggestion for an online implementation which could be useful. But not working in finance myself I might as well be missing the point (e.g. why the original implementation was rounding to 10 digits in intermediate results):

https://github.com/mathnet/mathnet-numerics/blob/atr-indicator/src/Numerics/Financial/Indicators.cs

Would this implementation work for you and be useful? Why do we round here at all, instead of later at the point of reporting? How important is it to use acronyms like ATR instead of, say, AverageTrueRange?

cdrnet avatar Jul 11 '15 17:07 cdrnet