FIX 4.0 Demo 1.0
Loading...
Searching...
No Matches
fix40::AccountManager Class Reference

账户管理器 More...

#include <account_manager.hpp>

Collaboration diagram for fix40::AccountManager:
Collaboration graph

Public Member Functions

 AccountManager ()
 默认构造函数
 
 AccountManager (IStore *store)
 带存储接口的构造函数
 
 ~AccountManager ()=default
 析构函数
 
 AccountManager (const AccountManager &)=delete
 
AccountManageroperator= (const AccountManager &)=delete
 
Account createAccount (const std::string &accountId, double initialBalance)
 创建账户
 
std::optional< AccountgetAccount (const std::string &accountId) const
 获取账户
 
bool hasAccount (const std::string &accountId) const
 检查账户是否存在
 
std::vector< std::string > getAllAccountIds () const
 获取所有账户ID
 
size_t size () const
 获取账户数量
 
bool freezeMargin (const std::string &accountId, double amount)
 冻结保证金(下单时)
 
bool unfreezeMargin (const std::string &accountId, double amount)
 释放冻结保证金(撤单/拒绝时)
 
bool confirmMargin (const std::string &accountId, double frozenAmount, double usedAmount)
 确认保证金(成交时:冻结转占用)
 
bool releaseMargin (const std::string &accountId, double amount)
 释放占用保证金(平仓时)
 
bool updatePositionProfit (const std::string &accountId, double profit)
 更新持仓盈亏
 
bool addCloseProfit (const std::string &accountId, double profit)
 记录平仓盈亏
 
void clear ()
 清空所有账户
 

Detailed Description

账户管理器

负责账户的创建、查询、资金冻结/释放等操作。 支持与IStore接口集成进行数据持久化。

线程安全
所有公共方法都是线程安全的,使用互斥锁保护内部数据。
保证金生命周期
  1. 下单时:freezeMargin() - 冻结保证金
  2. 成交时:confirmMargin() - 冻结转占用
  3. 撤单/拒绝时:unfreezeMargin() - 释放冻结
  4. 平仓时:releaseMargin() - 释放占用
使用示例
// 创建账户
auto account = mgr.createAccount("user001", 1000000.0);
// 下单时冻结保证金
mgr.freezeMargin("user001", 50000.0);
// 成交时转为占用保证金
mgr.confirmMargin("user001", 50000.0, 50000.0);
// 平仓时释放占用保证金
mgr.releaseMargin("user001", 50000.0);
账户管理器
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

Constructor & Destructor Documentation

◆ AccountManager() [1/3]

fix40::AccountManager::AccountManager ( )

默认构造函数

创建不带持久化的账户管理器。

◆ AccountManager() [2/3]

fix40::AccountManager::AccountManager ( IStore store)
explicit

带存储接口的构造函数

Parameters
store存储接口指针(可为nullptr)
Note
当 store 不为空时,会在构造阶段从存储中加载已存在的账户, 用于服务端重启后的资金状态恢复。
Here is the call graph for this function:

◆ ~AccountManager()

fix40::AccountManager::~AccountManager ( )
default

析构函数

◆ AccountManager() [3/3]

fix40::AccountManager::AccountManager ( const AccountManager )
delete

Member Function Documentation

◆ 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
如果账户已存在,返回现有账户
Here is the caller graph for this function:

◆ freezeMargin()

bool fix40::AccountManager::freezeMargin ( const std::string &  accountId,
double  amount 
)

冻结保证金(下单时)

从可用资金中冻结指定金额作为挂单保证金。

Parameters
accountId账户ID
amount冻结金额
Returns
成功返回 true,资金不足或账户不存在返回 false
资金变化
  • available -= amount
  • frozenMargin += amount

◆ getAccount()

std::optional< Account > fix40::AccountManager::getAccount ( const std::string &  accountId) const

获取账户

Parameters
accountId账户ID
Returns
账户信息,不存在时返回 std::nullopt
Here is the caller graph for this function:

◆ getAllAccountIds()

std::vector< std::string > fix40::AccountManager::getAllAccountIds ( ) const

获取所有账户ID

Returns
账户ID列表

◆ hasAccount()

bool fix40::AccountManager::hasAccount ( const std::string &  accountId) const

检查账户是否存在

Parameters
accountId账户ID
Returns
存在返回 true
Here is the caller graph for this function:

◆ operator=()

AccountManager & fix40::AccountManager::operator= ( const AccountManager )
delete

◆ releaseMargin()

bool fix40::AccountManager::releaseMargin ( const std::string &  accountId,
double  amount 
)

释放占用保证金(平仓时)

平仓后释放占用的保证金。

Parameters
accountId账户ID
amount释放金额
Returns
成功返回 true,账户不存在返回 false
资金变化
  • usedMargin -= amount
  • available += amount

◆ size()

size_t fix40::AccountManager::size ( ) const

获取账户数量

Returns
账户数量

◆ unfreezeMargin()

bool fix40::AccountManager::unfreezeMargin ( const std::string &  accountId,
double  amount 
)

释放冻结保证金(撤单/拒绝时)

将冻结的保证金释放回可用资金。

Parameters
accountId账户ID
amount释放金额
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: