hikyuu2/hikyuu_cpp/hikyuu/KQuery.h

277 lines
8.1 KiB
C++
Raw Normal View History

2015-01-07 01:26:14 +08:00
/*
* KQuery.h
*
* Created on: 2009-11-23
* Author: fasiondog
*/
#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, ///<按日期方式查询
INVALID = 2
2015-01-07 01:26:14 +08:00
};
///查询K线类型日线/周线等
/*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
};*/
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
2021-01-04 00:16:19 +08:00
/** 获取所有的 KType */
2021-01-23 23:03:28 +08:00
static vector<string>& getAllKType();
2021-01-04 00:16:19 +08:00
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),
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
/**
* K 线[start, end)
* @param start
* @param end
* @param ktype K线类型
* @param recoverType
*/
2020-11-02 23:47:15 +08:00
KQuery(Datetime start, // cppcheck-suppress [noExplicitConstructor]
Datetime end = Null<Datetime>(), KType ktype = DAY,
RecoverType recoverType = NO_RECOVER);
2015-01-07 01:26:14 +08:00
/**
* Null<int64_t>()
2015-01-07 01:26:14 +08:00
*/
int64_t start() const {
return m_queryType != INDEX ? Null<int64_t>() : m_start;
2015-01-07 01:26:14 +08:00
}
/**
* Null<int64_t>()
2015-01-07 01:26:14 +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:
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
*/
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
inline KQuery KQueryByIndex(int64_t start, int64_t end, KQuery::KType dataType,
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
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<>() == dNull可以放在左边
//
///////////////////////////////////////////////////////////////////////////////
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() {
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_ */