|
| 1 | +// |
| 2 | +// Created by sukai on 2024/3/8. |
| 3 | +// |
| 4 | + |
| 5 | +#ifndef TIGERAPI_PRICEUTIL_H |
| 6 | +#define TIGERAPI_PRICEUTIL_H |
| 7 | +#include <iostream> |
| 8 | +#include <vector> |
| 9 | +#include <cmath> |
| 10 | +#include <map> |
| 11 | +#include "cpprest/json.h" |
| 12 | +#include "cpprest/details/basic_types.h" |
| 13 | +#include "constants.h" |
| 14 | +#include "enums.h" |
| 15 | + |
| 16 | +namespace TIGER_API { |
| 17 | + |
| 18 | + |
| 19 | + class PriceUtil { |
| 20 | + public: |
| 21 | + static const std::string INF; |
| 22 | + static const double RELATIVE_TOLERANCE; |
| 23 | + static const utility::string_t TICK_SIZE; |
| 24 | + |
| 25 | + static bool match_tick_size(double price, const web::json::value &tick_sizes) { |
| 26 | + if (price == 0 || tick_sizes.size() == 0) { |
| 27 | + return false; |
| 28 | + } |
| 29 | + double fixed_price = fix_price_by_tick_size(price, tick_sizes); |
| 30 | + return std::abs(price - fixed_price) <= RELATIVE_TOLERANCE; |
| 31 | + } |
| 32 | + |
| 33 | + static double fix_price_by_tick_size(double price, const web::json::value tick_sizes, bool is_up = false) { |
| 34 | + if (price == 0) { |
| 35 | + return 0; |
| 36 | + } |
| 37 | + auto tick_size_item = find_tick_size_item(price, tick_sizes); |
| 38 | + if (tick_size_item.is_null()) { |
| 39 | + return price; |
| 40 | + } |
| 41 | + double min_tick = tick_size_item[TICK_SIZE].as_double(); |
| 42 | + |
| 43 | + double begin = stod(tick_size_item[P_BEGIN].as_string()); |
| 44 | + return round_with_tick(price, begin, min_tick, is_up); |
| 45 | + } |
| 46 | + |
| 47 | + static const web::json::value find_tick_size_item(double price, const web::json::value tick_sizes) { |
| 48 | + if (price == 0 || tick_sizes.size() == 0) { |
| 49 | + return {}; |
| 50 | + } |
| 51 | + for (const auto& item : tick_sizes.as_array()) { |
| 52 | + utility::string_t type = item.at(P_TYPE).as_string(); |
| 53 | + double begin = stod(item.at(P_BEGIN).as_string()); |
| 54 | + double end = (item.at(P_END).as_string() == INF) ? INFINITY : stod(item.at(P_END).as_string()); |
| 55 | + if (type == enum_to_str(TickSizeType::OPEN)) { |
| 56 | + if (begin < price && price < end) { |
| 57 | + return item; |
| 58 | + } |
| 59 | + } else if (type == enum_to_str(TickSizeType::CLOSED)) { |
| 60 | + if (begin <= price && price <= end) { |
| 61 | + return item; |
| 62 | + } |
| 63 | + } else if (type == enum_to_str(TickSizeType::CLOSED_OPEN)) { |
| 64 | + if (begin <= price && price < end) { |
| 65 | + return item; |
| 66 | + } |
| 67 | + } else if (type == enum_to_str(TickSizeType::OPEN_CLOSED)) { |
| 68 | + if (begin < price && price <= end) { |
| 69 | + return item; |
| 70 | + } |
| 71 | + } |
| 72 | + } |
| 73 | + return {}; |
| 74 | + } |
| 75 | + |
| 76 | + static double round_with_tick(double price, double begin, double min_tick, bool is_up) { |
| 77 | + double multiple = (price - begin) / min_tick; |
| 78 | + if (multiple <= 0) { |
| 79 | + return price; |
| 80 | + } |
| 81 | + if (is_up) { |
| 82 | + multiple += 1; |
| 83 | + } |
| 84 | + return std::floor(multiple) * min_tick + begin; |
| 85 | + } |
| 86 | + }; |
| 87 | + |
| 88 | + const utility::string_t PriceUtil::INF = U("Infinity"); |
| 89 | + const utility::string_t PriceUtil::TICK_SIZE = U("tickSize"); |
| 90 | + const double PriceUtil::RELATIVE_TOLERANCE = 1e-6; |
| 91 | + |
| 92 | +} // TIGER_API |
| 93 | + |
| 94 | +#endif //TIGERAPI_PRICEUTIL_H |
0 commit comments