-
Notifications
You must be signed in to change notification settings - Fork 3.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add cost to borrow of stocks. Data from IBKR (#1663)
* add ctb to dps * add test for ctb * reformat using black * fix tests for ctb Co-authored-by: didierlopes.eth <[email protected]> Co-authored-by: jmaslek <[email protected]>
- Loading branch information
1 parent
6535b1d
commit 73187d9
Showing
8 changed files
with
238 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
""" Interactive Broker Model """ | ||
__docformat__ = "numpy" | ||
|
||
import logging | ||
|
||
import ftplib | ||
from io import BytesIO | ||
import pandas as pd | ||
|
||
from openbb_terminal.decorators import log_start_end | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
@log_start_end(log=logger) | ||
def get_cost_to_borrow() -> pd.DataFrame: | ||
"""Get stocks with highest cost to borrow [Source: Interactive Broker] | ||
Returns | ||
------- | ||
pd.DataFrame | ||
Cost to borrow | ||
""" | ||
ftp = ftplib.FTP("ftp3.interactivebrokers.com", "shortstock") | ||
|
||
flo = BytesIO() | ||
ftp.retrbinary("RETR usa.txt", flo.write) | ||
flo.seek(0) | ||
|
||
data = pd.read_csv(flo, sep="|", skiprows=1) | ||
data = data[["#SYM", "FEERATE", "AVAILABLE"]] | ||
data["AVAILABLE"] = data["AVAILABLE"].replace(">10000000", 10000000) | ||
data.fillna(0, inplace=True) | ||
data["AVAILABLE"] = data["AVAILABLE"].astype(int) | ||
data.sort_values(by=["FEERATE"], ascending=False, inplace=True) | ||
data["FEERATE"] = data["FEERATE"].apply(lambda x: str(x) + "%") | ||
data.columns = ["Symbol", "Fees", "Available"] | ||
return data |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
""" Interactive Broker View """ | ||
__docformat__ = "numpy" | ||
|
||
import logging | ||
import os | ||
|
||
from openbb_terminal.decorators import log_start_end | ||
from openbb_terminal.helper_funcs import export_data, print_rich_table | ||
from openbb_terminal.rich_config import console | ||
from openbb_terminal.stocks.dark_pool_shorts import ibkr_model | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
@log_start_end(log=logger) | ||
def display_cost_to_borrow(num_stocks: int, export: str): | ||
"""Display stocks with highest cost to borrow. [Source: Interactive Broker] | ||
Parameters | ||
---------- | ||
num_stocks: int | ||
Number of stocks to display | ||
export : str | ||
Export dataframe data to csv,json,xlsx file | ||
""" | ||
df = ibkr_model.get_cost_to_borrow().head(num_stocks) | ||
|
||
if df.empty: | ||
console.print("No data found.") | ||
else: | ||
print_rich_table( | ||
df, | ||
headers=list(df.columns), | ||
show_index=False, | ||
title="Highest Cost to Borrow", | ||
) | ||
console.print("") | ||
|
||
export_data( | ||
export, | ||
os.path.dirname(os.path.abspath(__file__)), | ||
"cost_to_borrow", | ||
df, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
62 changes: 62 additions & 0 deletions
62
website/content/terminal/stocks/dark_pool_shorts/ctb/_index.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
``` | ||
usage: ctb [-n NUM] [--export {csv,json,xlsx}] [-h] | ||
``` | ||
|
||
Request a list of stocks that have the highest cost to borrow [Source: Interactive Broker] | ||
|
||
``` | ||
optional arguments: | ||
-n NUM, --num NUM Number of the stocks with high cost to borrow to retrieve. (default: 20) | ||
--export {csv,json,xlsx} | ||
Export dataframe data to csv,json,xlsx file | ||
-h, --help show this help message (default: False) | ||
``` | ||
|
||
Example: | ||
``` | ||
2022 Apr 07, 09:47 (�) /stocks/dps/ $ ctb | ||
Highest Cost to Borrow | ||
┌─────────┬───────────┬───────────┐ | ||
│ Symbol │ Fees │ Available │ | ||
├─────────┼───────────┼───────────┤ | ||
│ PIK │ 457.4402% │ 7000 │ | ||
├─────────┼───────────┼───────────┤ | ||
│ EVTL │ 413.7294% │ 10000 │ | ||
├─────────┼───────────┼───────────┤ | ||
│ CELZ │ 330.9411% │ 100000 │ | ||
├─────────┼───────────┼───────────┤ | ||
│ DRCT │ 304.9628% │ 300000 │ | ||
├─────────┼───────────┼───────────┤ | ||
│ MYNZ │ 291.8163% │ 3000 │ | ||
├─────────┼───────────┼───────────┤ | ||
│ DCFC │ 279.0673% │ 10000 │ | ||
├─────────┼───────────┼───────────┤ | ||
│ FGFPP │ 278.422% │ 8000 │ | ||
├─────────┼───────────┼───────────┤ | ||
│ ZTEK │ 250.0307% │ 75000 │ | ||
├─────────┼───────────┼───────────┤ | ||
│ FRGE │ 245.1883% │ 700 │ | ||
├─────────┼───────────┼───────────┤ | ||
│ SATL │ 240.7341% │ 500 │ | ||
├─────────┼───────────┼───────────┤ | ||
│ ARMR │ 240.5414% │ 5000 │ | ||
├─────────┼───────────┼───────────┤ | ||
│ PILL │ 223.538% │ 55000 │ | ||
├─────────┼───────────┼───────────┤ | ||
│ ARQQ │ 217.3978% │ 3000 │ | ||
├─────────┼───────────┼───────────┤ | ||
│ NRSN │ 216.9888% │ 6000 │ | ||
├─────────┼───────────┼───────────┤ | ||
│ ZIONL │ 215.2871% │ 35000 │ | ||
├─────────┼───────────┼───────────┤ | ||
│ BLBX │ 213.441% │ 15000 │ | ||
├─────────┼───────────┼───────────┤ | ||
│ WINSF │ 212.907% │ 10000 │ | ||
├─────────┼───────────┼───────────┤ | ||
│ SKYH │ 206.7746% │ 25000 │ | ||
├─────────┼───────────┼───────────┤ | ||
│ SOHON │ 205.1096% │ 40000 │ | ||
├─────────┼───────────┼───────────┤ | ||
│ MH PRA │ 202.1235% │ 100000 │ | ||
└─────────┴───────────┴───────────┘ | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters