Skip to content

Commit

Permalink
cxx, etf list
Browse files Browse the repository at this point in the history
  • Loading branch information
yssource committed May 5, 2023
1 parent cf4d809 commit 1c7ab45
Show file tree
Hide file tree
Showing 8 changed files with 323 additions and 9 deletions.
12 changes: 12 additions & 0 deletions binding/securitylist/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,15 @@
#include "abquant/actions/abquant.hpp"
#include "abquant/actions/securitylist.hpp"
#include "abquant/actions/utils.hpp"
#include "abquant/models/etflist.h"
#include "abquant/models/indexlist.h"
#include "abquant/models/stocklist.h"

namespace abq
{
using StockListActionPtr = std::shared_ptr<SecurityListAction<StockList>>;
using IndexListActionPtr = std::shared_ptr<SecurityListAction<IndexList>>;
using EtfListActionPtr = std::shared_ptr<SecurityListAction<EtfList>>;

class PySecurityList : public std::enable_shared_from_this<PySecurityList>
{
Expand All @@ -32,6 +34,9 @@ class PySecurityList : public std::enable_shared_from_this<PySecurityList>
if (ins_type == INSTRUMENT_TYPE::INDX) {
m_ila_ptr = std::make_shared<SecurityListAction<IndexList>>();
}
if (ins_type == INSTRUMENT_TYPE::ETF) {
m_ela_ptr = std::make_shared<SecurityListAction<EtfList>>();
}
};

PySecurityList(std::vector<std::string> codes, const string& end = "",
Expand All @@ -48,6 +53,9 @@ class PySecurityList : public std::enable_shared_from_this<PySecurityList>
if (ins_type == INSTRUMENT_TYPE::INDX) {
m_ila_ptr = std::make_shared<SecurityListAction<IndexList>>(qcodes, end.c_str());
}
if (ins_type == INSTRUMENT_TYPE::ETF) {
m_ela_ptr = std::make_shared<SecurityListAction<EtfList>>(qcodes, end.c_str());
}
};

template <class T>
Expand All @@ -60,6 +68,9 @@ class PySecurityList : public std::enable_shared_from_this<PySecurityList>
if (m_ins_type == INSTRUMENT_TYPE::INDX) {
series = m_ila_ptr->to_series<T>(col.c_str());
}
if (m_ins_type == INSTRUMENT_TYPE::ETF) {
series = m_ela_ptr->to_series<T>(col.c_str());
}
return series.toStdVector();
}

Expand All @@ -71,6 +82,7 @@ class PySecurityList : public std::enable_shared_from_this<PySecurityList>
INSTRUMENT_TYPE m_ins_type;
StockListActionPtr m_sla_ptr{nullptr};
IndexListActionPtr m_ila_ptr{nullptr};
EtfListActionPtr m_ela_ptr{nullptr};
};

namespace py = pybind11;
Expand Down
1 change: 1 addition & 0 deletions cxx/include/abquant/models/etflist.h
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
#include "../../../src/models/etflist.h"
1 change: 1 addition & 0 deletions cxx/include/abquant/models/mongoobjects/etflistobject.h
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
#include "../../../../src/models/mongoobjects/etflistobject.h"
158 changes: 158 additions & 0 deletions cxx/src/models/etflist.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
#include "abquant/models/etflist.h"

#include <TreeFrogModel>

#include "abquant/models/mongoobjects/etflistobject.h"
EtfList::EtfList() : TAbstractModel(), d(new EtfListObject())
{
d->volunit = 0;
d->decimal_point = 0;
d->pre_close = 0;
}

EtfList::EtfList(const EtfList& other) : TAbstractModel(), d(other.d) {}

EtfList::EtfList(const EtfListObject& object) : TAbstractModel(), d(new EtfListObject(object)) {}

EtfList::~EtfList()
{
// If the reference count becomes 0,
// the shared data object 'EtfListObject' is deleted.
}

QString EtfList::id() const { return d->_id; }

QString EtfList::code() const { return d->code; }

void EtfList::setCode(const QString& code) { d->code = code; }

int EtfList::volunit() const { return d->volunit; }

void EtfList::setVolunit(int volunit) { d->volunit = volunit; }

int EtfList::decimalPoint() const { return d->decimal_point; }

void EtfList::setDecimalPoint(int decimal_point) { d->decimal_point = decimal_point; }

QString EtfList::name() const { return d->name; }

void EtfList::setName(const QString& name) { d->name = name; }

double EtfList::preClose() const { return d->pre_close; }

void EtfList::setPreClose(double pre_close) { d->pre_close = pre_close; }

QString EtfList::sse() const { return d->sse; }

void EtfList::setSse(const QString& sse) { d->sse = sse; }

QString EtfList::sec() const { return d->sec; }

void EtfList::setSec(const QString& sec) { d->sec = sec; }

EtfList& EtfList::operator=(const EtfList& other)
{
d = other.d; // increments the reference count of the data
return *this;
}

bool EtfList::upsert(const QVariantMap& criteria)
{
auto* obj = dynamic_cast<TMongoObject*>(modelData());
return (obj) ? obj->upsert(criteria) : false;
}

EtfList EtfList::create(const QString& code, int volunit, int decimal_point, const QString& name, double pre_close,
const QString& sse, const QString& sec)
{
EtfListObject obj;
obj.code = code;
obj.volunit = volunit;
obj.decimal_point = decimal_point;
obj.name = name;
obj.pre_close = pre_close;
obj.sse = sse;
obj.sec = sec;
if (!obj.create()) {
return EtfList();
}
return EtfList(obj);
}

EtfList EtfList::create(const QVariantMap& values)
{
EtfList model;
model.setProperties(values);
if (!model.d->create()) {
model.d->clear();
}
return model;
}

EtfList EtfList::get(const QString& id)
{
TMongoODMapper<EtfListObject> mapper;
return EtfList(mapper.findByObjectId(id));
}

QList<EtfList> EtfList::get_all_securities(double end)
{
return tfGetModelListByMongoCriteria<EtfList, EtfListObject>(TCriteria());
}

QList<EtfList> EtfList::get_all_securities(const QStringList codes, double end)
{
TMongoODMapper<EtfListObject> mapper;
TCriteria cri;
// QDateTime start_ = QDateTime::fromString(start, Qt::ISODate);
cri.add(EtfListObject::Code, TMongo::In,
codes); // AND add to the end operator

// QDateTime end_ = QDateTime::fromString(end, Qt::ISODate);
// cri.add(EtfListObject::DateStamp, TMongo::LessEqual,
// end); // AND add to the end operator
return tfGetModelListByMongoCriteria<EtfList, EtfListObject>(cri);
}

int EtfList::count()
{
TMongoODMapper<EtfListObject> mapper;
return mapper.findCount();
}

QList<EtfList> EtfList::getAll() { return tfGetModelListByMongoCriteria<EtfList, EtfListObject>(TCriteria()); }

QJsonArray EtfList::getAllJson()
{
QJsonArray array;
TMongoODMapper<EtfListObject> mapper;

if (mapper.find()) {
while (mapper.next()) {
array.append(QJsonValue(QJsonObject::fromVariantMap(EtfList(mapper.value()).toVariantMap())));
}
}
return array;
}

TModelObject* EtfList::modelData() { return d.data(); }

const TModelObject* EtfList::modelData() const { return d.data(); }

QDataStream& operator<<(QDataStream& ds, const EtfList& model)
{
auto varmap = model.toVariantMap();
ds << varmap;
return ds;
}

QDataStream& operator>>(QDataStream& ds, EtfList& model)
{
QVariantMap varmap;
ds >> varmap;
model.setProperties(varmap);
return ds;
}

// Don't remove below this line
T_REGISTER_STREAM_OPERATORS(EtfList)
68 changes: 68 additions & 0 deletions cxx/src/models/etflist.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
#ifndef ETFLIST_H
#define ETFLIST_H

#include <QDateTime>
#include <QSharedDataPointer>
#include <QStringList>
#include <QVariant>
#include <TAbstractModel>
#include <TGlobal>

class TModelObject;
class EtfListObject;
class QJsonArray;

class T_MODEL_EXPORT EtfList : public TAbstractModel
{
public:
EtfList();
EtfList(const EtfList& other);
EtfList(const EtfListObject& object);
~EtfList();

QString id() const;
QString code() const;
void setCode(const QString& code);
int volunit() const;
void setVolunit(int volunit);
int decimalPoint() const;
void setDecimalPoint(int decimalPoint);
QString name() const;
void setName(const QString& name);
double preClose() const;
void setPreClose(double preClose);
QString sse() const;
void setSse(const QString& sse);
QString sec() const;
void setSec(const QString& sec);
EtfList& operator=(const EtfList& other);

bool create() override { return TAbstractModel::create(); }
bool update() override { return TAbstractModel::update(); }
bool upsert(const QVariantMap& criteria);
bool save() override { return TAbstractModel::save(); }
bool remove() override { return TAbstractModel::remove(); }

static EtfList create(const QString& code, int volunit, int decimalPoint, const QString& name, double preClose,
const QString& sse, const QString& sec);
static EtfList create(const QVariantMap& values);
static EtfList get(const QString& id);
static QList<EtfList> get_all_securities(double end = 0);
static QList<EtfList> get_all_securities(const QStringList codes, double end = 0);
static int count();
static QList<EtfList> getAll();
static QJsonArray getAllJson();

private:
QSharedDataPointer<EtfListObject> d;

TModelObject* modelData() override;
const TModelObject* modelData() const override;
friend QDataStream& operator<<(QDataStream& ds, const EtfList& model);
friend QDataStream& operator>>(QDataStream& ds, EtfList& model);
};

Q_DECLARE_METATYPE(EtfList)
Q_DECLARE_METATYPE(QList<EtfList>)

#endif // ETFLIST_H
3 changes: 3 additions & 0 deletions cxx/src/models/models.pro
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,6 @@ SOURCES += stockmin.cpp
HEADERS += mongoobjects/stockxdxrobject.h
HEADERS += stockxdxr.h
SOURCES += stockxdxr.cpp
HEADERS += mongoobjects/etflistobject.h
HEADERS += etflist.h
SOURCES += etflist.cpp
54 changes: 54 additions & 0 deletions cxx/src/models/mongoobjects/etflistobject.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
#ifndef ETFLISTOBJECT_H
#define ETFLISTOBJECT_H

#include <QSharedData>
#include <TMongoObject>

class T_MODEL_EXPORT EtfListObject : public TMongoObject, public QSharedData
{
public:
QString _id;
QString code;
int volunit;
int decimal_point;
QString name;
double pre_close;
QString sse;
QString sec;

enum PropertyEtf {
Id = 0,
Code,
Volunit,
DecimalPoint,
Name,
PreClose,
Sse,
Sec,
};

virtual QString collectionName() const override { return QStringLiteral("etf_list"); }
virtual QString objectId() const override { return _id; }
virtual QString& objectId() override { return _id; }

private:
Q_OBJECT
Q_PROPERTY(QString _id READ get_id WRITE set_id)
T_DEFINE_PROPERTY(QString, _id)
Q_PROPERTY(QString code READ getcode WRITE setcode)
T_DEFINE_PROPERTY(QString, code)
Q_PROPERTY(int volunit READ getvolunit WRITE setvolunit)
T_DEFINE_PROPERTY(int, volunit)
Q_PROPERTY(int decimal_point READ getdecimal_point WRITE setdecimal_point)
T_DEFINE_PROPERTY(int, decimal_point)
Q_PROPERTY(QString name READ getname WRITE setname)
T_DEFINE_PROPERTY(QString, name)
Q_PROPERTY(double pre_close READ getpre_close WRITE setpre_close)
T_DEFINE_PROPERTY(double, pre_close)
Q_PROPERTY(QString sse READ getsse WRITE setsse)
T_DEFINE_PROPERTY(QString, sse)
Q_PROPERTY(QString sec READ getsec WRITE setsec)
T_DEFINE_PROPERTY(QString, sec)
};

#endif // ETFLISTOBJECT_H
Loading

0 comments on commit 1c7ab45

Please sign in to comment.