WalkForwardSystem(continue)

This commit is contained in:
KongDong 2024-09-17 23:35:33 +08:00
parent 1986c86af2
commit 511ca4338b
6 changed files with 77 additions and 53 deletions

View File

@ -185,8 +185,8 @@ void System::reset() {
// m_stock
m_calculated = false;
m_pre_ev_valid = false; // true;
m_pre_cn_valid = false; // true;
m_pre_ev_valid = m_ev ? false : true;
m_pre_cn_valid = m_cn ? false : true;
m_buy_days = 0;
m_sell_short_days = 0;
@ -228,8 +228,8 @@ void System::forceResetAll() {
m_kdata = Null<KData>();
m_calculated = false;
m_pre_ev_valid = false; // true;
m_pre_cn_valid = false; // true;
m_pre_ev_valid = m_ev ? false : true;
m_pre_cn_valid = m_cn ? false : true;
m_buy_days = 0;
m_sell_short_days = 0;
@ -418,7 +418,7 @@ void System::run(const KData& kdata, bool reset, bool resetAll) {
if (trace) {
HKU_INFO_IF(!tr.isNull(), "{}", tr);
PositionRecord position = m_tm->getPosition(ks[i].datetime, m_stock);
FundsRecord funds = m_tm->getFunds(m_kdata.getQuery().kType());
FundsRecord funds = m_tm->getFunds(ks[i].datetime, m_kdata.getQuery().kType());
if (position.number > 0.0) {
// clang-format off
HKU_INFO("+-------------+-------------+-------------+-------------+-------------+-------------+-------------+-------------+-------------+");

View File

@ -10,23 +10,21 @@
namespace hku {
TradeRequest::TradeRequest()
: valid(false),
business(BUSINESS_INVALID),
datetime(Null<Datetime>()),
stoploss(0.0),
goal(0.0),
number(0),
from(PART_INVALID),
count(0) {}
HKU_API std::ostream& operator<<(std::ostream& os, const TradeRequest& tr) {
os << "TradeRequest(" << tr.valid << ", " << getBusinessName(tr.business) << ", " << tr.datetime
<< ", " << std::fixed << std::setprecision(4) << tr.stoploss << ", " << std::fixed
<< std::setprecision(4) << tr.goal << ", " << std::fixed << std::setprecision(4) << tr.number
<< ", " << getSystemPartName(tr.from) << ", " << tr.count << ")";
return os;
}
void TradeRequest::clear() {
void TradeRequest::clear() noexcept {
valid = false;
business = BUSINESS_INVALID;
datetime = Null<Datetime>();
stoploss = 0.0;
goal = 0.0;
number = 0;
number = 0.0;
from = PART_INVALID;
count = 0;
}

View File

@ -20,19 +20,19 @@ namespace hku {
*/
class HKU_API TradeRequest {
public:
TradeRequest();
void clear();
TradeRequest() = default;
void clear() noexcept;
bool valid;
BUSINESS business;
bool valid{false};
BUSINESS business{BUSINESS_INVALID};
Datetime datetime;
price_t stoploss;
price_t goal;
double number; //计划的买入/卖出数量,使用发出请求时刻的收盘价,
//用于避免实际买入时需用重新计算数量时,人工执行速度较慢
//可通过系统参数进行设置,是否使用
SystemPart from; //记录SystemBase::Part
int count; //因操作失败,连续延迟的次数
price_t stoploss{0.0};
price_t goal{0.0};
double number{0.0}; // 计划的买入/卖出数量,使用发出请求时刻的收盘价,
// 用于避免实际买入时需用重新计算数量时,人工执行速度较慢
// 可通过系统参数进行设置,是否使用
SystemPart from{PART_INVALID}; // 记录SystemBase::Part
int count{0}; // 因操作失败,连续延迟的次数
KRecord krecord;
//============================================
@ -82,5 +82,13 @@ private:
#endif /* HKU_SUPPORT_SERIALIZATION */
};
HKU_API std::ostream& operator<<(std::ostream& os, const TradeRequest& tr);
} /* namespace hku */
#if FMT_VERSION >= 90000
template <>
struct fmt::formatter<hku::TradeRequest> : ostream_formatter {};
#endif
#endif /* TRADEREQUEST_H_ */

View File

@ -160,12 +160,15 @@ void WalkForwardSystem::run(const KData& kdata, bool reset, bool resetAll) {
for (size_t i = 0, total = m_kdata_list.size(); i < total; i++) {
const auto& kdata = m_kdata_list[i];
if (kdata.empty()) {
CLS_INFO_IF(trace,
"\n+-------------------------------------------------------------------"
CLS_INFO_IF(
trace,
"\n+======================================================================="
"\n|"
"\n| name: {} "
"\n| iteration {}|{}"
"\n| ignore, this iteration kdata is empty."
"\n+-------------------------------------------------------------------\n",
"\n|"
"\n+=======================================================================\n",
name(), i + 1, total);
continue;
}
@ -173,28 +176,35 @@ void WalkForwardSystem::run(const KData& kdata, bool reset, bool resetAll) {
auto sw_list = m_se->getSelected(kdata[0].datetime);
if (!sw_list.empty()) {
auto& sys = sw_list.front().sys;
CLS_INFO_IF(trace,
"\n+-------------------------------------------------------------------"
CLS_INFO_IF(
trace,
"\n+======================================================================="
"\n|"
"\n| name: {} "
"\n| iteration {}|{}: {}, {}"
"\n| use sys: {}"
"\n+-------------------------------------------------------------------\n",
"\n|"
"\n+=======================================================================\n",
name(), i + 1, total, kdata[0].datetime, kdata[kdata.size() - 1].datetime,
sys->name());
sys->setParam<bool>("shared_tm", true);
sys->setParam<bool>("trace", trace);
sys->setTM(getTM());
sys->readyForRun();
sys->setParam<bool>("trace", trace);
syncDataToSystem(sys);
sys->run(kdata);
syncDataFromSystem(sys, kdata, false);
} else {
CLS_INFO_IF(trace,
"\n+-------------------------------------------------------------------"
CLS_INFO_IF(
trace,
"\n+======================================================================="
"\n|"
"\n| name: {} "
"\n| iteration {}|{}"
"\n| ignore no selected sys."
"\n+-------------------------------------------------------------------\n",
"\n|"
"\n+=======================================================================\n",
name(), i + 1, total);
}
}
@ -211,6 +221,8 @@ TradeRecord WalkForwardSystem::runMoment(const Datetime& datetime) {
if (sys != m_cur_sys) {
m_cur_kdata++;
m_cur_sys = sys;
m_cur_sys->setParam<bool>("shared_tm", true);
m_cur_sys->setParam<bool>("trace", getParam<bool>("trace"));
m_cur_sys->setTM(getTM());
m_cur_sys->readyForRun();
syncDataToSystem(m_cur_sys);

View File

@ -54,7 +54,8 @@ TEST_CASE("test_SYS_WalkForword") {
query = KQueryByIndex(-150);
sys->setParam<bool>("trace", true);
sys->run(stk, query);
// HKU_INFO("{}", sys->str());
HKU_INFO("{}", sys->getBuyTradeRequest());
// HKU_INFO("{}", sys);
// std::cout << sys << std::endl;
}

View File

@ -50,6 +50,9 @@ void export_System(py::module& m) {
)")
.def(py::init<>())
.def("__str__", to_py_str<TradeRequest>)
.def("__repr__", to_py_str<TradeRequest>)
.def_readwrite("valid", &TradeRequest::valid, "该交易请求记录是否有效True | False")
.def_readwrite("business", &TradeRequest::business,
"交易业务类型,参见::py:class:`hikyuu.trade_manage.BUSINESS`")
@ -82,8 +85,8 @@ void export_System(py::module& m) {
.def(py::init<const TradeManagerPtr&, const MoneyManagerPtr&, const EnvironmentPtr&,
const ConditionPtr&, const SignalPtr&, const StoplossPtr&, const StoplossPtr&,
const ProfitGoalPtr&, const SlippagePtr&, const string&>())
.def("__str__", &System::str)
.def("__repr__", &System::str)
.def("__str__", to_py_str<System>)
.def("__repr__", to_py_str<System>)
.def_property("name", py::overload_cast<>(&System::name, py::const_),
py::overload_cast<const string&>(&System::name), py::return_value_policy::copy,
@ -263,4 +266,6 @@ void export_System(py::module& m) {
:param ProfitGoalBase pg:
:param SlippageBase sp:
:return: system实例)");
m.def("SYS_WalkForward", SYS_WalkForward);
}