账户管理器
More...
#include <account_manager.hpp>
账户管理器
负责账户的创建、查询、资金冻结/释放等操作。 支持与IStore接口集成进行数据持久化。
- 线程安全
- 所有公共方法都是线程安全的,使用互斥锁保护内部数据。
- 保证金生命周期
- 下单时:freezeMargin() - 冻结保证金
- 成交时:confirmMargin() - 冻结转占用
- 撤单/拒绝时:unfreezeMargin() - 释放冻结
- 平仓时:releaseMargin() - 释放占用
- 使用示例
账户管理器
Definition account_manager.hpp:56
Account createAccount(const std::string &accountId, double initialBalance)
创建账户
Definition account_manager.cpp:41
bool freezeMargin(const std::string &accountId, double amount)
冻结保证金(下单时)
Definition account_manager.cpp:95
bool releaseMargin(const std::string &accountId, double amount)
释放占用保证金(平仓时)
Definition account_manager.cpp:171
bool confirmMargin(const std::string &accountId, double frozenAmount, double usedAmount)
确认保证金(成交时:冻结转占用)
Definition account_manager.cpp:142
◆ AccountManager() [1/3]
| fix40::AccountManager::AccountManager |
( |
| ) |
|
◆ AccountManager() [2/3]
| fix40::AccountManager::AccountManager |
( |
IStore * |
store | ) |
|
|
explicit |
带存储接口的构造函数
- Parameters
-
- Note
- 当 store 不为空时,会在构造阶段从存储中加载已存在的账户, 用于服务端重启后的资金状态恢复。
◆ ~AccountManager()
| fix40::AccountManager::~AccountManager |
( |
| ) |
|
|
default |
◆ AccountManager() [3/3]
◆ addCloseProfit()
| bool fix40::AccountManager::addCloseProfit |
( |
const std::string & |
accountId, |
|
|
double |
profit |
|
) |
| |
记录平仓盈亏
平仓时记录已实现盈亏,更新余额。
- Parameters
-
| accountId | 账户ID |
| profit | 平仓盈亏(正为盈利,负为亏损) |
- Returns
- 成功返回 true,账户不存在返回 false
- 资金变化
- balance += profit
- closeProfit += profit
- available += profit
◆ clear()
| void fix40::AccountManager::clear |
( |
| ) |
|
◆ confirmMargin()
| bool fix40::AccountManager::confirmMargin |
( |
const std::string & |
accountId, |
|
|
double |
frozenAmount, |
|
|
double |
usedAmount |
|
) |
| |
确认保证金(成交时:冻结转占用)
将冻结的保证金转为占用保证金。
- Parameters
-
| accountId | 账户ID |
| frozenAmount | 原冻结金额 |
| usedAmount | 实际占用金额(可能与冻结金额不同) |
- Returns
- 成功返回 true,账户不存在返回 false
- 资金变化
- frozenMargin -= frozenAmount
- usedMargin += usedAmount
- available += (frozenAmount - usedAmount) // 多冻结的部分返还
◆ createAccount()
| Account fix40::AccountManager::createAccount |
( |
const std::string & |
accountId, |
|
|
double |
initialBalance |
|
) |
| |
创建账户
创建一个新的虚拟账户,初始余额和可用资金相等。
- Parameters
-
| accountId | 账户ID |
| initialBalance | 初始余额 |
- Returns
- 创建的账户
- Note
- 如果账户已存在,返回现有账户
◆ freezeMargin()
| bool fix40::AccountManager::freezeMargin |
( |
const std::string & |
accountId, |
|
|
double |
amount |
|
) |
| |
冻结保证金(下单时)
从可用资金中冻结指定金额作为挂单保证金。
- Parameters
-
- Returns
- 成功返回 true,资金不足或账户不存在返回 false
- 资金变化
- available -= amount
- frozenMargin += amount
◆ getAccount()
| std::optional< Account > fix40::AccountManager::getAccount |
( |
const std::string & |
accountId | ) |
const |
获取账户
- Parameters
-
- Returns
- 账户信息,不存在时返回 std::nullopt
◆ getAllAccountIds()
| std::vector< std::string > fix40::AccountManager::getAllAccountIds |
( |
| ) |
const |
◆ hasAccount()
| bool fix40::AccountManager::hasAccount |
( |
const std::string & |
accountId | ) |
const |
检查账户是否存在
- Parameters
-
- Returns
- 存在返回 true
◆ operator=()
◆ releaseMargin()
| bool fix40::AccountManager::releaseMargin |
( |
const std::string & |
accountId, |
|
|
double |
amount |
|
) |
| |
释放占用保证金(平仓时)
平仓后释放占用的保证金。
- Parameters
-
- Returns
- 成功返回 true,账户不存在返回 false
- 资金变化
- usedMargin -= amount
- available += amount
◆ size()
| size_t fix40::AccountManager::size |
( |
| ) |
const |
◆ unfreezeMargin()
| bool fix40::AccountManager::unfreezeMargin |
( |
const std::string & |
accountId, |
|
|
double |
amount |
|
) |
| |
释放冻结保证金(撤单/拒绝时)
将冻结的保证金释放回可用资金。
- Parameters
-
- Returns
- 成功返回 true,账户不存在返回 false
- 资金变化
- available += amount
- frozenMargin -= amount
◆ updatePositionProfit()
| bool fix40::AccountManager::updatePositionProfit |
( |
const std::string & |
accountId, |
|
|
double |
profit |
|
) |
| |
更新持仓盈亏
更新账户的浮动盈亏,同时更新可用资金。
- Parameters
-
| accountId | 账户ID |
| profit | 新的持仓盈亏值(不是增量) |
- Returns
- 成功返回 true,账户不存在返回 false
The documentation for this class was generated from the following files: