Skip to content

Commit

Permalink
[WIP] cxx, easymoney concept history
Browse files Browse the repository at this point in the history
  • Loading branch information
yssource committed Nov 29, 2020
1 parent ba2884b commit ec4eb9f
Show file tree
Hide file tree
Showing 13 changed files with 503 additions and 4 deletions.
6 changes: 5 additions & 1 deletion README.org
Original file line number Diff line number Diff line change
Expand Up @@ -248,9 +248,13 @@ abquant 分为三大模块 a) 数据获取 b) 策略回测 c) 模拟/实盘交

*** plotting
[[file:./screenshot/abqstockday.png]]
* TODO TODO: 兼容 rqalpha apis
* TODO TODO:
- 兼容 rqalpha apis ::
1. [x] get_price()
2. [x] get_fundamentals()[fn:1]
- 东方财富 概念 apis ::
1. [x] easymoney concept base get_blocks()
2. [x] easymoney concept history get_price()
* 开发初衷
作者本人专业是理论物理,工作经验则是程序开发,对金融知识知之甚少,一点点股票交易经
验。很偶然在网上了解到了量化交易,觉得可以利用上以前的知识和经验,于是乎喜欢上
Expand Down
1 change: 1 addition & 0 deletions cxx/include/abquant/actions/emconcepthistory.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
#include "../../../src/actions/emconcepthistory.hpp"
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
#include "../../../src/controllers/emconcepthistorycontroller.h"
6 changes: 3 additions & 3 deletions 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/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 += ./controllers/applicationcontroller.h ./controllers/emconceptbasecontroller.h ./controllers/indexdaycontroller.h ./controllers/indexmincontroller.h ./controllers/stockdaycontroller.h ./controllers/stockmincontroller.h ./controllers/stockxdxrcontroller.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 += ./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/indexday.h ./models/indexmin.h ./models/stockday.h ./models/stockmin.h ./models/stockxdxr.h ./models/mongoobjects/emconceptbaseobject.h ./models/mongoobjects/indexdayobject.h ./models/mongoobjects/indexminobject.h ./models/mongoobjects/stockdayobject.h ./models/mongoobjects/stockminobject.h ./models/mongoobjects/stockxdxrobject.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/include/abquant/models/emconcepthistory.h
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
#include "../../../src/models/emconcepthistory.h"
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
#include "../../../../src/models/mongoobjects/emconcepthistoryobject.h"
2 changes: 2 additions & 0 deletions cxx/src/controllers/controllers.pro
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ HEADERS += applicationcontroller.h
SOURCES += applicationcontroller.cpp
HEADERS += emconceptbasecontroller.h
SOURCES += emconceptbasecontroller.cpp
HEADERS += emconcepthistorycontroller.h
SOURCES += emconcepthistorycontroller.cpp
HEADERS += indexdaycontroller.h
SOURCES += indexdaycontroller.cpp
HEADERS += stockxdxrcontroller.h
Expand Down
109 changes: 109 additions & 0 deletions cxx/src/controllers/emconcepthistorycontroller.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
#include "abquant/controllers/emconcepthistorycontroller.h"

#include "abquant/models/emconcepthistory.h"

void EmConceptHistoryController::index()
{
auto emconcepthistoryList = EmConceptHistory::getAll();
texport(emconcepthistoryList);
render();
}

void EmConceptHistoryController::show(const QString& id)
{
auto emconcepthistory = EmConceptHistory::get(id);
texport(emconcepthistory);
render();
}

void EmConceptHistoryController::create()
{
switch (httpRequest().method()) {
case Tf::Get:
render();
break;

case Tf::Post: {
auto emconcepthistory = httpRequest().formItems("emconcepthistory");
auto model = EmConceptHistory::create(emconcepthistory);

if (!model.isNull()) {
QString notice = "Created successfully.";
tflash(notice);
redirect(urla("show", model.id()));
} else {
QString error = "Failed to create.";
texport(error);
texport(emconcepthistory);
render();
}
break;
}

default:
renderErrorResponse(Tf::NotFound);
break;
}
}

void EmConceptHistoryController::save(const QString& id)
{
switch (httpRequest().method()) {
case Tf::Get: {
auto model = EmConceptHistory::get(id);
if (!model.isNull()) {
auto emconcepthistory = model.toVariantMap();
texport(emconcepthistory);
render();
}
break;
}

case Tf::Post: {
QString error;
auto model = EmConceptHistory::get(id);

if (model.isNull()) {
error =
"Original data not found. It may have been updated/removed by another "
"transaction.";
tflash(error);
redirect(urla("save", id));
break;
}

auto emconcepthistory = httpRequest().formItems("emconcepthistory");
model.setProperties(emconcepthistory);
if (model.save()) {
QString notice = "Updated successfully.";
tflash(notice);
redirect(urla("show", model.id()));
} else {
error = "Failed to update.";
texport(error);
texport(emconcepthistory);
render();
}
break;
}

default:
renderErrorResponse(Tf::NotFound);
break;
}
}

void EmConceptHistoryController::remove(const QString& id)
{
if (httpRequest().method() != Tf::Post) {
renderErrorResponse(Tf::NotFound);
return;
}

auto emconcepthistory = EmConceptHistory::get(id);
emconcepthistory.remove();
redirect(urla("index"));
}

// Don't remove below this line
T_DEFINE_CONTROLLER(EmConceptHistoryController)
20 changes: 20 additions & 0 deletions cxx/src/controllers/emconcepthistorycontroller.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#ifndef EMCONCEPTHISTORYCONTROLLER_H
#define EMCONCEPTHISTORYCONTROLLER_H

#include "abquant/controllers/applicationcontroller.h"

class T_CONTROLLER_EXPORT EmConceptHistoryController : public ApplicationController
{
Q_OBJECT
public:
EmConceptHistoryController() : ApplicationController() {}

public slots:
void index();
void show(const QString& id);
void create();
void save(const QString& id);
void remove(const QString& id);
};

#endif // EMCONCEPTHISTORYCONTROLLER_H
199 changes: 199 additions & 0 deletions cxx/src/models/emconcepthistory.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,199 @@
#include "abquant/models/emconcepthistory.h"

#include <TreeFrogModel>

#include "abquant/models/mongoobjects/emconcepthistoryobject.h"

EmConceptHistory::EmConceptHistory() : TAbstractModel(), d(new EmConceptHistoryObject())
{
d->open = 0;
d->close = 0;
d->high = 0;
d->low = 0;
d->volume = 0;
d->amount = 0;
d->amplitude = 0;
d->rise_fall_pct = 0;
d->rise_fall_amt = 0;
d->turnover = 0;
d->date_stamp = 0;
}

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

EmConceptHistory::EmConceptHistory(const EmConceptHistoryObject& object)
: TAbstractModel(), d(new EmConceptHistoryObject(object))
{
}

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

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

QString EmConceptHistory::date() const { return d->date; }

void EmConceptHistory::setDate(const QString& date) { d->date = date; }

double EmConceptHistory::open() const { return d->open; }

void EmConceptHistory::setOpen(double open) { d->open = open; }

double EmConceptHistory::close() const { return d->close; }

void EmConceptHistory::setClose(double close) { d->close = close; }

double EmConceptHistory::high() const { return d->high; }

void EmConceptHistory::setHigh(double high) { d->high = high; }

double EmConceptHistory::low() const { return d->low; }

void EmConceptHistory::setLow(double low) { d->low = low; }

double EmConceptHistory::volume() const { return d->volume; }

void EmConceptHistory::setVolume(double volume) { d->volume = volume; }

double EmConceptHistory::amount() const { return d->amount; }

void EmConceptHistory::setAmount(double amount) { d->amount = amount; }

double EmConceptHistory::riseFallPct() const { return d->rise_fall_pct; }

void EmConceptHistory::setRiseFallPct(double riseFallPct) { d->rise_fall_pct = riseFallPct; }

double EmConceptHistory::riseFallAmt() const { return d->rise_fall_amt; }

void EmConceptHistory::setRiseFallAmt(double riseFallAmt) { d->rise_fall_amt = riseFallAmt; }

double EmConceptHistory::turnover() const { return d->turnover; }

void EmConceptHistory::setTurnover(double turnover) { d->turnover = turnover; }

QString EmConceptHistory::mktCode() const { return d->mkt_code; }

void EmConceptHistory::setMktCode(const QString& mktCode) { d->mkt_code = mktCode; }

double EmConceptHistory::dateStamp() const { return d->date_stamp; }

void EmConceptHistory::setDateStamp(double dateStamp) { d->date_stamp = dateStamp; }

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

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

EmConceptHistory EmConceptHistory::create(const QString& date, double open, double close, double high, double low,
double volume, double amount, double amplitude, double riseFallPct,
double riseFallAmt, double turnover, const QString& mktCode, double dateStamp)
{
EmConceptHistoryObject obj;
obj.date = date;
obj.open = open;
obj.close = close;
obj.high = high;
obj.low = low;
obj.volume = volume;
obj.amount = amount;
obj.amplitude = amplitude;
obj.rise_fall_pct = riseFallPct;
obj.rise_fall_amt = riseFallAmt;
obj.turnover = turnover;
obj.mkt_code = mktCode;
obj.date_stamp = dateStamp;
if (!obj.create()) {
return EmConceptHistory();
}
return EmConceptHistory(obj);
}

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

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

QList<EmConceptHistory> EmConceptHistory::get_price(const QStringList& codes, double start, double end)
{
TMongoODMapper<EmConceptHistoryObject> mapper;
// mapper.setSortOrder(EmConceptHistoryObject::Date, Tf::DescendingOrder);
TCriteria cri;
// QDateTime start_ = QDateTime::fromString(start, Qt::ISODate);
cri.add(EmConceptHistoryObject::MktCode, TMongo::In,
codes); // AND add to the end operator

cri.add(EmConceptHistoryObject::DateStamp, TMongo::GreaterThan,
start); // AND add to the end operator

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

int EmConceptHistory::count()
{
TMongoODMapper<EmConceptHistoryObject> mapper;
return mapper.findCount();
}

QList<EmConceptHistory> EmConceptHistory::getAll()
{
return tfGetModelListByMongoCriteria<EmConceptHistory, EmConceptHistoryObject>(TCriteria());
}

QJsonArray EmConceptHistory::getAllJson()
{
QJsonArray array;
TMongoODMapper<EmConceptHistoryObject> mapper;

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

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

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

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

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

// Don't remove below this line
T_REGISTER_STREAM_OPERATORS(EmConceptHistory)
Loading

0 comments on commit ec4eb9f

Please sign in to comment.