hikyuu 2.1.0__cp310-none-win_amd64.whl → 2.1.2__cp310-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 (110) hide show
  1. hikyuu/cpp/boost_date_time-mt.dll +0 -0
  2. hikyuu/cpp/boost_serialization-mt.dll +0 -0
  3. hikyuu/cpp/boost_wserialization-mt.dll +0 -0
  4. hikyuu/cpp/core310.pyd +0 -0
  5. hikyuu/cpp/hikyuu.dll +0 -0
  6. hikyuu/cpp/sqlite3.dll +0 -0
  7. hikyuu/examples/notebook/006-TradeManager.ipynb +41 -39
  8. hikyuu/examples/notebook/008-Pickle.ipynb +27 -35
  9. hikyuu/extend.py +3 -1
  10. hikyuu/fetcher/stock/zh_stock_a_qmt.py +49 -0
  11. hikyuu/gui/HikyuuTDX.py +13 -7
  12. hikyuu/gui/data/MainWindow.py +126 -126
  13. hikyuu/gui/spot_server.py +16 -7
  14. hikyuu/gui/start_qmt.py +36 -0
  15. hikyuu/include/hikyuu/DataType.h +2 -1
  16. hikyuu/include/hikyuu/KRecord.h +1 -1
  17. hikyuu/include/hikyuu/Stock.h +1 -1
  18. hikyuu/include/hikyuu/StockManager.h +3 -0
  19. hikyuu/include/hikyuu/StrategyContext.h +7 -2
  20. hikyuu/include/hikyuu/analysis/combinate.h +1 -1
  21. hikyuu/include/hikyuu/config.h +0 -12
  22. hikyuu/include/hikyuu/data_driver/base_info/table/HistoryFinanceTable.h +2 -2
  23. hikyuu/include/hikyuu/doc.h +2 -2
  24. hikyuu/include/hikyuu/global/GlobalSpotAgent.h +1 -0
  25. hikyuu/include/hikyuu/global/GlobalTaskGroup.h +4 -2
  26. hikyuu/include/hikyuu/global/SpotRecord.h +52 -0
  27. hikyuu/include/hikyuu/global/agent/SpotAgent.h +14 -41
  28. hikyuu/include/hikyuu/hikyuu.h +1 -0
  29. hikyuu/include/hikyuu/strategy/{AccountTradeManager.h → BrokerTradeManager.h} +97 -95
  30. hikyuu/include/hikyuu/strategy/RunPortfolioInStrategy.h +36 -0
  31. hikyuu/include/hikyuu/strategy/RunSystemInStrategy.h +37 -0
  32. hikyuu/include/hikyuu/strategy/Strategy.h +174 -0
  33. hikyuu/include/hikyuu/trade_manage/FundsRecord.h +8 -8
  34. hikyuu/include/hikyuu/trade_manage/OrderBrokerBase.h +66 -14
  35. hikyuu/include/hikyuu/trade_manage/PositionRecord.h +12 -12
  36. hikyuu/include/hikyuu/trade_manage/TradeManager.h +9 -0
  37. hikyuu/include/hikyuu/trade_manage/TradeManagerBase.h +19 -0
  38. hikyuu/include/hikyuu/utilities/FilterNode.h +267 -0
  39. hikyuu/include/hikyuu/utilities/LRUCache11.h +230 -0
  40. hikyuu/include/hikyuu/{Log.h → utilities/Log.h} +91 -113
  41. hikyuu/include/hikyuu/utilities/Null.h +1 -0
  42. hikyuu/include/hikyuu/utilities/Parameter.h +2 -1
  43. hikyuu/include/hikyuu/utilities/ResourcePool.h +636 -0
  44. hikyuu/include/hikyuu/utilities/SpendTimer.h +10 -9
  45. hikyuu/include/hikyuu/utilities/TimerManager.h +25 -11
  46. hikyuu/include/hikyuu/utilities/any_to_string.h +142 -0
  47. hikyuu/include/hikyuu/utilities/arithmetic.h +71 -35
  48. hikyuu/include/hikyuu/utilities/base64.h +59 -0
  49. hikyuu/include/hikyuu/utilities/config.h +41 -0
  50. hikyuu/include/hikyuu/utilities/datetime/Datetime.h +42 -31
  51. hikyuu/include/hikyuu/utilities/datetime/TimeDelta.h +24 -13
  52. hikyuu/include/hikyuu/utilities/db_connect/DBCondition.h +48 -48
  53. hikyuu/include/hikyuu/utilities/db_connect/DBConnect.h +10 -0
  54. hikyuu/include/hikyuu/utilities/db_connect/DBConnectBase.h +5 -22
  55. hikyuu/include/hikyuu/utilities/db_connect/DBUpgrade.h +3 -3
  56. hikyuu/include/hikyuu/utilities/db_connect/SQLException.h +1 -1
  57. hikyuu/include/hikyuu/utilities/db_connect/SQLResultSet.h +1 -1
  58. hikyuu/include/hikyuu/utilities/db_connect/SQLStatementBase.h +7 -7
  59. hikyuu/include/hikyuu/utilities/db_connect/TableMacro.h +1 -2
  60. hikyuu/include/hikyuu/utilities/db_connect/mysql/MySQLConnect.h +9 -9
  61. hikyuu/include/hikyuu/utilities/db_connect/mysql/MySQLStatement.h +18 -18
  62. hikyuu/include/hikyuu/utilities/db_connect/sqlite/SQLiteConnect.h +3 -3
  63. hikyuu/include/hikyuu/utilities/db_connect/sqlite/SQLiteStatement.h +2 -2
  64. hikyuu/include/hikyuu/utilities/db_connect/sqlite/SQLiteUtil.h +6 -6
  65. hikyuu/include/hikyuu/{exception.h → utilities/exception.h} +15 -16
  66. hikyuu/include/hikyuu/utilities/http_client/HttpClient.h +229 -0
  67. hikyuu/include/hikyuu/utilities/http_client/nng_wrap.h +517 -0
  68. hikyuu/include/hikyuu/utilities/http_client/url.h +25 -0
  69. hikyuu/include/hikyuu/utilities/{IniParser.h → ini_parser/IniParser.h} +10 -5
  70. hikyuu/include/hikyuu/utilities/ini_parser/__init__.py +1 -0
  71. hikyuu/include/hikyuu/utilities/md5.h +41 -0
  72. hikyuu/include/hikyuu/utilities/mo/__init__.py +1 -0
  73. hikyuu/include/hikyuu/utilities/mo/mo.h +48 -0
  74. hikyuu/include/hikyuu/utilities/mo/moFileReader.h +836 -0
  75. hikyuu/include/hikyuu/{global → utilities}/node/NodeClient.h +25 -18
  76. hikyuu/include/hikyuu/{global → utilities}/node/NodeError.h +1 -1
  77. hikyuu/include/hikyuu/{global → utilities}/node/NodeMessage.h +3 -2
  78. hikyuu/include/hikyuu/utilities/node/NodeServer.h +246 -0
  79. hikyuu/include/hikyuu/utilities/node/__init__.py +1 -0
  80. hikyuu/include/hikyuu/utilities/os.h +16 -15
  81. hikyuu/include/hikyuu/utilities/snowflake.h +110 -0
  82. hikyuu/include/hikyuu/utilities/string_view.h +70 -0
  83. hikyuu/include/hikyuu/utilities/thread/MQStealThreadPool.h +3 -3
  84. hikyuu/include/hikyuu/utilities/thread/MQThreadPool.h +3 -3
  85. hikyuu/include/hikyuu/utilities/thread/StealThreadPool.h +3 -3
  86. hikyuu/include/hikyuu/utilities/thread/ThreadPool.h +3 -3
  87. hikyuu/include/hikyuu/version.h +4 -4
  88. hikyuu/interactive.py +42 -137
  89. hikyuu/sqlite3.dll +0 -0
  90. hikyuu/strategy/__init__.py +0 -1
  91. hikyuu/strategy/strategy_demo1.py +53 -0
  92. hikyuu/strategy/strategy_demo2.py +47 -0
  93. hikyuu/strategy/strategy_demo3.py +24 -0
  94. hikyuu/trade_manage/broker.py +27 -11
  95. hikyuu/trade_manage/broker_easytrader.py +52 -6
  96. hikyuu/trade_manage/broker_mail.py +17 -20
  97. hikyuu/vcruntime140.dll +0 -0
  98. hikyuu/vcruntime140_1.dll +0 -0
  99. hikyuu-2.1.2.dist-info/METADATA +115 -0
  100. {hikyuu-2.1.0.dist-info → hikyuu-2.1.2.dist-info}/RECORD +105 -79
  101. {hikyuu-2.1.0.dist-info → hikyuu-2.1.2.dist-info}/top_level.txt +4 -2
  102. hikyuu/README.rst +0 -79
  103. hikyuu/include/hikyuu/strategy/StrategyBase.h +0 -156
  104. hikyuu/strategy/demo/__init__.py +0 -3
  105. hikyuu/strategy/strategy.py +0 -27
  106. hikyuu-2.1.0.dist-info/METADATA +0 -126
  107. /hikyuu/include/hikyuu/{global/node → utilities/http_client}/__init__.py +0 -0
  108. {hikyuu-2.1.0.dist-info → hikyuu-2.1.2.dist-info}/LICENSE +0 -0
  109. {hikyuu-2.1.0.dist-info → hikyuu-2.1.2.dist-info}/WHEEL +0 -0
  110. {hikyuu-2.1.0.dist-info → hikyuu-2.1.2.dist-info}/entry_points.txt +0 -0
@@ -5,6 +5,7 @@
5
5
  * Author: fasiondog
6
6
  */
7
7
 
8
+ #pragma once
8
9
  #include "agent/SpotAgent.h"
9
10
 
10
11
  namespace hku {
@@ -11,15 +11,17 @@
11
11
  #ifndef HKU_GLOBAL_TASK_GROUP
12
12
  #define HKU_GLOBAL_TASK_GROUP
13
13
 
14
- #include "../utilities/thread/StealThreadPool.h"
14
+ #include "../utilities/thread/thread.h"
15
15
 
16
16
  namespace hku {
17
17
 
18
+ using TaskGroup = ThreadPool;
19
+
18
20
  /**
19
21
  * 获取全局线程池任务组
20
22
  * @note 请使用 future 获取任务返回
21
23
  */
22
- StealThreadPool* getGlobalTaskGroup();
24
+ TaskGroup* getGlobalTaskGroup();
23
25
 
24
26
  template <typename ResultType>
25
27
  using task_handle = std::future<ResultType>;
@@ -0,0 +1,52 @@
1
+ /*
2
+ * Copyright (c) 2024 hikyuu.org
3
+ *
4
+ * Created on: 2024-08-15
5
+ * Author: fasiondog
6
+ */
7
+
8
+ #pragma once
9
+
10
+ #include "hikyuu/DataType.h"
11
+
12
+ namespace hku {
13
+
14
+ /**
15
+ * 接收外部实时数据结构
16
+ * @ingroup Agent
17
+ */
18
+ struct HKU_API SpotRecord {
19
+ string market; ///< 市场标识
20
+ string code; ///< 证券代码
21
+ string name; ///< 证券名称
22
+ Datetime datetime; ///< 数据时间
23
+ price_t yesterday_close; ///< 昨日收盘价
24
+ price_t open; ///< 开盘价
25
+ price_t high; ///< 最高价
26
+ price_t low; ///< 最低价
27
+ price_t close; ///< 收盘价
28
+ price_t amount; ///< 成交金额 (千元)
29
+ price_t volume; ///< 成交量(手)
30
+ price_t bid1; ///< 买一价
31
+ price_t bid1_amount; ///< 买一数量(手)
32
+ price_t bid2; ///< 买二价
33
+ price_t bid2_amount; ///< 买二数量
34
+ price_t bid3; ///< 买三价
35
+ price_t bid3_amount; ///< 买三数量
36
+ price_t bid4; ///< 买四价
37
+ price_t bid4_amount; ///< 买四数量
38
+ price_t bid5; ///< 买五价
39
+ price_t bid5_amount; ///< 买五数量
40
+ price_t ask1; ///< 卖一价
41
+ price_t ask1_amount; ///< 卖一数量
42
+ price_t ask2; ///< 卖二价
43
+ price_t ask2_amount; ///< 卖二数量
44
+ price_t ask3; ///< 卖三价
45
+ price_t ask3_amount; ///< 卖三数量
46
+ price_t ask4; ///< 卖四价
47
+ price_t ask4_amount; ///< 卖四数量
48
+ price_t ask5; ///< 卖五价
49
+ price_t ask5_amount; ///< 卖五数量
50
+ };
51
+
52
+ } // namespace hku
@@ -9,49 +9,17 @@
9
9
 
10
10
  #include <thread>
11
11
  #include <functional>
12
- #include "spot_generated.h"
13
12
  #include "../../DataType.h"
14
13
  #include "../../utilities/thread/ThreadPool.h"
14
+ #include "../SpotRecord.h"
15
15
 
16
- namespace hku {
16
+ namespace hikyuu {
17
+ namespace flat {
18
+ struct Spot;
19
+ }
20
+ } // namespace hikyuu
17
21
 
18
- /**
19
- * 接收外部实时数据结构
20
- * @ingroup Agent
21
- */
22
- struct HKU_API SpotRecord {
23
- string market; ///< 市场标识
24
- string code; ///< 证券代码
25
- string name; ///< 证券名称
26
- Datetime datetime; ///< 数据时间
27
- price_t yesterday_close; ///< 昨日收盘价
28
- price_t open; ///< 开盘价
29
- price_t high; ///< 最高价
30
- price_t low; ///< 最低价
31
- price_t close; ///< 收盘价
32
- price_t amount; ///< 成交金额 (千元)
33
- price_t volume; ///< 成交量(手)
34
- price_t bid1; ///< 买一价
35
- price_t bid1_amount; ///< 买一数量(手)
36
- price_t bid2; ///< 买二价
37
- price_t bid2_amount; ///< 买二数量
38
- price_t bid3; ///< 买三价
39
- price_t bid3_amount; ///< 买三数量
40
- price_t bid4; ///< 买四价
41
- price_t bid4_amount; ///< 买四数量
42
- price_t bid5; ///< 买五价
43
- price_t bid5_amount; ///< 买五数量
44
- price_t ask1; ///< 卖一价
45
- price_t ask1_amount; ///< 卖一数量
46
- price_t ask2; ///< 卖二价
47
- price_t ask2_amount; ///< 卖二数量
48
- price_t ask3; ///< 卖三价
49
- price_t ask3_amount; ///< 卖三数量
50
- price_t ask4; ///< 卖四价
51
- price_t ask4_amount; ///< 卖四数量
52
- price_t ask5; ///< 卖五价
53
- price_t ask5_amount; ///< 卖五数量
54
- };
22
+ namespace hku {
55
23
 
56
24
  /**
57
25
  * 接收外部实时数据代理
@@ -77,6 +45,7 @@ public:
77
45
 
78
46
  /** 设置是否打印数据接收进展情况,主要用于在交互环境下关闭打印 */
79
47
  void setPrintFlag(bool print) {
48
+ std::lock_guard<std::mutex> lock(m_mutex);
80
49
  m_print = print;
81
50
  }
82
51
 
@@ -136,14 +105,18 @@ private:
136
105
  enum STATUS { WAITING, RECEIVING }; // 等待新的批次数据,正在接收批次数据中
137
106
  enum STATUS m_status = WAITING; // 当前内部状态
138
107
  std::atomic_bool m_stop = true; // 结束代理工作标识
139
- bool m_print = true; // 是否打印接收进度,防止的交互模式的影响
108
+
140
109
  int m_revTimeout = 100; // 连接数据服务超时时长(毫秒)
141
110
  size_t m_batch_count = 0; // 记录本次批次接收的数据数量
142
111
  std::thread m_receiveThread; // 数据接收线程
143
112
  ThreadPool m_tg; // 数据处理任务线程池
113
+ vector<std::future<void>> m_process_task_list;
114
+
115
+ // 下面属性被修改时需要加锁,以便可以使用多线程方式运行 strategy
116
+ std::mutex m_mutex;
117
+ bool m_print = true; // 是否打印接收进度,防止的交互模式的影响
144
118
  list<std::function<void(const SpotRecord&)>> m_processList; // 已注册的 spot 处理函数列表
145
119
  list<std::function<void(Datetime)>> m_postProcessList; // 已注册的批次后处理函数列表
146
- vector<std::future<void>> m_process_task_list;
147
120
  };
148
121
 
149
122
  } // namespace hku
@@ -16,6 +16,7 @@
16
16
  #include "indicator/build_in.h"
17
17
  #include "trade_manage/build_in.h"
18
18
  #include "trade_sys/all.h"
19
+ #include "strategy/Strategy.h"
19
20
 
20
21
  namespace hku {
21
22
 
@@ -1,28 +1,36 @@
1
1
  /*
2
- * Copyright(C) 2021 hikyuu.org
2
+ * Copyright (c) 2024 hikyuu.org
3
3
  *
4
- * Create on: 2021-03-23
5
- * Author: fasiondog
4
+ * Created on: 2024-08-16
5
+ * Author: fasiondog
6
6
  */
7
7
 
8
8
  #pragma once
9
9
 
10
- #include <httplib.h>
11
- #include "../trade_manage/TradeManagerBase.h"
10
+ #include "hikyuu/trade_manage/TradeManagerBase.h"
12
11
 
13
12
  namespace hku {
14
13
 
15
- class HKU_API AccountTradeManager : public TradeManagerBase {
14
+ class HKU_API BrokerTradeManager : public TradeManagerBase {
16
15
  public:
17
- AccountTradeManager() = default;
18
- AccountTradeManager(const string& name, const string& pwd);
19
- virtual ~AccountTradeManager() = default;
16
+ BrokerTradeManager() = default;
17
+ explicit BrokerTradeManager(const OrderBrokerPtr& broker,
18
+ const TradeCostPtr& costfunc = TC_Zero(),
19
+ const string& name = "SYS");
20
+ virtual ~BrokerTradeManager() {}
20
21
 
21
- virtual void _reset() override {}
22
+ virtual void _reset() override;
22
23
 
23
- virtual shared_ptr<TradeManagerBase> _clone() override {
24
- return std::make_shared<AccountTradeManager>();
25
- }
24
+ virtual shared_ptr<TradeManagerBase> _clone() override;
25
+
26
+ virtual void fetchAssetInfoFromBroker(const OrderBrokerPtr& broker) override;
27
+
28
+ /**
29
+ * 根据权息信息更新当前持仓与交易情况
30
+ * @note 必须按时间顺序调用
31
+ * @param datetime 当前时刻
32
+ */
33
+ virtual void updateWithWeight(const Datetime& datetime) override {}
26
34
 
27
35
  /**
28
36
  * 获取指定对象的保证金比率
@@ -36,26 +44,22 @@ public:
36
44
 
37
45
  /** 初始资金 */
38
46
  virtual price_t initCash() const override {
39
- HKU_WARN("The subclass does not implement this method");
40
- return 0.0;
47
+ return m_cash;
41
48
  }
42
49
 
43
50
  /** 账户建立日期 */
44
51
  virtual Datetime initDatetime() const override {
45
- HKU_WARN("The subclass does not implement this method");
46
- return Datetime();
52
+ return m_datetime;
47
53
  }
48
54
 
49
55
  /** 第一笔买入交易发生日期,如未发生交易返回Null<Datetime>() */
50
56
  virtual Datetime firstDatetime() const override {
51
- HKU_WARN("The subclass does not implement this method");
52
- return Datetime();
57
+ return m_datetime;
53
58
  }
54
59
 
55
60
  /** 最后一笔交易日期,注意和交易类型无关,如未发生交易返回账户建立日期 */
56
61
  virtual Datetime lastDatetime() const override {
57
- HKU_WARN("The subclass does not implement this method");
58
- return Datetime();
62
+ return m_datetime;
59
63
  }
60
64
 
61
65
  /**
@@ -63,8 +67,7 @@ public:
63
67
  * @note 仅返回当前信息,不会根据权息进行调整
64
68
  */
65
69
  virtual price_t currentCash() const override {
66
- HKU_WARN("The subclass does not implement this method");
67
- return 0.0;
70
+ return m_cash;
68
71
  }
69
72
 
70
73
  /**
@@ -72,8 +75,7 @@ public:
72
75
  * @note 如果不带日期参数,无法根据权息信息调整持仓
73
76
  */
74
77
  virtual price_t cash(const Datetime& datetime, KQuery::KType ktype = KQuery::DAY) override {
75
- HKU_WARN("The subclass does not implement this method");
76
- return 0.0;
78
+ return m_cash;
77
79
  }
78
80
 
79
81
  /**
@@ -83,8 +85,7 @@ public:
83
85
  * @return true 是 | false 否
84
86
  */
85
87
  virtual bool have(const Stock& stock) const override {
86
- HKU_WARN("The subclass does not implement this method");
87
- return false;
88
+ return m_position.count(stock.id()) ? true : false;
88
89
  }
89
90
 
90
91
  /**
@@ -93,49 +94,48 @@ public:
93
94
  * @param stock 指定证券
94
95
  * @return true 是 | false 否
95
96
  */
96
- virtual bool haveShort(const Stock& stock) const override {
97
+ virtual bool haveShort(const Stock& stock) const {
97
98
  HKU_WARN("The subclass does not implement this method");
98
99
  return false;
99
100
  }
100
101
 
101
102
  /** 当前持有的证券种类数量 */
102
- virtual size_t getStockNumber() const override {
103
- HKU_WARN("The subclass does not implement this method");
104
- return 0;
103
+ virtual size_t getStockNumber() const {
104
+ return m_position.size();
105
105
  }
106
106
 
107
107
  /** 当前空头持有的证券种类数量 */
108
- virtual size_t getShortStockNumber() const override {
108
+ virtual size_t getShortStockNumber() const {
109
109
  HKU_WARN("The subclass does not implement this method");
110
110
  return 0;
111
111
  }
112
112
 
113
113
  /** 获取指定时刻的某证券持有数量 */
114
- virtual double getHoldNumber(const Datetime& datetime, const Stock& stock) override {
114
+ virtual double getHoldNumber(const Datetime& datetime, const Stock& stock) {
115
115
  HKU_WARN("The subclass does not implement this method");
116
116
  return 0.0;
117
117
  }
118
118
 
119
119
  /** 获取指定时刻的空头某证券持有数量 */
120
- virtual double getShortHoldNumber(const Datetime& datetime, const Stock& stock) override {
120
+ virtual double getShortHoldNumber(const Datetime& datetime, const Stock& stock) {
121
121
  HKU_WARN("The subclass does not implement this method");
122
122
  return 0.0;
123
123
  }
124
124
 
125
125
  /** 获取指定时刻已借入的股票数量 */
126
- virtual double getDebtNumber(const Datetime& datetime, const Stock& stock) override {
126
+ virtual double getDebtNumber(const Datetime& datetime, const Stock& stock) {
127
127
  HKU_WARN("The subclass does not implement this method");
128
128
  return 0.0;
129
129
  }
130
130
 
131
131
  /** 获取指定时刻已借入的现金额 */
132
- virtual price_t getDebtCash(const Datetime& datetime) override {
132
+ virtual price_t getDebtCash(const Datetime& datetime) {
133
133
  HKU_WARN("The subclass does not implement this method");
134
134
  return 0.0;
135
135
  }
136
136
 
137
137
  /** 获取全部交易记录 */
138
- virtual TradeRecordList getTradeList() const override {
138
+ virtual TradeRecordList getTradeList() const {
139
139
  HKU_WARN("The subclass does not implement this method");
140
140
  return TradeRecordList();
141
141
  }
@@ -146,50 +146,51 @@ public:
146
146
  * @param end 结束日期
147
147
  * @return 交易记录列表
148
148
  */
149
- virtual TradeRecordList getTradeList(const Datetime& start,
150
- const Datetime& end) const override {
149
+ virtual TradeRecordList getTradeList(const Datetime& start, const Datetime& end) const {
151
150
  HKU_WARN("The subclass does not implement this method");
152
151
  return TradeRecordList();
153
152
  }
154
153
 
155
154
  /** 获取当前全部持仓记录 */
156
- virtual PositionRecordList getPositionList() const override {
157
- HKU_WARN("The subclass does not implement this method");
158
- return PositionRecordList();
159
- }
155
+ virtual PositionRecordList getPositionList() const override;
160
156
 
161
157
  /** 获取全部历史持仓记录,即已平仓记录 */
162
- virtual PositionRecordList getHistoryPositionList() const override {
158
+ virtual PositionRecordList getHistoryPositionList() const {
163
159
  HKU_WARN("The subclass does not implement this method");
164
160
  return PositionRecordList();
165
161
  }
166
162
 
167
163
  /** 获取当前全部空头仓位记录 */
168
- virtual PositionRecordList getShortPositionList() const override {
164
+ virtual PositionRecordList getShortPositionList() const {
169
165
  HKU_WARN("The subclass does not implement this method");
170
166
  return PositionRecordList();
171
167
  }
172
168
 
173
169
  /** 获取全部空头历史仓位记录 */
174
- virtual PositionRecordList getShortHistoryPositionList() const override {
170
+ virtual PositionRecordList getShortHistoryPositionList() const {
175
171
  HKU_WARN("The subclass does not implement this method");
176
172
  return PositionRecordList();
177
173
  }
178
174
 
179
- /** 获取指定证券的当前持仓记录,如当前未持有该票,返回Null<PositionRecord>() */
180
- virtual PositionRecord getPosition(const Datetime&, const Stock&) override {
181
- HKU_WARN("The subclass does not implement this method");
182
- return PositionRecord();
183
- }
175
+ /**
176
+ * 获取指定证券的持仓记录
177
+ * @param date 指定日期
178
+ * @param stock 指定的证券
179
+ */
180
+ virtual PositionRecord getPosition(const Datetime& date, const Stock& stock) override;
184
181
 
185
- /** 获取指定证券的当前空头仓位持仓记录,如当前未持有该票,返回Null<PositionRecord>() */
186
- virtual PositionRecord getShortPosition(const Stock&) const override {
182
+ /**
183
+ * 获取指定证券的空头持仓记录
184
+ * @param date 指定日期
185
+ * @param stock 指定的证券
186
+ */
187
+ virtual PositionRecord getShortPosition(const Stock&) const {
187
188
  HKU_WARN("The subclass does not implement this method");
188
189
  return PositionRecord();
189
190
  }
190
191
 
191
192
  /** 获取当前借入的股票列表 */
192
- virtual BorrowRecordList getBorrowStockList() const override {
193
+ virtual BorrowRecordList getBorrowStockList() const {
193
194
  HKU_WARN("The subclass does not implement this method");
194
195
  return BorrowRecordList();
195
196
  }
@@ -200,10 +201,7 @@ public:
200
201
  * @param cash 存入的资金量
201
202
  * @return true | false
202
203
  */
203
- virtual bool checkin(const Datetime& datetime, price_t cash) override {
204
- HKU_WARN("The subclass does not implement this method");
205
- return false;
206
- }
204
+ virtual bool checkin(const Datetime& datetime, price_t cash) override;
207
205
 
208
206
  /**
209
207
  * 取出资金
@@ -212,8 +210,8 @@ public:
212
210
  * @return true | false
213
211
  */
214
212
  virtual bool checkout(const Datetime& datetime, price_t cash) override {
215
- HKU_WARN("The subclass does not implement this method");
216
- return false;
213
+ m_cash = (cash > m_cash) ? 0.0 : m_cash - cash;
214
+ return true;
217
215
  }
218
216
 
219
217
  /**
@@ -225,7 +223,7 @@ public:
225
223
  * @return true | false
226
224
  */
227
225
  virtual bool checkinStock(const Datetime& datetime, const Stock& stock, price_t price,
228
- double number) override {
226
+ double number) {
229
227
  HKU_WARN("The subclass does not implement this method");
230
228
  return false;
231
229
  }
@@ -240,7 +238,7 @@ public:
240
238
  * @note 应该不会被用到
241
239
  */
242
240
  virtual bool checkoutStock(const Datetime& datetime, const Stock& stock, price_t price,
243
- double number) override {
241
+ double number) {
244
242
  HKU_WARN("The subclass does not implement this method");
245
243
  return false;
246
244
  }
@@ -259,10 +257,7 @@ public:
259
257
  */
260
258
  virtual TradeRecord buy(const Datetime& datetime, const Stock& stock, price_t realPrice,
261
259
  double number, price_t stoploss = 0.0, price_t goalPrice = 0.0,
262
- price_t planPrice = 0.0, SystemPart from = PART_INVALID) override {
263
- HKU_WARN("The subclass does not implement this method");
264
- return TradeRecord();
265
- }
260
+ price_t planPrice = 0.0, SystemPart from = PART_INVALID) override;
266
261
 
267
262
  /**
268
263
  * 卖出操作
@@ -279,10 +274,7 @@ public:
279
274
  virtual TradeRecord sell(const Datetime& datetime, const Stock& stock, price_t realPrice,
280
275
  double number = MAX_DOUBLE, price_t stoploss = 0.0,
281
276
  price_t goalPrice = 0.0, price_t planPrice = 0.0,
282
- SystemPart from = PART_INVALID) override {
283
- HKU_WARN("The subclass does not implement this method");
284
- return TradeRecord();
285
- }
277
+ SystemPart from = PART_INVALID) override;
286
278
 
287
279
  /**
288
280
  * 卖空
@@ -298,8 +290,7 @@ public:
298
290
  */
299
291
  virtual TradeRecord sellShort(const Datetime& datetime, const Stock& stock, price_t realPrice,
300
292
  double number, price_t stoploss = 0.0, price_t goalPrice = 0.0,
301
- price_t planPrice = 0.0,
302
- SystemPart from = PART_INVALID) override {
293
+ price_t planPrice = 0.0, SystemPart from = PART_INVALID) {
303
294
  HKU_WARN("The subclass does not implement this method");
304
295
  return TradeRecord();
305
296
  }
@@ -319,7 +310,7 @@ public:
319
310
  virtual TradeRecord buyShort(const Datetime& datetime, const Stock& stock, price_t realPrice,
320
311
  double number = MAX_DOUBLE, price_t stoploss = 0.0,
321
312
  price_t goalPrice = 0.0, price_t planPrice = 0.0,
322
- SystemPart from = PART_INVALID) override {
313
+ SystemPart from = PART_INVALID) {
323
314
  HKU_WARN("The subclass does not implement this method");
324
315
  return TradeRecord();
325
316
  }
@@ -330,7 +321,7 @@ public:
330
321
  * @param cash 借入的现金
331
322
  * @return true | false
332
323
  */
333
- virtual bool borrowCash(const Datetime& datetime, price_t cash) override {
324
+ virtual bool borrowCash(const Datetime& datetime, price_t cash) {
334
325
  HKU_WARN("The subclass does not implement this method");
335
326
  return false;
336
327
  }
@@ -341,7 +332,7 @@ public:
341
332
  * @param cash 归还现金
342
333
  * @return true | false
343
334
  */
344
- virtual bool returnCash(const Datetime& datetime, price_t cash) override {
335
+ virtual bool returnCash(const Datetime& datetime, price_t cash) {
345
336
  HKU_WARN("The subclass does not implement this method");
346
337
  return false;
347
338
  }
@@ -355,7 +346,7 @@ public:
355
346
  * @return true | false
356
347
  */
357
348
  virtual bool borrowStock(const Datetime& datetime, const Stock& stock, price_t price,
358
- double number) override {
349
+ double number) {
359
350
  HKU_WARN("The subclass does not implement this method");
360
351
  return false;
361
352
  }
@@ -369,11 +360,18 @@ public:
369
360
  * @return true | false
370
361
  */
371
362
  virtual bool returnStock(const Datetime& datetime, const Stock& stock, price_t price,
372
- double number) override {
363
+ double number) {
373
364
  HKU_WARN("The subclass does not implement this method");
374
365
  return false;
375
366
  }
376
367
 
368
+ /**
369
+ * 获取账户当前时刻的资产详情
370
+ * @param ktype 日期的类型
371
+ * @return 资产详情
372
+ */
373
+ virtual FundsRecord getFunds(KQuery::KType ktype = KQuery::DAY) const override;
374
+
377
375
  /**
378
376
  * 获取指定时刻的资产市值详情
379
377
  * @param datetime 必须大于帐户建立的初始日期,或为Null<Datetime>()
@@ -382,10 +380,7 @@ public:
382
380
  * @note 当datetime等于Null<Datetime>()时,与getFunds(KType)同
383
381
  */
384
382
  virtual FundsRecord getFunds(const Datetime& datetime,
385
- KQuery::KType ktype = KQuery::DAY) override {
386
- HKU_WARN("The subclass does not implement this method");
387
- return FundsRecord();
388
- }
383
+ KQuery::KType ktype = KQuery::DAY) override;
389
384
 
390
385
  /**
391
386
  * 直接加入交易记录
@@ -393,37 +388,44 @@ public:
393
388
  * @param tr 待加入的交易记录
394
389
  * @return bool true 成功 | false 失败
395
390
  */
396
- virtual bool addTradeRecord(const TradeRecord& tr) override {
391
+ virtual bool addTradeRecord(const TradeRecord& tr) {
397
392
  HKU_WARN("The subclass does not implement this method");
398
393
  return false;
399
394
  }
400
395
 
401
- /** 字符串输出 */
402
- virtual string str() const override {
396
+ /**
397
+ * 直接加入持仓记录
398
+ * @param pr 持仓记录
399
+ * @return true 成功
400
+ * @return false 失败
401
+ */
402
+ virtual bool addPosition(const PositionRecord& pr) {
403
403
  HKU_WARN("The subclass does not implement this method");
404
- return string();
404
+ return false;
405
405
  }
406
406
 
407
+ /** 字符串输出 */
408
+ virtual string str() const override;
409
+
407
410
  /**
408
411
  * 以csv格式输出交易记录、未平仓记录、已平仓记录、资产净值曲线
409
412
  * @param path 输出文件所在目录
410
413
  */
411
- virtual void tocsv(const string& path) override {
414
+ virtual void tocsv(const string& path) {
412
415
  HKU_WARN("The subclass does not implement this method");
413
416
  }
414
417
 
415
418
  private:
416
- string getToken();
419
+ Datetime m_datetime; // 当前日期
417
420
 
418
- private:
419
- std::unique_ptr<httplib::Client> m_client;
420
- string m_user;
421
- string m_password;
422
- string m_token;
421
+ price_t m_cash{0.0}; // 当前可用现金
422
+
423
+ typedef map<uint64_t, PositionRecord> position_map_type;
424
+ position_map_type m_position; // 当前持仓交易对象的持仓记录
423
425
  };
424
426
 
425
- inline TMPtr crtAccountTM(const string& name, const string& pwd) {
426
- return std::make_shared<AccountTradeManager>(name, pwd);
427
- }
427
+ TradeManagerPtr HKU_API crtBrokerTM(const OrderBrokerPtr& broker,
428
+ const TradeCostPtr& costfunc = TC_Zero(),
429
+ const string& name = "SYS");
428
430
 
429
431
  } // namespace hku
@@ -0,0 +1,36 @@
1
+ /*
2
+ * Copyright (c) 2024 hikyuu.org
3
+ *
4
+ * Created on: 2024-08-25
5
+ * Author: fasiondog
6
+ */
7
+
8
+ #pragma once
9
+
10
+ #include "hikyuu/trade_sys/portfolio/Portfolio.h"
11
+ #include "Strategy.h"
12
+
13
+ namespace hku {
14
+
15
+ class HKU_API RunPortfolioInStrategy {
16
+ public:
17
+ RunPortfolioInStrategy() = default;
18
+ RunPortfolioInStrategy(const PFPtr& pf, const KQuery& query, int adjust_cycle,
19
+ const OrderBrokerPtr& broker, const TradeCostPtr& costfunc);
20
+ virtual ~RunPortfolioInStrategy() = default;
21
+
22
+ void run();
23
+
24
+ private:
25
+ PFPtr m_pf;
26
+ OrderBrokerPtr m_broker;
27
+ KQuery m_query;
28
+ int m_adjust_cycle;
29
+ };
30
+
31
+ StrategyPtr HKU_API crtPFStrategy(const PFPtr& pf, const KQuery& query, int adjust_cycle,
32
+ const OrderBrokerPtr& broker, const TradeCostPtr& costfunc,
33
+ const string& name = "PFStrategy",
34
+ const string& config_file = "");
35
+
36
+ } // namespace hku