Crypto backtesting
Loading...
Searching...
No Matches
main.cxx
Go to the documentation of this file.
1#include "core.h"
2#include "ohlc.h"
3#include "trade.h"
4#include <algorithm>
5#include <array>
6#include <benchmark/benchmark.h>
7#include <numeric>
8
9// Create some test data for the trades
10namespace {
11
12/// An example of a full set of price data
13const std::vector<std::vector<double>> xs = [] {
14 auto &&xs = std::vector<std::vector<double>>{};
15 auto &&row = std::vector<double>{0.0f, 1.0f, 2.0f, 3.0f, 4.0f, 5.0f};
16
17 for (size_t i = 0; i < 2000; ++i) {
18 row[0] = static_cast<double>(i);
19 xs.emplace_back(row);
20 }
21
22 return xs;
23}();
24
25/// A single row of price data
26constexpr auto xs2 = std::array{1, 2, 3, 4, 5, 6};
27
28/// A single column of price data
29constexpr auto xs3 = [] {
30 std::array<double, 2000> xs;
31 for (auto i : std::views::iota(0, fx::to_size<int>(xs)))
32 xs[i] = static_cast<double>(i);
33
34 return xs;
35}();
36} // namespace
37
38// TRADE
39namespace fx {
40
41void BM_is_entry(benchmark::State &state) {
42 for (auto _ : state)
43 benchmark::DoNotOptimize(
44 std::ranges::for_each(xs | slide(win) | filter(is_entry), [](auto) {}));
45}
46
47void BM_is_entry2(benchmark::State &state) {
48 for (auto _ : state)
49 benchmark::DoNotOptimize(std::ranges::for_each(
50 xs | slide(win) | filter(is_entry2), [](auto) {}));
51}
52
53void BM_to_exit(benchmark::State &state) {
54 for (auto _ : state)
55 benchmark::DoNotOptimize(to_exit(xs | take(win)));
56}
57
58void BM_to_exit2(benchmark::State &state) {
59 for (auto _ : state)
60 benchmark::DoNotOptimize(to_exit2(xs | take(win)));
61}
62
63void BM_to_vwap(benchmark::State &state) {
64 for (auto _ : state)
65 benchmark::DoNotOptimize(to_vwap(xs));
66}
67
68void BM_to_vwap2(benchmark::State &state) {
69 for (auto _ : state)
70 benchmark::DoNotOptimize(to_vwap2(xs));
71}
72
73void BM_to_atr(benchmark::State &state) {
74 for (auto _ : state)
75 benchmark::DoNotOptimize(to_atr(xs));
76}
77
78void BM_to_atr2(benchmark::State &state) {
79 for (auto _ : state)
80 benchmark::DoNotOptimize(to_atr2(xs));
81}
82
83void BM_to_atr3(benchmark::State &state) {
84 for (auto _ : state)
85 benchmark::DoNotOptimize(to_atr3(xs));
86}
87
88void BM_is_recent_dip2(benchmark::State &state) {
89 for (auto _ : state)
90 benchmark::DoNotOptimize(is_recent_dip2(xs | take(win)));
91}
92
93void BM_to_average_func(benchmark::State &state) {
94 for (auto _ : state)
95 benchmark::DoNotOptimize(to_average_func(xs3, identity));
96}
97
98void BM_to_average_func2(benchmark::State &state) {
99 for (auto _ : state)
100 benchmark::DoNotOptimize(to_average_func2(xs3, identity));
101}
102
103void BM_to_average_volume(benchmark::State &state) {
104 for (auto _ : state)
105 benchmark::DoNotOptimize(to_average_volume(xs));
106}
107
108void BM_to_average_volume2(benchmark::State &state) {
109 for (auto _ : state)
110 benchmark::DoNotOptimize(to_average_volume2(xs));
111}
112
127
128} // namespace fx
129
130// CORE
131namespace fx {
132void BM_to_size(benchmark::State &state) {
133 for (auto _ : state)
134 benchmark::DoNotOptimize(to_size(xs));
135}
136
137void BM_to_first(benchmark::State &state) {
138 for (auto _ : state)
139 benchmark::DoNotOptimize(to_first(xs2));
140}
141
142void BM_to_last(benchmark::State &state) {
143 for (auto _ : state)
144 benchmark::DoNotOptimize(to_last(xs2));
145}
146
147void BM_to_sum(benchmark::State &state) {
148 for (auto _ : state)
149 benchmark::DoNotOptimize(to_sum(xs3));
150}
151
152void BM_to_sum2(benchmark::State &state) {
153 for (auto _ : state)
154 benchmark::DoNotOptimize(to_sum2(xs3));
155}
156
157void BM_to_spot(benchmark::State &state) {
158 for (auto _ : state)
159 benchmark::DoNotOptimize(to_spot(xs2));
160}
161
162void BM_identity(benchmark::State &state) {
163 for (auto _ : state)
164 benchmark::DoNotOptimize(identity(xs3));
165}
166
167void BM_identity2(benchmark::State &state) {
168 for (auto _ : state)
169 benchmark::DoNotOptimize(identity2(xs3));
170}
171
172void BM_to_profit(benchmark::State &state) {
173 for (auto _ : state)
174 benchmark::DoNotOptimize(to_profit(100.0, 110.0));
175}
176
186} // namespace fx
187
188// OHLC
189namespace fx {
190
191void BM_to_time(benchmark::State &state) {
192 for (auto _ : state)
193 benchmark::DoNotOptimize(to_time(xs2));
194}
195
196void BM_to_open(benchmark::State &state) {
197 for (auto _ : state)
198 benchmark::DoNotOptimize(to_open(xs2));
199}
200
201void BM_to_high(benchmark::State &state) {
202 for (auto _ : state)
203 benchmark::DoNotOptimize(to_high(xs2));
204}
205
206void BM_to_low(benchmark::State &state) {
207 for (auto _ : state)
208 benchmark::DoNotOptimize(to_low(xs2));
209}
210
211void BM_to_close(benchmark::State &state) {
212 for (auto _ : state)
213 benchmark::DoNotOptimize(to_close(xs2));
214}
215
216void BM_to_volume(benchmark::State &state) {
217 for (auto _ : state)
218 benchmark::DoNotOptimize(to_volume(xs2));
219}
220
227
228} // namespace fx
229
230int main(int argc, char **argv) {
231 ::benchmark::Initialize(&argc, argv);
232 ::benchmark::RunSpecifiedBenchmarks();
233}
int main(int argc, char **argv)
Definition main.cxx:230
constexpr auto xs2
A single row of price data.
Definition main.cxx:26
constexpr auto xs3
A single column of price data.
Definition main.cxx:29
const std::vector< std::vector< double > > xs
An example of a full set of price data.
Definition main.cxx:13
Core routines that don't depend on any other fx routines.
Definition constants.h:3
void BM_identity(benchmark::State &state)
Definition main.cxx:162
constexpr auto to_atr(auto &&series)
Calculate ATR of a series.
Definition trade.h:43
constexpr auto to_first(std::ranges::range auto &&xs)
Return the first entry in a series.
Definition core.h:19
void BM_to_spot(benchmark::State &state)
Definition main.cxx:157
constexpr double to_vwap(std::ranges::range auto &&xs)
Calculate VWAP rolling average.
Definition trade.h:110
constexpr auto to_time(std::ranges::range auto &&xs)
Get time of a data point.
Definition ohlc.h:9
void BM_to_exit(benchmark::State &state)
Definition main.cxx:53
void BM_to_average_volume2(benchmark::State &state)
Definition main.cxx:108
void BM_to_volume(benchmark::State &state)
Definition main.cxx:216
constexpr auto to_average_func
Definition trade.h:16
auto to_exit2
Find an exit in a series.
Definition trade.h:177
constexpr auto to_sum2(std::ranges::range auto &&xs)
Calculate sum of series.
Definition core.h:37
void BM_is_entry2(benchmark::State &state)
Definition main.cxx:47
constexpr auto to_atr2(auto &&series)
Calculate ATR of a series.
Definition trade.h:62
constexpr auto to_last
Return the last entry in a series.
Definition core.h:25
constexpr double to_vwap2(std::ranges::range auto &&xs)
Calculate VWAP rolling average.
Definition trade.h:125
constexpr auto to_low(std::ranges::range auto &&xs)
Get low price for a data point.
Definition ohlc.h:26
auto to_exit
Find an exit in a series.
Definition trade.h:152
void BM_to_vwap(benchmark::State &state)
Definition main.cxx:63
void BM_to_close(benchmark::State &state)
Definition main.cxx:211
BENCHMARK(BM_is_entry)
constexpr double to_atr3(auto &&series)
Calculate ATR of a series.
Definition trade.h:81
constexpr auto to_average_volume
Calculate average volume over a series.
Definition trade.h:100
void BM_identity2(benchmark::State &state)
Definition main.cxx:167
auto is_recent_dip2(auto &&series)
Test if there has been a recent minimum.
Definition trade.h:141
void BM_to_sum2(benchmark::State &state)
Definition main.cxx:152
constexpr auto to_average_func2
Definition trade.h:21
void BM_to_exit2(benchmark::State &state)
Definition main.cxx:58
void BM_to_sum(benchmark::State &state)
Definition main.cxx:147
constexpr auto win
The number of prices in a trading window.
Definition constants.h:6
void BM_to_atr(benchmark::State &state)
Definition main.cxx:73
void BM_to_time(benchmark::State &state)
Definition main.cxx:191
void BM_to_average_func(benchmark::State &state)
Definition main.cxx:93
constexpr auto to_profit(auto &&entry, auto &&exit)
Calculate profit from a trade.
Definition core.h:59
void BM_to_profit(benchmark::State &state)
Definition main.cxx:172
auto is_entry2
Calculate if the final price is an entry.
Definition trade.h:232
void BM_is_entry(benchmark::State &state)
Definition main.cxx:41
void BM_to_high(benchmark::State &state)
Definition main.cxx:201
void BM_to_size(benchmark::State &state)
Definition main.cxx:132
constexpr auto identity2
Just passing through.
Definition core.h:54
void BM_to_atr2(benchmark::State &state)
Definition main.cxx:78
void BM_to_open(benchmark::State &state)
Definition main.cxx:196
constexpr auto to_sum(std::ranges::range auto &&xs)
Calculate sum of series.
Definition core.h:31
void BM_to_low(benchmark::State &state)
Definition main.cxx:206
constexpr auto identity
Just passing through.
Definition core.h:51
void BM_to_average_func2(benchmark::State &state)
Definition main.cxx:98
void BM_to_vwap2(benchmark::State &state)
Definition main.cxx:68
void BM_to_average_volume(benchmark::State &state)
Definition main.cxx:103
constexpr auto to_close(std::ranges::range auto &&xs)
Get close price for a data point.
Definition ohlc.h:32
void BM_is_recent_dip2(benchmark::State &state)
Definition main.cxx:88
constexpr auto to_open(std::ranges::range auto &&xs)
Get open price for a data point.
Definition ohlc.h:14
constexpr auto to_volume
Get total volume for a data point.
Definition ohlc.h:38
constexpr auto to_spot
Calculate spot value, note we don't average all of the OHLC prices.
Definition trade.h:32
void BM_to_first(benchmark::State &state)
Definition main.cxx:137
constexpr auto to_high(std::ranges::range auto &&xs)
Get high price for a data point.
Definition ohlc.h:20
void BM_to_last(benchmark::State &state)
Definition main.cxx:142
constexpr T to_size(std::ranges::range auto &&xs)
Calculate size of a series, return type depends on input param.
Definition core.h:14
constexpr auto to_average_volume2
Calculate average volume over a series.
Definition trade.h:105
auto is_entry
Calculate if the final price is an entry.
Definition trade.h:199
void BM_to_atr3(benchmark::State &state)
Definition main.cxx:83