%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.1%22%2C%0A%23%20%20%20%20%20%22numpy%3D%3D2.4.4%22%2C%0A%23%20%20%20%20%20%22pandas%3D%3D3.0.2%22%2C%0A%23%20%20%20%20%20%22plotly%3D%3D6.7.0%22%2C%0A%23%20%20%20%20%20%22polars%3D%3D1.39.3%22%2C%0A%23%20%20%20%20%20%22pyarrow%3D%3D23.0.1%22%2C%0A%23%20%20%20%20%20%22cvxsimulator%3D%3D1.5.1%22%2C%0A%23%20%20%20%20%20%22tinycta%3D%3D0.9.5%22%0A%23%20%5D%0A%23%20%2F%2F%2F%0A%0A%22%22%22Experiment%204%3A%20CTA%20strategy%20with%20optimization%20and%20risk%20scaling.%0A%0AThis%20module%20demonstrates%20a%20more%20advanced%20trend-following%20strategy%20that%0Aincorporates%20portfolio%20optimization%20techniques%20and%20risk%20scaling%20to%0Aimprove%20performance%20and%20risk-adjusted%20returns.%0A%22%22%22%0A%0Aimport%20marimo%0A%0A__generated_with%20%3D%20%220.23.1%22%0Aapp%20%3D%20marimo.App()%0A%0Awith%20app.setup%3A%0A%20%20%20%20from%20pathlib%20import%20Path%0A%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%0A%20%20%20%20%23%20Compatibility%20shim%3A%20cvxsimulator%20imports%20from%20private%20jquantstats%20API%0A%20%20%20%20%23%20that%20doesn't%20exist%20in%20public%20jquantstats.%20Patch%20sys.modules%20before%0A%20%20%20%20%23%20importing%20cvx.simulator%20so%20portfolio.py%20can%20resolve%20these%20imports.%0A%20%20%20%20import%20sys%0A%20%20%20%20import%20types%0A%20%20%20%20import%20jquantstats.data%20as%20_jqs_data_mod%0A%0A%20%20%20%20_fake_jqs_data%20%3D%20types.ModuleType(%22jquantstats._data%22)%0A%20%20%20%20_fake_jqs_data.Data%20%3D%20_jqs_data_mod.Data%0A%20%20%20%20sys.modules%5B%22jquantstats._data%22%5D%20%3D%20_fake_jqs_data%0A%0A%20%20%20%20_fake_jqs_api%20%3D%20types.ModuleType(%22jquantstats.api%22)%0A%20%20%20%20_fake_jqs_api.build_data%20%3D%20lambda%20returns%3A%20_jqs_data_mod.Data.from_returns(returns%3Dreturns.reset_index())%0A%20%20%20%20sys.modules%5B%22jquantstats.api%22%5D%20%3D%20_fake_jqs_api%0A%0A%20%20%20%20from%20cvx.simulator%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%20Path(__file__).parent%20%2F%20%22public%22%20%2F%20%22Prices_hashed.csv%22%0A%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(%5Bpl.col(col).cast(pl.Float64)%20for%20col%20in%20dframe.columns%20if%20col%20!%3D%20date_col%5D)%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%0A%20%20%20%20%23%20CTA%204.0%20-%20Optimization%201.0%0A%20%20%20%20%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%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%20cvx.simulator%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
00e1280007a74a89c1f6310c397ce29a