DaveSkender / Stock.Indicators

Stock Indicators for .NET is a C# NuGet package that transforms raw equity, commodity, forex, or cryptocurrency financial market price quotes into technical indicators and trading insights. You'll need this essential data in the investment tools that you're building for algorithmic trading, technical analysis, machine learning, or visual charting.
https://dotnet.StockIndicators.dev
Apache License 2.0
980 stars 245 forks source link

MACD with insufficient quote history is wrong #22

Closed GelLiNN closed 4 years ago

GelLiNN commented 4 years ago

I'm getting my historical data from yahoo finance, I have ensured everything is present in the Quote class, and then I pass the data into

int fastPeriod = 12; int slowPeriod = 26; int signalPeriod = 9; IEnumerable<MacdResult> results = Indicator.GetMacd(history, fastPeriod, slowPeriod, signalPeriod); But it gives me an exception on this line, every time saying "No historical quote provided".

I also have a stack trace for you if that's helpful:

at Skender.Stock.Indicators.Cleaners.PrepareHistory(IEnumerable1 history)\r\n at Skender.Stock.Indicators.Indicator.GetEma(IEnumerable1 history, Int32 lookbackPeriod)\r\n at Skender.Stock.Indicators.Indicator.GetMacd(IEnumerable1 history, Int32 fastPeriod, Int32 slowPeriod, Int32 signalPeriod)\r\n at PT.Middleware.Indicators.GetIndicatorComposite(String symbol, String function, IEnumerable1 history, Int32 daysToCalculate) in C:\\Users\\linki\\Dev\\Pro-Trades\\Middleware\\Indicators.cs:line 55

I tried preparing it with the Cleaners class but it is still giving me the same exception. Please tell me what the fix is or otherwise fix this bug?

Thanks a bunch!

DaveSkender commented 4 years ago

Unit test shows it works, so the exception is catching something with your data, which is intended behavior, to help you troubleshoot.

if (history == null || !history.Any())
{
  throw new BadHistoryException("No historical quote provided.");
}
GelLiNN commented 4 years ago

I figured out what was going wrong. I was providing only 7 days of historical data, when the MACD indicator needs much more than that to compute effectively. I expanded it out to the last 100 days and it seems to be computing correctly. Thank you!

DaveSkender commented 4 years ago

Ah, yes. You'd have to provide at least 2×slowPeriod amount of data to get any results. And since MACD uses a smoothing technique, we recommend at least 200-250 extra data points for maximum precision. I'm taking a note to improve documentation and to perhaps add some additional indicator specific exceptions.

github-actions[bot] commented 3 years ago

This Issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new Issue for related bugs.