12using namespace std::views;
17 assert(not std::ranges::empty(xs));
22 assert(not std::ranges::empty(xs));
25 std::ranges::fold_left(xs | transform(func), 0.0,
26 [](
const auto acc,
auto x) {
return acc + x; });
40static_assert(
to_spot(std::vector{0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0}) == 2.0);
43constexpr auto to_atr(
auto &&series) {
45 auto pairs = series | slide(2);
48 auto true_ranges = pairs | transform([](
auto px) {
54 return (tr_high + tr_low) / 2.0;
64 auto pairs = series | slide(2);
67 auto true_ranges = pairs | transform([](
auto &&px) {
73 return (tr_high + tr_low) / 2.0;
83 auto &&pairs = series | slide(2);
86 auto &&true_ranges = pairs | transform([](
auto &&px) {
92 return (tr_high + tr_low) /
double{2.0f};
110constexpr double to_vwap(std::ranges::range
auto &&xs) {
112 auto cumulative_tp_volume = 0.0;
113 auto cumulative_volume = 0.0;
116 for (
auto &&x : xs) {
121 return cumulative_tp_volume / cumulative_volume;
125constexpr double to_vwap2(std::ranges::range
auto &&xs) {
127 auto &&cumulative_tp_volume = 0.0;
128 auto &&cumulative_volume = 0.0;
131 for (
auto &&x : xs) {
133 cumulative_tp_volume +=
to_spot(x) * vol;
134 cumulative_volume += vol;
137 return cumulative_tp_volume / cumulative_volume;
143 auto &&min_it = std::ranges::min_element(
144 series, [](
auto &&a,
auto &&b) {
return to_spot(a) <
to_spot(b); });
147 return std::ranges::distance(std::ranges::begin(series), min_it) ==
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;
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;
166 auto truncated = std::vector(std::begin(to_trunc), std::end(to_trunc));
169 if (std::ranges::empty(truncated))
180 auto &&take_price = open_price * (100.0 +
take_profit) / 100.0;
181 auto &&stop_price = open_price * (100.0 -
stop_loss) / 100.0;
185 series | drop_while([&](
auto &&xs) {
186 auto &&close_price =
to_spot(xs);
187 return close_price > stop_price and close_price < take_price;
191 auto &&truncated = std::vector(std::begin(to_trunc), std::end(to_trunc));
195 return std::ranges::empty(truncated) ?
to_last(series) :
to_first(truncated);
199auto is_entry = [](std::ranges::range
auto &&series) {
201 const auto last_half =
203 const auto last_quarter =
207 const auto entry =
to_last(series);
233 assert(not std::ranges::empty(series));
237 auto &&last_half = series | reverse | take(
fx::win2) | reverse;
238 auto &&last_quarter = series | reverse | take(
fx::win4) | reverse;
241 auto &&entry =
to_last(series);
Core routines that don't depend on any other fx routines.
constexpr auto to_atr(auto &&series)
Calculate ATR of a series.
constexpr auto to_first(std::ranges::range auto &&xs)
Return the first entry in a series.
constexpr double to_vwap(std::ranges::range auto &&xs)
Calculate VWAP rolling average.
constexpr auto to_time(std::ranges::range auto &&xs)
Get time of a data point.
constexpr auto stop_loss
Close position if price has decreased by this percentage.
constexpr auto to_average_func
auto to_exit2
Find an exit in a series.
constexpr auto to_atr2(auto &&series)
Calculate ATR of a series.
constexpr auto to_last
Return the last entry in a series.
constexpr double to_vwap2(std::ranges::range auto &&xs)
Calculate VWAP rolling average.
constexpr auto to_low(std::ranges::range auto &&xs)
Get low price for a data point.
auto to_exit
Find an exit in a series.
constexpr auto minimum_entry
Minimum price to consider a trade.
constexpr auto take_profit
Close position if price has increased by this percentage.
constexpr double to_atr3(auto &&series)
Calculate ATR of a series.
constexpr auto to_average_volume
Calculate average volume over a series.
constexpr auto earliest_entry_epoch
Furthest back in time to consider a trade.
auto is_recent_dip2(auto &&series)
Test if there has been a recent minimum.
constexpr auto to_average_func2
auto is_entry2
Calculate if the final price is an entry.
constexpr auto to_sum(std::ranges::range auto &&xs)
Calculate sum of series.
constexpr auto identity
Just passing through.
constexpr auto minimum_atr
Minimum (normalised) ATR to consider a trade.
constexpr auto to_close(std::ranges::range auto &&xs)
Get close price for a data point.
constexpr auto to_volume
Get total volume for a data point.
constexpr auto to_spot
Calculate spot value, note we don't average all of the OHLC prices.
constexpr auto to_high(std::ranges::range auto &&xs)
Get high price for a data point.
constexpr T to_size(std::ranges::range auto &&xs)
Calculate size of a series, return type depends on input param.
constexpr auto to_average_volume2
Calculate average volume over a series.
auto is_entry
Calculate if the final price is an entry.