Search Framework:
BrokerAccount
Namespace: WealthLab.Backtest
Parent: Object

The BrokerAccount class represents an account associated with a connected Broker Provider. It contains account balances, Broker-reported Positions, currency balances, Portfolio Sync Live Positions, Strategy Positions, and other account-specific information.

Account Balances
AccountValue
public double AccountValue

Represents the overall account balance or value. The default value is Double.NaN. If the Broker does not expose a value that maps cleanly to AccountValue, leave it unassigned and use CustomFields for Broker-specific information.


BuyingPower
public double BuyingPower

Represents the current buying power available in the account. The default value is Double.NaN. If the Broker does not expose a value that maps cleanly to BuyingPower, leave it unassigned and use CustomFields.


Cash
public double Cash

Represents the available cash balance in the account. The default value is Double.NaN. If the Broker does not expose a value that maps cleanly to Cash, leave it unassigned and use CustomFields.


CustomFields
public Dictionary<string, string> CustomFields

Contains Broker-specific account values that do not map cleanly to the standard AccountValue, Cash, BuyingPower, or other BrokerAccount properties. WealthLab can display these values along with the standard account balances.


TodaysGainLoss
public double TodaysGainLoss

Represents the account's gain or loss for the current trading day. The default value is Double.NaN.



Broker Positions
FindPosition
public BrokerPosition FindPosition(string symbol, PositionType pt)

Returns the first Broker-reported Position matching the specified symbol and PositionType, or null if no matching Position exists.


FindPositions
public List<BrokerPosition> FindPositions(string symbol, PositionType pt)

Returns all Broker-reported Positions matching the specified symbol and PositionType. This is useful for Brokers that maintain multiple separate lots for the same symbol and PositionType.


GetPositions
[Obsolete("Use GetPositionsSnapshot() instead � it provides a consistent view under concurrent access")]
public List<BrokerPosition> GetPositions()

Returns a copy of the Broker-reported Positions. This method is obsolete. Use GetPositionsSnapshot instead.


GetPositionsSnapshot
public IReadOnlyList<BrokerPosition> GetPositionsSnapshot()

Returns a stable snapshot of the current Broker-reported Positions. Use this method when code needs to enumerate or otherwise work with a consistent view of the Positions while the Broker Provider may be updating them concurrently.


Positions
public List<BrokerPosition> Positions

Gets or sets the Broker-reported Positions currently held in the account. Broker Providers typically create a BrokerPosition for each open Position reported by the Broker and assign or add them to this collection. Access to the underlying collection is protected by a reader/writer lock. Code that needs a stable copy of the Positions should use GetPositionsSnapshot.


PositionsProfit
public double PositionsProfit

Returns the total current profit of all Broker-reported Positions by summing each BrokerPosition's Profit.


PositionsProfitPct
public double PositionsProfitPct

Returns the total percentage profit of the Broker-reported Positions. WealthLab calculates the total cost basis from each Position's Quantity * BasisPrice, and expresses PositionsProfit as a percentage of that total basis. Returns zero if the total basis is zero.


PositionsValue
public double PositionsValue

Returns the total current value of all Broker-reported Positions by summing each BrokerPosition's Value.



Constructors
BrokerAccount
public BrokerAccount(BrokerBase broker)

Creates a BrokerAccount associated with the supplied BrokerBase instance.



Currency Balances
CurrencyBalances
public Dictionary<string, double> CurrencyBalances

Contains account balances keyed by currency symbol. Broker Providers can populate this Dictionary for accounts that hold balances in multiple currencies.


GetCurrencyBalance
public double GetCurrencyBalance(string symbol)

Returns the balance associated with the specified currency symbol. Returns zero if the currency is not present in CurrencyBalances.


SetCurrencyBalance
public void SetCurrencyBalance(string symbol, double value)

Sets the balance for the specified currency symbol. If value is zero, the currency is removed from CurrencyBalances.



Identification
AccountID
public string AccountID

Gets or sets the account number or identifier associated with this account.


Broker
public BrokerBase Broker

Gets or sets the BrokerBase instance associated with the account.


DisplayName
public string DisplayName

Returns the account's display name. If a Nickname has been assigned, returns the nickname followed by the AccountID in parentheses. Otherwise returns the AccountID.


FullName
public string FullName

Returns the Broker and account identifier combined in the form: BrokerName:AccountID


Nickname
public string Nickname

Gets or sets a user-defined nickname for the account. WealthLab persists the nickname in the user's settings.


ToString
public override string ToString()

Returns the value of DisplayName.



Live Positions
LivePositions
public List<BrokerPosition> LivePositions

Contains the account's Portfolio Sync Live Positions. These are generated and maintained by WealthLab when synchronizing the Broker's actual Positions with Strategy execution.


LiveStartDate
public DateTime? LiveStartDate

Returns the earliest EntryDate among the account's LivePositions. Returns null when no Live Positions are present.



Other Data
UserData
public object UserData

Gets or sets an arbitrary object that a Broker Provider can associate with the account for Provider-specific purposes.



Persistence
Clone
public BrokerAccount Clone()

Creates and returns a copy of the BrokerAccount using its persisted representation. The cloned account remains associated with the same BrokerBase instance.


Parse
public static BrokerAccount Parse(string s, BrokerBase bb)

Creates and returns a BrokerAccount from the persisted string s, associating it with the BrokerBase instance bb. The method restores the account balances, Broker Positions, and CurrencyBalances, and loads the account's persisted Strategy Positions.


Persist
public string Persist()

Returns a persisted representation of the BrokerAccount. The persisted data includes AccountID, AccountValue, Cash, BuyingPower, Broker-reported Positions, and CurrencyBalances.



Strategy Positions
FindStrategyPosition
public BrokerPosition FindStrategyPosition(Strategy s, string symbol, PositionType pt, bool considerMatchFlag)

Returns the Strategy Position matching the supplied Strategy, symbol, and PositionType. Strategy matching is based on the Strategy's QualifiedName. If considerMatchFlag is true, only a Strategy Position whose MatchedInPortSync flag is currently false is returned. The returned Position is then marked as matched. This behavior is used during Portfolio Sync processing.


OpenStrategyPositionCount
public int OpenStrategyPositionCount(Strategy s)

Returns the number of Strategy Positions associated with the supplied Strategy. Matching is based on the Strategy's QualifiedName. Returns zero if s is null.


RecordStrategyFill
public void RecordStrategyFill(Transaction t)

Updates the account's Strategy Positions based on a filled Transaction. For an entry Transaction, WealthLab creates or increases the matching Strategy Position. For an exit Transaction, it reduces the matching Strategy Position and removes it when its Quantity reaches zero. Updated Strategy Positions are persisted to the local trade history.


StrategyPositions
public List<BrokerPosition> StrategyPositions

Returns WealthLab's internal collection of Positions created from Strategy fills for this account. Strategy Positions are separate from the Broker-reported Positions collection and are used by Portfolio Sync features such as the trading preference that restricts Strategies to exiting only linked Positions.



Trading Thresholds
Thresholds
public TradingThresholds Thresholds

Gets or sets the TradingThresholds associated with this Broker account. WealthLab persists Trading Threshold settings separately for each Broker and AccountID. When first accessed, the property loads previously saved thresholds or creates a new TradingThresholds instance if none have been saved.