ITCHCPP 1.6.0
High-Performance NASDAQ TotalView-ITCH 5.0 Parser & Market-Data Platform
Loading...
Searching...
No Matches
vwap.hpp
Go to the documentation of this file.
1#pragma once
2
11
12#include <cstdint>
13#include <limits>
14
15#include "itch/price.hpp"
16
17namespace itch::analytics {
18
23class Vwap {
24 public:
29 auto add(StandardPrice price, std::uint64_t shares) -> void {
30 m_price_volume += price.to_double() * static_cast<double>(shares);
31 m_volume += shares;
32 }
33
37 [[nodiscard]] auto value() const -> double {
38 if (m_volume == 0) {
39 return std::numeric_limits<double>::quiet_NaN();
40 }
41 return m_price_volume / static_cast<double>(m_volume);
42 }
43
47 [[nodiscard]] auto volume() const noexcept -> std::uint64_t { return m_volume; }
48
50 auto reset() noexcept -> void {
51 m_price_volume = 0.0;
52 m_volume = 0;
53 }
54
55 private:
56 double m_price_volume {0.0};
57 std::uint64_t m_volume {0};
58};
59
65class Twap {
66 public:
71 auto add(StandardPrice price, std::uint64_t timestamp) -> void {
72 if (m_has_sample && timestamp > m_last_timestamp) {
73 const double elapsed = static_cast<double>(timestamp - m_last_timestamp);
74 m_price_time += m_last_price * elapsed;
75 m_total_time += elapsed;
76 }
77 m_last_price = price.to_double();
78 m_last_timestamp = timestamp;
79 m_has_sample = true;
80 }
81
85 [[nodiscard]] auto value() const -> double {
86 if (m_total_time <= 0.0) {
87 return m_has_sample ? m_last_price : std::numeric_limits<double>::quiet_NaN();
88 }
89 return m_price_time / m_total_time;
90 }
91
93 auto reset() noexcept -> void {
94 m_price_time = 0.0;
95 m_total_time = 0.0;
96 m_last_price = 0.0;
97 m_last_timestamp = 0;
98 m_has_sample = false;
99 }
100
101 private:
102 double m_price_time {0.0};
103 double m_total_time {0.0};
104 double m_last_price {0.0};
105 std::uint64_t m_last_timestamp {0};
106 bool m_has_sample {false};
107};
108
109} // namespace itch::analytics
Running time-weighted average price.
Definition vwap.hpp:65
auto add(StandardPrice price, std::uint64_t timestamp) -> void
Records that the price became price at timestamp (ns).
Definition vwap.hpp:71
auto value() const -> double
The current TWAP, or NaN when no span has elapsed.
Definition vwap.hpp:85
auto reset() noexcept -> void
Clears the accumulation (start a new interval).
Definition vwap.hpp:93
Running volume-weighted average price.
Definition vwap.hpp:23
auto reset() noexcept -> void
Clears the accumulation (start a new interval).
Definition vwap.hpp:50
auto add(StandardPrice price, std::uint64_t shares) -> void
Adds an execution of shares at price to the accumulation.
Definition vwap.hpp:29
auto volume() const noexcept -> std::uint64_t
The total shares accumulated so far.
Definition vwap.hpp:47
auto value() const -> double
The current VWAP, or NaN when no volume has been added.
Definition vwap.hpp:37
std::string price
Definition csv_sink.cpp:23
std::uint64_t timestamp
Definition csv_sink.cpp:16
std::string shares
Definition csv_sink.cpp:22
Strongly typed, fixed-point price representation for ITCH price fields.