-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinteractive_brokers_margin.py
executable file
·62 lines (51 loc) · 1.84 KB
/
interactive_brokers_margin.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#!/usr/bin/env python3
"""Store IBKR margin rate history for USD, CHF."""
import subprocess
import pandas as pd
from loguru import logger
import common
import ledger_amounts
def get_ibkr_loan_balance(currency):
"""Get ledger balance for IB."""
match currency:
case "USD":
ledger_currency = "\\\\$"
case "CHF":
ledger_currency = "CHF"
value = int(
float(
subprocess.check_output(
f"{ledger_amounts.LEDGER_BALANCE_CMD} --limit 'commodity=~/^{ledger_currency}$/' "
"'Interactive Brokers'",
text=True,
shell=True,
)
)
)
if value >= 0:
return 1
return abs(value)
def get_interest_rate(currency, loan):
"""Get interest rate from IB."""
logger.info(f"Getting interest rate for {currency=} {loan=}")
with common.run_with_browser_page(
"https://www.interactivebrokers.com/en/trading/margin-rates.php"
) as page:
page.get_by_role("link", name=" Accept Cookies").click()
page.locator("#int_calc_db_balance").click()
page.locator("#int_calc_db_balance").fill(f"{loan}")
page.locator("#int_calc_db_currency").select_option(currency)
page.get_by_role("link", name="Calculate Blended Rate", exact=True).click()
page.get_by_role("heading", name="%").click()
return float(page.locator("#int_calc_db_blendrate").inner_text().strip("%"))
def main():
"""Writes IB margin rates to DB."""
interest_rates = {}
for currency in ["USD", "CHF"]:
interest_rates[currency] = get_interest_rate(
currency, get_ibkr_loan_balance(currency)
)
new_df = pd.DataFrame(interest_rates, index=[pd.Timestamp.now()])
common.to_sql(new_df, "interactive_brokers_margin_rates")
if __name__ == "__main__":
main()