CBOE Emulator  1.0
rate_of_change.hpp
1 // A structure for calculating the Rate of Change (ROC) over a rolling window.
2 // Copyright 2020 Christian Kauten
3 //
4 // Author: Christian Kauten (kautenja@auburn.edu)
5 //
6 // This program is free software: you can redistribute it and/or modify
7 // it under the terms of the GNU General Public License as published by
8 // the Free Software Foundation, either version 3 of the License, or
9 // (at your option) any later version.
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License
15 // along with this program. If not, see <http://www.gnu.org/licenses/>.
16 //
17 
18 #ifndef MATHS_RATE_OF_CHANGE
19 #define MATHS_RATE_OF_CHANGE
20 
21 #include <algorithm>
22 #include <atomic>
23 #include <limits>
24 #include <vector>
25 
27 namespace Maths {
28 
48 template<typename Observation = double, typename RoC = double>
49 struct RateOfChange {
50  private:
52  std::vector<Observation> history;
54  std::size_t index = 0;
56  RoC rate_of_change = 0;
57 
58  public:
60  static constexpr std::size_t LENGTH_MIN = 2;
62  static constexpr std::size_t LENGTH_MAX = std::numeric_limits<uint16_t>::max() - 1;
63 
68  explicit RateOfChange(std::size_t length) {
69  if (length < LENGTH_MIN)
70  throw "length must be >= " + std::to_string(LENGTH_MIN);
71  if (length > LENGTH_MAX)
72  throw "length must be <= " + std::to_string(LENGTH_MAX);
73  history.resize(length, 0);
74  }
75 
80  std::size_t get_length() const { return history.size(); }
81 
86  inline RoC get_rate_of_change() const { return rate_of_change; }
87 
89  inline void reset() {
90  std::fill(history.begin(), history.end(), 0);
91  index = 0;
92  rate_of_change = 0;
93  }
94 
99  inline void process(Observation observation) {
100  // add the current observation to the history
101  history[index] = observation;
102  auto next_index = (index + 1) % history.size();
103  // get the price from the beginning of the history
104  auto value = history[next_index];
105  index = next_index;
106  // return if still learning value information (prevent divide by 0)
107  if (value == 0) return;
108  // set the rate of change
109  rate_of_change = (observation - value) / value;
110  }
111 };
112 
113 } // namespace Maths
114 
115 #endif // MATHS_RATE_OF_CHANGE
Maths::RateOfChange::LENGTH_MAX
static constexpr std::size_t LENGTH_MAX
the inclusive maximal size for the length of the history
Definition: rate_of_change.hpp:62
Maths::RateOfChange::reset
void reset()
Reset the history to its initial state.
Definition: rate_of_change.hpp:89
Maths::RateOfChange
A structure for calculating the Rate of Change (ROC) over a rolling window.
Definition: rate_of_change.hpp:49
Maths::RateOfChange::LENGTH_MIN
static constexpr std::size_t LENGTH_MIN
the inclusive minimal size for the length of the history
Definition: rate_of_change.hpp:60
Maths::RateOfChange::get_length
std::size_t get_length() const
Return the length of the rate of change history.
Definition: rate_of_change.hpp:80
Maths
Functions for doing maths.
Definition: exponential_moving_average.hpp:25
Maths::RateOfChange::process
void process(Observation observation)
Calculate the next RoC based on observation .
Definition: rate_of_change.hpp:99
Maths::RateOfChange::RateOfChange
RateOfChange(std::size_t length)
Initialize a new RoC with given history length .
Definition: rate_of_change.hpp:68
Maths::RateOfChange::get_rate_of_change
RoC get_rate_of_change() const
Return the current rate of change (RoC).
Definition: rate_of_change.hpp:86