-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathetf.py
163 lines (145 loc) · 5.6 KB
/
etf.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
#!/usr/bin/env python3
import argparse
import asyncio
import configparser
import json
import logging
import os
import sys
from aiorobinhood import RobinhoodClient, ClientError
async def main(args: argparse.Namespace) -> None:
# Load Robinhood account credentials
config = configparser.ConfigParser(interpolation=None)
config.read(args.path)
# Define symbols set
if args.weighting == "file":
try:
with open(args.symbols) as f:
weights = json.load(f)
if sum(weights.values()) > 1:
raise ValueError("Sum of custom weights must not exceed 1")
symbols = set(weights.keys())
except OSError:
raise RuntimeError(f"Could not find file '{args.symbols}'")
except json.JSONDecodeError:
raise RuntimeError(f"'{args.symbols}' does not contain valid JSON")
else:
symbols = set(args.symbols)
async with RobinhoodClient(timeout=args.timeout) as client:
await client.login(**config["credentials"])
portfolio, fundamentals, positions = await asyncio.gather(
client.get_portfolio(),
client.get_fundamentals(symbols=symbols),
client.get_positions(),
)
# Compute the target equity for each security
total_equity = 0.99 * float(portfolio["equity"])
if args.weighting == "cap":
total_cap = sum(float(f["market_cap"]) for f in fundamentals)
equity = {
symbol: total_equity * float(f["market_cap"]) / total_cap
for symbol, f in zip(symbols, fundamentals)
}
elif args.weighting == "equal":
equity = dict.fromkeys(symbols, total_equity / len(symbols))
elif args.weighting == "file":
equity = {
symbol: total_equity * weight
for symbol, weight in weights.items()
}
# Rebalance our existing positions
sell_orders = []
buy_orders = []
for position, quote in zip(
positions,
await client.get_quotes(instruments=[p["instrument"] for p in positions]),
):
if position["instrument"] != quote["instrument"]:
raise RuntimeError("Position and quote data are not aligned!")
symbol = quote["symbol"]
quantity = float(position["quantity"])
if symbol not in symbols:
logging.info(f"- {symbol:<5} (closing position)")
sell_orders.append(client.place_market_sell_order(symbol, quantity=quantity))
else:
current_equity = quantity * float(quote["last_trade_price"])
target_equity = equity[symbol]
if current_equity - target_equity > args.epsilon:
sell_value = current_equity - target_equity
logging.info(f"- {symbol:<5} ${sell_value:0.2f}")
sell_orders.append(client.place_market_sell_order(symbol, amount=sell_value))
elif target_equity - current_equity > args.epsilon:
buy_value = target_equity - current_equity
logging.info(f"+ {symbol:<5} ${buy_value:0.2f}")
buy_orders.append(client.place_market_buy_order(symbol, amount=buy_value))
else:
logging.info(f"= {symbol:<5} |${current_equity - target_equity:0.2f}| < ${args.epsilon}")
symbols.remove(symbol)
# Add new stocks to our positions
for symbol in symbols:
logging.info(f"+ {symbol:<5} ${equity[symbol]:0.2f} (new position)")
buy_orders.append(client.place_market_buy_order(symbol, amount=equity[symbol]))
# Process the orders (sell orders first)
for order in sell_orders + buy_orders:
if args.dry_run:
# Cancel the coroutine
order.close()
else:
try:
await order
except ClientError:
symbol = order.cr_frame.f_locals["args"][1]
logging.exception(f"{symbol} order failed")
finally:
await asyncio.sleep(6)
await client.logout()
if __name__ == "__main__":
logging.basicConfig(format="%(message)s", level=logging.INFO)
file_mode = "file" in sys.argv
parser = argparse.ArgumentParser(
description="Rebalance your Robinhood stock portfolio.",
formatter_class=argparse.ArgumentDefaultsHelpFormatter,
)
parser.add_argument(
"symbols",
metavar="SSSS",
type=str,
nargs=None if file_mode else "+",
help="path to JSON symbols file" if file_mode else "list of space separated stock symbols",
)
parser.add_argument(
"-d",
"--dry-run",
action="store_true",
help="disables live ordering",
)
parser.add_argument(
"-e",
"--epsilon",
type=float,
default=0.01,
help="minimum difference to trigger a rebalance (in dollars)",
)
parser.add_argument(
"-p",
"--path",
type=str,
default=f"{os.getenv('HOME')}/.config.ini",
help="path to the config file",
)
parser.add_argument(
"-t",
"--timeout",
type=int,
default=3,
help="aiorobinhood request timeout",
)
parser.add_argument(
"-w",
"--weighting",
type=str,
choices=["cap", "equal", "file"],
default="equal",
help="weighting scheme to use for rebalancing",
)
asyncio.run(main(parser.parse_args()))