2015-01-07 01:26:14 +08:00
|
|
|
|
/*
|
|
|
|
|
* KQuery.h
|
|
|
|
|
*
|
|
|
|
|
* Created on: 2009-11-23
|
|
|
|
|
* Author: fasiondog
|
|
|
|
|
*/
|
|
|
|
|
|
2019-08-03 21:53:24 +08:00
|
|
|
|
#pragma once
|
2015-01-07 01:26:14 +08:00
|
|
|
|
#ifndef KQUERY_H_
|
|
|
|
|
#define KQUERY_H_
|
|
|
|
|
|
|
|
|
|
#include "KRecord.h"
|
|
|
|
|
|
|
|
|
|
namespace hku {
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 按索引方式查询K线数据条件
|
|
|
|
|
* @ingroup StockManage
|
|
|
|
|
*/
|
|
|
|
|
class HKU_API KQuery {
|
|
|
|
|
public:
|
|
|
|
|
///查询方式:索引或日期
|
|
|
|
|
enum QueryType {
|
|
|
|
|
INDEX = 0, ///<按索引方式查询
|
2019-11-10 19:45:57 +08:00
|
|
|
|
DATE = 1, ///<按日期方式查询
|
2015-01-07 01:26:14 +08:00
|
|
|
|
INVALID = 3
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
///查询K线类型:日线/周线等
|
2018-10-04 20:55:39 +08:00
|
|
|
|
/*enum KType {
|
2015-01-07 01:26:14 +08:00
|
|
|
|
//notes: 如添加新类型,请注意按大小顺序添加,否则可能出错
|
|
|
|
|
MIN = 0, ///<1分钟线
|
|
|
|
|
MIN5 = 1, ///<5分钟线
|
|
|
|
|
MIN15 = 2, ///<15分钟线
|
|
|
|
|
MIN30 = 3, ///<30分钟线
|
|
|
|
|
MIN60 = 4, ///<60分钟线
|
|
|
|
|
DAY = 5, ///<日线
|
|
|
|
|
WEEK = 6, ///<周线
|
|
|
|
|
MONTH = 7, ///<月线
|
|
|
|
|
QUARTER = 8, ///<季线
|
|
|
|
|
HALFYEAR = 9, ///<半年线
|
|
|
|
|
YEAR = 10, ///<年线
|
2018-04-16 03:46:43 +08:00
|
|
|
|
|
|
|
|
|
//BTC扩展
|
|
|
|
|
MIN3 = 11, ///<3分钟线
|
|
|
|
|
HOUR2 = 12, ///<2小时线
|
|
|
|
|
HOUR4 = 13, ///<4小时线
|
|
|
|
|
HOUR6 = 14, ///<6小时线
|
|
|
|
|
HOUR12 = 15, ///<12小时线
|
|
|
|
|
INVALID_KTYPE = 16
|
2018-10-04 20:55:39 +08:00
|
|
|
|
};*/
|
|
|
|
|
typedef string KType;
|
|
|
|
|
|
|
|
|
|
static const string MIN;
|
|
|
|
|
static const string MIN5;
|
|
|
|
|
static const string MIN15;
|
|
|
|
|
static const string MIN30;
|
|
|
|
|
static const string MIN60;
|
|
|
|
|
static const string DAY;
|
|
|
|
|
static const string WEEK;
|
|
|
|
|
static const string MONTH;
|
|
|
|
|
static const string QUARTER;
|
|
|
|
|
static const string HALFYEAR;
|
|
|
|
|
static const string YEAR;
|
|
|
|
|
static const string MIN3;
|
|
|
|
|
static const string HOUR2;
|
|
|
|
|
static const string HOUR4;
|
|
|
|
|
static const string HOUR6;
|
|
|
|
|
static const string HOUR12;
|
2019-11-10 19:45:57 +08:00
|
|
|
|
// static const string INVALID_KTYPE;
|
2015-01-07 01:26:14 +08:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 复权类型
|
|
|
|
|
* @note 日线以上,如周线/月线不支持复权
|
|
|
|
|
*/
|
|
|
|
|
enum RecoverType {
|
2019-11-10 19:45:57 +08:00
|
|
|
|
NO_RECOVER = 0, ///<不复权
|
|
|
|
|
FORWARD = 1, ///<前向复权
|
|
|
|
|
BACKWARD = 2, ///<后向复权
|
|
|
|
|
EQUAL_FORWARD = 3, ///<等比前向复权
|
|
|
|
|
EQUAL_BACKWARD = 4, ///<等比后向复权
|
2015-01-07 01:26:14 +08:00
|
|
|
|
INVALID_RECOVER_TYPE = 5
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/** 默认构造,按索引方式查询全部日线数据,不复权 */
|
|
|
|
|
KQuery()
|
|
|
|
|
: m_start(0),
|
2020-10-01 22:52:50 +08:00
|
|
|
|
m_end(Null<int64_t>()),
|
2015-01-07 01:26:14 +08:00
|
|
|
|
m_queryType(INDEX),
|
|
|
|
|
m_dataType(DAY),
|
2019-11-10 19:45:57 +08:00
|
|
|
|
m_recoverType(NO_RECOVER){};
|
2015-01-07 01:26:14 +08:00
|
|
|
|
|
|
|
|
|
/**
|
2016-04-03 00:08:31 +08:00
|
|
|
|
* K线查询,范围[start, end)
|
2015-01-07 01:26:14 +08:00
|
|
|
|
* @param start 起始索引,支持负数
|
|
|
|
|
* @param end 结束索引(不包含本身),支持负数
|
|
|
|
|
* @param dataType K线类型
|
|
|
|
|
* @param recoverType 复权类型
|
2016-04-03 00:08:31 +08:00
|
|
|
|
* @param queryType 默认按索引方式查询
|
2015-01-07 01:26:14 +08:00
|
|
|
|
*/
|
2020-11-02 23:47:15 +08:00
|
|
|
|
KQuery(int64_t start, // cppcheck-suppress [noExplicitConstructor]
|
|
|
|
|
int64_t end = Null<int64_t>(), KType dataType = DAY,
|
2019-11-10 19:45:57 +08:00
|
|
|
|
RecoverType recoverType = NO_RECOVER, QueryType queryType = INDEX)
|
2015-01-07 01:26:14 +08:00
|
|
|
|
: m_start(start),
|
|
|
|
|
m_end(end),
|
2016-04-03 00:08:31 +08:00
|
|
|
|
m_queryType(queryType),
|
2015-01-07 01:26:14 +08:00
|
|
|
|
m_dataType(dataType),
|
2020-06-10 00:17:24 +08:00
|
|
|
|
m_recoverType(recoverType) {
|
|
|
|
|
to_upper(m_dataType);
|
|
|
|
|
}
|
2015-01-07 01:26:14 +08:00
|
|
|
|
|
2019-12-02 00:23:12 +08:00
|
|
|
|
/**
|
|
|
|
|
* 按指定日期查询 K 线,范围[start, end)
|
|
|
|
|
* @param start 起始日期
|
|
|
|
|
* @param end 结束日期
|
2019-12-16 02:09:43 +08:00
|
|
|
|
* @param ktype K线类型
|
2019-12-02 00:23:12 +08:00
|
|
|
|
* @param recoverType 复权类型
|
|
|
|
|
*/
|
2020-11-02 23:47:15 +08:00
|
|
|
|
KQuery(Datetime start, // cppcheck-suppress [noExplicitConstructor]
|
|
|
|
|
Datetime end = Null<Datetime>(), KType ktype = DAY,
|
2019-12-02 00:23:12 +08:00
|
|
|
|
RecoverType recoverType = NO_RECOVER);
|
|
|
|
|
|
2015-01-07 01:26:14 +08:00
|
|
|
|
/**
|
2020-10-01 22:52:50 +08:00
|
|
|
|
* 按索引方式查询时,返回指定的起始索引,否则返回Null<int64_t>()
|
2015-01-07 01:26:14 +08:00
|
|
|
|
*/
|
2020-10-01 22:52:50 +08:00
|
|
|
|
int64_t start() const {
|
|
|
|
|
return m_queryType != INDEX ? Null<int64_t>() : m_start;
|
2015-01-07 01:26:14 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2020-10-01 22:52:50 +08:00
|
|
|
|
* 按索引方式查询时,返回指定的结束索引,否则返回Null<int64_t>()
|
2015-01-07 01:26:14 +08:00
|
|
|
|
*/
|
2020-10-01 22:52:50 +08:00
|
|
|
|
int64_t end() const {
|
|
|
|
|
return m_queryType != INDEX ? Null<int64_t>() : m_end;
|
2015-01-07 01:26:14 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 按日期方式查询时,返回指定的起始日期,否则返回Null<Datetime>()
|
|
|
|
|
*/
|
2019-02-11 21:13:06 +08:00
|
|
|
|
Datetime startDatetime() const;
|
2015-01-07 01:26:14 +08:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 按日期方式查询时,返回指定的结束日期,否则返回Null<Datetime>()
|
|
|
|
|
*/
|
2019-02-11 21:13:06 +08:00
|
|
|
|
Datetime endDatetime() const;
|
2015-01-07 01:26:14 +08:00
|
|
|
|
|
|
|
|
|
/** 获取查询条件类型 */
|
2019-11-10 19:45:57 +08:00
|
|
|
|
QueryType queryType() const {
|
|
|
|
|
return m_queryType;
|
|
|
|
|
}
|
2015-01-07 01:26:14 +08:00
|
|
|
|
|
|
|
|
|
/** 获取K线数据类型 */
|
2019-11-10 19:45:57 +08:00
|
|
|
|
// KType kType() const { return m_dataType; }
|
|
|
|
|
string kType() const {
|
|
|
|
|
return m_dataType;
|
|
|
|
|
}
|
2015-01-07 01:26:14 +08:00
|
|
|
|
|
|
|
|
|
/** 获取复权类型 */
|
2019-11-10 19:45:57 +08:00
|
|
|
|
RecoverType recoverType() const {
|
|
|
|
|
return m_recoverType;
|
|
|
|
|
}
|
2015-01-07 01:26:14 +08:00
|
|
|
|
|
|
|
|
|
/** 获取queryType名称,用于显示输出 */
|
|
|
|
|
static string getQueryTypeName(QueryType);
|
|
|
|
|
|
|
|
|
|
/** 获取KType名称,用于显示输出 */
|
|
|
|
|
static string getKTypeName(KType);
|
|
|
|
|
|
|
|
|
|
/** 获取recoverType名称,用于显示输出 */
|
|
|
|
|
static string getRecoverTypeName(RecoverType);
|
|
|
|
|
|
|
|
|
|
/** 根据字符串名称获取相应的queryType枚举值 */
|
|
|
|
|
static QueryType getQueryTypeEnum(const string&);
|
|
|
|
|
|
|
|
|
|
/** 根据字符串名称,获取相应的枚举值 */
|
|
|
|
|
static KType getKTypeEnum(const string&);
|
|
|
|
|
|
|
|
|
|
/** 根据字符串名称,获取相应的枚举值 */
|
|
|
|
|
static RecoverType getRecoverTypeEnum(const string&);
|
|
|
|
|
|
2016-04-03 00:08:31 +08:00
|
|
|
|
private:
|
2020-10-01 22:52:50 +08:00
|
|
|
|
int64_t m_start;
|
|
|
|
|
int64_t m_end;
|
2015-01-07 01:26:14 +08:00
|
|
|
|
QueryType m_queryType;
|
|
|
|
|
KType m_dataType;
|
|
|
|
|
RecoverType m_recoverType;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/**
|
2016-04-03 00:08:31 +08:00
|
|
|
|
* 构造按索引方式K线查询,范围[start, end)
|
|
|
|
|
* @param start 起始索引,支持负数
|
|
|
|
|
* @param end 结束索引(不包含本身),支持负数
|
|
|
|
|
* @param dataType K线类型
|
|
|
|
|
* @param recoverType 复权类型
|
|
|
|
|
* @see KQuery
|
|
|
|
|
* @ingroup StockManage*
|
2015-01-07 01:26:14 +08:00
|
|
|
|
*/
|
2020-10-01 22:52:50 +08:00
|
|
|
|
KQuery HKU_API KQueryByIndex(int64_t start = 0, int64_t end = Null<int64_t>(),
|
2019-11-10 19:45:57 +08:00
|
|
|
|
KQuery::KType dataType = KQuery::DAY,
|
|
|
|
|
KQuery::RecoverType recoverType = KQuery::NO_RECOVER);
|
2015-01-07 01:26:14 +08:00
|
|
|
|
|
2020-10-01 22:52:50 +08:00
|
|
|
|
inline KQuery KQueryByIndex(int64_t start, int64_t end, KQuery::KType dataType,
|
2019-12-02 00:23:12 +08:00
|
|
|
|
KQuery::RecoverType recoverType) {
|
|
|
|
|
return KQuery(start, end, dataType, recoverType, KQuery::INDEX);
|
|
|
|
|
}
|
|
|
|
|
|
2015-01-07 01:26:14 +08:00
|
|
|
|
/**
|
2016-04-03 00:08:31 +08:00
|
|
|
|
* 构造按日期方式K线查询,范围[startDatetime, endDatetime)
|
|
|
|
|
* @param start 起始日期
|
|
|
|
|
* @param end 结束日期(不包含本身)
|
|
|
|
|
* @param dataType K线类型
|
|
|
|
|
* @param recoverType 复权类型
|
2015-01-07 01:26:14 +08:00
|
|
|
|
* @see KQuery
|
|
|
|
|
* @ingroup StockManage
|
|
|
|
|
*/
|
2019-11-10 19:45:57 +08:00
|
|
|
|
KQuery HKU_API KQueryByDate(const Datetime& start = Datetime::min(),
|
|
|
|
|
const Datetime& end = Null<Datetime>(),
|
|
|
|
|
KQuery::KType dataType = KQuery::DAY,
|
|
|
|
|
KQuery::RecoverType recoverType = KQuery::NO_RECOVER);
|
2015-01-07 01:26:14 +08:00
|
|
|
|
|
2019-12-02 00:23:12 +08:00
|
|
|
|
inline KQuery KQueryByDate(const Datetime& start, const Datetime& end, KQuery::KType dataType,
|
|
|
|
|
KQuery::RecoverType recoverType) {
|
|
|
|
|
return KQuery(start, end, dataType, recoverType);
|
|
|
|
|
}
|
|
|
|
|
|
2015-01-07 01:26:14 +08:00
|
|
|
|
/**
|
|
|
|
|
* 输出KQuery信息,如:KQuery(start, end, queryType, kType, recoverType)
|
|
|
|
|
* @ingroup StockManage
|
|
|
|
|
*/
|
2019-11-10 19:45:57 +08:00
|
|
|
|
HKU_API std::ostream& operator<<(std::ostream& os, const KQuery& query);
|
2016-05-18 02:03:29 +08:00
|
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
//
|
|
|
|
|
// 关系比较函数, 不直接在类中定义是为了支持 Null<>() == d,Null可以放在左边
|
|
|
|
|
//
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
bool operator==(const KQuery&, const KQuery&);
|
|
|
|
|
bool operator!=(const KQuery&, const KQuery&);
|
|
|
|
|
|
|
|
|
|
inline bool operator!=(const KQuery& q1, const KQuery& q2) {
|
2020-11-01 14:01:08 +08:00
|
|
|
|
// cppcheck-suppress [mismatchingContainerExpression]
|
2019-11-10 19:45:57 +08:00
|
|
|
|
if (q1.start() != q2.start() || q1.end() != q2.end() || q1.queryType() != q2.queryType() ||
|
|
|
|
|
q1.kType() != q2.kType() || q1.recoverType() != q2.recoverType()) {
|
2016-05-18 02:03:29 +08:00
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
inline bool operator==(const KQuery& q1, const KQuery& q2) {
|
|
|
|
|
if (q1 != q2) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 提供KQuery的Null值
|
|
|
|
|
* @ingroup StockManage
|
|
|
|
|
*/
|
|
|
|
|
template <>
|
|
|
|
|
class Null<KQuery> {
|
|
|
|
|
public:
|
|
|
|
|
Null() {}
|
|
|
|
|
operator KQuery() {
|
2020-10-01 22:52:50 +08:00
|
|
|
|
return KQuery(Null<int64_t>(), Null<int64_t>(),
|
2019-11-10 19:45:57 +08:00
|
|
|
|
"", // KQuery::INVALID_KTYPE,
|
|
|
|
|
KQuery::INVALID_RECOVER_TYPE, KQuery::INVALID);
|
2016-05-18 02:03:29 +08:00
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2019-11-10 19:45:57 +08:00
|
|
|
|
} // namespace hku
|
2015-01-07 01:26:14 +08:00
|
|
|
|
|
|
|
|
|
#endif /* KQUERY_H_ */
|