Created
September 8, 2021 20:09
-
-
Save lucasnad27/eb39dc7afdbbfc26821c459129aa3ad1 to your computer and use it in GitHub Desktop.
Revisions
-
lucasnad27 created this gist
Sep 8, 2021 .There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,61 @@ """ Portfolio implementation """ class Portfolio: """Used for backtesting strategies and taking actions through time.""" 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 get_adjusted_close(self, ticker: str, trading_day: Arrow) -> float: """Returns the closing price for a symbol on a given day.""" # no need to inject the s3_client since we can just depend on self! return qm.equities.historical.get_price(self.s3_client, self.s3_bucket, ticker, trading_day) def update_positions(self): # updates the transactions dataframe, which is port of a portfolio instance # that's ok...right? return df 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 """ @mock.patch('client.s3_client') def test_portfolio_update_postions(): # need to mock our s3_client so that our portfolio can initialize portfolio = Portfolio(capital_allocation=100000..., start_date=... end_date=...) # set the _transactions property so that our update_positions has the data it needs within # this Portfolio instance, hope we remember all the other pieces of state we need to set up # to get this test working! portfolio._transactions = pd.DataFrame(...) portfolio.update_positions() expected_positions = ... assert expected_positions == portfolio.positions