cryplish / multi_crypto.py
bintangyosua's picture
Update multi_crypto.py
9ae9773 verified
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()