While developing a trading strategy, you will definitely need information about the current balance of the selected account. To get this value, you need to use the Balance property of Account class.
But what about crypto balances? For example, if we have some crypto connection and we need to get all available balances of ‘BTC’ currency, or ‘ETH’ currency or both. How do we get these values? The answer is ‘Easy”)
CryptoAccount class
Quantower API supports a special CryptoAccount class for most crypto connections. This class is derived from the base Account class and extends its functionality with additional property - Balances and special BalanceUpdated event. In your code, you need to make sure the account you choose implements the CryptoAccountclass.
publicclassTestStrategy:Strategy{ [InputParameter("Selected account",10)]publicAccount inputAccount;privateCryptoAccount cryptoAccount;publicTestStrategy(): base() { // Defines strategy's name and description.this.Name="TestStrategy";this.Description="My strategy's annotation"; }protectedoverridevoidOnRun() { // if (inputAccount ==null) {Log("Input account is null",StrategyLoggingLevel.Error); Stop();return; } // check if selected account implements 'CryptoAccount' classif (inputAccount isCryptoAccount) { // cast to 'CryptoAccount' type and store as a variable cryptoAccount = (CryptoAccount)inputAccount; // subscribe to 'BalanceUpdated' eventcryptoAccount.BalanceUpdated+= CryptoAccount_BalanceUpdated; // log coin name and available balanceforeach (CryptoAssetBalances coin incryptoAccount.Balances) {Log($"Coin name: "+coin.AssetId);Log($"Avalilable balance: "+coin.AvailableBalance); } } }protectedoverridevoidOnStop() { // unsubscribe from 'BalanceUpdated' eventif (cryptoAccount !=null) {cryptoAccount.BalanceUpdated-= CryptoAccount_BalanceUpdated; cryptoAccount =null; } }privatevoidCryptoAccount_BalanceUpdated(object sender,CryptoAccountEventArgs e) {if (e.Reason==AccountBalanceEventReason.Update) {Log("Updated coin name: "+e.Balances.AssetId);Log("Updated available balance: "+e.Balances.AvailableBalance); } }}
The Balances property is an array that contains info about all available crypto coins and their states in this moment of time. Each item of this collection is an instance of CryptoAssetBalances class.
ReservedBalance - amount of reserved currency (submitting limit orders etc.)
AvailableBalance - amount of available currency (TotalBalance - ReservedBalance)
TotalInBTC - converted total amount of currency in ‘BTC’
TotalInUSD - converted total amount of currency in ‘USD’
If crypto connection doesn't support CryptoAccount class
Unfortunately, not all crypto connections support CryptoAccount class. The main reason for this is imperfection current broker API. For such connections, information about crypto balances is stored in a special AdditionalInfo collection.
All you need to do:
Run Debug-mode and find the required element in AdditionalInfo collection.
Store identifier(Id property) of this element as a constant.
Use 'TryGetItem' method with stored identifier to get required element.
Below is an example how to find the "BTC wallet balance" element for an Bybit-account.
Now, we can store this identifier as a constant and use it to get required additional field. Example below:
publicclassTestStrategy:Strategy{ // constprivateconststring BYBIT_BTC_WALLET_BALANCE ="BTC wallet balance"; // input parameter [InputParameter("Selected account",10)]publicAccount inputAccount;publicTestStrategy(): base() { // Defines strategy's name and description. Name ="TestStrategy"; Description ="My strategy's annotation"; }protectedoverridevoidOnRun() {if (inputAccount ==null) {Log("Input account is null",StrategyLoggingLevel.Error);Stop();return; }if (inputAccount.AdditionalInfo==null||inputAccount.AdditionalInfo.Count==0) {Log("AdditionalInfo collection is empty",StrategyLoggingLevel.Error);Stop();return; } // try to get 'BTC wallet balance' item if (inputAccount.AdditionalInfo.TryGetItem(BYBIT_BTC_WALLET_BALANCE,outAdditionalInfoItem btcWalletBalance)) {Log("Coin name: BTC");Log("Walet balance: "+btcWalletBalance.Value); } }}