File size: 5,449 Bytes
9128e73
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9ae9773
 
9128e73
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d4e3441
9128e73
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
import marimo

__generated_with = "0.9.15"
app = marimo.App(width="full")


@app.cell(hide_code=True)
def __(mo):
    mo.md(r"""# Cryptocurrency Minuet Dashboard""")
    return


@app.cell(hide_code=True)
def __():
    import marimo as mo
    import pandas as pd
    import numpy as np
    import matplotlib.pyplot as plt
    import seaborn as sns
    import yfinance as yf
    import mplfinance as mpf
    import altair as alt
    from datetime import date, timedelta, datetime
    import pytz

    from sklearn.preprocessing import MinMaxScaler
    return (
        MinMaxScaler,
        alt,
        date,
        datetime,
        mo,
        mpf,
        np,
        pd,
        plt,
        pytz,
        sns,
        timedelta,
        yf,
    )


@app.cell(hide_code=True)
def __(datetime, mo):
    start_date_input = mo.ui.date(value=(datetime.now() - timedelta(days=1)).date())

    mo.md('## Input')
    return (start_date_input,)


@app.cell(hide_code=True)
def __(mo, start_date_input):

    mo.md(f"""
    Please enter yout start date range: {start_date_input}
    """)
    return


@app.cell(hide_code=True)
def __(datetime, pd, pytz, start_date_input, yf):
    tickers = [f'{ticker}-USD' for ticker in [
        'BTC', 'ETH', 'AR', 'SOL', 'ADA', 'XMR', 'BNB', 'AVAX', 'MANTA',
         'DOGE', 'SHIB', 'PEPE24478', 'BONK', 'FLOKI'
    ]]

    # Mendapatkan waktu saat ini (jam, menit, detik)
    current_time = datetime.now().time()

    print(f'current time: {current_time}')

    # Menggabungkan tanggal dari input dengan waktu sekarang
    now = datetime.combine(start_date_input.value, current_time)

    # Menambahkan zona waktu UTC
    now = now.replace(tzinfo=pytz.UTC)

    # Mendapatkan tanggal untuk start_date (menggunakan tanggal input)
    start_date = start_date_input.value.strftime('%Y-%m-%d')

    # Mendapatkan data dari Yahoo Finance
    data = yf.download(tickers, start=start_date, interval='5m')

    # Mengonversi index ke datetime dan mengubah zona waktunya ke UTC
    data.index = pd.to_datetime(data.index).tz_convert('UTC')

    # Memfilter data untuk hanya yang sebelum waktu sekarang
    data = data[data.index > now]

    # Mengambil harga penutupan
    closes = data['Close']
    return closes, current_time, data, now, start_date, tickers


@app.cell
def __(closes):
    closes
    return


@app.cell(hide_code=True)
def __(closes):
    highest_percentage_change = {}
    for ticker in closes.columns:
        # Group by day and calculate min and max within the day
        daily_high = closes[ticker].resample('D').max()
        daily_low = closes[ticker].resample('D').min()
        # Calculate percentage change
        daily_percentage_change = ((daily_high - daily_low) / daily_low) * 100
        # Get the highest percentage change within the day
        highest_percentage_change[ticker] = daily_percentage_change.max()
    return (
        daily_high,
        daily_low,
        daily_percentage_change,
        highest_percentage_change,
        ticker,
    )


@app.cell(hide_code=True)
def __(mo):
    mo.md("""## Highest Returns since Yesterday""")
    return


@app.cell(hide_code=True)
def __(highest_percentage_change, pd):
    def show_highest_percentage(ticker):
        percentage = highest_percentage_change[ticker]
        return percentage

    def percentage_to_text(percentage):
        return '%.2f%%' % percentage if percentage is not None else 'N/A'

    # Calculate and sort percentage changes
    highest_percentage_sorted = sorted(highest_percentage_change.items(), key=lambda x: x[1], reverse=True)
    highest, second_highest = highest_percentage_sorted[0], highest_percentage_sorted[1]
    lowest, second_lowest = highest_percentage_sorted[-1], highest_percentage_sorted[-2]

    # Calculate and sort percentage changes
    # Urutkan berdasarkan persentase, hasilkan tuple (ticker, persentase)
    highest_percentage_sorted = sorted(highest_percentage_change.items(), key=lambda x: x[1], reverse=True)

    # Pisahkan ticker dan persentase yang sudah diurutkan
    sorted_tickers = [ticker for ticker, _ in highest_percentage_sorted]
    sorted_percentages = [percentage_to_text(percentage) for _, percentage in highest_percentage_sorted]

    # Membuat DataFrame dengan kolom yang sudah sesuai
    highest_percentage = pd.DataFrame(
        {
            'Ticker': sorted_tickers,
            'Percentage': sorted_percentages
        }
    )
    return (
        highest,
        highest_percentage,
        highest_percentage_sorted,
        lowest,
        percentage_to_text,
        second_highest,
        second_lowest,
        show_highest_percentage,
        sorted_percentages,
        sorted_tickers,
    )


@app.cell(hide_code=True)
def __(
    highest,
    highest_percentage,
    lowest,
    mo,
    second_highest,
    second_lowest,
):
    mo.vstack([
        mo.md(f"""
        ### Overview of Highest and Lowest Daily Percentage Changes
        The **highest daily percentage change** was recorded by `{highest[0]}` at {highest[1]:.2f}%. The **second highest** was `{second_highest[0]}` with a change of {second_highest[1]:.2f}%.
        The **lowest daily percentage change** occurred for `{lowest[0]}` at {lowest[1]:.2f}%. The **second lowest** was `{second_lowest[0]}` with a change of {second_lowest[1]:.2f}%.
        """), 
        highest_percentage
    ])
    return


@app.cell
def __():
    return


if __name__ == "__main__":
    app.run()