Parameter 支持 KData

This commit is contained in:
fasiondog 2019-03-04 23:30:43 +08:00
parent aa7f32a3e9
commit 59e6991a0d
5 changed files with 103 additions and 13 deletions

View File

@ -31,7 +31,7 @@ void load(Archive & ar, hku::KData& kdata, unsigned int version) {
hku::KQuery query;
ar & BOOST_SERIALIZATION_NVP(stock);
ar & BOOST_SERIALIZATION_NVP(query);
kdata = hku::KData(stock, query);
kdata = stock.isNull() ? hku::KData() : hku::KData(stock, query);
}
}} /* namespace boost::serailization */

View File

@ -37,6 +37,14 @@ HKU_API std::ostream& operator <<(std::ostream &os, const Parameter& param) {
} else if (iter->second.type() == typeid(KQuery)) {
os << "(Query): "
<< boost::any_cast<KQuery>(iter->second) << strip;
} else if (iter->second.type() == typeid(KData)) {
os << "(KData): ";
Stock stk = boost::any_cast<KData>(iter->second).getStock();
if (stk.isNull()) {
os << "Null" <<strip;
} else {
os << stk.market_code() << strip;
}
} else {
os << "Unsupported" << strip;
}
@ -77,7 +85,8 @@ bool Parameter::support(const boost::any& value) {
|| value.type() == typeid(double)
|| value.type() == typeid(string)
|| value.type() == typeid(Stock)
|| value.type() == typeid(KQuery)) {
|| value.type() == typeid(KQuery)
|| value.type() == typeid(KData)) {
return true;
}
@ -112,6 +121,8 @@ string Parameter::getValueList() const {
os << boost::any_cast<Stock>(iter->second);
} else if (iter->second.type() == typeid(KQuery)) {
os << boost::any_cast<KQuery>(iter->second);
} else if (iter->second.type() == typeid(KData)) {
os << boost::any_cast<KData>(iter->second);
} else {
os << "Unsupported";
}
@ -149,6 +160,9 @@ string Parameter::getNameValueList() const {
} else if (iter->second.type() == typeid(KQuery)) {
os << "\"" << iter->first << "\"" << equal
<< boost::any_cast<KQuery>(iter->second);
} else if (iter->second.type() == typeid(KData)) {
os << "\"" << iter->first << "\"" << equal
<< boost::any_cast<KData>(iter->second);
} else {
os << "Unsupported";
}

View File

@ -23,8 +23,7 @@
#include <boost/serialization/string.hpp>
#include <boost/serialization/split_member.hpp>
#include <boost/serialization/nvp.hpp>
#include "../serialization/Stock_serialization.h"
#include "../serialization/KQuery_serialization.h"
#include "../serialization/KData_serialization.h"
#endif
@ -183,6 +182,10 @@ private:
type = "query";
value = "query";
query = boost::any_cast<KQuery>(arg);
} else if (arg.type() == typeid(KData)) {
type = "kdata";
value = "kdata";
query = boost::any_cast<KQuery>(arg);
} else {
type = "Unknown";
value = "Unknown";
@ -192,8 +195,9 @@ private:
string name;
string type;
string value;
Stock stock;
Stock stock;
KQuery query;
KData kdata;
template<class Archive>
void serialize(Archive & ar, const unsigned int version) {
@ -202,6 +206,7 @@ private:
ar & BOOST_SERIALIZATION_NVP(value);
ar & BOOST_SERIALIZATION_NVP(stock);
ar & BOOST_SERIALIZATION_NVP(query);
ar & BOOST_SERIALIZATION_NVP(kdata);
}
};
@ -238,6 +243,8 @@ private:
m_params[record.name] = record.stock;
} else if (record.type == "query") {
m_params[record.name] = record.query;
} else if (record.type == "kdata") {
m_params[record.name] = record.kdata;
} else {
std::cout << "Unknown type! [Parameter::load]" << std::endl;
}

View File

@ -91,6 +91,22 @@ BOOST_AUTO_TEST_CASE( test_Parameter ) {
q2 = p.get<KQuery>("query");
BOOST_CHECK(query == q2);
/** @arg 测试使用 KData 做为参数 */
KData k = stk.getKData(query);
p = Parameter();
p.set<KData>("k", k);
KData k2 = p.get<KData>("k");
BOOST_CHECK(k.size() == k2.size());
BOOST_CHECK(k.getStock() == k2.getStock());
BOOST_CHECK(k.getQuery() == k2.getQuery());
k = KData();
p = Parameter();
p.set<KData>("k", k);
k2 = p.get<KData>("k");
BOOST_CHECK(k.size() == k2.size());
BOOST_CHECK(k.getStock() == k2.getStock());
BOOST_CHECK(k.getQuery() == k2.getQuery());
}
@ -105,8 +121,12 @@ BOOST_AUTO_TEST_CASE( test_Parameter_serialize ) {
p1.set<bool>("bool", true);
p1.set<double>("p", 0.101);
p1.set<string>("string", "This is string!");
p1.set<Stock>("stk", getStock("sh600000"));
p1.set<KQuery>("query", KQueryByDate(Datetime(200001041025), Datetime(200001041100), KQuery::MIN5));
Stock stk = getStock("sh600000");
KQuery q = KQueryByDate(Datetime(200001041025), Datetime(200001041100), KQuery::MIN5);
KData k = stk.getKData(q);
p1.set<Stock>("stk", stk);
p1.set<KQuery>("query", q);
//p1.set<KData>("kdata", k);
{
std::ofstream ofs(filename);
boost::archive::xml_oarchive oa(ofs);
@ -124,8 +144,12 @@ BOOST_AUTO_TEST_CASE( test_Parameter_serialize ) {
BOOST_CHECK(p2.get<bool>("bool") == true);
BOOST_CHECK(p2.get<double>("p") == 0.101);
BOOST_CHECK(p2.get<string>("string") == "This is string!");
BOOST_CHECK(p2.get<Stock>("stk") == getStock("sh600000"));
BOOST_CHECK(p2.get<KQuery>("query") == KQueryByDate(Datetime(200001041025), Datetime(200001041100), KQuery::MIN5));
BOOST_CHECK(p2.get<Stock>("stk") == stk);
BOOST_CHECK(p2.get<KQuery>("query") == q);
/*KData k2 = p2.get<KData>("kdata");
BOOST_CHECK(k.size() == k2.size());
BOOST_CHECK(k.getStock() == k2.getStock());
BOOST_CHECK(k.getQuery() == k2.getQuery());*/
}
#endif /* HKU_SUPPORT_SERIALIZATION */

View File

@ -11,8 +11,7 @@
#include <boost/python.hpp>
#include <hikyuu/utilities/Parameter.h>
#include <hikyuu/Log.h>
#include <hikyuu/Stock.h>
#include <hikyuu/KQuery.h>
#include <hikyuu/KData.h>
#include "pickle_support.h"
namespace hku {
@ -42,7 +41,12 @@ struct AnyToPython{
} else if (x.type() == typeid(Stock)) {
const Stock& stk = boost::any_cast<Stock>(x);
string cmd("getStock('" + stk.market_code() + "')");
string cmd;
if (stk.isNull()) {
cmd = "Stock()";
} else {
cmd = "getStock('" + stk.market_code() + "')";
}
object* o = new object(eval(cmd.c_str()));
return o->ptr();
@ -62,7 +66,32 @@ struct AnyToPython{
//std::cout << cmd.str() << std::endl;
object* o = new object(eval(cmd.str().c_str()));
return o->ptr();
} else if (x.type() == typeid(KData)) {
KData kdata = boost::any_cast<KData>(x);
Stock stock = kdata.getStock();
KQuery query = kdata.getQuery();
std::stringstream cmd;
if (stock.isNull()) {
cmd << "KData()";
} else {
cmd << "getStock('" << stock.market_code() << "').getKData(";
if (query.queryType() == KQuery::INDEX) {
cmd << "QueryByIndex(" << query.start() << "," << query.end()
<< ", Query." << KQuery::getKTypeName(query.kType())
<< ", Query." << KQuery::getRecoverTypeName(query.recoverType()) << ")";
} else {
cmd << "QueryByDate(Datetime(" << query.startDatetime()
<< "), Datetime(" << query.endDatetime() << "), "
<< "Query." << KQuery::getKTypeName(query.kType())
<< "Query." << KQuery::getRecoverTypeName(query.recoverType()) << ")";
}
cmd << ")";
}
std::cout << cmd.str() << std::endl;
object* o = new object(eval(cmd.str().c_str()));
return o->ptr();
} else {
HKU_ERROR("convert failed! Unkown type! Will return None!"
" [AnyToPython::convert]");
@ -120,6 +149,12 @@ inline void Parameter::set<object>(const string& name, const object& o) {
return;
}
extract<KData> x7(o);
if (x7.check()) {
m_params[name] = x7();
return;
}
throw std::logic_error("Unsuport Type! " + name);
return;
}
@ -185,6 +220,16 @@ inline void Parameter::set<object>(const string& name, const object& o) {
return;
}
if (m_params[name].type() == typeid(KData)) {
extract<KData> x6(o);
if (x6.check()) {
m_params[name] = x6();
return;
}
throw std::logic_error(mismatch);
return;
}
throw std::logic_error("Unsupported type! " + name);
}