hikyuu2/hikyuu_cpp/hikyuu/KQuery.h

256 lines
7.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, ///<按索引方式查询
DATE = 1, ///<按日期方式查询
INVALID = 3
};
///查询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;
//static const string INVALID_KTYPE;
2015-01-07 01:26:14 +08:00
/**
*
* @note 线线/线
*/
enum RecoverType {
NO_RECOVER = 0, ///<不复权
FORWARD = 1, ///<前向复权
BACKWARD = 2, ///<后向复权
EQUAL_FORWARD = 3, ///<等比前向复权
EQUAL_BACKWARD = 4, ///<等比后向复权
INVALID_RECOVER_TYPE = 5
};
/** 默认构造,按索引方式查询全部日线数据,不复权 */
KQuery()
: m_start(0),
m_end(Null<int64>()),
2015-01-07 01:26:14 +08:00
m_queryType(INDEX),
m_dataType(DAY),
m_recoverType(NO_RECOVER) { };
/**
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
*/
KQuery(int64 start,
int64 end = Null<int64>(),
2015-01-07 01:26:14 +08:00
KType dataType = DAY,
2016-04-03 00:08:31 +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),
m_recoverType(recoverType) { }
/**
* Null<int64>()
2015-01-07 01:26:14 +08:00
*/
int64 start() const {
return m_queryType != INDEX ? Null<int64>() : m_start;
2015-01-07 01:26:14 +08:00
}
/**
* Null<int64>()
2015-01-07 01:26:14 +08:00
*/
int64 end() const {
return m_queryType != INDEX ? Null<int64>() : 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
/** 获取查询条件类型 */
2016-05-16 01:45:01 +08:00
QueryType queryType() const { return m_queryType; }
2015-01-07 01:26:14 +08:00
/** 获取K线数据类型 */
//KType kType() const { return m_dataType; }
string kType() const { return m_dataType; }
2015-01-07 01:26:14 +08:00
/** 获取复权类型 */
2016-05-16 01:45:01 +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 m_start;
int64 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 start = 0,
int64 end = Null<int64>(),
2016-04-03 00:08:31 +08:00
KQuery::KType dataType = KQuery::DAY,
2019-02-11 21:13:06 +08:00
KQuery::RecoverType recoverType = KQuery::NO_RECOVER);
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-02-11 21:13:06 +08:00
KQuery HKU_API KQueryByDate(
const Datetime& start = Datetime::min(),
2016-04-03 00:08:31 +08:00
const Datetime& end = Null<Datetime>(),
KQuery::KType dataType = KQuery::DAY,
2019-02-11 21:13:06 +08:00
KQuery::RecoverType recoverType = KQuery::NO_RECOVER);
2015-01-07 01:26:14 +08:00
/**
* KQuery信息KQuery(start, end, queryType, kType, recoverType)
* @ingroup StockManage
*/
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) {
if (q1.start() != q2.start()
|| q1.end() != q2.end()
|| q1.queryType() != q2.queryType()
|| q1.kType() != q2.kType()
|| q1.recoverType() != q2.recoverType()) {
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>(),
Null<int64>(),
"", //KQuery::INVALID_KTYPE,
2016-05-18 02:03:29 +08:00
KQuery::INVALID_RECOVER_TYPE,
KQuery::INVALID
);
}
};
2015-01-07 01:26:14 +08:00
} /* namespace */
#endif /* KQUERY_H_ */