hikyuu2/libs/hikyuu/trade_sys/moneymanager/MoneyManagerBase.cpp

196 lines
5.5 KiB
C++
Raw Normal View History

2015-01-07 01:26:14 +08:00
/*
* MoneyManagerBase.cpp
*
* Created on: 2013-3-3
* Author: fasiondog
*/
#include "MoneyManagerBase.h"
namespace hku {
HKU_API std::ostream & operator<<(std::ostream& os, const MoneyManagerBase& mm) {
os << "MoneyManager(" << mm.name() << ", " << mm.getParameter() << ")";
return os;
}
HKU_API std::ostream & operator<<(std::ostream& os, const MoneyManagerPtr& mm) {
if (mm) {
os << *mm;
} else {
os << "MoneyManager(NULL)";
}
return os;
}
2016-04-28 02:39:49 +08:00
MoneyManagerBase::MoneyManagerBase()
2016-05-18 02:03:29 +08:00
: m_name("MoneyManagerBase") {
2016-05-03 02:44:03 +08:00
setParam<bool>("auto-checkin", false);
2016-04-28 02:39:49 +08:00
}
2015-01-07 01:26:14 +08:00
MoneyManagerBase::MoneyManagerBase(const string& name)
2016-05-18 02:03:29 +08:00
: m_name(name) {
2016-05-03 02:44:03 +08:00
setParam<bool>("auto-checkin", false);
2015-01-07 01:26:14 +08:00
}
MoneyManagerBase::~MoneyManagerBase() {
}
void MoneyManagerBase::buyNotify(const TradeRecord&) {
}
void MoneyManagerBase::sellNotify(const TradeRecord&) {
}
MoneyManagerPtr MoneyManagerBase::clone() {
MoneyManagerPtr p = _clone();
p->m_params = m_params;
p->m_name = m_name;
p->m_tm = m_tm;
2016-05-18 02:03:29 +08:00
p->m_query = m_query;
2015-01-07 01:26:14 +08:00
return p;
}
2016-04-03 00:08:31 +08:00
size_t MoneyManagerBase
::getSellNumber(const Datetime& datetime, const Stock& stock,
price_t price, price_t risk) {
if (!m_tm) {
HKU_ERROR("m_tm is null! Datetime(" << datetime << ") Stock("
<< stock.market_code() << ") price(" << price
<< ") risk(" << risk
<< ") [MoneyManagerBase::getSellNumber]");
return 0;
}
if (risk <= 0.0) {
HKU_ERROR("risk is zero! Datetime(" << datetime << ") Stock("
<< stock.market_code() << ") price(" << price
<< ") risk(" << risk
<< ") [MoneyManagerBase::getSellNumber]");
return 0;
}
2016-05-03 02:44:03 +08:00
return _getSellNumber(datetime, stock, price, risk);;
2016-04-03 00:08:31 +08:00
}
size_t MoneyManagerBase
::getBuyNumber(const Datetime& datetime, const Stock& stock,
price_t price, price_t risk) {
if (!m_tm) {
HKU_ERROR("m_tm is null! Datetime(" << datetime << ") Stock("
<< stock.market_code() << ") price(" << price
<< ") risk(" << risk
<< ") [MoneyManagerBase::getBuyNumber]");
return 0;
}
2015-01-07 01:26:14 +08:00
2016-04-27 03:27:55 +08:00
if (stock.isNull()) {
HKU_ERROR("stock is Null!" << " [MoneyManagerBase::getBuyNumber]");
return 0;
}
2016-04-03 00:08:31 +08:00
if (risk <= 0.0) {
HKU_ERROR("risk is zero! Datetime(" << datetime << ") Stock("
<< stock.market_code() << ") price(" << price
<< ") risk(" << risk
<< ") [MoneyManagerBase::getBuyNumber]");
return 0;
}
2016-05-03 02:44:03 +08:00
size_t n = _getBuyNumber(datetime, stock, price, risk);
if (n < stock.minTradeNumber()) {
HKU_INFO("Ignore! Is less than the minimum number of transactions("
<< stock.minTradeNumber()
<< ")! [MoneyManagerBase::getBuyNumber]");
return 0;
}
//转换为最小交易量的整数倍
n = (n / stock.minTradeNumber()) * stock.minTradeNumber();
2016-05-16 01:45:01 +08:00
if (n > stock.maxTradeNumber()) {
n = stock.maxTradeNumber();
HKU_INFO("Over stock.maxTradeNumber! MoneyManagerBase::getBuyNumber]");
}
2016-05-03 02:44:03 +08:00
//在现金不足时,自动补充存入现金
if (getParam<bool>("auto-checkin")) {
price_t cash = m_tm->currentCash();
CostRecord cost = m_tm->getBuyCost(datetime, stock, price, n);
price_t money = price * n + cost.total;
if (money > cash) {
m_tm->checkin(datetime, roundUp(money - cash, stock.precision()));
}
}
return n;
2016-04-03 00:08:31 +08:00
}
size_t MoneyManagerBase
::getSellShortNumber(const Datetime& datetime, const Stock& stock,
price_t price, price_t risk) {
if (!m_tm) {
HKU_ERROR("m_tm is null! Datetime(" << datetime << ") Stock("
<< stock.market_code() << ") price(" << price
<< ") risk(" << risk
<< ") [MoneyManagerBase::getSellShortNumber]");
return 0;
}
if (risk <= 0.0) {
HKU_ERROR("risk is zero! Datetime(" << datetime << ") Stock("
<< stock.market_code() << ") price(" << price
<< ") risk(" << risk
<< ") [MoneyManagerBase::getSellShortNumber]");
return 0;
}
return _getSellShortNumber(datetime, stock, price, risk);
}
size_t MoneyManagerBase
::getBuyShortNumber(const Datetime& datetime, const Stock& stock,
price_t price, price_t risk) {
if (!m_tm) {
HKU_ERROR("m_tm is null! Datetime(" << datetime << ") Stock("
<< stock.market_code() << ") price(" << price
<< ") risk(" << risk
<< ") [MoneyManagerBase::getBuyShortNumber]");
return 0;
}
if (risk <= 0.0) {
HKU_ERROR("risk is zero! Datetime(" << datetime << ") Stock("
<< stock.market_code() << ") price(" << price
<< ") risk(" << risk
<< ") [MoneyManagerBase::getBuyShortNumber]");
return 0;
}
return _getBuyShortNumber(datetime, stock, price, risk);
}
size_t MoneyManagerBase::_getSellNumber(const Datetime& datetime, const Stock& stock,
2015-01-07 01:26:14 +08:00
price_t price, price_t risk) {
2016-04-03 00:08:31 +08:00
//默认卖出全部
return m_tm->getHoldNumber(datetime, stock);
}
size_t MoneyManagerBase::_getSellShortNumber(const Datetime& datetime,
const Stock& stock, price_t price, price_t risk) {
return 0;
}
size_t MoneyManagerBase::_getBuyShortNumber(const Datetime& datetime,
const Stock& stock, price_t price, price_t risk) {
return 0;
2015-01-07 01:26:14 +08:00
}
} /* namespace hku */