Overview
The accounts module (server/accounts.ts) provides comprehensive account management including CRUD operations, metrics calculation, payouts, and bulk operations on accounts and instruments.
Account Operations
setupAccountAction
Create or update a trading account with full configuration.server/accounts.ts:218
Account object with configuration. Can include:
number: Account number (required)propfirm: Prop firm namestartingBalance: Initial balancedrawdownThreshold: Max allowed drawdownprofitTarget: Target profitisPerformance: Whether this is a performance accountgroupId: Optional group assignmentconsiderBuffer: Whether to consider buffer in calculations
The saved account with payouts and group relations loaded
If an account with the same number already exists for the user, it will be updated instead of creating a duplicate.
createAccountAction
Create a minimal account with just an account number.server/accounts.ts:520
The account number
The created account with default values
getAccountsAction
Retrieve all accounts for the authenticated user.server/accounts.ts:347
Array of accounts with payouts included
deleteAccountAction
Delete a trading account.server/accounts.ts:335
The account object to delete
renameAccountAction
Rename an account number (updates account, trades, and payouts).server/accounts.ts:133
Current account number
New account number
This operation uses a database transaction to ensure all updates succeed together or all fail.
Trade Operations
fetchGroupedTradesAction
Fetch all trades for a user, grouped by account and instrument.server/accounts.ts:32
The user ID to fetch trades for
Trades grouped by account number, then by instrument
All trades in a flat array
removeAccountFromTradesAction
Delete all trades and the account for a specific account number.server/accounts.ts:78
The account number to remove
removeAccountsFromTradesAction
Delete multiple accounts and their trades in one operation.server/accounts.ts:60
Array of account numbers to remove
deleteTradesByIdsAction
Delete specific trades by their IDs.server/accounts.ts:205
Array of trade IDs to delete
Instrument Operations
deleteInstrumentGroupAction
Delete all trades for a specific instrument in an account.server/accounts.ts:90
The account number
Instrument prefix (e.g., ‘ES’, ‘NQ’)
User ID for verification
Uses
startsWith matching, so ‘ES’ will match ‘ES’, ‘ESH24’, ‘ESM24’, etc.renameInstrumentAction
Rename an instrument across all trades in an account.server/accounts.ts:470
The account number
Current instrument name
New instrument name
updateCommissionForGroupAction
Update commission for all trades in an instrument group.server/accounts.ts:106
The account number
Instrument prefix
Commission per contract
Commission is automatically calculated as:
newCommission × trade.quantityMetrics Calculation
calculateAccountBalanceAction
Calculate current balance for accounts based on trades and starting balance.server/accounts.ts:546
Array of accounts to calculate balance for
Optional pre-fetched trades (improves performance)
Accounts with
balanceToDate property populatedcalculateAccountMetricsAction
Calculate comprehensive metrics including consistency, trading days, and daily performance.server/accounts.ts:636
Array of accounts to calculate metrics for
Accounts with
metrics and dailyMetrics properties populated- Win rate and loss rate
- Average win and average loss
- Profit factor
- Consistency score
- Trading days count
- Daily PnL breakdown
This function fetches trades internally for optimal performance and uses shared computation logic with the client.
Payout Management
savePayoutAction
Create or update a payout record.server/accounts.ts:378
Payout object with:
id: Unique ID (generated if creating new)accountNumber: Associated accountamount: Payout amountdate: Payout datestatus: Payout status
The saved payout record
deletePayoutAction
Delete a payout and decrement the account’s payout count.server/accounts.ts:430
The payout ID to delete
Whether deletion was successful
Account Reset
checkAndResetAccountsAction
Check for accounts with reset dates and reset them if the date has passed.server/accounts.ts:495
This should be called periodically (e.g., daily cron job) to automatically reset accounts.
Cache Management
Account operations automatically update relevant cache tags:Best Practices
Use transactions for related updates
Use transactions for related updates
Calculate metrics efficiently
Calculate metrics efficiently
Pass pre-fetched trades to
calculateAccountBalanceAction to avoid redundant queries:Verify account ownership
Verify account ownership
Always include userId in queries to prevent unauthorized access:
Related Functions
Trade Operations
Manage trades, tags, and images
Subscriptions
Manage user subscriptions and billing