|
| 1 | +/**************************************************************************** |
| 2 | + * Copyright (c) 2020-2026 Jimmy M. Gong * |
| 3 | + * All rights reserved. * |
| 4 | + * * |
| 5 | + * Distributed under the terms of the GPL3 and LGPL3 Licenses. * |
| 6 | + * * |
| 7 | + * The full license is in the file LICENSE, distributed with this software. * |
| 8 | + ****************************************************************************/ |
| 9 | + |
| 10 | +#include "abquant/actions/stockday.hpp" |
| 11 | +#include "abquant/actions/stockmin.hpp" |
| 12 | +#include "abquant/actions/stockxdxr.hpp" |
| 13 | +#include "abquant/actions/xdxr_p.hpp" |
| 14 | + |
| 15 | +namespace abq |
| 16 | +{ |
| 17 | +/******************* |
| 18 | + * Xdxr * |
| 19 | + *******************/ |
| 20 | + |
| 21 | +Xdxr::Xdxr(const StockDayAction& sa) : pImpl{std::make_shared<impl>(sa)} {} |
| 22 | + |
| 23 | +Xdxr::Xdxr(const StockMinAction& sa) : pImpl{std::make_shared<impl>(sa)} {} |
| 24 | + |
| 25 | +//! Destructor |
| 26 | +Xdxr::~Xdxr() noexcept = default; |
| 27 | + |
| 28 | +//! Move assignment operator |
| 29 | +Xdxr& Xdxr::operator=(Xdxr&& other) noexcept |
| 30 | +{ |
| 31 | + if (&other == this) { |
| 32 | + return *this; |
| 33 | + } |
| 34 | + swap(pImpl, other.pImpl); |
| 35 | + |
| 36 | + return *this; |
| 37 | +}; |
| 38 | + |
| 39 | +MyDataFramePtr Xdxr::getXdxr(MyDataFramePtr df, FQ_TYPE fq) { return pImpl->getXdxr(df, fq); } |
| 40 | + |
| 41 | +/*********************** |
| 42 | + * Xdxr impl * |
| 43 | + **********************/ |
| 44 | +Xdxr::impl::impl(const StockDayAction& sa) |
| 45 | +{ |
| 46 | + m_codes = sa.getCodes(); |
| 47 | + std::shared_ptr<StockXdxrAction> sap = std::make_shared<StockXdxrAction>(m_codes, 1); |
| 48 | + m_xdxr_df = sap->getDataFrame(); |
| 49 | +}; |
| 50 | + |
| 51 | +Xdxr::impl::impl(const StockMinAction& sa) |
| 52 | +{ |
| 53 | + m_codes = sa.getCodes(); |
| 54 | + std::shared_ptr<StockXdxrAction> sap = std::make_shared<StockXdxrAction>(m_codes, 1); |
| 55 | + m_xdxr_df = sap->getDataFrame(); |
| 56 | +}; |
| 57 | + |
| 58 | +MyDataFramePtr Xdxr::impl::getXdxr(const MyDataFramePtr df, FQ_TYPE fq) |
| 59 | +{ |
| 60 | + // df->template write<std::ostream, index_t, double, int>(std::cout); |
| 61 | + MyDataFramePtr rdf = concat(df, *m_xdxr_df); |
| 62 | + // rdf->template write<std::ostream, index_t, double, int>(std::cout); |
| 63 | + fillConcatDataframe(rdf); |
| 64 | + return calc(rdf, fq); |
| 65 | +} |
| 66 | + |
| 67 | +MyDataFramePtr Xdxr::impl::concat(const MyDataFramePtr ldf, const MyDataFrame& rdf) const |
| 68 | +{ |
| 69 | + MyDataFrame df = |
| 70 | + ldf->join_by_index<std::decay_t<decltype(rdf)>, index_t, double, int>(rdf, join_policy::left_right_join); |
| 71 | + return std::make_shared<MyDataFrame>(df); |
| 72 | +} |
| 73 | + |
| 74 | +void Xdxr::impl::fillConcatDataframe(MyDataFramePtr df) const |
| 75 | +{ |
| 76 | + df->fill_missing<double, 1>({"if_trade"}, fill_policy::value, {0}); |
| 77 | + df->fill_missing<double, 6>( |
| 78 | + { |
| 79 | + "open", |
| 80 | + "close", |
| 81 | + "high", |
| 82 | + "low", |
| 83 | + "if_trade", |
| 84 | + "category", |
| 85 | + }, |
| 86 | + fill_policy::fill_forward); |
| 87 | + df->fill_missing<double, 4>( |
| 88 | + { |
| 89 | + "fenhong", |
| 90 | + "peigu", |
| 91 | + "peigujia", |
| 92 | + "songzhuangu", |
| 93 | + }, |
| 94 | + fill_policy::value, {0}); |
| 95 | + df->fill_missing<double, 1>({"if_trade"}, fill_policy::value, {1}); |
| 96 | +} |
| 97 | + |
| 98 | +MyDataFramePtr Xdxr::impl::calc(const MyDataFramePtr df, FQ_TYPE fq) const |
| 99 | +{ |
| 100 | + auto lhs_code = df->get_column<string>("lhs.code"); |
| 101 | + auto rhs_code = df->get_column<string>("rhs.code"); |
| 102 | + auto open = df->get_column<double>("open"); |
| 103 | + auto close = df->get_column<double>("close"); |
| 104 | + auto high = df->get_column<double>("high"); |
| 105 | + auto low = df->get_column<double>("low"); |
| 106 | + auto vol = df->get_column<double>("vol"); |
| 107 | + auto fenhong = df->get_column<double>("fenhong"); |
| 108 | + auto peigu = df->get_column<double>("peigu"); |
| 109 | + auto peigujia = df->get_column<double>("peigujia"); |
| 110 | + auto songzhuangu = df->get_column<double>("songzhuangu"); |
| 111 | + |
| 112 | + xt::xarray<double> xopen = xt::adapt(open); |
| 113 | + xt::xarray<double> xclose = xt::adapt(close); |
| 114 | + xt::xarray<double> xhigh = xt::adapt(high); |
| 115 | + xt::xarray<double> xlow = xt::adapt(low); |
| 116 | + xt::xarray<double> xvol = xt::adapt(vol); |
| 117 | + xt::xarray<double> xfenhong = xt::adapt(fenhong); |
| 118 | + xt::xarray<double> xpeigu = xt::adapt(peigu); |
| 119 | + xt::xarray<double> xpeigujia = xt::adapt(peigujia); |
| 120 | + xt::xarray<double> xsongzhuangu = xt::adapt(songzhuangu); |
| 121 | + |
| 122 | + // TODO: It's possible that xopen, xclose, etc., are empty, it's better to try catch |
| 123 | + auto xclose_sf1 = xt::roll(xclose, 1); |
| 124 | + *xclose_sf1.begin() = std::numeric_limits<double>::quiet_NaN(); |
| 125 | + xt::xarray<double> xpreclose = (xclose_sf1 * 10 - xfenhong + xpeigu * xpeigujia) / (10 + xpeigu + xsongzhuangu); |
| 126 | + |
| 127 | + xt::xarray<double> adj = {}; |
| 128 | + if (fq == FQ_TYPE::PRE) { |
| 129 | + // todo: may be need to fill the head or tail with a pre/post value, instead of NAN |
| 130 | + adj = xt::roll(xpreclose, -1); |
| 131 | + *adj.rbegin() = std::numeric_limits<double>::quiet_NaN(); |
| 132 | + adj = xt::eval(adj / xclose); |
| 133 | + // fillna(1) |
| 134 | + xfillna<double>(adj, 1); |
| 135 | + // minic pandas cumprod, flip first |
| 136 | + adj = xt::flip(adj, 0); |
| 137 | + adj = xt::nancumprod(adj); |
| 138 | + // minic pandas cumprod, need flip again |
| 139 | + adj = xt::flip(adj, 0); |
| 140 | + } else { |
| 141 | + // todo: may be need to fill the head or tail with a pre/post value, instead of NAN |
| 142 | + adj = xt::roll(xpreclose, -1); |
| 143 | + *adj.rbegin() = std::numeric_limits<double>::quiet_NaN(); |
| 144 | + adj = xt::eval(xclose / adj); |
| 145 | + adj = xt::nancumprod(adj); |
| 146 | + // shift(1) |
| 147 | + adj = xt::roll(adj, 1); |
| 148 | + *adj.begin() = std::numeric_limits<double>::quiet_NaN(); |
| 149 | + // fillna(1) |
| 150 | + xfillna<double>(adj, 1); |
| 151 | + } |
| 152 | + xopen = xopen * adj; |
| 153 | + xclose = xclose * adj; |
| 154 | + xhigh = xhigh * adj; |
| 155 | + xlow = xlow * adj; |
| 156 | + xpreclose = xpreclose * adj; |
| 157 | + xvol = xvol / adj; |
| 158 | + |
| 159 | + df->load_column<double>("open", {xopen.begin(), xopen.end()}, nan_policy::pad_with_nans); |
| 160 | + df->load_column<double>("high", {xhigh.begin(), xhigh.end()}, nan_policy::pad_with_nans); |
| 161 | + df->load_column<double>("close", {xclose.begin(), xclose.end()}, nan_policy::pad_with_nans); |
| 162 | + df->load_column<double>("low", {xlow.begin(), xlow.end()}, nan_policy::pad_with_nans); |
| 163 | + df->load_column<double>("preclose", {xpreclose.begin(), xpreclose.end()}, nan_policy::pad_with_nans); |
| 164 | + df->load_column<double>("vol", {xvol.begin(), xvol.end()}, nan_policy::pad_with_nans); |
| 165 | + df->load_column<double>("adj", {adj.begin(), adj.end()}, nan_policy::pad_with_nans); |
| 166 | + |
| 167 | + df->remove_column("fenhong"); |
| 168 | + df->remove_column("peigu"); |
| 169 | + df->remove_column("peigujia"); |
| 170 | + df->remove_column("songzhuangu"); |
| 171 | + df->remove_column("suogu"); |
| 172 | + df->remove_column("liquidity_before"); |
| 173 | + df->remove_column("liquidity_after"); |
| 174 | + |
| 175 | + auto functor = [](const std::string&, const int& if_trade, const double& open) -> bool { |
| 176 | + return (if_trade == 1 && open != 0); |
| 177 | + }; |
| 178 | + auto res = df->get_data_by_sel<double, double, decltype(functor), double, std::string>("if_trade", "open", functor); |
| 179 | + return std::make_shared<MyDataFrame>(res); |
| 180 | +} |
| 181 | + |
| 182 | +} // namespace abq |
0 commit comments