The script portfolio_manager.py
contains a class that
makes it easy to manage busket of stocks based on the shares or weight for each symbol name.
It is designed so that you can feed your own desired portfolio either by
calculation or simply from CSV.
You need to obtain Alpaca API key, found in the dashboard and set it in the environment variables. Please read API documents or Python SDK documents for more details.
The PortfolioManager
class implements the following methods.
- add_items(items) Add desired portfolio structure, in two dimentional list.
- rebalance(order_style, timeout=60)
Start rebalancing based on the shares by sending orders now.
order_style
can be either "timeout" or "block" - percent_rebalance(order_style, timeout=60)
Start rebalancing based on the percentage by sending orders now.
order_style
can be either "timeout" or "block"
The timeout
order style times out if orders are not executed in the specified timeout seconds.
The block
order style blocks the process indifinitely until orders are executed.
import os
from portfolio_manager import PortfolioManager
os.environ['APCA_API_KEY_ID'] = 'API_KEY_ID'
os.environ['APCA_API_SECRET_KEY'] = 'API_SECRET_KEY'
manager = PortfolioManager()
# Hedging SPY with GLD 1:1
manager.add_items([
['SPY', 0.5],
['GLD', 0.5],
])
manager.percent_rebalance('block')