%23%20%2F%2F%2F%20script%0A%23%20requires-python%20%3D%20%22%3E%3D3.12%22%0A%23%20dependencies%20%3D%20%5B%0A%23%20%20%20%20%20%22marimo%3D%3D0.23.9%22%2C%0A%23%20%20%20%20%20%22numpy%3D%3D2.4.6%22%2C%0A%23%20%20%20%20%20%22plotly%3D%3D6.8.0%22%2C%0A%23%20%20%20%20%20%22polars%3D%3D1.41.2%22%2C%0A%23%20%20%20%20%20%22jquantstats%3D%3D0.9.6%22%0A%23%20%5D%0A%23%20%2F%2F%2F%0A%0A%22%22%22Experiment%202%3A%20Improved%20CTA%20strategy%20with%20volatility%20scaling.%0A%0AThis%20module%20enhances%20the%20basic%20trend-following%20strategy%20by%20incorporating%0Avolatility%20scaling%20to%20adjust%20position%20sizes%20based%20on%20market%20conditions.%0A%22%22%22%0A%0Aimport%20marimo%0A%0A__generated_with%20%3D%20%220.23.9%22%0Aapp%20%3D%20marimo.App()%0A%0Awith%20app.setup%3A%0A%20%20%20%20import%20sys%0A%20%20%20%20from%20pathlib%20import%20Path%0A%0A%20%20%20%20import%20marimo%20as%20mo%0A%20%20%20%20import%20polars%20as%20pl%0A%20%20%20%20from%20jquantstats%20import%20Portfolio%0A%0A%20%20%20%20sys.path.insert(0%2C%20str(Path(__file__).parent))%0A%0A%20%20%20%20from%20preamble%20import%20date_col%2C%20load_prices%0A%0A%20%20%20%20prices%20%3D%20load_prices(__file__)%0A%20%20%20%20prices_only%20%3D%20prices.drop(date_col)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%20CTA%202.0%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.function%0Adef%20f(price%3A%20%22pl.Expr%22%2C%20fast%3D32%2C%20slow%3D96%2C%20volatility%3D32)%20-%3E%20%22pl.Expr%22%3A%0A%20%20%20%20%22%22%22Return%20the%20volatility-scaled%20EWM%20crossover%20signal.%22%22%22%0A%20%20%20%20return%20(%0A%20%20%20%20%20%20%20%20price.ewm_mean(com%3Dfast%2C%20min_samples%3D300)%20-%20price.ewm_mean(com%3Dslow%2C%20min_samples%3D300)%0A%20%20%20%20).sign()%20%2F%20price.pct_change().ewm_std(com%3Dvolatility%2C%20min_samples%3D300)%0A%0A%0A%40app.cell%0Adef%20_()%3A%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%20mo.vstack(%5Bfast%2C%20slow%2C%20vola%5D)%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%20signals%20%3D%20prices_only.select(%0A%20%20%20%20%20%20%20%20f(pl.all()%2C%20fast%3Dfast.value%2C%20slow%3Dslow.value%2C%20volatility%3Dvola.value).fill_null(0.0)%20*%201e5%0A%20%20%20%20)%0A%20%20%20%20portfolio%20%3D%20Portfolio.from_cash_position(prices%3Dprices%2C%20cash_position%3Dsignals%2C%20aum%3D1e8)%0A%20%20%20%20return%20(portfolio%2C)%0A%0A%0A%40app.cell%0Adef%20_(portfolio)%3A%0A%20%20%20%20print(portfolio.stats.sharpe())%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.md(r%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*%20Scaling%20the%20bet-size%20by%20volatility%20has%20improved%20the%20situation.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.md(r%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%20return%0A%0A%0A%40app.cell%0Adef%20_(portfolio)%3A%0A%20%20%20%20portfolio.plots.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
5e2eb2b9379f4b87d403bc7648c0da0a