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%202.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.function%0Adef%20f(price%2C%20fast%3D32%2C%20slow%3D96%2C%20volatility%3D32)%3A%0A%20%20%20%20s%20%3D%20price.ewm(com%3Dslow%2C%20min_periods%3D300).mean()%0A%20%20%20%20f%20%3D%20price.ewm(com%3Dfast%2C%20min_periods%3D300).mean()%0A%20%20%20%20std%20%3D%20price.pct_change().ewm(com%3Dvolatility%2C%20min_periods%3D300).std()%0A%20%20%20%20return%20np.sign(f%20-%20s)%20%2F%20std%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%0A%20%20%20%20%23%20Display%20the%20sliders%20in%20a%20vertical%20stack%0A%20%20%20%20mo.vstack(%5Bfast%2C%20slow%2C%20vola%5D)%0A%0A%20%20%20%20return%20fast%2C%20slow%2C%20vola%0A%0A%0A%40app.cell%0Adef%20_(fast%2C%20slow%2C%20vola)%3A%0A%20%20%20%20from%20cvxsimulator%20import%20Portfolio%0A%0A%20%20%20%20pos%20%3D%201e5%20*%20f(prices%2C%20fast%3Dfast.value%2C%20slow%3Dslow.value%2C%20volatility%3Dvola.value)%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(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.md(%0A%20%20%20%20%20%20%20%20r%22%22%22%0A%20%20%20%20*%20This%20is%20a%20**univariate**%20trading%20system%2C%20we%20map%20the%20(real)%20price%20of%20an%20asset%20to%20its%20(cash)position%0A%20%20%20%20*%20Only%203%20**free%20parameters**%20used%20here.%0A%20%20%20%20*%20Only%204%20lines%20of%20code%0A%20%20%20%20*%20Scaling%20the%20bet-size%20by%20volatility%20has%20improved%20the%20situation.%0A%20%20%20%20%22%22%22%0A%20%20%20%20)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.md(%0A%20%20%20%20%20%20%20%20r%22%22%22%0A%20%20%20%20Results%20do%20not%20look%20terrible%20but...%0A%20%20%20%20*%20No%20concept%20of%20risk%20integrated%0A%0A%20%20%20%20Often%20hedge%20funds%20outsource%20the%20risk%20management%20to%20some%20board%20or%20committee%0A%20%20%20%20and%20develop%20machinery%20for%20more%20systematic%20**parameter-hacking**.%0A%20%20%20%20%22%22%22%0A%20%20%20%20)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(portfolio)%3A%0A%20%20%20%20portfolio.snapshot()%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
60b325131092495b4283a920990be343496a1587d3655f03bb331d30419287d9