hikyuu 2.6.2__py3-none-win_amd64.whl → 2.6.5__py3-none-win_amd64.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (136) hide show
  1. hikyuu/__init__.py +1 -1
  2. hikyuu/__init__.pyi +28 -13
  3. hikyuu/analysis/__init__.pyi +20 -0
  4. hikyuu/analysis/analysis.pyi +21 -1
  5. hikyuu/core.pyi +22 -2
  6. hikyuu/cpp/core310.pyd +0 -0
  7. hikyuu/cpp/core310.pyi +499 -50
  8. hikyuu/cpp/core311.pyd +0 -0
  9. hikyuu/cpp/core311.pyi +499 -50
  10. hikyuu/cpp/core312.pyd +0 -0
  11. hikyuu/cpp/core312.pyi +499 -50
  12. hikyuu/cpp/core313.pyd +0 -0
  13. hikyuu/cpp/core313.pyi +499 -50
  14. hikyuu/cpp/core39.pyd +0 -0
  15. hikyuu/cpp/core39.pyi +499 -50
  16. hikyuu/cpp/hikyuu.dll +0 -0
  17. hikyuu/cpp/hikyuu.lib +0 -0
  18. hikyuu/data/common.py +1 -1
  19. hikyuu/data/common_mysql.py +19 -0
  20. hikyuu/data/common_pytdx.py +2 -0
  21. hikyuu/data/common_sqlite3.py +1 -0
  22. hikyuu/data/hku_config_template.py +14 -0
  23. hikyuu/data/mysql_upgrade/0028.sql +95 -0
  24. hikyuu/data/pytdx_to_h5.py +53 -13
  25. hikyuu/data/pytdx_to_mysql.py +42 -9
  26. hikyuu/data/pytdx_to_taos.py +736 -0
  27. hikyuu/data/sqlite_upgrade/0028.sql +97 -0
  28. hikyuu/draw/__init__.pyi +1 -1
  29. hikyuu/draw/drawplot/__init__.pyi +1 -1
  30. hikyuu/draw/drawplot/bokeh_draw.pyi +24 -9
  31. hikyuu/draw/drawplot/echarts_draw.pyi +24 -9
  32. hikyuu/draw/drawplot/matplotlib_draw.py +26 -4
  33. hikyuu/draw/drawplot/matplotlib_draw.pyi +24 -9
  34. hikyuu/draw/kaufman.py +2 -2
  35. hikyuu/draw/kaufman.pyi +2 -2
  36. hikyuu/examples/notebook/001-overview.ipynb +65 -100
  37. hikyuu/examples/notebook/004-IndicatorOverview.ipynb +34 -32
  38. hikyuu/examples/notebook/007-SystemDetails.ipynb +64 -50
  39. hikyuu/examples/notebook/010-Portfolio.ipynb +120 -124
  40. hikyuu/extend.py +1 -1
  41. hikyuu/extend.pyi +24 -9
  42. hikyuu/fetcher/stock/zh_block_em.py +349 -5
  43. hikyuu/fetcher/stock/zh_stock_a_pytdx.py +2 -1
  44. hikyuu/gui/HikyuuTDX.py +47 -24
  45. hikyuu/gui/data/ImportBlockInfoTask.py +1 -1
  46. hikyuu/gui/data/ImportHistoryFinanceTask.py +48 -44
  47. hikyuu/gui/data/ImportPytdxTimeToH5Task.py +3 -1
  48. hikyuu/gui/data/ImportPytdxToH5Task.py +4 -2
  49. hikyuu/gui/data/ImportPytdxTransToH5Task.py +3 -1
  50. hikyuu/gui/data/ImportWeightToSqliteTask.py +2 -1
  51. hikyuu/gui/data/ImportZhBond10Task.py +1 -1
  52. hikyuu/gui/data/MainWindow.py +123 -106
  53. hikyuu/gui/data/UsePytdxImportToH5Thread.py +7 -3
  54. hikyuu/gui/data/UseQmtImportToH5Thread.py +1 -0
  55. hikyuu/gui/data/UseTdxImportToH5Thread.py +2 -1
  56. hikyuu/hub.pyi +6 -6
  57. hikyuu/include/hikyuu/Block.h +20 -0
  58. hikyuu/include/hikyuu/KQuery.h +8 -0
  59. hikyuu/include/hikyuu/MarketInfo.h +6 -0
  60. hikyuu/include/hikyuu/Stock.h +1 -1
  61. hikyuu/include/hikyuu/StockManager.h +6 -0
  62. hikyuu/include/hikyuu/data_driver/BaseInfoDriver.h +35 -0
  63. hikyuu/include/hikyuu/indicator/Indicator.h +5 -0
  64. hikyuu/include/hikyuu/indicator/IndicatorImp.h +8 -3
  65. hikyuu/include/hikyuu/indicator/build_in.h +1 -0
  66. hikyuu/include/hikyuu/indicator/crt/BARSLASTCOUNT.h +33 -0
  67. hikyuu/include/hikyuu/indicator/crt/INSUM.h +5 -10
  68. hikyuu/include/hikyuu/indicator/crt/RSI.h +2 -18
  69. hikyuu/include/hikyuu/indicator/imp/IBarsLastCount.h +27 -0
  70. hikyuu/include/hikyuu/plugin/backtest.h +3 -2
  71. hikyuu/include/hikyuu/plugin/device.h +6 -3
  72. hikyuu/include/hikyuu/plugin/extind.h +150 -0
  73. hikyuu/include/hikyuu/plugin/interface/BackTestPluginInterface.h +2 -1
  74. hikyuu/include/hikyuu/plugin/interface/DevicePluginInterface.h +1 -0
  75. hikyuu/include/hikyuu/plugin/interface/ExtendIndicatorsPluginInterface.h +26 -0
  76. hikyuu/include/hikyuu/plugin/interface/TMReportPluginInterface.h +80 -0
  77. hikyuu/include/hikyuu/plugin/interface/plugins.h +4 -0
  78. hikyuu/include/hikyuu/strategy/BrokerTradeManager.h +7 -5
  79. hikyuu/include/hikyuu/strategy/Strategy.h +22 -9
  80. hikyuu/include/hikyuu/trade_manage/OrderBrokerBase.h +11 -4
  81. hikyuu/include/hikyuu/trade_manage/Performance.h +17 -9
  82. hikyuu/include/hikyuu/trade_manage/PositionExtInfo.h +92 -0
  83. hikyuu/include/hikyuu/trade_manage/PositionRecord.h +7 -1
  84. hikyuu/include/hikyuu/trade_manage/TradeManager.h +8 -5
  85. hikyuu/include/hikyuu/trade_manage/TradeManagerBase.h +66 -5
  86. hikyuu/include/hikyuu/trade_manage/TradeRecord.h +9 -1
  87. hikyuu/include/hikyuu/trade_sys/multifactor/MultiFactorBase.h +8 -5
  88. hikyuu/include/hikyuu/trade_sys/multifactor/crt/MF_EqualWeight.h +4 -1
  89. hikyuu/include/hikyuu/trade_sys/multifactor/crt/MF_ICIRWeight.h +4 -1
  90. hikyuu/include/hikyuu/trade_sys/multifactor/crt/MF_ICWeight.h +4 -1
  91. hikyuu/include/hikyuu/trade_sys/multifactor/crt/MF_Weight.h +4 -1
  92. hikyuu/include/hikyuu/trade_sys/multifactor/imp/EqualWeightMultiFactor.h +2 -1
  93. hikyuu/include/hikyuu/trade_sys/multifactor/imp/ICIRMultiFactor.h +2 -1
  94. hikyuu/include/hikyuu/trade_sys/multifactor/imp/ICMultiFactor.h +2 -1
  95. hikyuu/include/hikyuu/trade_sys/multifactor/imp/WeightMultiFactor.h +1 -1
  96. hikyuu/include/hikyuu/trade_sys/selector/crt/SE_Optimal.h +8 -0
  97. hikyuu/include/hikyuu/trade_sys/selector/imp/optimal/OptimalEvaluateSelector.h +28 -0
  98. hikyuu/include/hikyuu/trade_sys/selector/imp/optimal/OptimalSelectorBase.h +1 -0
  99. hikyuu/include/hikyuu/trade_sys/system/TradeRequest.h +7 -4
  100. hikyuu/include/hikyuu/trade_sys/system/imp/WalkForwardTradeManager.h +17 -13
  101. hikyuu/include/hikyuu/utilities/DllLoader.h +226 -0
  102. hikyuu/include/hikyuu/utilities/datetime/Datetime.h +20 -0
  103. hikyuu/include/hikyuu/utilities/datetime/TimeDelta.h +6 -0
  104. hikyuu/include/hikyuu/utilities/plugin/PluginLoader.h +10 -10
  105. hikyuu/include/hikyuu/utilities/thread/{MQStealThreadPool.h → GlobalMQStealThreadPool.h} +12 -12
  106. hikyuu/include/hikyuu/utilities/thread/GlobalMQThreadPool.h +271 -0
  107. hikyuu/include/hikyuu/utilities/thread/{StealThreadPool.h → GlobalStealThreadPool.h} +11 -10
  108. hikyuu/include/hikyuu/utilities/thread/GlobalThreadPool.h +224 -0
  109. hikyuu/include/hikyuu/utilities/thread/InterruptFlag.h +16 -0
  110. hikyuu/include/hikyuu/utilities/thread/MQThreadPool.h +40 -77
  111. hikyuu/include/hikyuu/utilities/thread/ThreadPool.h +31 -59
  112. hikyuu/include/hikyuu/utilities/thread/ThreadSafeQueue.h +4 -0
  113. hikyuu/include/hikyuu/utilities/thread/algorithm.h +9 -9
  114. hikyuu/include/hikyuu/utilities/thread/thread.h +4 -0
  115. hikyuu/include/hikyuu/version.h +4 -4
  116. hikyuu/plugin/backtest.dll +0 -0
  117. hikyuu/plugin/dataserver.dll +0 -0
  118. hikyuu/plugin/device.dll +0 -0
  119. hikyuu/plugin/extind.dll +0 -0
  120. hikyuu/plugin/import2hdf5.dll +0 -0
  121. hikyuu/plugin/tmreport.dll +0 -0
  122. hikyuu/trade_manage/__init__.pyi +23 -8
  123. hikyuu/trade_manage/broker.py +8 -8
  124. hikyuu/trade_manage/broker.pyi +4 -4
  125. hikyuu/trade_manage/broker_easytrader.py +3 -3
  126. hikyuu/trade_manage/broker_easytrader.pyi +2 -2
  127. hikyuu/trade_manage/broker_mail.py +2 -2
  128. hikyuu/trade_manage/broker_mail.pyi +2 -2
  129. hikyuu/trade_manage/trade.pyi +23 -8
  130. hikyuu/util/singleton.pyi +1 -1
  131. {hikyuu-2.6.2.dist-info → hikyuu-2.6.5.dist-info}/METADATA +4 -3
  132. {hikyuu-2.6.2.dist-info → hikyuu-2.6.5.dist-info}/RECORD +136 -121
  133. {hikyuu-2.6.2.dist-info → hikyuu-2.6.5.dist-info}/LICENSE +0 -0
  134. {hikyuu-2.6.2.dist-info → hikyuu-2.6.5.dist-info}/WHEEL +0 -0
  135. {hikyuu-2.6.2.dist-info → hikyuu-2.6.5.dist-info}/entry_points.txt +0 -0
  136. {hikyuu-2.6.2.dist-info → hikyuu-2.6.5.dist-info}/top_level.txt +0 -0
@@ -258,7 +258,8 @@ public:
258
258
  */
259
259
  virtual TradeRecord buy(const Datetime& datetime, const Stock& stock, price_t realPrice,
260
260
  double number, price_t stoploss = 0.0, price_t goalPrice = 0.0,
261
- price_t planPrice = 0.0, SystemPart from = PART_INVALID) override;
261
+ price_t planPrice = 0.0, SystemPart from = PART_INVALID,
262
+ const string& remark = "") override;
262
263
 
263
264
  /**
264
265
  * 卖出操作
@@ -275,7 +276,7 @@ public:
275
276
  virtual TradeRecord sell(const Datetime& datetime, const Stock& stock, price_t realPrice,
276
277
  double number = MAX_DOUBLE, price_t stoploss = 0.0,
277
278
  price_t goalPrice = 0.0, price_t planPrice = 0.0,
278
- SystemPart from = PART_INVALID) override;
279
+ SystemPart from = PART_INVALID, const string& remark = "") override;
279
280
 
280
281
  /**
281
282
  * 卖空
@@ -291,8 +292,8 @@ public:
291
292
  */
292
293
  virtual TradeRecord sellShort(const Datetime& datetime, const Stock& stock, price_t realPrice,
293
294
  double number, price_t stoploss = 0.0, price_t goalPrice = 0.0,
294
- price_t planPrice = 0.0,
295
- SystemPart from = PART_INVALID) override {
295
+ price_t planPrice = 0.0, SystemPart from = PART_INVALID,
296
+ const string& remark = "") override {
296
297
  HKU_WARN("The subclass does not implement this method");
297
298
  return TradeRecord();
298
299
  }
@@ -312,7 +313,8 @@ public:
312
313
  virtual TradeRecord buyShort(const Datetime& datetime, const Stock& stock, price_t realPrice,
313
314
  double number = MAX_DOUBLE, price_t stoploss = 0.0,
314
315
  price_t goalPrice = 0.0, price_t planPrice = 0.0,
315
- SystemPart from = PART_INVALID) override {
316
+ SystemPart from = PART_INVALID,
317
+ const string& remark = "") override {
316
318
  HKU_WARN("The subclass does not implement this method");
317
319
  return TradeRecord();
318
320
  }
@@ -128,6 +128,24 @@ public:
128
128
  KData getLastKData(const Stock& stk, size_t lastnum, const KQuery::KType& ktype,
129
129
  KQuery::RecoverType recover_type = KQuery::NO_RECOVER) const;
130
130
 
131
+ /**
132
+ * @brief 按股下单,+正数买入,-负数卖出
133
+ * @param stk 交易标的
134
+ * @param num 交易数量
135
+ * @param remark 交易备注
136
+ * @return TradeRecord
137
+ */
138
+ virtual TradeRecord order(const Stock& stk, double num, const string& remark = "");
139
+
140
+ /**
141
+ * @brief 按价值下单,即买入指定资金数量的股票
142
+ * @param stk 交易标的
143
+ * @param value 价值
144
+ * @param remark 交易备注
145
+ * @return TradeRecord
146
+ */
147
+ virtual TradeRecord orderValue(const Stock& stk, price_t value, const string& remark = "");
148
+
131
149
  virtual KData getKData(const Stock& stk, const Datetime& start_date, const Datetime& end_date,
132
150
  const KQuery::KType& ktype,
133
151
  KQuery::RecoverType recover_type = KQuery::NO_RECOVER) const;
@@ -144,18 +162,13 @@ public:
144
162
  }
145
163
 
146
164
  virtual TradeRecord buy(const Stock& stk, price_t price, double num, double stoploss = 0.0,
147
- double goal_price = 0.0,
148
- SystemPart part_from = SystemPart::PART_SIGNAL) {
149
- HKU_ASSERT(m_tm);
150
- return m_tm->buy(Datetime::now(), stk, price, num, stoploss, goal_price, price, part_from);
151
- }
165
+ double goal_price = 0.0, SystemPart part_from = SystemPart::PART_SIGNAL,
166
+ const string& remark = "");
152
167
 
153
168
  virtual TradeRecord sell(const Stock& stk, price_t price, double num, price_t stoploss = 0.0,
154
169
  price_t goal_price = 0.0,
155
- SystemPart part_from = SystemPart::PART_SIGNAL) {
156
- HKU_ASSERT(m_tm);
157
- return m_tm->sell(Datetime::now(), stk, price, num, stoploss, goal_price, price, part_from);
158
- }
170
+ SystemPart part_from = SystemPart::PART_SIGNAL,
171
+ const string& remark = "");
159
172
 
160
173
  virtual bool isBacktesting() const {
161
174
  return false;
@@ -71,9 +71,10 @@ public:
71
71
  * @param stoploss 预期的止损价
72
72
  * @param goalPrice 预期的目标价位
73
73
  * @param from 系统部件来源
74
+ * @param remark 备注信息
74
75
  */
75
76
  void buy(Datetime datetime, const string& market, const string& code, price_t price, double num,
76
- price_t stoploss, price_t goalPrice, SystemPart from) noexcept;
77
+ price_t stoploss, price_t goalPrice, SystemPart from, const string& remark) noexcept;
77
78
 
78
79
  /**
79
80
  * 执行卖出操作
@@ -85,9 +86,11 @@ public:
85
86
  * @param stoploss 新的预期止损价
86
87
  * @param goalPrice 新的预期目标价位
87
88
  * @param from 系统部件来源
89
+ * @param remark 备注信息
88
90
  */
89
91
  void sell(Datetime datetime, const string& market, const string& code, price_t price,
90
- double num, price_t stoploss, price_t goalPrice, SystemPart from) noexcept;
92
+ double num, price_t stoploss, price_t goalPrice, SystemPart from,
93
+ const string& remark) noexcept;
91
94
 
92
95
  /**
93
96
  * 获取当前资产信息
@@ -123,9 +126,11 @@ public:
123
126
  * @param stoploss 预期的止损价
124
127
  * @param goalPrice 预期的目标价位
125
128
  * @param from 系统部件来源
129
+ * @param remark 备注信息
126
130
  */
127
131
  virtual void _buy(Datetime datetime, const string& market, const string& code, price_t price,
128
- double num, price_t stoploss, price_t goalPrice, SystemPart from) = 0;
132
+ double num, price_t stoploss, price_t goalPrice, SystemPart from,
133
+ const string& remark) = 0;
129
134
 
130
135
  /**
131
136
  * 子类实现接口,执行实际卖出操作
@@ -137,9 +142,11 @@ public:
137
142
  * @param stoploss 新预期的止损价
138
143
  * @param goalPrice 新预期的目标价位
139
144
  * @param from 系统部件来源
145
+ * @param remark 备注信息
140
146
  */
141
147
  virtual void _sell(Datetime datetime, const string& market, const string& code, price_t price,
142
- double num, price_t stoploss, price_t goalPrice, SystemPart from) = 0;
148
+ double num, price_t stoploss, price_t goalPrice, SystemPart from,
149
+ const string& remark) = 0;
143
150
 
144
151
  virtual string _getAssetInfo() {
145
152
  return string();
@@ -10,7 +10,7 @@
10
10
  #define PERFORMANCE_H_
11
11
 
12
12
  #include <boost/function.hpp>
13
- #include "TradeManager.h"
13
+ #include "TradeManagerBase.h"
14
14
 
15
15
  namespace hku {
16
16
 
@@ -23,14 +23,15 @@ public:
23
23
  Performance();
24
24
  virtual ~Performance();
25
25
 
26
- Performance(const Performance& other) : m_result(other.m_result) {}
27
- Performance(Performance&& other) : m_result(std::move(other.m_result)) {}
26
+ Performance(const Performance& other) = default;
27
+ Performance(Performance&& other)
28
+ : m_result(std::move(other.m_result)), m_keys(std::move(other.m_keys)) {}
28
29
 
29
30
  Performance& operator=(const Performance& other);
30
31
  Performance& operator=(Performance&& other);
31
32
 
32
33
  /** 是否为合法的统计项 */
33
- static bool exist(const string& key);
34
+ bool exist(const string& key);
34
35
 
35
36
  /** 复位,清除已计算的结果 */
36
37
  void reset();
@@ -44,12 +45,12 @@ public:
44
45
  }
45
46
 
46
47
  /**
47
- * 简单的文本统计报告,用于直接输出打印
48
- * @param tm
48
+ * 简单的文本统计报告,用于直接输出打印。
49
+ * @note 只有运行 statistics 后或 Performance 本身为从 TM 获取的结果时才生效
49
50
  * @param datetime 指定的截止时刻
50
51
  * @return
51
52
  */
52
- string report(const TradeManagerPtr& tm, const Datetime& datetime = Datetime::now());
53
+ string report();
53
54
 
54
55
  /**
55
56
  * 根据交易记录,统计截至某一时刻的系统绩效, datetime必须大于等于lastDatetime,
@@ -61,7 +62,7 @@ public:
61
62
 
62
63
  /** 获取所有统计项名称,顺序与 values 相同 */
63
64
  const StringList& names() const {
64
- return ms_keys;
65
+ return m_keys;
65
66
  }
66
67
 
67
68
  /** 获取所有统计项值,顺序与 names 相同*/
@@ -71,9 +72,16 @@ public:
71
72
  typedef map_type::iterator iterator;
72
73
  typedef map_type::const_iterator const_iterator;
73
74
 
75
+ const map_type& getAll() const {
76
+ return m_result;
77
+ }
78
+
79
+ void addKey(const string& key);
80
+ void setValue(const string& key, double value);
81
+
74
82
  private:
75
83
  map_type m_result;
76
- static StringList ms_keys; // 保存统计项顺序, map/unordered_map都不能保持按插入顺序遍历
84
+ StringList m_keys; // 保存统计项顺序, map/unordered_map都不能保持按插入顺序遍历
77
85
  };
78
86
 
79
87
  } /* namespace hku */
@@ -0,0 +1,92 @@
1
+ /*
2
+ * Copyright (c) 2025 hikyuu.org
3
+ *
4
+ * Created on: 2025-05-30
5
+ * Author: fasiondog
6
+ */
7
+
8
+ #pragma once
9
+
10
+ #include "PositionRecord.h"
11
+
12
+ namespace hku {
13
+ // 持仓扩展信息(只适合一买一卖的情况,一买多卖的情况,部分统计不准确仅供参考)
14
+ struct HKU_API PositionExtInfo final {
15
+ PositionRecord position;
16
+ price_t maxHighPrice{0.}; // 期间最高价最大值
17
+ price_t minLowPrice{0.0}; // 期间最低价最小值
18
+ price_t maxClosePrice{0.}; // 期间收盘价最高值
19
+ price_t minClosePrice{0.0}; // 期间收盘价最低值
20
+ price_t currentClosePrice{0.}; // 当前收盘价
21
+ price_t maxPullBack1{0.}; // 最大回撤百分比1(仅使用最大收盘价和最低收盘价计算)(负数)
22
+ price_t maxPullBack2{0.}; // 最大回撤百分比2(使用期间最高价最大值和最低价最小值计算)(负数)
23
+ price_t currentProfit{0.}; // 当前浮动盈亏(不含预计卖出成本)
24
+
25
+ /** 当前回撤百分比1(仅使用最大收盘价和当前收盘价计算) */
26
+ price_t currentPullBack1() const {
27
+ price_t ret = (maxClosePrice - currentClosePrice) / maxClosePrice;
28
+ return ret > 0. ? 0. : ret;
29
+ }
30
+
31
+ /** 当前回撤百分比2(使用期间最高价最大值和当前收盘价计算) */
32
+ price_t currentPullBack2() const {
33
+ price_t ret = (maxHighPrice - currentClosePrice) / maxHighPrice;
34
+ return ret > 0. ? 0. : ret;
35
+ }
36
+
37
+ /** 期间最大浮盈1 (正数, 仅使用收盘价计算, 不含预计卖出成本, 多次买卖时统计不准) */
38
+ price_t maxFloatingProfit1() const {
39
+ price_t ret = maxClosePrice * position.number + position.sellMoney - position.buyMoney;
40
+ return ret < 0. ? 0. : ret;
41
+ }
42
+
43
+ /** 期间最大浮盈2 (正数, 使用最高值最大值进行计算,不含预计卖出成本,多次买卖时统计不准) */
44
+ price_t maxFloatingProfit2() const {
45
+ price_t ret = maxHighPrice * position.number + position.sellMoney - position.buyMoney;
46
+ return ret < 0. ? 0. : ret;
47
+ }
48
+
49
+ /** 期间最大浮亏1(负数,仅使用收盘价计算, 不含预计卖出成本,多次买卖时统计不准) */
50
+ price_t minLossProfit1() const {
51
+ price_t ret = minClosePrice * position.number + position.sellMoney - position.buyMoney;
52
+ return ret > 0. ? 0. : ret;
53
+ }
54
+
55
+ /** 期间最大浮亏2(负数,仅期间最低价计算, 不含预计卖出成本,多次买卖时统计不准) */
56
+ price_t minLossProfit2() const {
57
+ price_t ret = minLowPrice * position.number + position.sellMoney - position.buyMoney;
58
+ return ret > 0. ? 0. : ret;
59
+ }
60
+
61
+ PositionExtInfo() = default;
62
+ PositionExtInfo(const PositionExtInfo&) = default;
63
+ PositionExtInfo& operator=(const PositionExtInfo&) = default;
64
+
65
+ PositionExtInfo(PositionExtInfo&& rhs)
66
+ : position(std::move(rhs.position)),
67
+ maxHighPrice(rhs.maxHighPrice),
68
+ minLowPrice(rhs.minLowPrice),
69
+ maxClosePrice(rhs.maxClosePrice),
70
+ minClosePrice(rhs.minClosePrice),
71
+ currentClosePrice(rhs.currentClosePrice),
72
+ maxPullBack1(rhs.maxPullBack1),
73
+ maxPullBack2(rhs.maxPullBack2),
74
+ currentProfit(rhs.currentProfit) {}
75
+
76
+ PositionExtInfo& operator=(PositionExtInfo&& rhs) {
77
+ if (this != &rhs) {
78
+ position = std::move(rhs.position);
79
+ maxHighPrice = rhs.maxHighPrice;
80
+ minLowPrice = rhs.minLowPrice;
81
+ maxClosePrice = rhs.maxClosePrice;
82
+ minClosePrice = rhs.minClosePrice;
83
+ currentClosePrice = rhs.currentClosePrice;
84
+ maxPullBack1 = rhs.maxPullBack1;
85
+ maxPullBack2 = rhs.maxPullBack2;
86
+ currentProfit = rhs.currentProfit;
87
+ }
88
+ return *this;
89
+ }
90
+ };
91
+
92
+ } // namespace hku
@@ -25,11 +25,17 @@ public:
25
25
  double number, price_t stoploss, price_t goalPrice, double totalNumber,
26
26
  price_t buyMoney, price_t totalCost, price_t totalRisk, price_t sellMoney);
27
27
 
28
+ PositionRecord(const PositionRecord& ths) = default;
29
+ PositionRecord& operator=(const PositionRecord& ths) = default;
30
+
31
+ PositionRecord(PositionRecord&& rhs);
32
+ PositionRecord& operator=(PositionRecord&& rhs);
33
+
28
34
  /** 仅用于python的__str__ */
29
35
  string str() const;
30
36
 
31
37
  /**
32
- * @brief 盈亏 = 买入资金 - 累计交易总成本 - 卖出资金
38
+ * @brief 盈亏 = 卖出资金 - 累计交易总成本 - 买入资金
33
39
  * @note 只对已清仓的记录有效,未清仓将返回0.0
34
40
  */
35
41
  price_t totalProfit() const;
@@ -229,7 +229,8 @@ public:
229
229
  */
230
230
  virtual TradeRecord buy(const Datetime& datetime, const Stock& stock, price_t realPrice,
231
231
  double number, price_t stoploss = 0.0, price_t goalPrice = 0.0,
232
- price_t planPrice = 0.0, SystemPart from = PART_INVALID) override;
232
+ price_t planPrice = 0.0, SystemPart from = PART_INVALID,
233
+ const string& remark = "") override;
233
234
 
234
235
  /**
235
236
  * 卖出操作
@@ -246,7 +247,7 @@ public:
246
247
  virtual TradeRecord sell(const Datetime& datetime, const Stock& stock, price_t realPrice,
247
248
  double number = MAX_DOUBLE, price_t stoploss = 0.0,
248
249
  price_t goalPrice = 0.0, price_t planPrice = 0.0,
249
- SystemPart from = PART_INVALID) override;
250
+ SystemPart from = PART_INVALID, const string& remark = "") override;
250
251
 
251
252
  /**
252
253
  * 卖空
@@ -262,7 +263,8 @@ public:
262
263
  */
263
264
  virtual TradeRecord sellShort(const Datetime& datetime, const Stock& stock, price_t realPrice,
264
265
  double number, price_t stoploss = 0.0, price_t goalPrice = 0.0,
265
- price_t planPrice = 0.0, SystemPart from = PART_INVALID) override;
266
+ price_t planPrice = 0.0, SystemPart from = PART_INVALID,
267
+ const string& remark = "") override;
266
268
 
267
269
  /**
268
270
  * 卖空后回补
@@ -279,7 +281,8 @@ public:
279
281
  virtual TradeRecord buyShort(const Datetime& datetime, const Stock& stock, price_t realPrice,
280
282
  double number = MAX_DOUBLE, price_t stoploss = 0.0,
281
283
  price_t goalPrice = 0.0, price_t planPrice = 0.0,
282
- SystemPart from = PART_INVALID) override;
284
+ SystemPart from = PART_INVALID,
285
+ const string& remark = "") override;
283
286
 
284
287
  /**
285
288
  * 借入资金,从其他来源借取的资金,如融资
@@ -400,7 +403,7 @@ private:
400
403
  TradeRecordList m_trade_list; // 交易记录
401
404
 
402
405
  typedef map<uint64_t, PositionRecord> position_map_type;
403
- position_map_type m_position; // 当前持仓交易对象的持仓记录 ["sh000001"-> ]
406
+ position_map_type m_position; // 当前持仓交易对象的持仓记录 ["sh000001"-> ]
404
407
  PositionRecordList m_position_history; // 持仓历史记录
405
408
  position_map_type m_short_position; // 空头仓位记录
406
409
  PositionRecordList m_short_position_history; // 空头仓位历史记录
@@ -10,6 +10,7 @@
10
10
  #include "../utilities/Parameter.h"
11
11
  #include "TradeRecord.h"
12
12
  #include "PositionRecord.h"
13
+ #include "PositionExtInfo.h"
13
14
  #include "BorrowRecord.h"
14
15
  #include "FundsRecord.h"
15
16
  #include "LoanRecord.h"
@@ -19,6 +20,8 @@
19
20
 
20
21
  namespace hku {
21
22
 
23
+ class HKU_API Performance;
24
+
22
25
  /**
23
26
  * 账户交易管理基类,管理帐户的交易记录及资金使用情况
24
27
  * @details
@@ -28,7 +31,7 @@ namespace hku {
28
31
  * </pre>
29
32
  * @ingroup TradeManagerClass
30
33
  */
31
- class HKU_API TradeManagerBase {
34
+ class HKU_API TradeManagerBase : public enable_shared_from_this<TradeManagerBase> {
32
35
  PARAMETER_SUPPORT_WITH_CHECK
33
36
 
34
37
  public:
@@ -522,7 +525,8 @@ public:
522
525
  */
523
526
  virtual TradeRecord buy(const Datetime& datetime, const Stock& stock, price_t realPrice,
524
527
  double number, price_t stoploss = 0.0, price_t goalPrice = 0.0,
525
- price_t planPrice = 0.0, SystemPart from = PART_INVALID) {
528
+ price_t planPrice = 0.0, SystemPart from = PART_INVALID,
529
+ const string& remark = "") {
526
530
  HKU_WARN("The subclass does not implement this method");
527
531
  return TradeRecord();
528
532
  }
@@ -542,7 +546,7 @@ public:
542
546
  virtual TradeRecord sell(const Datetime& datetime, const Stock& stock, price_t realPrice,
543
547
  double number = MAX_DOUBLE, price_t stoploss = 0.0,
544
548
  price_t goalPrice = 0.0, price_t planPrice = 0.0,
545
- SystemPart from = PART_INVALID) {
549
+ SystemPart from = PART_INVALID, const string& remark = "") {
546
550
  HKU_WARN("The subclass does not implement this method");
547
551
  return TradeRecord();
548
552
  }
@@ -561,7 +565,8 @@ public:
561
565
  */
562
566
  virtual TradeRecord sellShort(const Datetime& datetime, const Stock& stock, price_t realPrice,
563
567
  double number, price_t stoploss = 0.0, price_t goalPrice = 0.0,
564
- price_t planPrice = 0.0, SystemPart from = PART_INVALID) {
568
+ price_t planPrice = 0.0, SystemPart from = PART_INVALID,
569
+ const string& remark = "") {
565
570
  HKU_WARN("The subclass does not implement this method");
566
571
  return TradeRecord();
567
572
  }
@@ -581,7 +586,7 @@ public:
581
586
  virtual TradeRecord buyShort(const Datetime& datetime, const Stock& stock, price_t realPrice,
582
587
  double number = MAX_DOUBLE, price_t stoploss = 0.0,
583
588
  price_t goalPrice = 0.0, price_t planPrice = 0.0,
584
- SystemPart from = PART_INVALID) {
589
+ SystemPart from = PART_INVALID, const string& remark = "") {
585
590
  HKU_WARN("The subclass does not implement this method");
586
591
  return TradeRecord();
587
592
  }
@@ -704,6 +709,62 @@ public:
704
709
  HKU_WARN("The subclass does not implement this method");
705
710
  }
706
711
 
712
+ //-------------------------------------------------------------
713
+ // 以下为可能需要授权的功能
714
+ //-------------------------------------------------------------
715
+ /**
716
+ * 统计截至某一时刻的系统绩效, datetime必须大于等于lastDatetime,
717
+ * 以便用于计算当前市值
718
+ * @param datetime 统计截止时刻
719
+ */
720
+ Performance getPerformance(const Datetime& datetime = Datetime::now(),
721
+ const KQuery::KType& ktype = KQuery::DAY);
722
+
723
+ /**
724
+ * @brief 获取指定时刻时账户的最大回撤百分比(负数)(仅根据收盘价计算)
725
+ * @param tm 指定账户
726
+ * @param date 指定日期(包含该时刻)
727
+ * @param ktype k线类型
728
+ * @return price_t
729
+ */
730
+ price_t getMaxPullBack(const Datetime& date, const KQuery::KType& ktype = KQuery::DAY);
731
+
732
+ /**
733
+ * @brief 获取账户历史持仓扩展详情
734
+ * @param ktype k线类型
735
+ * @param trade_mode 交易模式,影响部分统计项: 0-收盘时交易, 1-下一开盘时交易
736
+ * @return std::vector<PositionExtInfo>
737
+ */
738
+ std::vector<PositionExtInfo> getHistoryPositionExtInfoList(
739
+ const KQuery::KType& ktype = KQuery::DAY, int trade_mode = 0);
740
+
741
+ /**
742
+ * @brief 获取账户最后交易时刻后持仓详情
743
+ * @param current_time 当前时刻(需大于等于最后交易时刻)
744
+ * @param ktype k线类型
745
+ * @param trade_mode 交易模式,影响部分统计项: 0-收盘时交易, 1-下一开盘时交易
746
+ * @return std::vector<PositionExtInfo>
747
+ */
748
+ std::vector<PositionExtInfo> getPositionExtInfoList(
749
+ const Datetime& current_time = Datetime::now(), const KQuery::KType& ktype = KQuery::DAY,
750
+ int trade_mode = 0);
751
+
752
+ /**
753
+ * @brief 获取指定截止时间前各月的收益百分比
754
+ * @param datetime
755
+ * @return std::vector<std::pair<Datetime, double>>
756
+ */
757
+ std::vector<std::pair<Datetime, double>> getProfitPercentMonthly(
758
+ const Datetime& datetime = Datetime::now());
759
+
760
+ /**
761
+ * @brief 获取指定截止时间前各年的收益百分比
762
+ * @param datetime
763
+ * @return std::vector<std::pair<Datetime, double>>
764
+ */
765
+ std::vector<std::pair<Datetime, double>> getProfitPercentYearly(
766
+ const Datetime& datetime = Datetime::now());
767
+
707
768
  protected:
708
769
  string m_name; // 账户名称
709
770
  TradeCostPtr m_costfunc; // 成本算法
@@ -61,7 +61,12 @@ public:
61
61
  TradeRecord();
62
62
  TradeRecord(const Stock& stock, const Datetime& datetime, BUSINESS business, price_t planPrice,
63
63
  price_t realPrice, price_t goalPrice, double number, const CostRecord& cost,
64
- price_t stoploss, price_t cash, SystemPart from);
64
+ price_t stoploss, price_t cash, SystemPart from, const string& remark = "");
65
+ TradeRecord(const TradeRecord&) = default;
66
+ TradeRecord& operator=(const TradeRecord&) = default;
67
+
68
+ TradeRecord(TradeRecord&&);
69
+ TradeRecord& operator=(TradeRecord&&);
65
70
 
66
71
  /** 仅用于python的__str__ */
67
72
  string toString() const;
@@ -79,6 +84,7 @@ public:
79
84
  price_t stoploss; ///< 止损价
80
85
  price_t cash; ///< 现金余额
81
86
  SystemPart from; ///< 辅助记录交易系统部件,区别是哪个部件发出的指示,Null<int>()表示无效
87
+ string remark; ///< 备注
82
88
 
83
89
  #if HKU_SUPPORT_SERIALIZATION
84
90
  private:
@@ -98,6 +104,7 @@ private:
98
104
  ar& BOOST_SERIALIZATION_NVP(cost);
99
105
  ar& BOOST_SERIALIZATION_NVP(stoploss);
100
106
  ar& BOOST_SERIALIZATION_NVP(cash);
107
+ ar& BOOST_SERIALIZATION_NVP(remark);
101
108
  string part_name(getSystemPartName(from));
102
109
  ar& bs::make_nvp<string>("from", part_name);
103
110
  }
@@ -119,6 +126,7 @@ private:
119
126
  ar& BOOST_SERIALIZATION_NVP(cost);
120
127
  ar& BOOST_SERIALIZATION_NVP(stoploss);
121
128
  ar& BOOST_SERIALIZATION_NVP(cash);
129
+ ar& BOOST_SERIALIZATION_NVP(remark);
122
130
  string part_name;
123
131
  ar& bs::make_nvp<string>("from", part_name);
124
132
  from = getSystemPartEnum(part_name);
@@ -29,7 +29,8 @@ public:
29
29
  MultiFactorBase();
30
30
  explicit MultiFactorBase(const string& name);
31
31
  MultiFactorBase(const IndicatorList& inds, const StockList& stks, const KQuery& query,
32
- const Stock& ref_stk, const string& name, int ic_n, bool spearman);
32
+ const Stock& ref_stk, const string& name, int ic_n, bool spearman, int mode,
33
+ bool save_all_factors);
33
34
  MultiFactorBase(const MultiFactorBase&);
34
35
  virtual ~MultiFactorBase() = default;
35
36
 
@@ -44,9 +45,7 @@ public:
44
45
  }
45
46
 
46
47
  /** 获取参考日期列表 */
47
- const DatetimeList& getDatetimeList() const {
48
- return m_ref_dates;
49
- }
48
+ const DatetimeList& getDatetimeList();
50
49
 
51
50
  /** 获取查询范围 */
52
51
  const KQuery& getQuery() const {
@@ -153,7 +152,11 @@ private:
153
152
  void initParam();
154
153
 
155
154
  protected:
156
- void _buildIndex(); // 计算完成后创建截面索引
155
+ void _buildIndex(); // 计算完成后创建截面索引
156
+ void _buildIndexDesc(); // 创建降序排列的索引
157
+ void _buildIndexAsc(); // 创建升序排列的索引
158
+ void _buildIndexNone(); // build index when no index
159
+
157
160
  IndicatorList _getAllReturns(int ndays) const;
158
161
  void _checkData();
159
162
 
@@ -18,11 +18,14 @@ namespace hku {
18
18
  * @param ref_stk 参考证券
19
19
  * @param ic_n 默认 IC 对应的 N 日收益率
20
20
  * @param spearman 默认使用 spearman 计算相关系数,否则为 pearson
21
+ * @param mode 获取截面数据时排序模式: 0-降序, 1-升序, 2-不排序
22
+ * @param save_all_factors 是否保留因子数据
21
23
  * @return MultiFactorPtr
22
24
  */
23
25
  MultiFactorPtr HKU_API MF_EqualWeight(const IndicatorList& inds, const StockList& stks,
24
26
  const KQuery& query, const Stock& ref_stk, int ic_n = 5,
25
- bool spearman = true);
27
+ bool spearman = true, int mode = 0,
28
+ bool save_all_factors = false);
26
29
 
27
30
  MultiFactorPtr HKU_API MF_EqualWeight();
28
31
 
@@ -20,11 +20,14 @@ namespace hku {
20
20
  * @param ic_n 默认 IC 对应的 N 日收益率
21
21
  * @param ic_rolling_n IC 滚动窗口
22
22
  * @param spearman 默认使用 spearman 计算相关系数,否则为 pearson
23
+ * @param mode 获取截面数据时排序模式: 0-降序, 1-升序, 2-不排序
24
+ * @param save_all_factors 是否保留因子数据
23
25
  * @return MultiFactorPtr
24
26
  */
25
27
  MultiFactorPtr HKU_API MF_ICIRWeight(const IndicatorList& inds, const StockList& stks,
26
28
  const KQuery& query, const Stock& ref_stk, int ic_n = 5,
27
- int ic_rolling_n = 120, bool spearman = true);
29
+ int ic_rolling_n = 120, bool spearman = true, int mode = 0,
30
+ bool save_all_factors = false);
28
31
 
29
32
  MultiFactorPtr HKU_API MF_ICIRWeight();
30
33
  } // namespace hku
@@ -20,11 +20,14 @@ namespace hku {
20
20
  * @param ic_n 默认 IC 对应的 N 日收益率
21
21
  * @param ic_rolling_n IC 滚动窗口
22
22
  * @param spearman 默认使用 spearman 计算相关系数,否则为 pearson
23
+ * @param mode 获取截面数据时排序模式: 0-降序, 1-升序, 2-不排序
24
+ * @param save_all_factors 是否保留因子数据
23
25
  * @return MultiFactorPtr
24
26
  */
25
27
  MultiFactorPtr HKU_API MF_ICWeight(const IndicatorList& inds, const StockList& stks,
26
28
  const KQuery& query, const Stock& ref_stk, int ic_n = 5,
27
- int ic_rolling_n = 120, bool spearman = true);
29
+ int ic_rolling_n = 120, bool spearman = true, int mode = 0,
30
+ bool save_all_factors = false);
28
31
  MultiFactorPtr HKU_API MF_ICWeight();
29
32
 
30
33
  } // namespace hku
@@ -19,11 +19,14 @@ namespace hku {
19
19
  * @param ref_stk 参考证券
20
20
  * @param ic_n 默认 IC 对应的 N 日收益率
21
21
  * @param spearman 默认使用 spearman 计算相关系数,否则为 pearson
22
+ * @param mode 获取截面数据时排序模式: 0-降序, 1-升序, 2-不排序
23
+ * @param save_all_factors 是否保留因子数据
22
24
  * @return MultiFactorPtr
23
25
  */
24
26
  MultiFactorPtr HKU_API MF_Weight(const IndicatorList& inds, const PriceList& weights,
25
27
  const StockList& stks, const KQuery& query, const Stock& ref_stk,
26
- int ic_n = 5, bool spearman = true);
28
+ int ic_n = 5, bool spearman = true, int mode = 0,
29
+ bool save_all_factors = false);
27
30
 
28
31
  MultiFactorPtr HKU_API MF_Weight();
29
32
 
@@ -18,7 +18,8 @@ class EqualWeightMultiFactor : public MultiFactorBase {
18
18
  public:
19
19
  EqualWeightMultiFactor();
20
20
  EqualWeightMultiFactor(const vector<Indicator>& inds, const StockList& stks,
21
- const KQuery& query, const Stock& ref_stk, int ic_n, bool spearman);
21
+ const KQuery& query, const Stock& ref_stk, int ic_n, bool spearman,
22
+ int mode, bool save_all_factors);
22
23
  virtual ~EqualWeightMultiFactor() = default;
23
24
  };
24
25
 
@@ -18,7 +18,8 @@ class ICIRMultiFactor : public MultiFactorBase {
18
18
  public:
19
19
  ICIRMultiFactor();
20
20
  ICIRMultiFactor(const IndicatorList& inds, const StockList& stks, const KQuery& query,
21
- const Stock& ref_stk, int ic_n, int ic_rolling_n, bool spearman);
21
+ const Stock& ref_stk, int ic_n, int ic_rolling_n, bool spearman, int mode,
22
+ bool save_all_factors);
22
23
  virtual ~ICIRMultiFactor() = default;
23
24
 
24
25
  virtual void _checkParam(const string& name) const override;