完善TradeManager命令保存功能

Signed-off-by: fasiondog <fasiondog@163.com>
This commit is contained in:
fasiondog 2017-07-07 02:24:18 +08:00
parent 3a30342e9c
commit 06dd8ce7d0
6 changed files with 25 additions and 10 deletions

View File

@ -154,6 +154,7 @@ TradeManager::TradeManager(const Datetime& datetime, price_t initcash,
setParam<int>("precision", 2); //计算精度 setParam<int>("precision", 2); //计算精度
setParam<bool>("support_borrow_cash", false); //是否自动融资 setParam<bool>("support_borrow_cash", false); //是否自动融资
setParam<bool>("support_borrow_stock", false); //是否自动融券 setParam<bool>("support_borrow_stock", false); //是否自动融券
setParam<bool>("save_action", true); //是否保存命令
m_init_cash = roundEx(initcash, 2); m_init_cash = roundEx(initcash, 2);
m_cash = m_init_cash; m_cash = m_init_cash;
m_checkin_cash = m_init_cash; m_checkin_cash = m_init_cash;
@ -161,6 +162,7 @@ TradeManager::TradeManager(const Datetime& datetime, price_t initcash,
BUSINESS_INIT, m_init_cash, m_init_cash, 0.0, 0, BUSINESS_INIT, m_init_cash, m_init_cash, 0.0, 0,
CostRecord(), 0.0, m_cash, PART_INVALID)); CostRecord(), 0.0, m_cash, PART_INVALID));
m_broker_last_datetime = Datetime::now(); m_broker_last_datetime = Datetime::now();
_saveAction(m_trade_list.back());
} }
@ -1885,21 +1887,34 @@ void TradeManager::_update(const Datetime& datetime){
void TradeManager::_saveAction(const TradeRecord& record) { void TradeManager::_saveAction(const TradeRecord& record) {
if (getParam<bool>("save_action") == false)
return;
std::stringstream buf(std::stringstream::out); std::stringstream buf(std::stringstream::out);
string my_tm("td = my_tm."); string my_tm("td = my_tm.");
string sep(", "); string sep(", ");
switch (record.business) { switch (record.business) {
case BUSINESS_INIT:
buf << "my_tm = crtTM(datetime=Datetime('"
<< record.datetime.toString() << "'), "
<< "initCash=" << record.cash << sep
<< "costFunc=" << m_costfunc->name() << "("
<< m_costfunc->getParameter().getNameValueList() << "), "
<< "name='" << m_name << "'"
<< ")";
break;
case BUSINESS_CHECKIN: case BUSINESS_CHECKIN:
buf << my_tm buf << my_tm
<< "checkin(Datetime('" << record.datetime.toString() << "'), " << "checkin(Datetime('" << record.datetime.toString() << "'), "
<< record.cash << sep << record.cash
<< ")"; << ")";
break; break;
case BUSINESS_CHECKOUT: case BUSINESS_CHECKOUT:
buf << my_tm buf << my_tm
<< "checkout(Datetime('" << record.datetime.toString() << "'), " << "checkout(Datetime('" << record.datetime.toString() << "'), "
<< record.cash << sep << record.cash
<< ")"; << ")";
break; break;
@ -1919,7 +1934,7 @@ void TradeManager::_saveAction(const TradeRecord& record) {
case BUSINESS_SELL: case BUSINESS_SELL:
buf << my_tm buf << my_tm
<< "sell(Datetime('" << record.datetime.toString() << "')," << "sell(Datetime('" << record.datetime.toString() << "'),"
<< "sm['" << record.stock.market_code() << "']" << "sm['" << record.stock.market_code() << "'], "
<< record.realPrice << sep << record.realPrice << sep
<< record.number << sep << record.number << sep
<< record.stoploss << sep << record.stoploss << sep

View File

@ -10,7 +10,7 @@
namespace hku { namespace hku {
FixedA2015TradeCost::FixedA2015TradeCost() : TradeCostBase("FixedA2015") { FixedA2015TradeCost::FixedA2015TradeCost() : TradeCostBase("TC_FixedA2015") {
setParam<price_t>("commission", 0.0018); setParam<price_t>("commission", 0.0018);
setParam<price_t>("lowest_commission", 5.0); setParam<price_t>("lowest_commission", 5.0);
setParam<price_t>("stamptax", 0.001); setParam<price_t>("stamptax", 0.001);

View File

@ -11,7 +11,7 @@
namespace hku { namespace hku {
FixedATradeCost::FixedATradeCost(): TradeCostBase("FixedA") { FixedATradeCost::FixedATradeCost(): TradeCostBase("TC_FixedA") {
setParam<price_t>("commission", 0.0018); setParam<price_t>("commission", 0.0018);
setParam<price_t>("lowest_commission", 5.0); setParam<price_t>("lowest_commission", 5.0);
setParam<price_t>("stamptax", 0.001); setParam<price_t>("stamptax", 0.001);

View File

@ -9,7 +9,7 @@
namespace hku { namespace hku {
ZeroTradeCost::ZeroTradeCost(): TradeCostBase("Zero") { ZeroTradeCost::ZeroTradeCost(): TradeCostBase("TC_Zero") {
} }

View File

@ -128,7 +128,7 @@ string Parameter::getNameValueList() const {
os << iter->first << equal os << iter->first << equal
<< boost::any_cast<double>(iter->second); << boost::any_cast<double>(iter->second);
} else if (iter->second.type() == typeid(string)) { } else if (iter->second.type() == typeid(string)) {
os << iter->first << equal os << "\"" << iter->first << "\"" << equal
<< boost::any_cast<string>(iter->second); << boost::any_cast<string>(iter->second);
} else { } else {
os << "Unsupported"; os << "Unsupported";

View File

@ -38,7 +38,7 @@ BOOST_AUTO_TEST_CASE( test_TC_Zero ) {
CostRecord result; CostRecord result;
/** @arg 检查name */ /** @arg 检查name */
BOOST_CHECK(cost_func->name() == "Zero"); BOOST_CHECK(cost_func->name() == "TC_Zero");
/** @arg 计算买入成本 */ /** @arg 计算买入成本 */
result = cost_func->getBuyCost(Datetime(200101010000), stock, 9.01, 1000); result = cost_func->getBuyCost(Datetime(200101010000), stock, 9.01, 1000);
@ -50,7 +50,7 @@ BOOST_AUTO_TEST_CASE( test_TC_Zero ) {
/** @arg 测试clone */ /** @arg 测试clone */
TradeCostPtr cost_clone_func = cost_func->clone(); TradeCostPtr cost_clone_func = cost_func->clone();
BOOST_CHECK(cost_clone_func->name() == "Zero"); BOOST_CHECK(cost_clone_func->name() == "TC_Zero");
result = cost_clone_func->getBuyCost(Datetime(200101010000), stock, 9.01, 1000); result = cost_clone_func->getBuyCost(Datetime(200101010000), stock, 9.01, 1000);
BOOST_CHECK(result == Null<CostRecord>()); BOOST_CHECK(result == Null<CostRecord>());
result = cost_clone_func->getSellCost(Datetime(200101010000), stock, 9.01, 1000); result = cost_clone_func->getSellCost(Datetime(200101010000), stock, 9.01, 1000);
@ -83,7 +83,7 @@ BOOST_AUTO_TEST_CASE( test_ZeroCost_export ) {
Stock stock = sm.getStock("sh600004"); Stock stock = sm.getStock("sh600004");
CostRecord result; CostRecord result;
BOOST_CHECK(zero2->name() == "Zero"); BOOST_CHECK(zero2->name() == "TC_Zero");
result = zero2->getBuyCost(Datetime(200101010000), stock, 9.01, 1000); result = zero2->getBuyCost(Datetime(200101010000), stock, 9.01, 1000);
BOOST_CHECK(result == Null<CostRecord>()); BOOST_CHECK(result == Null<CostRecord>());
result = zero2->getSellCost(Datetime(200101010000), stock, 9.01, 1000); result = zero2->getSellCost(Datetime(200101010000), stock, 9.01, 1000);