import%20marimo%0A%0A__generated_with%20%3D%20%220.14.9%22%0Aapp%20%3D%20marimo.App()%0A%0Awith%20app.setup%3A%0A%20%20%20%20import%20marimo%20as%20mo%0A%20%20%20%20import%20pandas%20as%20pd%0A%20%20%20%20from%20mongoengine%20import%20connect%0A%20%20%20%20from%20mongomock%20import%20MongoClient%0A%0A%20%20%20%20from%20antarctic.pandas_field%20import%20PandasField%0A%0A%20%20%20%20pd.options.plotting.backend%20%3D%20%22plotly%22%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20%23%20connect%20with%20your%20existing%20MongoDB%20(here%20I%20am%20using%20a%20popular%20interface%20mocking%20a%20MongoDB)%0A%20%20%20%20connect(db%3D%22test%22%2C%20mongo_client_class%3DMongoClient)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20from%20mongoengine%20import%20Document%0A%0A%20%20%20%20class%20Portfolio(Document)%3A%0A%20%20%20%20%20%20%20%20weights%20%3D%20PandasField()%0A%20%20%20%20%20%20%20%20prices%20%3D%20PandasField()%0A%0A%20%20%20%20%20%20%20%20%40property%0A%20%20%20%20%20%20%20%20def%20nav(self)%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%22%22%22Compute%20NAV%20from%20weights%20and%20prices.%0A%0A%20%20%20%20%20%20%20%20%20%20%20%20Returns%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20pd.DataFrame%3A%20DataFrame%20with%20computed%20NAV%20values%0A%0A%20%20%20%20%20%20%20%20%20%20%20%20%22%22%22%0A%20%20%20%20%20%20%20%20%20%20%20%20%23%20Ensure%20weights%20and%20prices%20have%20the%20same%20index%20and%20columns%0A%20%20%20%20%20%20%20%20%20%20%20%20common_index%20%3D%20self.weights.index.intersection(self.prices.index)%0A%20%20%20%20%20%20%20%20%20%20%20%20common_columns%20%3D%20self.weights.columns.intersection(self.prices.columns)%0A%0A%20%20%20%20%20%20%20%20%20%20%20%20%23%20Filter%20weights%20and%20prices%20to%20common%20index%20and%20columns%0A%20%20%20%20%20%20%20%20%20%20%20%20weights%20%3D%20self.weights.loc%5Bcommon_index%2C%20common_columns%5D%0A%20%20%20%20%20%20%20%20%20%20%20%20prices%20%3D%20self.prices.loc%5Bcommon_index%2C%20common_columns%5D%0A%0A%20%20%20%20%20%20%20%20%20%20%20%20%23%20Compute%20weighted%20prices%20(element-wise%20multiplication)%0A%20%20%20%20%20%20%20%20%20%20%20%20weighted_prices%20%3D%20weights%20*%20prices%0A%0A%20%20%20%20%20%20%20%20%20%20%20%20%23%20Sum%20across%20assets%20to%20get%20NAV%0A%20%20%20%20%20%20%20%20%20%20%20%20computed_nav%20%3D%20weighted_prices.sum(axis%3D1).to_frame(name%3D%22computed_nav%22)%0A%0A%20%20%20%20%20%20%20%20%20%20%20%20return%20computed_nav%0A%0A%20%20%20%20return%20(Portfolio%2C)%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20prices%20%3D%20pd.read_csv(%0A%20%20%20%20%20%20%20%20mo.notebook_location()%20%2F%20%22public%22%20%2F%20%22price.csv%22%2C%0A%20%20%20%20%20%20%20%20index_col%3D0%2C%0A%20%20%20%20%20%20%20%20parse_dates%3DTrue%2C%0A%20%20%20%20%20%20%20%20header%3D0%2C%0A%20%20%20%20).ffill()%0A%20%20%20%20print(prices)%0A%20%20%20%20return%20(prices%2C)%0A%0A%0A%40app.cell%0Adef%20_(Portfolio%2C%20prices)%3A%0A%20%20%20%20portfolio%20%3D%20Portfolio(%0A%20%20%20%20%20%20%20%20prices%3Dprices%2C%0A%20%20%20%20%20%20%20%20weights%3Dpd.DataFrame(index%3Dprices.index%2C%20columns%3Dprices.columns%2C%20data%3D1.0%20%2F%207)%2C%0A%20%20%20%20)%0A%20%20%20%20portfolio.save()%0A%20%20%20%20portfolio.nav%0A%20%20%20%20return%20(portfolio%2C)%0A%0A%0A%40app.cell%0Adef%20_(portfolio)%3A%0A%20%20%20%20%23%20Plot%20the%20nav%20curve%0A%20%20%20%20portfolio.nav.plot()%0A%0A%20%20%20%20return%0A%0A%0Aif%20__name__%20%3D%3D%20%22__main__%22%3A%0A%20%20%20%20app.run()%0A
00546185f63ee0b3d1da801501b1ab9497f89043b25ff55a12170d7b52f32dc1