FIX 4.0 Demo 1.0
Loading...
Searching...
No Matches
account_manager.hpp
Go to the documentation of this file.
1
9#pragma once
10
11#include <string>
12#include <unordered_map>
13#include <optional>
14#include <mutex>
15#include <vector>
16#include "app/model/account.hpp"
17
18namespace fix40 {
19
20// 前向声明
21class IStore;
22
57public:
58 // -------------------------------------------------------------------------
59 // 构造函数
60 // -------------------------------------------------------------------------
61
68
77 explicit AccountManager(IStore* store);
78
82 ~AccountManager() = default;
83
84 // 禁用拷贝
87
88 // -------------------------------------------------------------------------
89 // 账户管理
90 // -------------------------------------------------------------------------
91
103 Account createAccount(const std::string& accountId, double initialBalance);
104
111 std::optional<Account> getAccount(const std::string& accountId) const;
112
119 bool hasAccount(const std::string& accountId) const;
120
126 std::vector<std::string> getAllAccountIds() const;
127
133 size_t size() const;
134
135 // -------------------------------------------------------------------------
136 // 保证金操作
137 // -------------------------------------------------------------------------
138
152 bool freezeMargin(const std::string& accountId, double amount);
153
167 bool unfreezeMargin(const std::string& accountId, double amount);
168
184 bool confirmMargin(const std::string& accountId, double frozenAmount, double usedAmount);
185
199 bool releaseMargin(const std::string& accountId, double amount);
200
201 // -------------------------------------------------------------------------
202 // 盈亏操作
203 // -------------------------------------------------------------------------
204
214 bool updatePositionProfit(const std::string& accountId, double profit);
215
230 bool addCloseProfit(const std::string& accountId, double profit);
231
232 // -------------------------------------------------------------------------
233 // 清理方法
234 // -------------------------------------------------------------------------
235
239 void clear();
240
241private:
247 void persistAccount(const Account& account);
248
250 std::unordered_map<std::string, Account> accounts_;
251
253 IStore* store_;
254
256 mutable std::mutex mutex_;
257};
258
259} // namespace fix40
虚拟交易账户数据结构
账户管理器
Definition account_manager.hpp:56
bool updatePositionProfit(const std::string &accountId, double profit)
更新持仓盈亏
Definition account_manager.cpp:196
bool hasAccount(const std::string &accountId) const
检查账户是否存在
Definition account_manager.cpp:70
~AccountManager()=default
析构函数
bool addCloseProfit(const std::string &accountId, double profit)
记录平仓盈亏
Definition account_manager.cpp:220
AccountManager()
默认构造函数
Definition account_manager.cpp:16
AccountManager & operator=(const AccountManager &)=delete
Account createAccount(const std::string &accountId, double initialBalance)
创建账户
Definition account_manager.cpp:41
bool unfreezeMargin(const std::string &accountId, double amount)
释放冻结保证金(撤单/拒绝时)
Definition account_manager.cpp:121
bool freezeMargin(const std::string &accountId, double amount)
冻结保证金(下单时)
Definition account_manager.cpp:95
std::optional< Account > getAccount(const std::string &accountId) const
获取账户
Definition account_manager.cpp:60
bool releaseMargin(const std::string &accountId, double amount)
释放占用保证金(平仓时)
Definition account_manager.cpp:171
size_t size() const
获取账户数量
Definition account_manager.cpp:86
bool confirmMargin(const std::string &accountId, double frozenAmount, double usedAmount)
确认保证金(成交时:冻结转占用)
Definition account_manager.cpp:142
AccountManager(const AccountManager &)=delete
void clear()
清空所有账户
Definition account_manager.cpp:246
std::vector< std::string > getAllAccountIds() const
获取所有账户ID
Definition account_manager.cpp:75
存储接口
Definition store.hpp:61
Definition matching_engine.hpp:23
虚拟交易账户
Definition account.hpp:44