%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%201%3A%20Basic%20CTA%20strategy%20implementation%20using%20moving%20averages.%0A%0AThis%20module%20demonstrates%20a%20simple%20trend-following%20strategy%20using%20exponential%0Amoving%20averages%20with%20different%20lookback%20periods.%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%201.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)%20-%3E%20%22pl.Expr%22%3A%0A%20%20%20%20%22%22%22Return%20the%20sign%20of%20the%20fast-minus-slow%20EWM%20crossover.%22%22%22%0A%20%20%20%20return%20(price.ewm_mean(com%3Dfast%2C%20min_samples%3D100)%20-%20price.ewm_mean(com%3Dslow%2C%20min_samples%3D100)).sign()%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%0A%20%20%20%20mo.vstack(%5Bfast%2C%20slow%5D)%0A%20%20%20%20return%20fast%2C%20slow%0A%0A%0A%40app.cell%0Adef%20_(fast%2C%20slow)%3A%0A%20%20%20%20signals%20%3D%20prices_only.select(f(pl.all()%2C%20fast%3Dfast.value%2C%20slow%3Dslow.value).fill_null(0.0)%20*%205e6)%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%20Results%20do%20not%20look%20terrible%20but...%0A%20%20%20%20*%20No%20concept%20of%20risk%20integrated.%0A%20%20%20%20*%20The%20size%20of%20each%20bet%20is%20constant%20regardless%20of%20the%20underlying%20asset.%0A%20%20%20%20*%20The%20system%20lost%20its%20mojo%20in%202009%20and%20has%20never%20really%20recovered.%0A%20%20%20%20*%20The%20sign%20function%20is%20very%20expensive%20to%20trade%20as%20position%20changes%20are%20too%20extreme.%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%20Such%20fundamental%20flaws%20are%20not%20addressed%20by%20**parameter-hacking**%0A%20%20%20%20or%20**pimp-my-trading-system**%20steps%20(remove%20the%20worst%20performing%20assets%2C%0A%20%20%20%20insane%20quantity%20of%20stop-loss%20limits%2C%20...)%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%20fig%20%3D%20portfolio.plots.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
c7bc255e4d8b291e39237da6f2e65e46