|
| 1 | +import marimo |
| 2 | + |
| 3 | +__generated_with = "0.19.7" |
| 4 | +app = marimo.App(width="medium") |
| 5 | + |
| 6 | + |
| 7 | +@app.cell(hide_code=True) |
| 8 | +def _(mo): |
| 9 | + mo.md(r""" |
| 10 | + # Supermsoother & EWMA |
| 11 | + """) |
| 12 | + return |
| 13 | + |
| 14 | + |
| 15 | +@app.cell |
| 16 | +def _(): |
| 17 | + import marimo as mo |
| 18 | + |
| 19 | + app = mo.App( |
| 20 | + requirements=["quantflow"] |
| 21 | + ) |
| 22 | + return (mo,) |
| 23 | + |
| 24 | + |
| 25 | +@app.cell |
| 26 | +def _(): |
| 27 | + from quantflow.data.fmp import FMP |
| 28 | + fmp = FMP() |
| 29 | + return (fmp,) |
| 30 | + |
| 31 | + |
| 32 | +@app.cell |
| 33 | +async def _(fmp): |
| 34 | + from datetime import date |
| 35 | + data = await fmp.prices("BTCUSD", from_date=date(2024,1,1)) |
| 36 | + data |
| 37 | + return (data,) |
| 38 | + |
| 39 | + |
| 40 | +@app.cell |
| 41 | +def _(): |
| 42 | + import altair as alt |
| 43 | + import pandas as pd |
| 44 | + return (alt,) |
| 45 | + |
| 46 | + |
| 47 | +@app.cell |
| 48 | +def _(mo): |
| 49 | + period = mo.ui.slider(start=2, stop=100, step=1, value=10, label="Period:") |
| 50 | + period |
| 51 | + return (period,) |
| 52 | + |
| 53 | + |
| 54 | +@app.cell |
| 55 | +def _(data, period): |
| 56 | + from quantflow.ta.supersmoother import SuperSmoother |
| 57 | + from quantflow.ta.ewma import EWMA |
| 58 | + smoother = SuperSmoother(period=period.value) |
| 59 | + ewma = EWMA(period=period.value) |
| 60 | + sm = data[["date", "close"]].copy() |
| 61 | + sm["supersmoother"] = data["close"].apply(smoother.update) |
| 62 | + sm["ewma"] = data["close"].apply(ewma.update) |
| 63 | + return (sm,) |
| 64 | + |
| 65 | + |
| 66 | +@app.cell(hide_code=True) |
| 67 | +def _(alt, sm): |
| 68 | + # Melt the dataframe to a long format suitable for Altair |
| 69 | + sm_long = sm.melt( |
| 70 | + id_vars=['date'], |
| 71 | + value_vars=['close', 'supersmoother', "ewma"], |
| 72 | + var_name='Signal', |
| 73 | + value_name='Price' |
| 74 | + ) |
| 75 | + |
| 76 | + # Create the chart with both SuperSmoothers |
| 77 | + line_chart_combined = alt.Chart(sm_long).mark_line().encode( |
| 78 | + x=alt.X('date:T', title='Date'), |
| 79 | + y=alt.Y('Price:Q', title='Price (USD)', scale=alt.Scale(zero=False)), |
| 80 | + color=alt.Color('Signal:N', title='Signal', |
| 81 | + scale=alt.Scale( |
| 82 | + domain=['close', 'supersmoother', 'ewma'], |
| 83 | + range=['#4c78a8', '#f58518', '#e45756']) # Vega-Lite default palette |
| 84 | + ), |
| 85 | + tooltip=[ |
| 86 | + alt.Tooltip('date:T', title='Date'), |
| 87 | + alt.Tooltip('Signal:N', title='Signal'), |
| 88 | + alt.Tooltip('Price:Q', title='Price', format='$,.2f') |
| 89 | + ] |
| 90 | + ).properties( |
| 91 | + title='BTCUSD Close Price vs. SuperSmoothers' |
| 92 | + ).interactive() |
| 93 | + |
| 94 | + line_chart_combined |
| 95 | + return |
| 96 | + |
| 97 | + |
| 98 | +@app.cell |
| 99 | +def _(sm): |
| 100 | + sm |
| 101 | + return |
| 102 | + |
| 103 | + |
| 104 | +@app.cell |
| 105 | +def _(): |
| 106 | + return |
| 107 | + |
| 108 | + |
| 109 | +if __name__ == "__main__": |
| 110 | + app.run() |
0 commit comments