Skip to content

Commit

Permalink
cxx, stockday refactoring with pimpl pattern.
Browse files Browse the repository at this point in the history
  • Loading branch information
yssource committed Jan 27, 2021
1 parent fe43029 commit 57863a2
Show file tree
Hide file tree
Showing 9 changed files with 253 additions and 163 deletions.
1 change: 1 addition & 0 deletions cxx/include/abquant/actions/stockday_p.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
#include "../../../src/actions/stockday_p.hpp"
2 changes: 1 addition & 1 deletion cxx/include/abquant/headers.pri
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
# HEADER_CLASSES += ../include/abquant/helpers/applicationhelper.h
# HEADER_CLASSES += ../include/abquant/models/indexday.h ../include/abquant/models/stockday.h ../include/abquant/models/stockmin.h ../include/abquant/models/stockxdxr.h ../include/abquant/models/mongoobjects/indexdayobject.h ../include/abquant/models/mongoobjects/stockdayobject.h ../include/abquant/models/mongoobjects/stockminobject.h ../include/abquant/models/mongoobjects/stockxdxrobject.h

HEADER_FILES = ./actions/emconcept.hpp ./actions/emconceptbase.hpp ./actions/emconcepthistory.hpp ./actions/index.hpp ./actions/indexday.hpp ./actions/indexmin.hpp ./actions/stock.hpp ./actions/stockday.hpp ./actions/stockmin.hpp ./actions/stockxdxr.hpp ./actions/xdxr.hpp ./actions/indicator.hpp
HEADER_FILES = ./actions/emconcept.hpp ./actions/emconceptbase.hpp ./actions/emconcepthistory.hpp ./actions/index.hpp ./actions/indexday.hpp ./actions/indexmin.hpp ./actions/stock.hpp ./actions/stockday.hpp ./actions/stockday_p.hpp ./actions/stockmin.hpp ./actions/stockxdxr.hpp ./actions/xdxr.hpp ./actions/indicator.hpp
HEADER_FILES += ./controllers/applicationcontroller.h ./controllers/emconceptbasecontroller.h ./controllers/emconcepthistorycontroller.h ./controllers/indexdaycontroller.h ./controllers/indexmincontroller.h ./controllers/stockdaycontroller.h ./controllers/stockmincontroller.h ./controllers/stockxdxrcontroller.h
HEADER_FILES += ./helpers/applicationhelper.h
HEADER_FILES += ./models/emconceptbase.h ./models/emconcepthistory.h ./models/indexday.h ./models/indexmin.h ./models/stockday.h ./models/stockmin.h ./models/stockxdxr.h ./models/mongoobjects/emconceptbaseobject.h ./models/mongoobjects/emconcepthistoryobject.h ./models/mongoobjects/indexdayobject.h ./models/mongoobjects/indexminobject.h ./models/mongoobjects/stockdayobject.h ./models/mongoobjects/stockminobject.h ./models/mongoobjects/stockxdxrobject.h
1 change: 1 addition & 0 deletions cxx/src/actions/actions.pro
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ HEADERS += indexday.hpp
HEADERS += indexmin.hpp
HEADERS += stock.hpp
HEADERS += stockday.hpp
HEADERS += stockday_p.hpp
HEADERS += stockmin.hpp
HEADERS += stockxdxr.hpp
HEADERS += xdxr.hpp
Expand Down
49 changes: 22 additions & 27 deletions cxx/src/actions/stock.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,18 +33,25 @@

namespace abq
{
using namespace hmdf;
using namespace std;
using index_t = std::string;
using MyDataFrame = StdDataFrame<index_t>;
using namespace hmdf;
using index_t = std::string;
using MyDataFrame = StdDataFrame<index_t>;
using MyDataFramePtr = std::shared_ptr<MyDataFrame>;

/***************************
* StockAction declaration *
***************************/
using xseries_cst_t = const xt::xarray<double>&;
using xseries_t = xt::xarray<double>&;
using series_cst_t = const std::vector<double>&;
using series_t = std::vector<double>&;
using xseries_no_cvr_t = std::decay<xseries_t>::type;
using series_no_cvr_t = std::decay<series_t>::type;

template <class SA>
class Indicator;

/***************************
* StockAction declaration *
***************************/
template <class SA>
class StockAction : public TActionContext
{
Expand All @@ -54,12 +61,11 @@ class StockAction : public TActionContext

public:
// derived_type &derived_cast() & noexcept;

derived_type* derived_cast() & noexcept;
const derived_type& derived_cast() const& noexcept;
derived_type derived_cast() && noexcept;

// TODO: friend class Indicator<S, derived_type, StockAction>
QStringList getCodes() const;
Indicator<derived_type> makeIndicator();

protected:
Expand Down Expand Up @@ -87,8 +93,6 @@ class StockAction : public TActionContext
template <typename T>
QVector<T> toSeries(const char*) const noexcept;

MyDataFrame toFq();
MyDataFrame toDataFrame();
template <typename S>
QList<S> run(const QStringList codes, const char* start, const char* end);
template <typename S>
Expand All @@ -107,19 +111,16 @@ class StockAction : public TActionContext
private:
friend SA;
friend class Indicator<self_type>;
QStringList m_codes;
const char* m_start;
const char* m_end;
};

/******************************
* StockAction implementation *
******************************/

template <class SA>
StockAction<SA>::StockAction(QStringList codes) : m_codes{codes}
{
}
// template <class SA>
// StockAction<SA>::StockAction(QStringList codes) : m_codes{codes}
// {
// }

// since: /usr/include/treefrog/tactioncontext.h:59:20: note: 'TActionContext'
// has been explicitly marked deleted here
Expand All @@ -138,7 +139,7 @@ StockAction<SA>::StockAction(QStringList codes) : m_codes{codes}
* Returns a pointer to the actual derived type of the StockAction.
*/
template <class SA>
inline auto StockAction<SA>::derived_cast() & noexcept -> derived_type*
inline auto StockAction<SA>::derived_cast() & noexcept -> derived_type*
{
return static_cast<derived_type*>(this);
}
Expand All @@ -147,7 +148,7 @@ template <class SA>
* Returns a constant reference to the actual derived type of the StockAction.
*/
template <class SA>
inline auto StockAction<SA>::derived_cast() const & noexcept -> const derived_type&
inline auto StockAction<SA>::derived_cast() const& noexcept -> const derived_type&
{
return *static_cast<const derived_type*>(this);
}
Expand All @@ -156,7 +157,7 @@ template <class SA>
* Returns a constant reference to the actual derived type of the StockAction.
*/
template <class SA>
inline auto StockAction<SA>::derived_cast() && noexcept -> derived_type
inline auto StockAction<SA>::derived_cast() && noexcept -> derived_type
{
return *static_cast<derived_type*>(this);
}
Expand Down Expand Up @@ -208,7 +209,7 @@ QList<S> StockAction<SA>::get_price(const QStringList codes, const char* start,
auto sa = derived_cast();
TDatabaseContext::setCurrentDatabaseContext(sa);
bool EnableTransactions = true;
setTransactionEnabled(EnableTransactions);
TDatabaseContext::setTransactionEnabled(EnableTransactions);
QList<S> stocks = S::get_price(codes, start_d, end_d);
commitTransactions();
return stocks;
Expand Down Expand Up @@ -242,12 +243,6 @@ QList<S> StockAction<SA>::get_price(const QStringList codes, int category)
return stocks;
}

template <class SA>
QStringList StockAction<SA>::getCodes() const
{
return m_codes;
}

template <class SA>
inline auto StockAction<SA>::getStocks()
{
Expand Down
134 changes: 82 additions & 52 deletions cxx/src/actions/stockday.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,33 +7,86 @@
* The full license is in the file LICENSE, distributed with this software. *
****************************************************************************/

#include "abquant/actions/stockday.hpp"
#include "abquant/actions/stockday_p.hpp"

namespace abq
{
StockDayAction::StockDayAction(QStringList codes, const char* start, const char* end, FQ_TYPE xdxr)
: StockAction(codes), m_codes{codes}, m_start{start}, m_end{end}, m_xdxr{xdxr}
: pImpl{std::make_shared<impl>(*this, codes, start, end, xdxr)}, m_xdxr{xdxr}
{
m_stockdays = run<StockDay>(codes, start, end);
if (m_stockdays.isEmpty()) {
qDebug() << "No stock day data.\n"
<< codes << "\n"
<< "start: " << start << "\n"
<< "end: " << end << "\n";
}

MyDataFramePtr StockDayAction::impl::getDataFrame(const StockDayAction& sa) const
{
// m_df->template write<std::ostream, index_t, double, int>(std::cout);
return m_df;
}

MyDataFramePtr StockDayAction::getDataFrame() const { return pImpl->getDataFrame(*this); }

MyDataFramePtr StockDayAction::impl::toFq(const StockDayAction& sa, FQ_TYPE fq)
{
// qDebug() << fq << "\n";
auto x = Xdxr<StockDayAction>(sa);
if (fq == FQ_TYPE::NONE || (m_df && !m_df->get_index().size())) {
return m_df;
}
m_df = x.getXdxr(m_df, fq);
// m_df->template write<std::ostream, index_t, double, int>(std::cout);
return m_df;
}

const MyDataFrame StockDayAction::toFq(FQ_TYPE fq) const
MyDataFramePtr StockDayAction::toFq(FQ_TYPE fq) { return pImpl->toFq(*this, fq); }

series_no_cvr_t StockDayAction::impl::getOpen(const StockDayAction& sa) const
{
auto x = Xdxr<StockDayAction>(*this);
auto df = toDataFrame();
if (fq == FQ_TYPE::NONE || !df.get_index().size()) {
return df;
series_no_cvr_t open;
if (m_df != nullptr) {
try {
// df->write<std::ostream, std::string, double, int>(std::cout);
open = m_df->template get_column<double>("open");
} catch (const std::exception& e) {
std::cout << e.what();
}
}
return x.getXdxr(df, fq);
return open;
}

MyDataFrame StockDayAction::toDataFrame() const
series_no_cvr_t StockDayAction::getOpen() const { return pImpl->getOpen(*this); }

// void StockDayAction::impl::getName(const StockDayAction& sa) const
// {
// if (m_name == nullptr) {
// }
// std::cout << *(m_name.get()) << "\n";
// // return m_name;
// }

// void StockDayAction::getName() const { return pImpl->getName(*this); }

QStringList StockDayAction::getCodes() const { return pImpl->getCodes(*this); }

QStringList StockDayAction::impl::getCodes(const StockDayAction& sa) const { return m_codes; }

inline QList<StockDay> StockDayAction::getStocks() const { return pImpl->getStocks(*this); };

QVector<const char*> StockDayAction::getColumns() const { return pImpl->getColumns(*this); }

//! Destructor
StockDayAction::~StockDayAction() noexcept = default;

//! Move assignment operator

StockDayAction& StockDayAction::operator=(StockDayAction&& other) noexcept
{
if (&other == this) {
return *this;
}
swap(pImpl, other.pImpl);
return *this;
};

void StockDayAction::impl::setDataFrame(const StockDayAction& sa)
{
MyDataFrame df;
try {
Expand All @@ -47,7 +100,7 @@ MyDataFrame StockDayAction::toDataFrame() const
// code" : "000001",
// date_stamp" : 670608000.0

std::vector<std::string> datetimeCodeIdx;
std::vector<index_t> datetimeCodeIdx;
std::vector<double> open;
std::vector<double> close;
std::vector<double> high;
Expand All @@ -73,56 +126,33 @@ MyDataFrame StockDayAction::toDataFrame() const
if_trade.push_back(1);
}

int rc =
df.load_data(std::move(datetimeCodeIdx), std::make_pair("open", open), std::make_pair("close", close),
std::make_pair("high", high), std::make_pair("low", low), std::make_pair("vol", vol),
std::make_pair("amount", amount), std::make_pair("date", date), std::make_pair("code", code),
std::make_pair("date_stamp", date_stamp), std::make_pair("if_trade", if_trade));
df.load_data(std::move(datetimeCodeIdx), std::make_pair("open", open), std::make_pair("close", close),
std::make_pair("high", high), std::make_pair("low", low), std::make_pair("vol", vol),
std::make_pair("amount", amount), std::make_pair("date", date), std::make_pair("code", code),
std::make_pair("date_stamp", date_stamp), std::make_pair("if_trade", if_trade));

// df.write<std::ostream, std::string, double, int>(std::cout);
// df.template write<std::ostream, index_t, double, int>(std::cout);
} catch (exception& e) {
cout << e.what() << endl;
}
return df;
};

std::shared_ptr<MyDataFrame> StockDayAction::getDataFrame() const { return m_df; }

vector<double> StockDayAction::getOpen() const
{
vector<double> open;
if (m_df != nullptr) {
try {
// m_df->write<std::ostream, std::string, double, int>(std::cout);
open = m_df->template get_column<double>("open");
} catch (const std::exception& e) {
std::cout << e.what();
}
}
return open;
}
// m_df = std::make_shared<MyDataFrame>(std::move(df));
m_df = std::make_shared<MyDataFrame>(df);
};

void StockDayAction::setDataFrame()
{
MyDataFrame df = toFq(m_xdxr);
m_df = std::make_shared<MyDataFrame>(df);
// m_df->write<std::ostream, std::string, double, int>(std::cout);
}
series_no_cvr_t StockDayAction::get_pyseries(const char* col) const noexcept { return pImpl->get_pyseries(*this, col); }

vector<double> StockDayAction::get_pyseries(const char* col) const noexcept
series_no_cvr_t StockDayAction::impl::get_pyseries(const StockDayAction& sa, const char* col) const noexcept
{
vector<double> series;
auto cols = getColumns();
auto cols = getColumns(sa);
if (std::none_of(cols.cbegin(), cols.cend(), [col](const char* c) { return QString(c) == QString(col); })) {
return series;
}

if (m_xdxr == FQ_TYPE::PRE || m_xdxr == FQ_TYPE::POST) {
std::shared_ptr<MyDataFrame> df;

try {
df = getDataFrame();
// df->write<std::ostream, std::string, double, int>(std::cout);
// m_df->write<std::ostream, index_t, double, int>(std::cout);
const char* colname;
if (QString(col) == QString("code")) {
colname = "lhs.code";
Expand All @@ -131,7 +161,7 @@ vector<double> StockDayAction::get_pyseries(const char* col) const noexcept
} else {
colname = col;
}
series = df->get_column<double>(colname);
series = m_df->get_column<double>(colname);
} catch (...) {
std::cout << " error ... "
<< "\n";
Expand Down
Loading

0 comments on commit 57863a2

Please sign in to comment.