Skip to content

Instantly share code, notes, and snippets.

@lucasnad27
Last active September 8, 2021 20:09
Show Gist options
  • Save lucasnad27/a7243a816ab5ab4c41d983125f7aacc9 to your computer and use it in GitHub Desktop.
Save lucasnad27/a7243a816ab5ab4c41d983125f7aacc9 to your computer and use it in GitHub Desktop.
Implementing class structure with minimal logic
"""
Portfolio implementation
"""
def eligible_for_rebalance(trading_day: Arrow) -> bool:
"""Decides if a given trading day should trigger a rebalance and returns a boolean."""
return is_eligible
def get_adjusted_close(s3_client: S3Client, s3_bucket: str, ticker: str, trading_day: Arrow) -> float:
"""Returns the closing price for a symbol on a given day."""
return qm.equities.historical.get_price(s3_client, s3_bucket, ticker, trading_day)
def update_positions(
s3_client: S3Client, s3_bucket: str, transactions: pd.DataFrame, trading_day: Arrow, cash: float
) -> pd.DataFrame:
# updates transactions df and returns it
# NOTE: s3_client is injected to make the connection, rather than depending on class attributes
return df
def calculate_returns(positions: pd.DataFrame) -> pd.Series:
"""
Calculates daily returns over the same time period and returns a Series.
> NOTE: Return calculations will mirror the date range of `positions`
"""
return positions.sum(axis=1).pct_change().fillna(0)
class Portfolio:
def __init__(self, **kwargs):
self.capital_allocation = capital_allocation
...
self.s3_client = boto3.client("s3")
@property
def transactions(self):
"""Records all transactions through portfolio's existence period."""
if self._transactions.empty:
self.run()
return self._transactions
def run(self):
"""
Iterates through each day of the portfolio time period.
Add record to positions dataframe and returns series identify if action should be taken if so.
pull data for equity universe and identify top n stocks
calculate position size for each stock and number of stocks to purchase
liquidate all existing positions (add to transactions dataframe)
enter new positions (add to transactions dataframe)
Rebalance quarterly, before quarter ending months: end of February, May, August, and November.
"""
# orchestrates calls to various funcitons and updates "private" variables like _transactions
pass
"""
Testing our Portfolio implementation
"""
def test_update_positions(s3_client, s3_bucket, transactions):
expected_positions = pd.DataFrame(...)
positions = update_positions(s3_client, s3_bucket, transactions, "2020-01-01")
assert expected_positions == positions
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment