Skip to content

Commit b575019

Browse files
committed
cxx, xdxr impl pattern
1 parent 6c4435b commit b575019

File tree

12 files changed

+239
-189
lines changed

12 files changed

+239
-189
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
#include "../../../src/actions/xdxr_p.hpp"

cxx/src/actions/abquant.hpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,6 @@ class Abquant
156156
return inst;
157157
}
158158
static void finish() { _exit(0); }
159-
void hello() { return; }
160159

161160
private:
162161
Abquant(){ABQ_OPEN_MAIN()};

cxx/src/actions/actions.pro

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ HEADERS += stockday_p.hpp
3131
HEADERS += stockmin.hpp
3232
HEADERS += stockxdxr.hpp
3333
HEADERS += xdxr.hpp
34+
HEADERS += xdxr_p.hpp
3435
HEADERS += indicator.hpp
3536
HEADERS += utils.hpp
3637

@@ -41,4 +42,5 @@ SOURCES += indexmin.cpp
4142
SOURCES += stockday.cpp
4243
SOURCES += stockmin.cpp
4344
SOURCES += stockxdxr.cpp
45+
SOURCES += xdxr.cpp
4446
SOURCES += indicator.cpp

cxx/src/actions/index.hpp

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ namespace abq
3838
{
3939
using namespace hmdf;
4040
using namespace std;
41-
using index_t = std::string;
41+
using index_t = std::string;
4242
using MyDataFrame = StdDataFrame<index_t>;
4343

4444
/***************************
@@ -125,15 +125,6 @@ IndexAction<IA>::IndexAction(QStringList codes) : m_codes{codes}
125125
{
126126
}
127127

128-
// since: /usr/include/treefrog/tactioncontext.h:59:20: note: 'TActionContext'
129-
// has been explicitly marked deleted here
130-
// T_DIIABLE_COPY(TActionContext)
131-
// workaround here, IA*
132-
// template <class IA>
133-
// inline auto IndexAction<IA>::derived_cast() & noexcept -> derived_type & {
134-
// return *static_cast<derived_type *>(this);
135-
// }
136-
137128
/**
138129
* @name Downcast functions
139130
*/

cxx/src/actions/indicator.hpp

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -82,9 +82,6 @@ class Indicator : public std::enable_shared_from_this<Indicator<A>>
8282

8383
~Indicator() noexcept = default;
8484

85-
template <typename T>
86-
void hello(T h);
87-
8885
// 威廉 SMA 算法
8986
xt::xarray<double> SMA(xseries_cst_t series, size_t N, size_t M = 1) const;
9087
xt::xarray<double> REF(xseries_cst_t series, int N) const;
@@ -102,7 +99,6 @@ class Indicator : public std::enable_shared_from_this<Indicator<A>>
10299
// void KDJ(size_t, size_t, size_t);
103100

104101
private:
105-
const char* m_hello{"hello world!"};
106102
const A* m_a;
107103
};
108104

@@ -120,14 +116,6 @@ Indicator<A>::Indicator(const A* a) : m_a(std::move(a))
120116
}
121117
}
122118

123-
template <typename A>
124-
template <typename T>
125-
void Indicator<A>::hello(T h)
126-
{
127-
qDebug() << m_hello << " "
128-
<< "\n";
129-
}
130-
131119
template <typename A>
132120
xt::xarray<double> Indicator<A>::SMA(xseries_cst_t series, size_t N, size_t M) const
133121
{

cxx/src/actions/stockday.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
* The full license is in the file LICENSE, distributed with this software. *
88
****************************************************************************/
99

10+
#include "abquant/actions/xdxr.hpp"
1011
#include "abquant/actions/stockday_p.hpp"
1112

1213
namespace abq
@@ -76,7 +77,7 @@ MyDataFramePtr StockDayAction::impl::getDataFrame(const StockDayAction&) const
7677

7778
MyDataFramePtr StockDayAction::impl::toFq(const StockDayAction& sa, FQ_TYPE fq)
7879
{
79-
auto x = Xdxr<StockDayAction>(sa);
80+
auto x = Xdxr(sa);
8081
if (fq == FQ_TYPE::NONE || (m_df && !m_df->get_index().size())) {
8182
return m_df;
8283
}

cxx/src/actions/stockday.hpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515

1616
#include "abquant/actions/stock.hpp"
1717
#include "abquant/actions/utils.hpp"
18-
#include "abquant/actions/xdxr.hpp"
1918
#include "abquant/models/stockday.h"
2019

2120
namespace abq

cxx/src/actions/stockmin.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
* The full license is in the file LICENSE, distributed with this software. *
88
****************************************************************************/
99

10+
#include "abquant/actions/xdxr.hpp"
1011
#include "abquant/actions/stockmin_p.hpp"
1112

1213
namespace abq
@@ -75,7 +76,7 @@ MyDataFramePtr StockMinAction::impl::getDataFrame(const StockMinAction&) const
7576

7677
MyDataFramePtr StockMinAction::impl::toFq(const StockMinAction& sa, FQ_TYPE fq)
7778
{
78-
auto x = Xdxr<StockMinAction>(sa);
79+
auto x = Xdxr(sa);
7980
if (fq == FQ_TYPE::NONE || (m_df && !m_df->get_index().size())) {
8081
return m_df;
8182
}

cxx/src/actions/stockmin.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515

1616
#include "abquant/actions/stock.hpp"
1717
#include "abquant/actions/utils.hpp"
18-
#include "abquant/actions/xdxr.hpp"
18+
// #include "abquant/actions/xdxr.hpp"
1919
#include "abquant/models/stockmin.h"
2020

2121
namespace abq

cxx/src/actions/xdxr.cpp

Lines changed: 182 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,182 @@
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

Comments
 (0)