import%20marimo%0A%0A__generated_with%20%3D%20%220.13.15%22%0Aapp%20%3D%20marimo.App()%0A%0Awith%20app.setup%3A%0A%20%20%20%20import%20marimo%20as%20mo%0A%20%20%20%20import%20numpy%20as%20np%0A%20%20%20%20import%20pandas%20as%20pd%0A%20%20%20%20import%20plotly.io%20as%20pio%0A%20%20%20%20import%20polars%20as%20pl%0A%20%20%20%20from%20cvxsimulator%20import%20interpolate%0A%0A%20%20%20%20%23%20Ensure%20Plotly%20works%20with%20Marimo%0A%20%20%20%20pio.renderers.default%20%3D%20%22plotly_mimetype%22%0A%20%20%20%20pd.options.plotting.backend%20%3D%20%22plotly%22%0A%0A%20%20%20%20path%20%3D%20mo.notebook_location()%20%2F%20%22public%22%20%2F%20%22Prices_hashed.csv%22%0A%20%20%20%20date_col%20%3D%20%22date%22%0A%0A%20%20%20%20dframe%20%3D%20pl.read_csv(str(path)%2C%20try_parse_dates%3DTrue)%0A%0A%20%20%20%20dframe%20%3D%20dframe.with_columns(pl.col(date_col).cast(pl.Datetime(%22ns%22)))%0A%20%20%20%20dframe%20%3D%20dframe.with_columns(%0A%20%20%20%20%20%20%20%20%5Bpl.col(col).cast(pl.Float64)%20for%20col%20in%20dframe.columns%20if%20col%20!%3D%20date_col%5D%0A%20%20%20%20)%0A%20%20%20%20prices%20%3D%20dframe.to_pandas().set_index(date_col).apply(interpolate)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.md(r%22%22%22%23%20CTA%204.0%20-%20Optimization%201.0%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20import%20warnings%0A%0A%20%20%20%20%23%20Suppress%20noisy%20warnings%0A%20%20%20%20warnings.simplefilter(action%3D%22ignore%22%2C%20category%3DFutureWarning)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20from%20tinycta.signal%20import%20osc%2C%20returns_adjust%0A%0A%20%20%20%20return%20osc%2C%20returns_adjust%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20%23%20Create%20sliders%20using%20marimo's%20UI%20components%0A%20%20%20%20fast%20%3D%20mo.ui.slider(4%2C%20192%2C%20step%3D4%2C%20value%3D32%2C%20label%3D%22Fast%20Moving%20Average%22)%0A%20%20%20%20slow%20%3D%20mo.ui.slider(4%2C%20192%2C%20step%3D4%2C%20value%3D96%2C%20label%3D%22Slow%20Moving%20Average%22)%0A%20%20%20%20vola%20%3D%20mo.ui.slider(4%2C%20192%2C%20step%3D4%2C%20value%3D32%2C%20label%3D%22Volatility%22)%0A%20%20%20%20winsor%20%3D%20mo.ui.slider(1.0%2C%206.0%2C%20step%3D0.1%2C%20value%3D4.2%2C%20label%3D%22Winsorizing%22)%0A%0A%20%20%20%20%23%20Display%20the%20sliders%20in%20a%20vertical%20stack%0A%20%20%20%20mo.vstack(%5Bfast%2C%20slow%2C%20vola%2C%20winsor%5D)%0A%0A%20%20%20%20return%20fast%2C%20slow%2C%20vola%2C%20winsor%0A%0A%0A%40app.cell%0Adef%20_(fast%2C%20osc%2C%20returns_adjust%2C%20slow%2C%20vola%2C%20winsor)%3A%0A%20%20%20%20from%20cvxsimulator%20import%20Portfolio%0A%0A%20%20%20%20mu%20%3D%20np.tanh(%0A%20%20%20%20%20%20%20%20prices.apply(returns_adjust%2C%20com%3Dvola.value%2C%20clip%3Dwinsor.value)%0A%20%20%20%20%20%20%20%20.cumsum()%0A%20%20%20%20%20%20%20%20.apply(osc%2C%20fast%3Dfast.value%2C%20slow%3Dslow.value)%0A%20%20%20%20)%0A%20%20%20%20volax%20%3D%20prices.pct_change().ewm(com%3Dvola.value%2C%20min_periods%3Dvola.value).std()%0A%0A%20%20%20%20%23%20compute%20the%20series%20of%20Euclidean%20norms%20by%20compute%20the%20sum%20of%20squares%20for%20each%20row%0A%20%20%20%20euclid_norm%20%3D%20np.sqrt((mu%20*%20mu).sum(axis%3D1))%0A%0A%20%20%20%20%23%20Divide%20each%20column%20of%20mu%20by%20the%20Euclidean%20norm%0A%20%20%20%20risk_scaled%20%3D%20mu.apply(lambda%20x%3A%20x%20%2F%20euclid_norm%2C%20axis%3D0)%0A%0A%20%20%20%20pos%20%3D%205e5%20*%20risk_scaled%20%2F%20volax%0A%20%20%20%20portfolio%20%3D%20Portfolio.from_cashpos_prices(prices%3Dprices%2C%20cashposition%3Dpos%2C%20aum%3D1e8)%0A%20%20%20%20print(portfolio.sharpe())%0A%20%20%20%20return%20(portfolio%2C)%0A%0A%0A%40app.cell%0Adef%20_(portfolio)%3A%0A%20%20%20%20fig%20%3D%20portfolio.snapshot()%0A%20%20%20%20fig%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
6693ec31f76d3a4ae31b798ef861799c073229d438a8a70fcfb01fffc8e49bd8