IB API code for retrieving account value to calculate number of shares to be traded

I'm batch 54 student.  I'm looking to include in my algo a calculation, based on account value and historical price, the number of shares to be traded.  As Python is something new to me, I have no confidence at all in writing the code.  Would anyone have a similar code that you could share or point me in the right direction?  

Hello,





You can use the Accounts Updates and Accounts Summary functions provided by the API to receive regular account updates in your code. Basis the details you receive in these functions, you can devise your own logic on when to calculate the number of shares and whatnot. Unfortunately, I do not have the codes for the same. But they follow the same code structure as we learned in the TBP-01 class. Thanks.

Hi Jay,





Thanks for your reply.  I have run updateAccountValue method (copied from documentation and shown below) to obtain various account info.



    def updateAccountValue(self, key, val, currency, accountName):

        super().updateAccountValue(key, val, currency, accountName)

        print("UpdateAccountValue. Key:", key, "Value:", val,

                   "Currency:", currency, "AccountName:", accountName)





The print statement prints all the parameters available for "key" as well as all the currencies.  But I only need the CashBalance in USD.  How do I extract the CashBalance in USD value from key?  I'll then assign the CashBalance value to a variable.



Thanks.

Hello,



In that case, you can simply filter out the fields that you are interested in. If you only want CashBalance in USD, you can add if conditions as shown below:



def updateAccountValue(self, key, val, currency, accountName):

        if key == 'TotalCashBalance' and currency == 'BASE':

            print('\nUpdateAccountValue. Key:', key,

                  'Value:', val,

                  'Currency:', currency,

                  'AccountName:', accountName, '\n')



Though, IB TWS will return you all the fields, you only work with those which you need.