moving_average v0.1.0
📈 Moving Average 📉
A Crystal library for calculating different moving averages.
What is a Moving Average?
A moving average is a calculation used for analysing data to determine trends and momentum. Commonly used as an indicator in the financial industry to track price changes of a security.
Installation
Add this to your application's shard.yml
:
dependencies:
moving_average:
github: ChadBowman/moving_average
In your terminal, install Crystal dependencies with:
$ shards install
or
$ crystal deps
Usage
Start by requring the MovingAverage
module.
require "moving_average"
MovingAverage
comes included in Array
, so we can compute the simple moving average (SMA) by simply:
a = [1, 2, 3, 4, 5, 6]
a.simple_moving_average(5) # => [3, 4]
or
a.sma(5) # => [3, 4]
Calculating the exponential moving average (EMA) is very similar:
a = [1.0, 2.0, 3.0, 4.0, 5.0, 6.0]
ema = a.ema(5) # => [3.0, 4.0]
Now say you're maintaining a real-time EMA chart and you want to update your series for a new price:
ema.ema!(5, 9.0) # => [3.0, 4.0, 5.666666666666667]
A smoothed moving average (SMMA) may be computed in the same way.
Additionally you can include the module in your custom class given that it and the included type respond to the correct methods:
require "moving_average"
class MyCustomClass(T)
include MovingAverage(T)
end
MyCustomClass.new.sma(5) # => new instance of MyCustomClass evaluated for SMA
Contributing
- Fork it ( https://github.com/ChadBowman/moving_average/fork )
- Create your feature branch (git checkout -b my-new-feature)
- Commit your changes (git commit -am 'Add some feature')
- Push to the branch (git push origin my-new-feature)
- Create a new Pull Request
Contributors
- ChadBowman Chad Bowman - creator, maintainer
Inspired by Brad Cater's moving_averages gem
moving_average
- 1
- 1
- 0
- 0
- 0
- almost 7 years ago
- December 20, 2017
MIT License
Sun, 24 Nov 2024 22:27:08 GMT