hikyuu2/hikyuu_cpp/hikyuu/Stock.cpp

731 lines
23 KiB
C++
Raw Normal View History

2016-04-11 01:27:04 +08:00
/*
2015-01-07 01:26:14 +08:00
* Stock.cpp
*
* Created on: 2011-11-9
* Author: fasiondog
*/
#include "GlobalInitializer.h"
2015-01-07 01:26:14 +08:00
#include "StockManager.h"
#include "data_driver/KDataDriver.h"
2019-04-06 02:40:53 +08:00
#include "data_driver/HistoryFinanceReader.h"
2016-04-03 00:08:31 +08:00
#include "utilities/util.h"
2019-02-08 22:41:20 +08:00
#include "KData.h"
2015-01-07 01:26:14 +08:00
namespace hku {
const string Stock::default_market;
2019-03-28 02:11:13 +08:00
const string Stock::default_code;
2015-01-07 01:26:14 +08:00
const string Stock::default_market_code;
const string Stock::default_name;
const uint32_t Stock::default_type = Null<uint32_t>();
2015-01-07 01:26:14 +08:00
const bool Stock::default_valid = false;
2019-11-10 03:27:57 +08:00
const Datetime Stock::default_startDate; // = Null<Datetime>();
const Datetime Stock::default_lastDate; // = Null<Datetime>();
2015-01-07 01:26:14 +08:00
const price_t Stock::default_tick = 0.01;
const price_t Stock::default_tickValue = 0.01;
const price_t Stock::default_unit = 1.0;
const int Stock::default_precision = 2;
const size_t Stock::default_minTradeNumber = 100;
const size_t Stock::default_maxTradeNumber = 1000000;
2019-11-10 03:27:57 +08:00
HKU_API std::ostream& operator<<(std::ostream& os, const Stock& stock) {
2015-01-07 01:26:14 +08:00
string strip(", ");
2019-11-10 19:45:57 +08:00
const StockManager& sm = StockManager::instance();
2015-01-07 01:26:14 +08:00
StockTypeInfo typeInfo(sm.getStockTypeInfo(stock.type()));
os << "Stock(" << stock.market() << strip << stock.code() << strip << stock.name() << strip
<< typeInfo.description() << strip << stock.valid() << strip << stock.startDatetime()
<< strip << stock.lastDatetime() << ")";
2015-01-07 01:26:14 +08:00
return os;
}
2016-04-03 00:08:31 +08:00
string Stock::toString() const {
std::stringstream os;
string strip(", ");
2019-11-10 19:45:57 +08:00
const StockManager& sm = StockManager::instance();
2016-04-03 00:08:31 +08:00
StockTypeInfo typeInfo(sm.getStockTypeInfo(type()));
os << "Stock(" << market() << strip << code() << strip << name() << strip
<< typeInfo.description() << strip << valid() << strip << startDatetime() << strip
<< lastDatetime() << ")";
2016-04-03 00:08:31 +08:00
return os.str();
}
2015-01-07 01:26:14 +08:00
Stock::Data::Data()
: m_market(default_market),
m_code(default_code),
m_name(default_name),
m_type(default_type),
m_valid(default_valid),
m_startDate(default_startDate),
m_lastDate(default_lastDate),
m_tick(default_tick),
m_tickValue(default_tickValue),
m_unit(default_unit),
m_precision(default_precision),
m_minTradeNumber(default_minTradeNumber),
2021-01-04 00:16:19 +08:00
m_maxTradeNumber(default_maxTradeNumber) {
2021-03-24 23:20:01 +08:00
const auto& ktype_list = KQuery::getAllKType();
2021-01-04 00:16:19 +08:00
for (auto& ktype : ktype_list) {
pKData[ktype] = nullptr;
pMutex[ktype] = nullptr;
}
}
2015-01-07 01:26:14 +08:00
Stock::Data::Data(const string& market, const string& code, const string& name, uint32_t type,
bool valid, const Datetime& startDate, const Datetime& lastDate, price_t tick,
price_t tickValue, int precision, double minTradeNumber, double maxTradeNumber)
2015-01-07 01:26:14 +08:00
: m_market(market),
m_code(code),
m_name(name),
m_type(type),
m_valid(valid),
m_startDate(startDate),
m_lastDate(lastDate),
m_tick(tick),
m_tickValue(tickValue),
m_precision(precision),
m_minTradeNumber(minTradeNumber),
m_maxTradeNumber(maxTradeNumber) {
if (0.0 == m_tick) {
HKU_WARN("tick should not be zero! now use as 1.0");
2015-01-07 01:26:14 +08:00
m_unit = 1.0;
} else {
m_unit = m_tickValue / m_tick;
}
to_upper(m_market);
2015-01-07 01:26:14 +08:00
m_market_code = m_market + m_code;
2021-01-04 00:16:19 +08:00
2021-03-24 23:20:01 +08:00
const auto& ktype_list = KQuery::getAllKType();
2021-01-04 00:16:19 +08:00
for (auto& ktype : ktype_list) {
2021-01-23 23:03:28 +08:00
pMutex[ktype] = new std::shared_mutex();
2021-01-04 00:16:19 +08:00
pKData[ktype] = nullptr;
}
2015-01-07 01:26:14 +08:00
}
Stock::Data::~Data() {
2021-01-23 23:03:28 +08:00
for (auto iter = pKData.begin(); iter != pKData.end(); ++iter) {
if (iter->second) {
delete iter->second;
}
}
2020-11-11 00:07:56 +08:00
2021-01-23 23:03:28 +08:00
for (auto iter = pMutex.begin(); iter != pMutex.end(); ++iter) {
if (iter->second) {
delete iter->second;
2020-11-11 00:07:56 +08:00
}
}
2015-01-07 01:26:14 +08:00
}
Stock::Stock() {}
2015-01-07 01:26:14 +08:00
2019-11-10 03:27:57 +08:00
Stock::~Stock() {}
2015-01-07 01:26:14 +08:00
2019-11-10 19:45:57 +08:00
Stock::Stock(const Stock& x) : m_data(x.m_data), m_kdataDriver(x.m_kdataDriver) {}
2015-01-07 01:26:14 +08:00
Stock& Stock::operator=(const Stock& x) {
HKU_IF_RETURN(this == &x, *this);
2019-11-10 19:45:57 +08:00
m_data = x.m_data;
m_kdataDriver = x.m_kdataDriver;
2015-01-07 01:26:14 +08:00
return *this;
}
2019-11-10 03:27:57 +08:00
Stock::Stock(const string& market, const string& code, const string& name) {
m_data =
shared_ptr<Data>(new Data(market, code, name, default_type, default_valid, default_startDate,
default_lastDate, default_tick, default_tickValue,
default_precision, default_minTradeNumber, default_maxTradeNumber));
2015-01-07 01:26:14 +08:00
}
Stock::Stock(const string& market, const string& code, const string& name, uint32_t type,
bool valid, const Datetime& startDate, const Datetime& lastDate) {
m_data = shared_ptr<Data>(new Data(market, code, name, type, valid, startDate, lastDate,
default_tick, default_tickValue, default_precision,
default_minTradeNumber, default_maxTradeNumber));
2015-01-07 01:26:14 +08:00
}
Stock::Stock(const string& market, const string& code, const string& name, uint32_t type,
bool valid, const Datetime& startDate, const Datetime& lastDate, price_t tick,
price_t tickValue, int precision, size_t minTradeNumber, size_t maxTradeNumber)
: m_data(make_shared<Data>(market, code, name, type, valid, startDate, lastDate, tick, tickValue,
precision, minTradeNumber, maxTradeNumber)) {}
2015-01-07 01:26:14 +08:00
bool Stock::operator!=(const Stock& stock) const {
HKU_IF_RETURN(this == &stock, false);
HKU_IF_RETURN(m_data == stock.m_data, false);
HKU_IF_RETURN(!m_data || !stock.m_data, true);
HKU_IF_RETURN(m_data->m_code != stock.code() || m_data->m_market != stock.market(), true);
2015-01-07 01:26:14 +08:00
return false;
}
const string& Stock::market() const {
return m_data ? m_data->m_market : default_market;
}
const string& Stock::code() const {
return m_data ? m_data->m_code : default_code;
}
const string& Stock::market_code() const {
return m_data ? m_data->m_market_code : default_market_code;
}
const string& Stock::name() const {
return m_data ? m_data->m_name : default_name;
}
uint32_t Stock::type() const {
2015-01-07 01:26:14 +08:00
return m_data ? m_data->m_type : default_type;
}
bool Stock::valid() const {
return m_data ? m_data->m_valid : default_valid;
}
Datetime Stock::startDatetime() const {
return m_data ? m_data->m_startDate : default_startDate;
}
Datetime Stock::lastDatetime() const {
return m_data ? m_data->m_lastDate : default_lastDate;
}
price_t Stock::tick() const {
return m_data ? m_data->m_tick : default_tick;
}
price_t Stock::tickValue() const {
return m_data ? m_data->m_tickValue : default_tickValue;
}
price_t Stock::unit() const {
return m_data ? m_data->m_unit : default_unit;
}
int Stock::precision() const {
return m_data ? m_data->m_precision : default_precision;
}
double Stock::atom() const {
2015-01-07 01:26:14 +08:00
return m_data ? m_data->m_minTradeNumber : default_minTradeNumber;
}
double Stock::minTradeNumber() const {
2015-01-07 01:26:14 +08:00
return m_data ? m_data->m_minTradeNumber : default_minTradeNumber;
}
double Stock::maxTradeNumber() const {
2015-01-07 01:26:14 +08:00
return m_data ? m_data->m_maxTradeNumber : default_maxTradeNumber;
}
2021-01-24 20:47:38 +08:00
void Stock::setKDataDriver(const KDataDriverConnectPoolPtr& kdataDriver) {
HKU_CHECK(kdataDriver, "kdataDriver is nullptr!");
m_kdataDriver = kdataDriver;
2020-10-06 17:56:10 +08:00
if (m_data) {
2021-01-23 23:03:28 +08:00
auto& ktype_list = KQuery::getAllKType();
2021-01-04 00:16:19 +08:00
for (auto& ktype : ktype_list) {
2021-01-23 23:03:28 +08:00
std::unique_lock<std::shared_mutex> lock(*(m_data->pMutex[ktype]));
delete m_data->pKData[ktype];
m_data->pKData[ktype] = nullptr;
2020-11-11 00:07:56 +08:00
}
2020-10-06 17:56:10 +08:00
}
2015-01-07 01:26:14 +08:00
}
KDataDriverConnectPoolPtr Stock::getKDataDirver() const {
return m_kdataDriver;
}
2015-01-07 01:26:14 +08:00
void Stock::setWeightList(const StockWeightList& weightList) {
if (m_data) {
std::lock_guard<std::mutex> lock(m_data->m_weight_mutex);
2015-01-07 01:26:14 +08:00
m_data->m_weightList = weightList;
}
2015-01-07 01:26:14 +08:00
}
bool Stock::isBuffer(KQuery::KType ktype) const {
HKU_IF_RETURN(!m_data, false);
2020-06-10 00:17:24 +08:00
string nktype(ktype);
2020-10-05 18:04:53 +08:00
to_upper(nktype);
2021-01-04 00:16:19 +08:00
return m_data->pKData.find(nktype) != m_data->pKData.end() && m_data->pKData[nktype];
2015-01-07 01:26:14 +08:00
}
bool Stock::isNull() const {
2020-10-05 18:04:53 +08:00
return !m_data || !m_kdataDriver;
2015-01-07 01:26:14 +08:00
}
2020-06-10 00:17:24 +08:00
void Stock::releaseKDataBuffer(KQuery::KType inkType) {
2021-01-23 23:03:28 +08:00
HKU_IF_RETURN(!m_data, void());
2020-11-11 00:07:56 +08:00
2021-01-23 23:03:28 +08:00
string ktype(inkType);
to_upper(ktype);
HKU_IF_RETURN(m_data->pMutex.find(ktype) == m_data->pMutex.end(), void());
std::unique_lock<std::shared_mutex> lock(*(m_data->pMutex[ktype]));
auto iter = m_data->pKData.find(ktype);
if (iter->second) {
delete iter->second;
iter->second = nullptr;
2020-10-05 18:04:53 +08:00
}
2015-01-07 01:26:14 +08:00
}
2020-11-11 00:07:56 +08:00
// 仅在初始化时调用
2020-06-10 00:17:24 +08:00
void Stock::loadKDataToBuffer(KQuery::KType inkType) {
2021-01-17 00:27:09 +08:00
HKU_IF_RETURN(!m_data || !m_kdataDriver, void());
string kType(inkType);
to_upper(kType);
HKU_IF_RETURN(m_data->pKData.find(kType) == m_data->pKData.end(), void());
releaseKDataBuffer(kType);
const auto& param = StockManager::instance().getPreloadParameter();
string preload_type = fmt::format("{}_max", kType);
to_lower(preload_type);
int max_num = param.tryGet<int>(preload_type, 4096);
HKU_ERROR_IF_RETURN(max_num < 0, void(), "Invalid preload {} param: {}", preload_type, max_num);
2021-01-23 23:03:28 +08:00
2021-01-24 00:38:22 +08:00
auto driver = m_kdataDriver->getConnect();
2021-01-24 20:47:38 +08:00
size_t total = driver->getCount(m_data->m_market, m_data->m_code, kType);
2021-01-23 23:03:28 +08:00
HKU_IF_RETURN(total == 0, void());
2021-01-17 00:27:09 +08:00
int start = total <= max_num ? 0 : total - max_num;
{
2021-01-23 23:03:28 +08:00
std::unique_lock<std::shared_mutex> lock(*(m_data->pMutex[kType]));
KRecordList* ptr_klist = new KRecordList;
m_data->pKData[kType] = ptr_klist;
2021-01-24 20:47:38 +08:00
(*ptr_klist) = driver->getKRecordList(m_data->m_market, m_data->m_code,
KQuery(start, Null<int64_t>(), kType));
}
2015-01-07 01:26:14 +08:00
}
2019-11-10 03:27:57 +08:00
StockWeightList Stock::getWeight(const Datetime& start, const Datetime& end) const {
2015-01-07 01:26:14 +08:00
StockWeightList result;
HKU_IF_RETURN(!m_data || start >= end, result);
std::lock_guard<std::mutex> lock(m_data->m_weight_mutex);
2015-01-07 01:26:14 +08:00
StockWeightList::const_iterator start_iter, end_iter;
2020-11-02 23:47:15 +08:00
start_iter = lower_bound(m_data->m_weightList.begin(), m_data->m_weightList.end(),
StockWeight(start), std::less<StockWeight>());
2019-11-10 03:27:57 +08:00
if (start_iter == m_data->m_weightList.end()) {
2015-01-07 01:26:14 +08:00
return result;
}
end_iter = lower_bound(start_iter, (StockWeightList::const_iterator)m_data->m_weightList.end(),
2020-11-02 23:47:15 +08:00
StockWeight(end), std::less<StockWeight>());
2019-11-10 03:27:57 +08:00
for (; start_iter != end_iter; ++start_iter) {
2015-01-07 01:26:14 +08:00
result.push_back(*start_iter);
}
return result;
}
KData Stock::getKData(const KQuery& query) const {
return KData(*this, query);
}
2020-11-11 23:58:08 +08:00
size_t Stock::_getCountFromBuffer(KQuery::KType ktype) const {
std::shared_lock<std::shared_mutex> lock(*(m_data->pMutex[ktype]));
return m_data->pKData[ktype]->size();
}
2015-01-07 01:26:14 +08:00
size_t Stock::getCount(KQuery::KType kType) const {
HKU_IF_RETURN(!m_data, 0);
2020-06-10 00:17:24 +08:00
string nktype(kType);
to_upper(nktype);
2021-01-04 00:16:19 +08:00
if (m_data->pKData.find(nktype) != m_data->pKData.end() && m_data->pKData[nktype]) {
2020-11-11 23:58:08 +08:00
return _getCountFromBuffer(nktype);
}
2015-01-07 01:26:14 +08:00
2021-01-24 20:47:38 +08:00
return m_kdataDriver ? m_kdataDriver->getConnect()->getCount(market(), code(), nktype) : 0;
2015-01-07 01:26:14 +08:00
}
2020-06-10 00:17:24 +08:00
price_t Stock::getMarketValue(const Datetime& datetime, KQuery::KType inktype) const {
HKU_IF_RETURN(isNull(), 0.0);
HKU_IF_RETURN(!valid() && datetime > lastDatetime(), 0.0);
2015-01-07 01:26:14 +08:00
2020-06-10 00:17:24 +08:00
string ktype(inktype);
to_upper(ktype);
2020-10-08 15:46:41 +08:00
// 如果为内存缓存或者数据驱动为索引优先,则按索引方式获取
2021-01-24 20:47:38 +08:00
if (isBuffer(ktype) || m_kdataDriver->getConnect()->isIndexFirst()) {
2020-10-08 15:46:41 +08:00
KQuery query = KQueryByDate(datetime, Null<Datetime>(), ktype);
2020-10-08 02:26:02 +08:00
size_t out_start, out_end;
if (getIndexRange(query, out_start, out_end)) {
//找到的是>=datetime的记录
KRecord k = getKRecord(out_start, ktype);
if (k.datetime == datetime) {
2020-10-08 15:46:41 +08:00
return k.closePrice;
}
if (out_start != 0) {
k = getKRecord(out_start - 1, ktype);
return k.closePrice;
2015-01-07 01:26:14 +08:00
}
}
2020-10-08 15:46:41 +08:00
2015-01-07 01:26:14 +08:00
} else {
2020-10-08 15:46:41 +08:00
// 未在缓存中,且日期优先的情况下
// 先尝试获取等于该日期的K线数据
// 如未找到,则获取小于该日期的最后一条记录
KQuery query = KQueryByDate(datetime, datetime + Minutes(1), ktype);
2020-10-08 02:26:02 +08:00
auto k_list = getKRecordList(query);
2020-10-08 15:46:41 +08:00
if (k_list.size() > 0 && k_list[0].datetime == datetime) {
return k_list[0].closePrice;
}
query = KQueryByDate(startDatetime(), datetime, ktype);
k_list = getKRecordList(query);
2020-10-08 02:26:02 +08:00
if (k_list.size() > 0) {
2020-10-08 15:46:41 +08:00
return k_list[k_list.size() - 1].closePrice;
2020-10-08 02:26:02 +08:00
}
}
2020-10-08 15:46:41 +08:00
//没有找到,则取最后一条记录
price_t price = 0.0;
size_t total = getCount(ktype);
if (total > 0) {
price = getKRecord(total - 1, ktype).closePrice;
2015-01-07 01:26:14 +08:00
}
return price;
}
2019-11-10 03:27:57 +08:00
bool Stock::getIndexRange(const KQuery& query, size_t& out_start, size_t& out_end) const {
2015-01-07 01:26:14 +08:00
out_start = 0;
out_end = 0;
HKU_IF_RETURN(!m_data || !m_kdataDriver, false);
2015-01-07 01:26:14 +08:00
if (KQuery::INDEX == query.queryType())
return _getIndexRangeByIndex(query, out_start, out_end);
2020-10-08 15:46:41 +08:00
if ((KQuery::DATE != query.queryType()) || query.startDatetime() >= query.endDatetime())
2015-01-07 01:26:14 +08:00
return false;
if (isBuffer(query.kType())) {
2015-01-07 01:26:14 +08:00
return _getIndexRangeByDateFromBuffer(query, out_start, out_end);
}
2021-01-24 20:47:38 +08:00
if (!m_kdataDriver->getConnect()->getIndexRangeByDate(m_data->m_market, m_data->m_code, query,
out_start, out_end)) {
2015-01-07 01:26:14 +08:00
out_start = 0;
out_end = 0;
return false;
}
return true;
}
2019-11-10 03:27:57 +08:00
bool Stock::_getIndexRangeByIndex(const KQuery& query, size_t& out_start, size_t& out_end) const {
2015-01-07 01:26:14 +08:00
assert(query.queryType() == KQuery::INDEX);
out_start = 0;
out_end = 0;
size_t total = getCount(query.kType());
HKU_IF_RETURN(0 == total, false);
2015-01-07 01:26:14 +08:00
int64_t startix, endix;
2015-01-07 01:26:14 +08:00
startix = query.start();
2019-11-10 03:27:57 +08:00
if (startix < 0) {
2015-01-07 01:26:14 +08:00
startix += total;
2019-11-10 03:27:57 +08:00
if (startix < 0)
2015-01-07 01:26:14 +08:00
startix = 0;
}
endix = query.end();
2019-11-10 03:27:57 +08:00
if (endix < 0) {
2015-01-07 01:26:14 +08:00
endix += total;
2019-11-10 03:27:57 +08:00
if (endix < 0)
2015-01-07 01:26:14 +08:00
endix = 0;
}
2017-05-15 01:38:18 +08:00
size_t null_size_t = Null<size_t>();
size_t startpos = 0;
size_t endpos = null_size_t;
try {
startpos = boost::numeric_cast<size_t>(startix);
2019-11-10 03:27:57 +08:00
} catch (...) {
2017-05-15 01:38:18 +08:00
startpos = null_size_t;
}
try {
endpos = boost::numeric_cast<size_t>(endix);
} catch (...) {
endpos = null_size_t;
}
2015-01-07 01:26:14 +08:00
2019-11-10 03:27:57 +08:00
if (endpos > total) {
2015-01-07 01:26:14 +08:00
endpos = total;
}
2019-11-10 03:27:57 +08:00
if (startpos >= endpos) {
2015-01-07 01:26:14 +08:00
return false;
}
out_start = startpos;
out_end = endpos;
return true;
}
bool Stock::_getIndexRangeByDateFromBuffer(const KQuery& query, size_t& out_start,
size_t& out_end) const {
2020-11-11 23:58:08 +08:00
std::shared_lock<std::shared_mutex> lock(*(m_data->pMutex[query.kType()]));
2015-01-07 01:26:14 +08:00
out_start = 0;
out_end = 0;
2020-11-11 23:58:08 +08:00
const KRecordList& kdata = *(m_data->pKData[query.kType()]);
size_t total = kdata.size();
HKU_IF_RETURN(0 == total, false);
2015-01-07 01:26:14 +08:00
2020-11-01 14:01:08 +08:00
size_t mid = total, low = 0, high = total - 1;
2015-01-07 01:26:14 +08:00
size_t startpos, endpos;
2019-11-10 03:27:57 +08:00
while (low <= high) {
if (query.startDatetime() > kdata[high].datetime) {
2015-01-07 01:26:14 +08:00
mid = high + 1;
break;
}
2019-11-10 03:27:57 +08:00
if (kdata[low].datetime >= query.startDatetime()) {
2015-01-07 01:26:14 +08:00
mid = low;
break;
}
mid = (low + high) / 2;
2019-11-10 03:27:57 +08:00
if (query.startDatetime() > kdata[mid].datetime) {
2015-01-07 01:26:14 +08:00
low = mid + 1;
} else {
high = mid - 1;
}
}
2019-11-10 03:27:57 +08:00
if (mid >= total) {
2015-01-07 01:26:14 +08:00
return false;
}
startpos = mid;
low = mid;
high = total - 1;
2019-11-10 03:27:57 +08:00
while (low <= high) {
if (query.endDatetime() > kdata[high].datetime) {
2015-01-07 01:26:14 +08:00
mid = high + 1;
break;
}
2019-11-10 03:27:57 +08:00
if (kdata[low].datetime >= query.endDatetime()) {
2015-01-07 01:26:14 +08:00
mid = low;
break;
}
mid = (low + high) / 2;
2019-11-10 03:27:57 +08:00
if (query.endDatetime() > kdata[mid].datetime) {
2015-01-07 01:26:14 +08:00
low = mid + 1;
} else {
high = mid - 1;
}
}
endpos = (mid >= total) ? total : mid;
2019-11-10 03:27:57 +08:00
if (startpos >= endpos) {
2015-01-07 01:26:14 +08:00
return false;
}
out_start = startpos;
out_end = endpos;
return true;
}
2020-11-11 23:58:08 +08:00
KRecord Stock::_getKRecordFromBuffer(size_t pos, KQuery::KType ktype) const {
std::shared_lock<std::shared_mutex> lock(*(m_data->pMutex[ktype]));
return m_data->pKData[ktype]->at(pos);
}
2020-10-09 01:02:52 +08:00
KRecord Stock::getKRecord(size_t pos, KQuery::KType kType) const {
HKU_IF_RETURN(!m_data, Null<KRecord>());
2021-01-04 23:10:06 +08:00
if (isBuffer(kType)) {
2020-11-11 23:58:08 +08:00
return _getKRecordFromBuffer(pos, kType);
}
HKU_IF_RETURN(!m_kdataDriver || pos >= size_t(Null<int64_t>()), Null<KRecord>());
2021-01-24 20:47:38 +08:00
auto klist =
m_kdataDriver->getConnect()->getKRecordList(market(), code(), KQuery(pos, pos + 1, kType));
2020-10-08 23:56:43 +08:00
return klist.size() > 0 ? klist[0] : Null<KRecord>();
2015-01-07 01:26:14 +08:00
}
2020-10-11 01:37:19 +08:00
KRecord Stock::getKRecord(const Datetime& datetime, KQuery::KType ktype) const {
2020-10-10 01:33:43 +08:00
KRecord result;
HKU_IF_RETURN(isNull(), result);
2020-10-08 15:46:41 +08:00
2020-10-11 01:37:19 +08:00
KQuery query = KQueryByDate(datetime, datetime + Minutes(1), ktype);
2021-01-24 00:38:22 +08:00
auto driver = m_kdataDriver->getConnect();
2021-01-24 20:47:38 +08:00
if (isBuffer(query.kType()) || driver->isIndexFirst()) {
2020-11-01 14:01:08 +08:00
size_t startix = 0, endix = 0;
2020-10-11 01:37:19 +08:00
return getIndexRange(query, startix, endix) ? getKRecord(startix, ktype) : Null<KRecord>();
2020-10-08 15:46:41 +08:00
}
2021-01-24 20:47:38 +08:00
auto klist = driver->getKRecordList(market(), code(), query);
2020-10-11 01:37:19 +08:00
return klist.size() > 0 ? klist[0] : Null<KRecord>();
2020-10-08 15:46:41 +08:00
}
2020-11-11 23:58:08 +08:00
KRecordList Stock::_getKRecordListFromBuffer(size_t start_ix, size_t end_ix,
KQuery::KType ktype) const {
std::shared_lock<std::shared_mutex> lock(*(m_data->pMutex[ktype]));
KRecordList result;
size_t total = m_data->pKData[ktype]->size();
2021-01-23 23:03:28 +08:00
HKU_IF_RETURN(total == 0, result);
HKU_WARN_IF_RETURN(start_ix >= end_ix || start_ix >= total, result,
"Invalid param (start_ix: {}, end_ix: {})! current total: {}", start_ix,
end_ix, total);
2020-11-11 23:58:08 +08:00
size_t length = end_ix > total ? total - start_ix : end_ix - start_ix;
result.resize(length);
std::memcpy(&(result.front()), &((*m_data->pKData[ktype])[start_ix]), sizeof(KRecord) * length);
return result;
}
2020-10-03 00:02:03 +08:00
KRecordList Stock::getKRecordList(const KQuery& query) const {
KRecordList result;
HKU_IF_RETURN(isNull(), result);
2020-10-03 23:58:04 +08:00
// 如果是在内存缓存中
2021-01-04 23:10:06 +08:00
if (isBuffer(query.kType())) {
2020-10-03 00:02:03 +08:00
size_t start_ix = 0, end_ix = 0;
if (query.queryType() == KQuery::DATE) {
if (!_getIndexRangeByDateFromBuffer(query, start_ix, end_ix)) {
return result;
}
} else {
2020-10-03 23:58:04 +08:00
if (query.start() < 0 || query.end() < 0) {
2020-10-05 00:07:29 +08:00
// 处理负数索引
2020-10-03 23:58:04 +08:00
if (!getIndexRange(query, start_ix, end_ix)) {
return result;
}
} else {
start_ix = query.start();
end_ix = query.end();
}
2020-10-03 00:02:03 +08:00
}
2020-11-11 23:58:08 +08:00
result = _getKRecordListFromBuffer(start_ix, end_ix, query.kType());
2020-10-03 00:02:03 +08:00
} else {
2020-10-06 17:56:10 +08:00
if (query.queryType() == KQuery::DATE) {
2021-01-24 20:47:38 +08:00
result =
m_kdataDriver->getConnect()->getKRecordList(m_data->m_market, m_data->m_code, query);
2020-10-06 17:56:10 +08:00
} else {
size_t start_ix = 0, end_ix = 0;
if (query.queryType() == KQuery::INDEX) {
if (query.start() < 0 || query.end() < 0) {
// 处理负数索引
if (!getIndexRange(query, start_ix, end_ix)) {
return result;
2020-10-05 18:04:53 +08:00
}
2020-10-06 17:56:10 +08:00
} else {
start_ix = query.start();
end_ix = query.end();
2020-10-05 18:04:53 +08:00
}
2020-10-05 00:07:29 +08:00
}
2021-01-24 20:47:38 +08:00
result = m_kdataDriver->getConnect()->getKRecordList(
2021-01-24 00:38:22 +08:00
m_data->m_market, m_data->m_code, KQuery(start_ix, end_ix, query.kType()));
2020-10-03 00:02:03 +08:00
}
}
2015-01-07 01:26:14 +08:00
return result;
}
2016-04-25 01:39:07 +08:00
DatetimeList Stock::getDatetimeList(const KQuery& query) const {
DatetimeList result;
2020-10-03 23:58:04 +08:00
KRecordList k_list = getKRecordList(query);
result.reserve(k_list.size());
for (auto& k : k_list) {
2020-11-02 23:47:15 +08:00
result.push_back(k.datetime); // cppcheck-suppress useStlAlgorithm
2016-04-25 01:39:07 +08:00
}
return result;
}
2019-02-11 16:33:55 +08:00
TimeLineList Stock::getTimeLineList(const KQuery& query) const {
2021-01-24 20:47:38 +08:00
return m_kdataDriver ? m_kdataDriver->getConnect()->getTimeLineList(market(), code(), query)
: TimeLineList();
2019-02-08 22:41:20 +08:00
}
2019-02-11 21:13:06 +08:00
TransList Stock::getTransList(const KQuery& query) const {
2021-01-24 20:47:38 +08:00
return m_kdataDriver ? m_kdataDriver->getConnect()->getTransList(market(), code(), query)
2021-01-24 00:38:22 +08:00
: TransList();
2019-02-11 21:13:06 +08:00
}
Parameter Stock::getFinanceInfo() const {
Parameter result;
HKU_IF_RETURN(type() != STOCKTYPE_A, result);
BaseInfoDriverPtr driver = StockManager::instance().getBaseInfoDriver();
if (driver) {
result = driver->getFinanceInfo(market(), code());
}
2019-11-10 03:27:57 +08:00
return result;
}
2019-04-06 02:40:53 +08:00
PriceList Stock::getHistoryFinanceInfo(const Datetime& date) const {
PriceList result;
HKU_IF_RETURN(type() != STOCKTYPE_A, result);
const StockManager& sm = StockManager::instance();
HistoryFinanceReader rd(sm.datadir() + "/downloads/finance");
result = rd.getHistoryFinanceInfo(date, market(), code());
return result;
2019-04-06 02:40:53 +08:00
}
// 判断是否在交易时间段内(不判断日期)
2021-01-06 00:06:58 +08:00
bool Stock::isTransactionTime(Datetime time) {
MarketInfo market_info = StockManager::instance().getMarketInfo(market());
HKU_IF_RETURN(market_info == Null<MarketInfo>(), false);
2021-01-01 23:26:16 +08:00
HKU_ERROR_IF_RETURN(market_info.openTime1() > market_info.closeTime1() ||
market_info.openTime2() > market_info.closeTime2(),
false, "Error transaction time in market({})!", market_info.market());
// 如果交易时间段的起始和终止时间都相等则认未做限定,全天都为可交易时间
HKU_IF_RETURN(market_info.openTime1() == market_info.closeTime1() &&
market_info.openTime2() == market_info.closeTime2(),
2021-01-01 23:26:16 +08:00
true);
Datetime today = Datetime::today();
Datetime openTime1 = today + market_info.openTime1();
Datetime closeTime1 = today + market_info.closeTime1();
2021-01-01 23:26:16 +08:00
HKU_IF_RETURN(time >= openTime1 && time < closeTime1, true); // close判断不包括等于
Datetime openTime2 = today + market_info.openTime2();
Datetime closeTime2 = today + market_info.closeTime2();
2021-01-01 23:26:16 +08:00
return time >= openTime2 && time < closeTime2;
}
2020-12-19 23:57:24 +08:00
void Stock::realtimeUpdate(KRecord record, KQuery::KType inktype) {
HKU_IF_RETURN(!isBuffer(inktype) || record.datetime.isNull() ||
StockManager::instance().isHoliday(record.datetime),
void());
2021-01-04 23:10:06 +08:00
string ktype(inktype);
2020-12-19 23:57:24 +08:00
to_upper(ktype);
2016-04-03 00:08:31 +08:00
// 加写锁
std::unique_lock<std::shared_mutex> lock(*(m_data->pMutex[ktype]));
if (m_data->pKData[ktype]->empty()) {
m_data->pKData[ktype]->push_back(record);
return;
}
KRecord& tmp = m_data->pKData[ktype]->back();
// 如果传入的记录日期等于最后一条记录日期,则更新最后一条记录;否则,追加入缓存
2016-04-03 00:08:31 +08:00
if (tmp.datetime == record.datetime) {
tmp = record;
} else if (tmp.datetime < record.datetime) {
m_data->pKData[ktype]->push_back(record);
} else {
HKU_INFO("Ignore record, datetime < last record.datetime!");
2016-04-03 00:08:31 +08:00
}
}
2019-03-03 20:47:28 +08:00
Stock HKU_API getStock(const string& querystr) {
2019-11-10 19:45:57 +08:00
const StockManager& sm = StockManager::instance();
2019-03-03 20:47:28 +08:00
return sm.getStock(querystr);
}
2019-11-10 03:27:57 +08:00
} // namespace hku