|
| 1 | +from pprint import pprint |
| 2 | +from ibw.client import IBClient |
| 3 | + |
| 4 | +# Create a new session of the IB Web API. |
| 5 | +ib_client = IBClient( |
| 6 | + username="PAPER_USERNAME", |
| 7 | + account="PAPER_ACCOUNT_ACCOUNT_NUMBER" |
| 8 | +) |
| 9 | + |
| 10 | +# create a new session |
| 11 | +ib_client.create_session() |
| 12 | + |
| 13 | +# grab the account data. |
| 14 | +account_data = ib_client.portfolio_accounts() |
| 15 | +pprint(account_data) |
| 16 | +pprint('') |
| 17 | + |
| 18 | +# grab account portfolios |
| 19 | +account_positions = ib_client.portfolio_account_positions( |
| 20 | + account_id="PAPER_ACCOUNT_ACCOUNT_NUMBER", |
| 21 | + page_id=0 |
| 22 | +) |
| 23 | +pprint(account_positions) |
| 24 | +pprint('') |
| 25 | + |
| 26 | +# grab account PNL |
| 27 | +account_pnl = ib_client.server_account_pnl() |
| 28 | +pprint(account_pnl) |
| 29 | +pprint('') |
| 30 | + |
| 31 | +# grab server accounts |
| 32 | +server_accounts = ib_client.server_accounts() |
| 33 | +pprint(server_accounts) |
| 34 | +pprint('') |
| 35 | + |
| 36 | +# Grab historical prices. |
| 37 | +aapl_prices = ib_client.market_data_history( |
| 38 | + conid=['265598'], |
| 39 | + period='1d', |
| 40 | + bar='5min' |
| 41 | +) |
| 42 | +pprint(aapl_prices) |
| 43 | +pprint('') |
| 44 | + |
| 45 | +# Grab current quotes |
| 46 | +quote_fields = [55, 7296, 7295, 86, 70, 71, 84, 31] |
| 47 | +aapl_current_prices = ib_client.market_data( |
| 48 | + conids=['265598'], |
| 49 | + since='0', |
| 50 | + fields=quote_fields |
| 51 | +) |
| 52 | +pprint(aapl_current_prices) |
| 53 | +pprint('') |
| 54 | + |
| 55 | +# Grab the quarterly income statement |
| 56 | +aapl_income_statement = ib_client.fundamentals_financials( |
| 57 | + conid='265598', |
| 58 | + financial_statement='income', |
| 59 | + period='quarter' |
| 60 | +) |
| 61 | +pprint(aapl_income_statement) |
| 62 | +pprint('') |
| 63 | + |
| 64 | +# Grab the annual income statement |
| 65 | +aapl_balance_sheet = ib_client.fundamentals_financials( |
| 66 | + conid='265598', |
| 67 | + financial_statement='balance', |
| 68 | + period='annual' |
| 69 | +) |
| 70 | +pprint(aapl_balance_sheet) |
| 71 | +pprint('') |
| 72 | + |
| 73 | +# Grab Key Ratios for Apple. |
| 74 | +aapl_key_ratios = ib_client.fundamentals_key_ratios(conid='265598') |
| 75 | +pprint(aapl_key_ratios) |
| 76 | +pprint('') |
| 77 | + |
| 78 | +# Grab Dividends for Apple. |
| 79 | +aapl_dividends = ib_client.fundamentals_dividends(conid='265598') |
| 80 | +pprint(aapl_dividends) |
| 81 | +pprint('') |
| 82 | + |
| 83 | +# Grab ESG (Environmental, Social, and Governance) for Apple. |
| 84 | +aapl_esg = ib_client.fundamentals_esg(conid='265598') |
| 85 | +pprint(aapl_esg) |
| 86 | +pprint('') |
| 87 | + |
| 88 | +# search for a symbol |
| 89 | +search_symbol_result = ib_client.symbol_search(symbol='AAPL') |
| 90 | +pprint(search_symbol_result) |
| 91 | +pprint('') |
| 92 | + |
| 93 | +# Grab the news |
| 94 | +aapl_news = ib_client.data_news(conid='265598') |
| 95 | +pprint(aapl_news) |
| 96 | +pprint('') |
| 97 | + |
| 98 | +# Grab the analyst ratings for Apple |
| 99 | +aapl_analyst_ratings = ib_client.data_ratings(conid='265598') |
| 100 | +pprint(aapl_analyst_ratings) |
| 101 | +pprint('') |
| 102 | + |
| 103 | +# Grab Ownership details for Apple |
| 104 | +aapl_ownership = ib_client.data_ownership(conid='265598') |
| 105 | +pprint(aapl_ownership) |
| 106 | +pprint('') |
| 107 | + |
| 108 | +# Grab Comeptitor Details For Apple |
| 109 | +aapl_competitors = ib_client.data_competitors(conid='265598') |
| 110 | +pprint(aapl_competitors) |
| 111 | +pprint('') |
| 112 | + |
| 113 | +# Grab Analyst Forecast for Apple |
| 114 | +aapl_analyst_forecast = ib_client.data_analyst_forecast(conid='265598') |
| 115 | +pprint(aapl_analyst_forecast) |
| 116 | +pprint('') |
| 117 | + |
| 118 | +# grab live orders |
| 119 | +account_live_orders = ib_client.get_live_orders() |
| 120 | +pprint(account_live_orders) |
| 121 | +pprint('') |
| 122 | + |
| 123 | +# grab trades |
| 124 | +account_trades = ib_client.trades() |
| 125 | +pprint(account_trades) |
| 126 | +pprint('') |
| 127 | + |
| 128 | +# Do a Futures Search |
| 129 | +futures_search = ib_client.futures_search(symbols=['ES']) |
| 130 | +pprint.pprint(futures_search) |
| 131 | +pprint('') |
| 132 | + |
| 133 | +# Grab Portfolio Accounts |
| 134 | +portfolio_accounts = ib_client.portfolio_accounts() |
| 135 | +pprint.pprint(portfolio_accounts) |
| 136 | +pprint('') |
| 137 | + |
| 138 | +# Grab Account Info |
| 139 | +portfolio_account_info = ib_client.portfolio_account_info( |
| 140 | + account_id="PAPER_ACCOUNT_ACCOUNT_NUMBER" |
| 141 | +) |
| 142 | +pprint.pprint(portfolio_account_info) |
| 143 | +pprint('') |
| 144 | + |
| 145 | +# Grab the Positions in the portfolio. |
| 146 | +portfolio_positions = ib_client.portfolio_account_positions( |
| 147 | + account_id="PAPER_ACCOUNT_ACCOUNT_NUMBER", |
| 148 | + page_id=0 |
| 149 | +) |
| 150 | +pprint.pprint(portfolio_positions) |
| 151 | +pprint('') |
| 152 | + |
| 153 | +# Grab the Specific Postion in a Portfolio. |
| 154 | +portfolio_position = ib_client.portfolio_account_position( |
| 155 | + account_id="PAPER_ACCOUNT_ACCOUNT_NUMBER", |
| 156 | + conid=272093 |
| 157 | +) |
| 158 | +pprint.pprint(portfolio_position) |
| 159 | +pprint('') |
| 160 | + |
| 161 | +# Grab a Summary of the Portfolio. |
| 162 | +portfolio_summary = ib_client.portfolio_account_summary( |
| 163 | + account_id="PAPER_ACCOUNT_ACCOUNT_NUMBER" |
| 164 | +) |
| 165 | +pprint.pprint(portfolio_summary) |
| 166 | +pprint('') |
| 167 | + |
| 168 | +# Grab the Portfolio Ledger. |
| 169 | +portfolio_ledger = ib_client.portfolio_account_ledger( |
| 170 | + account_id="PAPER_ACCOUNT_ACCOUNT_NUMBER" |
| 171 | +) |
| 172 | +pprint.pprint(portfolio_ledger) |
| 173 | +pprint('') |
| 174 | + |
| 175 | +# Grab the portfolio Allocation. |
| 176 | +portfolio_allocation = ib_client.portfolio_account_allocation( |
| 177 | + account_id="PAPER_ACCOUNT_ACCOUNT_NUMBER" |
| 178 | +) |
| 179 | +pprint.pprint(portfolio_allocation) |
| 180 | +pprint('') |
| 181 | + |
| 182 | +# Grab Portfolio Allocations. |
| 183 | +portfolio_allocations = ib_client.portfolio_accounts_allocation( |
| 184 | + account_ids="PAPER_ACCOUNT_ACCOUNT_NUMBER" |
| 185 | +) |
| 186 | +pprint.pprint(portfolio_allocations) |
| 187 | +pprint('') |
| 188 | + |
| 189 | +# Grab Customer Info. |
| 190 | +customer_info = ib_client.customer_info() |
| 191 | +pprint.pprint(customer_info) |
| 192 | +pprint('') |
| 193 | + |
| 194 | +# Get the number of Unread messages. |
| 195 | +unread_messages = ib_client.get_unread_messages() |
| 196 | +unread_for_bn = unread_messages['BN'] |
| 197 | +pprint.pprint(unread_messages) |
| 198 | +pprint('') |
| 199 | + |
| 200 | +# Grab the Subscriptions Codes. |
| 201 | +subscriptions = ib_client.get_subscriptions() |
| 202 | +pprint.pprint(subscriptions) |
| 203 | +pprint('') |
| 204 | + |
| 205 | +# Grab the Delivery Options for a Subscription. |
| 206 | +subscriptions_delivery = ib_client.subscriptions_delivery_options() |
| 207 | +pprint.pprint(subscriptions_delivery) |
| 208 | +pprint('') |
| 209 | + |
| 210 | +# Grab a Discaimer for a specific Subscription. |
| 211 | +sub_code = 'M8' |
| 212 | +subscriptions_disclaimer = ib_client.subscriptions_disclaimer( |
| 213 | + type_code=sub_code |
| 214 | +) |
| 215 | +pprint.pprint(subscriptions_disclaimer) |
| 216 | +pprint('') |
| 217 | + |
| 218 | +# Define a Mutual Fund Contract ID. |
| 219 | +mutual_fund_conid = '10753238' |
| 220 | + |
| 221 | +# Grab Fees and Objectives. |
| 222 | +mutual_fund_fees = ib_client.mutual_funds_portfolios_and_fees( |
| 223 | + conid=mutual_fund_conid) |
| 224 | +pprint.pprint(mutual_fund_fees) |
| 225 | +pprint('') |
| 226 | + |
| 227 | +# Possible Values for Performance. |
| 228 | +POSSIBLE_VALUES = ['6M', '1Y', '3Y', '5Y', '10Y'] |
| 229 | + |
| 230 | +# Grab Performance. |
| 231 | +mutual_fund_ratings = ib_client.mutual_funds_performance( |
| 232 | + conid=mutual_fund_conid, |
| 233 | + risk_period='6M', |
| 234 | + yield_period='6M', |
| 235 | + statistic_period='6M' |
| 236 | +) |
| 237 | +pprint.pprint(mutual_fund_ratings) |
| 238 | +pprint('') |
0 commit comments