2016-04-03 00:08:31 +08:00
|
|
|
/*
|
|
|
|
* _Parameter.h
|
|
|
|
*
|
|
|
|
* Created on: 2015年3月11日
|
2016-04-14 01:36:53 +08:00
|
|
|
* Author: fasiondog
|
2016-04-03 00:08:31 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef PYTHON_PARAMETER_H_
|
|
|
|
#define PYTHON_PARAMETER_H_
|
|
|
|
|
|
|
|
#include <boost/python.hpp>
|
|
|
|
#include <hikyuu/utilities/Parameter.h>
|
|
|
|
#include <hikyuu/Log.h>
|
2019-03-04 23:30:43 +08:00
|
|
|
#include <hikyuu/KData.h>
|
2020-06-26 21:39:53 +08:00
|
|
|
#include "pybind_utils.h"
|
2016-04-03 00:08:31 +08:00
|
|
|
#include "pickle_support.h"
|
|
|
|
|
|
|
|
namespace hku {
|
|
|
|
|
|
|
|
using namespace boost::python;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* boost::any To Python转换器
|
|
|
|
*/
|
2020-06-25 23:53:31 +08:00
|
|
|
struct AnyToPython {
|
2016-04-03 00:08:31 +08:00
|
|
|
static PyObject* convert(boost::any x) {
|
|
|
|
if (x.type() == typeid(bool)) {
|
|
|
|
bool tmp = boost::any_cast<bool>(x);
|
|
|
|
return tmp ? Py_True : Py_False;
|
2020-06-25 23:53:31 +08:00
|
|
|
// object *o = new object(boost::any_cast<bool>(x));
|
|
|
|
// return (*o).ptr();
|
2016-04-03 00:08:31 +08:00
|
|
|
|
|
|
|
} else if (x.type() == typeid(int)) {
|
|
|
|
return Py_BuildValue("n", boost::any_cast<int>(x));
|
|
|
|
|
2020-06-25 23:53:31 +08:00
|
|
|
} else if (x.type() == typeid(double)) {
|
2016-04-03 00:08:31 +08:00
|
|
|
return Py_BuildValue("d", boost::any_cast<double>(x));
|
|
|
|
|
|
|
|
} else if (x.type() == typeid(string)) {
|
|
|
|
string s(boost::any_cast<string>(x));
|
|
|
|
return Py_BuildValue("s", s.c_str());
|
|
|
|
|
2019-03-03 20:47:28 +08:00
|
|
|
} else if (x.type() == typeid(Stock)) {
|
2019-03-04 01:17:38 +08:00
|
|
|
const Stock& stk = boost::any_cast<Stock>(x);
|
2019-03-04 23:30:43 +08:00
|
|
|
string cmd;
|
|
|
|
if (stk.isNull()) {
|
|
|
|
cmd = "Stock()";
|
|
|
|
} else {
|
|
|
|
cmd = "getStock('" + stk.market_code() + "')";
|
|
|
|
}
|
2020-06-25 23:53:31 +08:00
|
|
|
object o = eval(cmd.c_str());
|
|
|
|
return boost::python::incref(o.ptr());
|
2019-03-03 20:47:28 +08:00
|
|
|
|
2019-03-04 01:17:38 +08:00
|
|
|
} else if (x.type() == typeid(KQuery)) {
|
2019-03-03 21:55:22 +08:00
|
|
|
const KQuery& query = boost::any_cast<KQuery>(x);
|
2020-06-25 23:53:31 +08:00
|
|
|
std::stringstream cmd(std::stringstream::out);
|
2019-03-03 21:55:22 +08:00
|
|
|
if (query.queryType() == KQuery::INDEX) {
|
2020-06-25 23:53:31 +08:00
|
|
|
cmd << "QueryByIndex(" << query.start() << "," << query.end() << ", Query."
|
|
|
|
<< KQuery::getKTypeName(query.kType()) << ", Query."
|
|
|
|
<< KQuery::getRecoverTypeName(query.recoverType()) << ")";
|
2019-03-03 21:55:22 +08:00
|
|
|
} else {
|
2020-06-25 23:53:31 +08:00
|
|
|
cmd << "QueryByDate(Datetime(" << query.startDatetime() << "), Datetime("
|
|
|
|
<< query.endDatetime() << "), "
|
|
|
|
<< "Query." << KQuery::getKTypeName(query.kType()) << "Query."
|
|
|
|
<< KQuery::getRecoverTypeName(query.recoverType()) << ")";
|
2019-03-03 21:55:22 +08:00
|
|
|
}
|
2020-06-25 23:53:31 +08:00
|
|
|
object o = eval(cmd.str().c_str());
|
|
|
|
return boost::python::incref(o.ptr());
|
2019-03-04 23:30:43 +08:00
|
|
|
|
|
|
|
} 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) {
|
2020-06-25 23:53:31 +08:00
|
|
|
cmd << "QueryByIndex(" << query.start() << "," << query.end() << ", Query."
|
|
|
|
<< KQuery::getKTypeName(query.kType()) << ", Query."
|
|
|
|
<< KQuery::getRecoverTypeName(query.recoverType()) << ")";
|
2019-03-04 23:30:43 +08:00
|
|
|
} else {
|
2020-06-25 23:53:31 +08:00
|
|
|
cmd << "QueryByDate(Datetime(" << query.startDatetime() << "), Datetime("
|
|
|
|
<< query.endDatetime() << "), "
|
|
|
|
<< "Query." << KQuery::getKTypeName(query.kType()) << "Query."
|
|
|
|
<< KQuery::getRecoverTypeName(query.recoverType()) << ")";
|
2019-03-04 23:30:43 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
std::cout << cmd.str() << std::endl;
|
2020-06-25 23:53:31 +08:00
|
|
|
object o = eval(cmd.str().c_str());
|
|
|
|
return boost::python::incref(o.ptr());
|
2019-03-04 23:30:43 +08:00
|
|
|
|
2019-03-16 14:43:50 +08:00
|
|
|
} else if (x.type() == typeid(PriceList)) {
|
|
|
|
const PriceList& price_list = boost::any_cast<PriceList>(x);
|
2020-06-25 23:53:31 +08:00
|
|
|
boost::python::list o;
|
2019-03-16 14:43:50 +08:00
|
|
|
for (auto iter = price_list.begin(); iter != price_list.end(); ++iter) {
|
2020-06-25 23:53:31 +08:00
|
|
|
o.append(*iter);
|
2019-03-16 14:43:50 +08:00
|
|
|
}
|
2020-06-25 23:53:31 +08:00
|
|
|
return boost::python::incref(o.ptr());
|
2019-03-16 14:43:50 +08:00
|
|
|
|
2019-05-20 23:30:51 +08:00
|
|
|
} else if (x.type() == typeid(DatetimeList)) {
|
|
|
|
const DatetimeList& date_list = boost::any_cast<DatetimeList>(x);
|
2020-06-25 23:53:31 +08:00
|
|
|
boost::python::list o;
|
2019-05-20 23:30:51 +08:00
|
|
|
for (auto iter = date_list.begin(); iter != date_list.end(); ++iter) {
|
2020-06-25 23:53:31 +08:00
|
|
|
o.append(*iter);
|
2019-05-20 23:30:51 +08:00
|
|
|
}
|
2020-06-25 23:53:31 +08:00
|
|
|
return boost::python::incref(o.ptr());
|
2019-05-20 23:30:51 +08:00
|
|
|
|
2016-04-03 00:08:31 +08:00
|
|
|
} else {
|
2019-06-16 17:56:34 +08:00
|
|
|
HKU_ERROR("convert failed! Unkown type! Will return None!");
|
2020-06-25 23:53:31 +08:00
|
|
|
return Py_BuildValue("s", (char*)0);
|
2016-04-03 00:08:31 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template <>
|
2016-04-18 19:54:42 +08:00
|
|
|
inline void Parameter::set<object>(const string& name, const object& o) {
|
2020-06-25 23:53:31 +08:00
|
|
|
if (o.is_none()) {
|
|
|
|
HKU_THROW_EXCEPTION(std::logic_error, "Not support None!");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!have(name)) {
|
2016-04-03 00:08:31 +08:00
|
|
|
if (PyBool_Check(o.ptr())) {
|
|
|
|
m_params[name] = bool(extract<bool>(o));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
extract<int> x2(o);
|
|
|
|
if (x2.check()) {
|
|
|
|
m_params[name] = x2();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
extract<double> x3(o);
|
|
|
|
if (x3.check()) {
|
|
|
|
m_params[name] = x3();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
extract<string> x4(o);
|
|
|
|
if (x4.check()) {
|
|
|
|
m_params[name] = x4();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-03-03 20:47:28 +08:00
|
|
|
extract<Stock> x5(o);
|
|
|
|
if (x5.check()) {
|
|
|
|
m_params[name] = x5();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-03-03 21:55:22 +08:00
|
|
|
extract<KQuery> x6(o);
|
|
|
|
if (x6.check()) {
|
|
|
|
m_params[name] = x6();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-03-04 23:30:43 +08:00
|
|
|
extract<KData> x7(o);
|
|
|
|
if (x7.check()) {
|
|
|
|
m_params[name] = x7();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-06-25 23:53:31 +08:00
|
|
|
try {
|
|
|
|
boost::python::list pylist(o);
|
|
|
|
size_t total = len(pylist);
|
|
|
|
if (total > 0) {
|
|
|
|
extract<price_t> x8(pylist[0]);
|
|
|
|
if (x8.check()) {
|
2020-06-26 21:39:53 +08:00
|
|
|
m_params[name] = python_list_to_vector<PriceList>(pylist);
|
2020-06-25 23:53:31 +08:00
|
|
|
return;
|
|
|
|
}
|
2019-03-16 14:43:50 +08:00
|
|
|
|
2020-06-25 23:53:31 +08:00
|
|
|
extract<Datetime> x9(pylist[0]);
|
|
|
|
if (x9.check()) {
|
2020-06-26 21:39:53 +08:00
|
|
|
m_params[name] = python_list_to_vector<DatetimeList>(pylist);
|
2020-06-25 23:53:31 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
} catch (...) {
|
|
|
|
// do nothing
|
2019-05-20 23:30:51 +08:00
|
|
|
}
|
|
|
|
|
2016-04-03 00:08:31 +08:00
|
|
|
throw std::logic_error("Unsuport Type! " + name);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
string mismatch("Mismatch Type! " + name);
|
|
|
|
if (m_params[name].type() == typeid(bool)) {
|
|
|
|
extract<bool> x1(o);
|
|
|
|
if (x1.check()) {
|
|
|
|
m_params[name] = x1();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
throw std::logic_error(mismatch);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (m_params[name].type() == typeid(int)) {
|
|
|
|
extract<int> x2(o);
|
|
|
|
if (x2.check()) {
|
|
|
|
m_params[name] = x2();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
throw std::logic_error(mismatch);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (m_params[name].type() == typeid(double)) {
|
|
|
|
extract<double> x3(o);
|
|
|
|
if (x3.check()) {
|
|
|
|
m_params[name] = x3();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
throw std::logic_error(mismatch);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (m_params[name].type() == typeid(string)) {
|
|
|
|
extract<string> x4(o);
|
|
|
|
if (x4.check()) {
|
|
|
|
m_params[name] = x4();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
throw std::logic_error(mismatch);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-03-03 20:47:28 +08:00
|
|
|
if (m_params[name].type() == typeid(Stock)) {
|
2019-03-04 01:17:38 +08:00
|
|
|
extract<Stock> x5(o);
|
2019-03-03 20:47:28 +08:00
|
|
|
if (x5.check()) {
|
|
|
|
m_params[name] = x5();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
throw std::logic_error(mismatch);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-03-03 21:55:22 +08:00
|
|
|
if (m_params[name].type() == typeid(KQuery)) {
|
2019-03-04 01:17:38 +08:00
|
|
|
extract<KQuery> x6(o);
|
2019-03-03 21:55:22 +08:00
|
|
|
if (x6.check()) {
|
|
|
|
m_params[name] = x6();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
throw std::logic_error(mismatch);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-03-04 23:30:43 +08:00
|
|
|
if (m_params[name].type() == typeid(KData)) {
|
2019-03-16 14:43:50 +08:00
|
|
|
extract<KData> x7(o);
|
|
|
|
if (x7.check()) {
|
|
|
|
m_params[name] = x7();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
throw std::logic_error(mismatch);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (m_params[name].type() == typeid(PriceList)) {
|
2020-06-26 21:39:53 +08:00
|
|
|
m_params[name] = python_list_to_vector<PriceList>(boost::python::list(o));
|
2019-03-04 23:30:43 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-05-20 23:30:51 +08:00
|
|
|
if (m_params[name].type() == typeid(DatetimeList)) {
|
2020-06-26 21:39:53 +08:00
|
|
|
m_params[name] = python_list_to_vector<DatetimeList>(boost::python::list(o));
|
2019-05-20 23:30:51 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-04-03 00:08:31 +08:00
|
|
|
throw std::logic_error("Unsupported type! " + name);
|
|
|
|
}
|
|
|
|
|
|
|
|
} /* namespace hku */
|
|
|
|
|
|
|
|
#endif /* PYTHON_PARAMETER_H_ */
|