Crypto backtesting
Loading...
Searching...
No Matches
fx Namespace Reference

Core routines that don't depend on any other fx routines. More...

Functions

template<typename T = size_t>
constexpr T to_size (std::ranges::range auto &&xs)
 Calculate size of a series, return type depends on input param.
constexpr auto to_first (std::ranges::range auto &&xs)
 Return the first entry in a series.
constexpr auto to_sum (std::ranges::range auto &&xs)
 Calculate sum of series.
constexpr auto to_sum2 (std::ranges::range auto &&xs)
 Calculate sum of series.
constexpr auto to_profit (auto &&entry, auto &&exit)
 Calculate profit from a trade.
void BM_is_entry (benchmark::State &state)
void BM_is_entry2 (benchmark::State &state)
void BM_to_exit (benchmark::State &state)
void BM_to_exit2 (benchmark::State &state)
void BM_to_vwap (benchmark::State &state)
void BM_to_vwap2 (benchmark::State &state)
void BM_to_atr (benchmark::State &state)
void BM_to_atr2 (benchmark::State &state)
void BM_to_atr3 (benchmark::State &state)
void BM_is_recent_dip2 (benchmark::State &state)
void BM_to_average_func (benchmark::State &state)
void BM_to_average_func2 (benchmark::State &state)
void BM_to_average_volume (benchmark::State &state)
void BM_to_average_volume2 (benchmark::State &state)
 BENCHMARK (BM_is_entry)
 BENCHMARK (BM_is_entry2)
 BENCHMARK (BM_to_exit)
 BENCHMARK (BM_to_exit2)
 BENCHMARK (BM_to_vwap)
 BENCHMARK (BM_to_vwap2)
 BENCHMARK (BM_is_recent_dip2)
 BENCHMARK (BM_to_atr)
 BENCHMARK (BM_to_atr2)
 BENCHMARK (BM_to_atr3)
 BENCHMARK (BM_to_average_func)
 BENCHMARK (BM_to_average_func2)
 BENCHMARK (BM_to_average_volume)
 BENCHMARK (BM_to_average_volume2)
void BM_to_size (benchmark::State &state)
void BM_to_first (benchmark::State &state)
void BM_to_last (benchmark::State &state)
void BM_to_sum (benchmark::State &state)
void BM_to_sum2 (benchmark::State &state)
void BM_to_spot (benchmark::State &state)
void BM_identity (benchmark::State &state)
void BM_identity2 (benchmark::State &state)
void BM_to_profit (benchmark::State &state)
 BENCHMARK (BM_to_size)
 BENCHMARK (BM_to_first)
 BENCHMARK (BM_to_last)
 BENCHMARK (BM_to_sum)
 BENCHMARK (BM_to_sum2)
 BENCHMARK (BM_to_spot)
 BENCHMARK (BM_identity)
 BENCHMARK (BM_identity2)
 BENCHMARK (BM_to_profit)
void BM_to_time (benchmark::State &state)
void BM_to_open (benchmark::State &state)
void BM_to_high (benchmark::State &state)
void BM_to_low (benchmark::State &state)
void BM_to_close (benchmark::State &state)
void BM_to_volume (benchmark::State &state)
 BENCHMARK (BM_to_time)
 BENCHMARK (BM_to_open)
 BENCHMARK (BM_to_high)
 BENCHMARK (BM_to_low)
 BENCHMARK (BM_to_close)
 BENCHMARK (BM_to_volume)
constexpr auto to_time (std::ranges::range auto &&xs)
 Get time of a data point.
constexpr auto to_open (std::ranges::range auto &&xs)
 Get open price for a data point.
constexpr auto to_high (std::ranges::range auto &&xs)
 Get high price for a data point.
constexpr auto to_low (std::ranges::range auto &&xs)
 Get low price for a data point.
constexpr auto to_close (std::ranges::range auto &&xs)
 Get close price for a data point.
constexpr auto to_atr (auto &&series)
 Calculate ATR of a series.
constexpr auto to_atr2 (auto &&series)
 Calculate ATR of a series.
constexpr double to_atr3 (auto &&series)
 Calculate ATR of a series.
constexpr double to_vwap (std::ranges::range auto &&xs)
 Calculate VWAP rolling average.
constexpr double to_vwap2 (std::ranges::range auto &&xs)
 Calculate VWAP rolling average.
auto is_recent_dip2 (auto &&series)
 Test if there has been a recent minimum.

Variables

constexpr auto win {68uz}
 The number of prices in a trading window.
constexpr auto win2 {34uz}
constexpr auto win4 {17uz}
constexpr auto take_profit {3.0}
 Close position if price has increased by this percentage.
constexpr auto stop_loss {take_profit * 2.0}
 Close position if price has decreased by this percentage.
constexpr auto minimum_entry {4.3}
 Minimum price to consider a trade.
constexpr auto minimum_atr {take_profit / 6.0}
 Minimum (normalised) ATR to consider a trade.
constexpr auto cells {6uz}
 Number of cells in a row of price data.
constexpr auto earliest_entry_epoch {1689798790}
 Furthest back in time to consider a trade.
constexpr auto to_last
 Return the last entry in a series.
constexpr auto identity = [](auto &&xs) { return xs; }
 Just passing through.
constexpr auto identity2
 Just passing through.
constexpr auto to_volume
 Get total volume for a data point.
constexpr auto to_average_func
constexpr auto to_average_func2
constexpr auto to_spot
 Calculate spot value, note we don't average all of the OHLC prices.
constexpr auto to_average_volume
 Calculate average volume over a series.
constexpr auto to_average_volume2
 Calculate average volume over a series.
auto to_exit
 Find an exit in a series.
auto to_exit2
 Find an exit in a series.
auto is_entry
 Calculate if the final price is an entry.
auto is_entry2
 Calculate if the final price is an entry.

Detailed Description

Core routines that don't depend on any other fx routines.

Function Documentation

◆ BENCHMARK() [1/29]

fx::BENCHMARK ( BM_identity )
Here is the call graph for this function:

◆ BENCHMARK() [2/29]

fx::BENCHMARK ( BM_identity2 )
Here is the call graph for this function:

◆ BENCHMARK() [3/29]

fx::BENCHMARK ( BM_is_entry )
Here is the call graph for this function:

◆ BENCHMARK() [4/29]

fx::BENCHMARK ( BM_is_entry2 )
Here is the call graph for this function:

◆ BENCHMARK() [5/29]

fx::BENCHMARK ( BM_is_recent_dip2 )
Here is the call graph for this function:

◆ BENCHMARK() [6/29]

fx::BENCHMARK ( BM_to_atr )
Here is the call graph for this function:

◆ BENCHMARK() [7/29]

fx::BENCHMARK ( BM_to_atr2 )
Here is the call graph for this function:

◆ BENCHMARK() [8/29]

fx::BENCHMARK ( BM_to_atr3 )
Here is the call graph for this function:

◆ BENCHMARK() [9/29]

fx::BENCHMARK ( BM_to_average_func )
Here is the call graph for this function:

◆ BENCHMARK() [10/29]

fx::BENCHMARK ( BM_to_average_func2 )
Here is the call graph for this function:

◆ BENCHMARK() [11/29]

fx::BENCHMARK ( BM_to_average_volume )
Here is the call graph for this function:

◆ BENCHMARK() [12/29]

fx::BENCHMARK ( BM_to_average_volume2 )
Here is the call graph for this function:

◆ BENCHMARK() [13/29]

fx::BENCHMARK ( BM_to_close )
Here is the call graph for this function:

◆ BENCHMARK() [14/29]

fx::BENCHMARK ( BM_to_exit )
Here is the call graph for this function:

◆ BENCHMARK() [15/29]

fx::BENCHMARK ( BM_to_exit2 )
Here is the call graph for this function:

◆ BENCHMARK() [16/29]

fx::BENCHMARK ( BM_to_first )
Here is the call graph for this function:

◆ BENCHMARK() [17/29]

fx::BENCHMARK ( BM_to_high )
Here is the call graph for this function:

◆ BENCHMARK() [18/29]

fx::BENCHMARK ( BM_to_last )
Here is the call graph for this function:

◆ BENCHMARK() [19/29]

fx::BENCHMARK ( BM_to_low )
Here is the call graph for this function:

◆ BENCHMARK() [20/29]

fx::BENCHMARK ( BM_to_open )
Here is the call graph for this function:

◆ BENCHMARK() [21/29]

fx::BENCHMARK ( BM_to_profit )
Here is the call graph for this function:

◆ BENCHMARK() [22/29]

fx::BENCHMARK ( BM_to_size )
Here is the call graph for this function:

◆ BENCHMARK() [23/29]

fx::BENCHMARK ( BM_to_spot )
Here is the call graph for this function:

◆ BENCHMARK() [24/29]

fx::BENCHMARK ( BM_to_sum )
Here is the call graph for this function:

◆ BENCHMARK() [25/29]

fx::BENCHMARK ( BM_to_sum2 )
Here is the call graph for this function:

◆ BENCHMARK() [26/29]

fx::BENCHMARK ( BM_to_time )
Here is the call graph for this function:

◆ BENCHMARK() [27/29]

fx::BENCHMARK ( BM_to_volume )
Here is the call graph for this function:

◆ BENCHMARK() [28/29]

fx::BENCHMARK ( BM_to_vwap )
Here is the call graph for this function:

◆ BENCHMARK() [29/29]

fx::BENCHMARK ( BM_to_vwap2 )
Here is the call graph for this function:

◆ BM_identity()

void fx::BM_identity ( benchmark::State & state)

Definition at line 162 of file main.cxx.

162 {
163 for (auto _ : state)
164 benchmark::DoNotOptimize(identity(xs3));
165}
constexpr auto identity
Just passing through.
Definition core.h:51
Here is the caller graph for this function:

◆ BM_identity2()

void fx::BM_identity2 ( benchmark::State & state)

Definition at line 167 of file main.cxx.

167 {
168 for (auto _ : state)
169 benchmark::DoNotOptimize(identity2(xs3));
170}
constexpr auto identity2
Just passing through.
Definition core.h:54
Here is the caller graph for this function:

◆ BM_is_entry()

void fx::BM_is_entry ( benchmark::State & state)

Definition at line 41 of file main.cxx.

41 {
42 for (auto _ : state)
43 benchmark::DoNotOptimize(
44 std::ranges::for_each(xs | slide(win) | filter(is_entry), [](auto) {}));
45}
constexpr auto win
The number of prices in a trading window.
Definition constants.h:6
auto is_entry
Calculate if the final price is an entry.
Definition trade.h:199
Here is the caller graph for this function:

◆ BM_is_entry2()

void fx::BM_is_entry2 ( benchmark::State & state)

Definition at line 47 of file main.cxx.

47 {
48 for (auto _ : state)
49 benchmark::DoNotOptimize(std::ranges::for_each(
50 xs | slide(win) | filter(is_entry2), [](auto) {}));
51}
auto is_entry2
Calculate if the final price is an entry.
Definition trade.h:232
Here is the caller graph for this function:

◆ BM_is_recent_dip2()

void fx::BM_is_recent_dip2 ( benchmark::State & state)

Definition at line 88 of file main.cxx.

88 {
89 for (auto _ : state)
90 benchmark::DoNotOptimize(is_recent_dip2(xs | take(win)));
91}
auto is_recent_dip2(auto &&series)
Test if there has been a recent minimum.
Definition trade.h:141
Here is the call graph for this function:
Here is the caller graph for this function:

◆ BM_to_atr()

void fx::BM_to_atr ( benchmark::State & state)

Definition at line 73 of file main.cxx.

73 {
74 for (auto _ : state)
75 benchmark::DoNotOptimize(to_atr(xs));
76}
constexpr auto to_atr(auto &&series)
Calculate ATR of a series.
Definition trade.h:43
Here is the call graph for this function:
Here is the caller graph for this function:

◆ BM_to_atr2()

void fx::BM_to_atr2 ( benchmark::State & state)

Definition at line 78 of file main.cxx.

78 {
79 for (auto _ : state)
80 benchmark::DoNotOptimize(to_atr2(xs));
81}
constexpr auto to_atr2(auto &&series)
Calculate ATR of a series.
Definition trade.h:62
Here is the call graph for this function:
Here is the caller graph for this function:

◆ BM_to_atr3()

void fx::BM_to_atr3 ( benchmark::State & state)

Definition at line 83 of file main.cxx.

83 {
84 for (auto _ : state)
85 benchmark::DoNotOptimize(to_atr3(xs));
86}
constexpr double to_atr3(auto &&series)
Calculate ATR of a series.
Definition trade.h:81
Here is the call graph for this function:
Here is the caller graph for this function:

◆ BM_to_average_func()

void fx::BM_to_average_func ( benchmark::State & state)

Definition at line 93 of file main.cxx.

93 {
94 for (auto _ : state)
95 benchmark::DoNotOptimize(to_average_func(xs3, identity));
96}
constexpr auto to_average_func
Definition trade.h:16
Here is the caller graph for this function:

◆ BM_to_average_func2()

void fx::BM_to_average_func2 ( benchmark::State & state)

Definition at line 98 of file main.cxx.

98 {
99 for (auto _ : state)
100 benchmark::DoNotOptimize(to_average_func2(xs3, identity));
101}
constexpr auto to_average_func2
Definition trade.h:21
Here is the caller graph for this function:

◆ BM_to_average_volume()

void fx::BM_to_average_volume ( benchmark::State & state)

Definition at line 103 of file main.cxx.

103 {
104 for (auto _ : state)
105 benchmark::DoNotOptimize(to_average_volume(xs));
106}
constexpr auto to_average_volume
Calculate average volume over a series.
Definition trade.h:100
Here is the caller graph for this function:

◆ BM_to_average_volume2()

void fx::BM_to_average_volume2 ( benchmark::State & state)

Definition at line 108 of file main.cxx.

108 {
109 for (auto _ : state)
110 benchmark::DoNotOptimize(to_average_volume2(xs));
111}
constexpr auto to_average_volume2
Calculate average volume over a series.
Definition trade.h:105
Here is the caller graph for this function:

◆ BM_to_close()

void fx::BM_to_close ( benchmark::State & state)

Definition at line 211 of file main.cxx.

211 {
212 for (auto _ : state)
213 benchmark::DoNotOptimize(to_close(xs2));
214}
constexpr auto to_close(std::ranges::range auto &&xs)
Get close price for a data point.
Definition ohlc.h:32
Here is the call graph for this function:
Here is the caller graph for this function:

◆ BM_to_exit()

void fx::BM_to_exit ( benchmark::State & state)

Definition at line 53 of file main.cxx.

53 {
54 for (auto _ : state)
55 benchmark::DoNotOptimize(to_exit(xs | take(win)));
56}
auto to_exit
Find an exit in a series.
Definition trade.h:152
Here is the caller graph for this function:

◆ BM_to_exit2()

void fx::BM_to_exit2 ( benchmark::State & state)

Definition at line 58 of file main.cxx.

58 {
59 for (auto _ : state)
60 benchmark::DoNotOptimize(to_exit2(xs | take(win)));
61}
auto to_exit2
Find an exit in a series.
Definition trade.h:177
Here is the caller graph for this function:

◆ BM_to_first()

void fx::BM_to_first ( benchmark::State & state)

Definition at line 137 of file main.cxx.

137 {
138 for (auto _ : state)
139 benchmark::DoNotOptimize(to_first(xs2));
140}
constexpr auto to_first(std::ranges::range auto &&xs)
Return the first entry in a series.
Definition core.h:19
Here is the call graph for this function:
Here is the caller graph for this function:

◆ BM_to_high()

void fx::BM_to_high ( benchmark::State & state)

Definition at line 201 of file main.cxx.

201 {
202 for (auto _ : state)
203 benchmark::DoNotOptimize(to_high(xs2));
204}
constexpr auto to_high(std::ranges::range auto &&xs)
Get high price for a data point.
Definition ohlc.h:20
Here is the call graph for this function:
Here is the caller graph for this function:

◆ BM_to_last()

void fx::BM_to_last ( benchmark::State & state)

Definition at line 142 of file main.cxx.

142 {
143 for (auto _ : state)
144 benchmark::DoNotOptimize(to_last(xs2));
145}
constexpr auto to_last
Return the last entry in a series.
Definition core.h:25
Here is the caller graph for this function:

◆ BM_to_low()

void fx::BM_to_low ( benchmark::State & state)

Definition at line 206 of file main.cxx.

206 {
207 for (auto _ : state)
208 benchmark::DoNotOptimize(to_low(xs2));
209}
constexpr auto to_low(std::ranges::range auto &&xs)
Get low price for a data point.
Definition ohlc.h:26
Here is the call graph for this function:
Here is the caller graph for this function:

◆ BM_to_open()

void fx::BM_to_open ( benchmark::State & state)

Definition at line 196 of file main.cxx.

196 {
197 for (auto _ : state)
198 benchmark::DoNotOptimize(to_open(xs2));
199}
constexpr auto to_open(std::ranges::range auto &&xs)
Get open price for a data point.
Definition ohlc.h:14
Here is the call graph for this function:
Here is the caller graph for this function:

◆ BM_to_profit()

void fx::BM_to_profit ( benchmark::State & state)

Definition at line 172 of file main.cxx.

172 {
173 for (auto _ : state)
174 benchmark::DoNotOptimize(to_profit(100.0, 110.0));
175}
constexpr auto to_profit(auto &&entry, auto &&exit)
Calculate profit from a trade.
Definition core.h:59
Here is the call graph for this function:
Here is the caller graph for this function:

◆ BM_to_size()

void fx::BM_to_size ( benchmark::State & state)

Definition at line 132 of file main.cxx.

132 {
133 for (auto _ : state)
134 benchmark::DoNotOptimize(to_size(xs));
135}
constexpr T to_size(std::ranges::range auto &&xs)
Calculate size of a series, return type depends on input param.
Definition core.h:14
Here is the call graph for this function:
Here is the caller graph for this function:

◆ BM_to_spot()

void fx::BM_to_spot ( benchmark::State & state)

Definition at line 157 of file main.cxx.

157 {
158 for (auto _ : state)
159 benchmark::DoNotOptimize(to_spot(xs2));
160}
constexpr auto to_spot
Calculate spot value, note we don't average all of the OHLC prices.
Definition trade.h:32
Here is the caller graph for this function:

◆ BM_to_sum()

void fx::BM_to_sum ( benchmark::State & state)

Definition at line 147 of file main.cxx.

147 {
148 for (auto _ : state)
149 benchmark::DoNotOptimize(to_sum(xs3));
150}
constexpr auto to_sum(std::ranges::range auto &&xs)
Calculate sum of series.
Definition core.h:31
Here is the call graph for this function:
Here is the caller graph for this function:

◆ BM_to_sum2()

void fx::BM_to_sum2 ( benchmark::State & state)

Definition at line 152 of file main.cxx.

152 {
153 for (auto _ : state)
154 benchmark::DoNotOptimize(to_sum2(xs3));
155}
constexpr auto to_sum2(std::ranges::range auto &&xs)
Calculate sum of series.
Definition core.h:37
Here is the call graph for this function:
Here is the caller graph for this function:

◆ BM_to_time()

void fx::BM_to_time ( benchmark::State & state)

Definition at line 191 of file main.cxx.

191 {
192 for (auto _ : state)
193 benchmark::DoNotOptimize(to_time(xs2));
194}
constexpr auto to_time(std::ranges::range auto &&xs)
Get time of a data point.
Definition ohlc.h:9
Here is the call graph for this function:
Here is the caller graph for this function:

◆ BM_to_volume()

void fx::BM_to_volume ( benchmark::State & state)

Definition at line 216 of file main.cxx.

216 {
217 for (auto _ : state)
218 benchmark::DoNotOptimize(to_volume(xs2));
219}
constexpr auto to_volume
Get total volume for a data point.
Definition ohlc.h:38
Here is the caller graph for this function:

◆ BM_to_vwap()

void fx::BM_to_vwap ( benchmark::State & state)

Definition at line 63 of file main.cxx.

63 {
64 for (auto _ : state)
65 benchmark::DoNotOptimize(to_vwap(xs));
66}
constexpr double to_vwap(std::ranges::range auto &&xs)
Calculate VWAP rolling average.
Definition trade.h:110
Here is the call graph for this function:
Here is the caller graph for this function:

◆ BM_to_vwap2()

void fx::BM_to_vwap2 ( benchmark::State & state)

Definition at line 68 of file main.cxx.

68 {
69 for (auto _ : state)
70 benchmark::DoNotOptimize(to_vwap2(xs));
71}
constexpr double to_vwap2(std::ranges::range auto &&xs)
Calculate VWAP rolling average.
Definition trade.h:125
Here is the call graph for this function:
Here is the caller graph for this function:

◆ is_recent_dip2()

auto fx::is_recent_dip2 ( auto && series)

Test if there has been a recent minimum.

Definition at line 141 of file trade.h.

141 {
142 // Find minimum spot value
143 auto &&min_it = std::ranges::min_element(
144 series, [](auto &&a, auto &&b) { return to_spot(a) < to_spot(b); });
145
146 // Return true if the minimum is in the middle
147 return std::ranges::distance(std::ranges::begin(series), min_it) ==
148 to_size<ssize_t>(series) / 2;
149}
Here is the call graph for this function:
Here is the caller graph for this function:

◆ to_atr()

auto fx::to_atr ( auto && series)
constexpr

Calculate ATR of a series.

Definition at line 43 of file trade.h.

43 {
44 // Get all adjacent pairs
45 auto pairs = series | slide(2);
46
47 // Calculate the true price and volume
48 auto true_ranges = pairs | transform([](auto px) {
49 assert(to_size(px) == 2);
50 auto a = px[0];
51 auto b = px[1];
52 auto tr_high = std::abs(to_high(b) - to_close(a));
53 auto tr_low = std::abs(to_low(b) - to_close(a));
54 return (tr_high + tr_low) / 2.0;
55 });
56
57 // Calculate average true range
58 return to_average_func(true_ranges, identity);
59}
Here is the call graph for this function:
Here is the caller graph for this function:

◆ to_atr2()

auto fx::to_atr2 ( auto && series)
constexpr

Calculate ATR of a series.

Definition at line 62 of file trade.h.

62 {
63 // Get all adjacent pairs
64 auto pairs = series | slide(2);
65
66 // Calculate the true price and volume
67 auto true_ranges = pairs | transform([](auto &&px) {
68 assert(to_size(px) == 2);
69 auto &&a = px[0];
70 auto &&b = px[1];
71 auto &&tr_high = std::abs(to_high(b) - to_close(a));
72 auto &&tr_low = std::abs(to_low(b) - to_close(a));
73 return (tr_high + tr_low) / 2.0;
74 });
75
76 // Calculate average true range
77 return to_average_func(true_ranges, identity);
78}
Here is the call graph for this function:
Here is the caller graph for this function:

◆ to_atr3()

double fx::to_atr3 ( auto && series)
constexpr

Calculate ATR of a series.

Definition at line 81 of file trade.h.

81 {
82 // Get all adjacent pairs
83 auto &&pairs = series | slide(2);
84
85 // Calculate the true price and volume
86 auto &&true_ranges = pairs | transform([](auto &&px) {
87 assert(to_size(px) == 2);
88 auto &&a = px[0];
89 auto &&b = px[1];
90 auto &&tr_high = std::abs(to_high(b) - to_close(a));
91 auto &&tr_low = std::abs(to_low(b) - to_close(a));
92 return (tr_high + tr_low) / double{2.0f};
93 });
94
95 // Calculate average true range
96 return to_average_func(true_ranges, [](auto &&x) { return x; });
97}
Here is the call graph for this function:
Here is the caller graph for this function:

◆ to_close()

auto fx::to_close ( std::ranges::range auto && xs)
constexpr

Get close price for a data point.

Definition at line 32 of file ohlc.h.

32 {
33 assert(to_size(xs) > 4uz);
34 return xs[4];
35}
Here is the call graph for this function:
Here is the caller graph for this function:

◆ to_first()

auto fx::to_first ( std::ranges::range auto && xs)
constexpr

Return the first entry in a series.

Definition at line 19 of file core.h.

19 {
20 assert(not std::ranges::empty(xs));
21 return xs[0];
22}
Here is the caller graph for this function:

◆ to_high()

auto fx::to_high ( std::ranges::range auto && xs)
constexpr

Get high price for a data point.

Definition at line 20 of file ohlc.h.

20 {
21 assert(to_size(xs) > 2uz);
22 return xs[2];
23}
Here is the call graph for this function:
Here is the caller graph for this function:

◆ to_low()

auto fx::to_low ( std::ranges::range auto && xs)
constexpr

Get low price for a data point.

Definition at line 26 of file ohlc.h.

26 {
27 assert(to_size(xs) > 3uz);
28 return xs[3];
29}
Here is the call graph for this function:
Here is the caller graph for this function:

◆ to_open()

auto fx::to_open ( std::ranges::range auto && xs)
constexpr

Get open price for a data point.

Definition at line 14 of file ohlc.h.

14 {
15 assert(to_size(xs) > 1uz);
16 return xs[1];
17}
Here is the call graph for this function:
Here is the caller graph for this function:

◆ to_profit()

auto fx::to_profit ( auto && entry,
auto && exit )
constexpr

Calculate profit from a trade.

Definition at line 59 of file core.h.

59 {
60 auto &&profit = decltype(entry){100.0f} * (exit - entry) / entry;
61
62 static_assert(std::is_same_v<decltype(entry), decltype(exit)>);
63 static_assert(std::is_same_v<decltype(profit), decltype(entry)>);
64
65 return profit;
66}
Here is the caller graph for this function:

◆ to_size()

template<typename T = size_t>
T fx::to_size ( std::ranges::range auto && xs)
constexpr

Calculate size of a series, return type depends on input param.

Definition at line 14 of file core.h.

14 {
15 return static_cast<T>(std::ranges::size(xs));
16}
Here is the caller graph for this function:

◆ to_sum()

auto fx::to_sum ( std::ranges::range auto && xs)
constexpr

Calculate sum of series.

Definition at line 31 of file core.h.

31 {
32 return std::ranges::fold_left(
33 xs, 0.0, [](const double acc, double x) { return acc + x; });
34}
Here is the caller graph for this function:

◆ to_sum2()

auto fx::to_sum2 ( std::ranges::range auto && xs)
constexpr

Calculate sum of series.

Definition at line 37 of file core.h.

37 {
38 assert(not std::ranges::empty(xs));
39
40 const auto &&sum = std::ranges::fold_left(
41 xs, decltype(xs[0]){}, [](auto acc, auto &&x) { return acc + x; });
42
43 // Check the type of the first element is the same as the sum
44 static_assert(std::is_same_v<std::remove_cvref_t<decltype(xs[0])>,
45 std::remove_cvref_t<decltype(sum)>>);
46
47 return sum;
48}
const std::vector< std::vector< double > > xs
An example of a full set of price data.
Definition main.cxx:13
Here is the caller graph for this function:

◆ to_time()

auto fx::to_time ( std::ranges::range auto && xs)
constexpr

Get time of a data point.

Definition at line 9 of file ohlc.h.

9 {
10 return std::lround(to_first(xs));
11};
Here is the call graph for this function:
Here is the caller graph for this function:

◆ to_vwap()

double fx::to_vwap ( std::ranges::range auto && xs)
constexpr

Calculate VWAP rolling average.

Definition at line 110 of file trade.h.

110 {
111 // Initialise cumulative volumes
112 auto cumulative_tp_volume = 0.0;
113 auto cumulative_volume = 0.0;
114
115 // Calculate VWAP for each sliding window
116 for (auto &&x : xs) {
117 cumulative_tp_volume += to_spot(x) * to_volume(x);
118 cumulative_volume += to_volume(x);
119 }
120
121 return cumulative_tp_volume / cumulative_volume;
122}
Here is the caller graph for this function:

◆ to_vwap2()

double fx::to_vwap2 ( std::ranges::range auto && xs)
constexpr

Calculate VWAP rolling average.

Definition at line 125 of file trade.h.

125 {
126 // Initialise cumulative volumes
127 auto &&cumulative_tp_volume = 0.0;
128 auto &&cumulative_volume = 0.0;
129
130 // Calculate VWAP for each sliding window
131 for (auto &&x : xs) {
132 auto &&vol = to_volume(x);
133 cumulative_tp_volume += to_spot(x) * vol;
134 cumulative_volume += vol;
135 }
136
137 return cumulative_tp_volume / cumulative_volume;
138}
Here is the caller graph for this function:

Variable Documentation

◆ cells

auto fx::cells {6uz}
constexpr

Number of cells in a row of price data.

Definition at line 23 of file constants.h.

23{6uz};

◆ earliest_entry_epoch

auto fx::earliest_entry_epoch {1689798790}
constexpr

Furthest back in time to consider a trade.

Definition at line 26 of file constants.h.

26{1689798790};

◆ identity

auto fx::identity = [](auto &&xs) { return xs; }
constexpr

Just passing through.

Definition at line 51 of file core.h.

51{ return xs; };

◆ identity2

auto fx::identity2
constexpr
Initial value:
= [](auto &&xs) {
return std::forward<decltype(xs)>(xs);
}

Just passing through.

Definition at line 54 of file core.h.

54 {
55 return std::forward<decltype(xs)>(xs);
56};

◆ is_entry

auto fx::is_entry

Calculate if the final price is an entry.

Definition at line 199 of file trade.h.

199 {
200 // Create subsets of complete series
201 const auto last_half =
202 series | drop(std::llround(to_size<double>(series) / 2.0));
203 const auto last_quarter =
204 series | drop(std::llround(to_size<double>(series) * 3.0 / 4.0));
205
206 // Copy the final price: the entry point
207 const auto entry = to_last(series);
208
209 // Test all the criteria for a trade
210 return
211 // No small change
213
214 // Above minimum ATR
215 and (100.0 * to_atr3(series) / to_spot(entry)) > fx::minimum_atr
216
217 // Average volume is used to determine if the last price is a spike
218 and
219 to_volume(entry) > to_average_volume(series | take(to_size(series) - 1))
220
221 // There has been a recent minimum
222 and is_recent_dip2(last_quarter)
223
224 // Price is below the long VWAP
225 and to_spot(entry) < to_vwap(series)
226
227 // Short VWAP is above the long VWAP
228 and to_vwap(last_half) > to_vwap(series);
229};
constexpr auto minimum_entry
Minimum price to consider a trade.
Definition constants.h:17
constexpr auto minimum_atr
Minimum (normalised) ATR to consider a trade.
Definition constants.h:20

◆ is_entry2

auto fx::is_entry2

Calculate if the final price is an entry.

Definition at line 232 of file trade.h.

232 {
233 assert(not std::ranges::empty(series));
234
235 // Create subsets of complete series
236 // Using reverse simplifies the drop logic
237 auto &&last_half = series | reverse | take(fx::win2) | reverse;
238 auto &&last_quarter = series | reverse | take(fx::win4) | reverse;
239
240 // Copy the final price: the entry point
241 auto &&entry = to_last(series);
242
243 // Test all the criteria for a trade
244 return
245
246 // No old trades
248
249 // No small change
250 and to_spot(entry) > fx::minimum_entry
251
252 // Above minimum ATR
253 and (100.0 * to_atr3(series) / to_spot(entry)) > fx::minimum_atr
254
255 // Average volume is used to determine if the last price is a spike
256 and to_volume(entry) >
257 to_average_volume(series | reverse | drop(1uz) | reverse)
258
259 // There has been a recent minimum
260 and is_recent_dip2(last_quarter)
261
262 // Price is below the long VWAP
263 and to_spot(entry) < to_vwap(series)
264
265 // Short VWAP is above the long VWAP
266 and to_vwap(last_half) > to_vwap(series);
267};
constexpr auto earliest_entry_epoch
Furthest back in time to consider a trade.
Definition constants.h:26
constexpr auto win4
Definition constants.h:8
constexpr auto win2
Definition constants.h:7

◆ minimum_atr

auto fx::minimum_atr {take_profit / 6.0}
constexpr

Minimum (normalised) ATR to consider a trade.

Definition at line 20 of file constants.h.

20{take_profit / 6.0};
constexpr auto take_profit
Close position if price has increased by this percentage.
Definition constants.h:11

◆ minimum_entry

auto fx::minimum_entry {4.3}
constexpr

Minimum price to consider a trade.

Definition at line 17 of file constants.h.

17{4.3};

◆ stop_loss

auto fx::stop_loss {take_profit * 2.0}
constexpr

Close position if price has decreased by this percentage.

Definition at line 14 of file constants.h.

14{take_profit * 2.0};

◆ take_profit

auto fx::take_profit {3.0}
constexpr

Close position if price has increased by this percentage.

Definition at line 11 of file constants.h.

11{3.0};

◆ to_average_func

auto fx::to_average_func
constexpr
Initial value:
= [](auto &&xs, auto func) {
assert(not std::ranges::empty(xs));
return to_sum(xs | transform(func)) / to_size(xs);
}

Calculate average of series, applying a function to each element (which could be a no-op)

Definition at line 16 of file trade.h.

16 {
17 assert(not std::ranges::empty(xs));
18 return to_sum(xs | transform(func)) / to_size(xs);
19};

◆ to_average_func2

auto fx::to_average_func2
constexpr
Initial value:
= [](auto &&xs, auto &&func) {
assert(not std::ranges::empty(xs));
auto &&sum =
std::ranges::fold_left(xs | transform(func), 0.0,
[](const auto acc, auto x) { return acc + x; });
return sum;
}

Definition at line 21 of file trade.h.

21 {
22 assert(not std::ranges::empty(xs));
23
24 auto &&sum =
25 std::ranges::fold_left(xs | transform(func), 0.0,
26 [](const auto acc, auto x) { return acc + x; });
27
28 return sum;
29};

◆ to_average_volume

auto fx::to_average_volume
constexpr
Initial value:
= [](auto series) {
return to_average_func(series, to_volume);
}

Calculate average volume over a series.

Definition at line 100 of file trade.h.

100 {
101 return to_average_func(series, to_volume);
102};

◆ to_average_volume2

auto fx::to_average_volume2
constexpr
Initial value:
= [](auto &&series) {
return to_average_func2(series, to_volume);
}

Calculate average volume over a series.

Definition at line 105 of file trade.h.

105 {
106 return to_average_func2(series, to_volume);
107};

◆ to_exit

auto fx::to_exit
Initial value:
= [](auto &&series) {
const double open_price = to_spot(to_first(series));
const double take_price = open_price * (100.0 + take_profit) / 100.0;
const double stop_price = open_price * (100.0 - stop_loss) / 100.0;
auto to_trunc =
series | drop_while([=](auto &&xs) {
const double close_price = to_spot(xs);
return close_price > stop_price and close_price < take_price;
}) |
common;
auto truncated = std::vector(std::begin(to_trunc), std::end(to_trunc));
if (std::ranges::empty(truncated))
return to_last(series);
return to_first(truncated);
}
constexpr auto stop_loss
Close position if price has decreased by this percentage.
Definition constants.h:14

Find an exit in a series.

Definition at line 152 of file trade.h.

152 {
153 // Calculate the exit thresholds
154 const double open_price = to_spot(to_first(series));
155 const double take_price = open_price * (100.0 + take_profit) / 100.0;
156 const double stop_price = open_price * (100.0 - stop_loss) / 100.0;
157
158 // Drop everything up to the exit
159 auto to_trunc =
160 series | drop_while([=](auto &&xs) {
161 const double close_price = to_spot(xs);
162 return close_price > stop_price and close_price < take_price;
163 }) |
164 common;
165
166 auto truncated = std::vector(std::begin(to_trunc), std::end(to_trunc));
167
168 // If it's been truncated to nothing, then return the last price
169 if (std::ranges::empty(truncated))
170 return to_last(series);
171
172 // Otherwise, the first price in the truncated series is the exit
173 return to_first(truncated);
174};

◆ to_exit2

auto fx::to_exit2
Initial value:
= [](auto &&series) {
auto &&open_price = to_spot(to_first(series));
auto &&take_price = open_price * (100.0 + take_profit) / 100.0;
auto &&stop_price = open_price * (100.0 - stop_loss) / 100.0;
auto &&to_trunc =
series | drop_while([&](auto &&xs) {
auto &&close_price = to_spot(xs);
return close_price > stop_price and close_price < take_price;
}) |
common;
auto &&truncated = std::vector(std::begin(to_trunc), std::end(to_trunc));
return std::ranges::empty(truncated) ? to_last(series) : to_first(truncated);
}

Find an exit in a series.

Definition at line 177 of file trade.h.

177 {
178 // Calculate the exit thresholds
179 auto &&open_price = to_spot(to_first(series));
180 auto &&take_price = open_price * (100.0 + take_profit) / 100.0;
181 auto &&stop_price = open_price * (100.0 - stop_loss) / 100.0;
182
183 // Drop everything up to the exit
184 auto &&to_trunc =
185 series | drop_while([&](auto &&xs) {
186 auto &&close_price = to_spot(xs);
187 return close_price > stop_price and close_price < take_price;
188 }) |
189 common;
190
191 auto &&truncated = std::vector(std::begin(to_trunc), std::end(to_trunc));
192
193 // If it's been truncated to nothing, then return the last price
194 // Otherwise, the first price in the truncated series is the exit
195 return std::ranges::empty(truncated) ? to_last(series) : to_first(truncated);
196};

◆ to_last

auto fx::to_last
constexpr
Initial value:
= [](std::ranges::range auto &&xs) {
assert(not std::ranges::empty(xs));
return xs[to_size(xs) - 1uz];
}

Return the last entry in a series.

Definition at line 25 of file core.h.

25 {
26 assert(not std::ranges::empty(xs));
27 return xs[to_size(xs) - 1uz];
28};

◆ to_spot

auto fx::to_spot
constexpr
Initial value:
= [](auto &&xs) {
assert(to_size(xs) > 4);
auto &&spot = to_average_func(xs | drop(1) | take(3), identity);
return spot;
}

Calculate spot value, note we don't average all of the OHLC prices.

Definition at line 32 of file trade.h.

32 {
33 // Don't include the close price in the average
34 assert(to_size(xs) > 4);
35
36 auto &&spot = to_average_func(xs | drop(1) | take(3), identity);
37 return spot;
38};

◆ to_volume

auto fx::to_volume
constexpr
Initial value:
= [](std::ranges::range auto &&xs) {
assert(to_size(xs) > 5uz);
return xs[5];
}

Get total volume for a data point.

Definition at line 38 of file ohlc.h.

38 {
39 assert(to_size(xs) > 5uz);
40 return xs[5];
41};

◆ win

auto fx::win {68uz}
constexpr

The number of prices in a trading window.

Definition at line 6 of file constants.h.

6{68uz};

◆ win2

auto fx::win2 {34uz}
constexpr

Definition at line 7 of file constants.h.

7{34uz};

◆ win4

auto fx::win4 {17uz}
constexpr

Definition at line 8 of file constants.h.

8{17uz};