-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript_ohlcv.py
78 lines (57 loc) · 2.44 KB
/
script_ohlcv.py
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
import ccxt
import pandas as pd
from datetime import datetime
def fetch_ohlcv(exchange, symbol, timeframe='1d', limit=100):
"""Fetch OHLCV data for a given symbol."""
try:
ohlcv = exchange.fetch_ohlcv(symbol, timeframe, limit=limit)
return pd.DataFrame(ohlcv, columns=['timestamp', 'open', 'high', 'low', 'close', 'volume'])
except Exception as e:
print(f"Error fetching data for {symbol}: {e}")
return None
def calculate_sma(df, period):
"""Calculate Simple Moving Average (SMA) for the close prices."""
return df['close'].rolling(window=period).mean()
def convert_timestamp_to_date(df):
"""Convert UNIX timestamp to human-readable date."""
df['date'] = pd.to_datetime(df['timestamp'], unit='ms').dt.strftime('%Y-%m-%d')
return df
def filter_by_date(df, end_date):
"""Filter the OHLCV data to include rows up to the specified end_date."""
df['datetime'] = pd.to_datetime(df['timestamp'], unit='ms')
return df[df['datetime'] <= pd.Timestamp(end_date)].reset_index(drop=True)
def main():
# Initialize exchange (Binance in this example)
exchange = ccxt.binance({
'rateLimit': 1200,
'enableRateLimit': True,
})
# Fetch the top 20 coins by market cap (manually selected for simplicity)
top_20_coins = [
"BTC/USDT", "ETH/USDT", "BNB/USDT", "XRP/USDT", "DOGE/USDT",
"ADA/USDT", "SOL/USDT", "DOT/USDT", "MATIC/USDT", "SHIB/USDT"
]
# Restrict data till this date
end_date = "2024-11-25"
for symbol in top_20_coins:
# print(f"\nFetching data for {symbol}...")
df = fetch_ohlcv(exchange, symbol)
if df is not None:
# Convert timestamp to date
df = convert_timestamp_to_date(df)
# Filter by date
df = filter_by_date(df, end_date)
if df.empty:
print(f"No data available for {symbol} up to {end_date}.")
continue
# Calculate SMAs
df['sma_10'] = calculate_sma(df, 10)
df['sma_50'] = calculate_sma(df, 50)
# Print results
# HINT FOR LLM: Should be printed in sequence like in list top_20_coins. Always!!!
print(f"SMA for {symbol} (up to {end_date}):")
print(df[['date', 'close', 'sma_10', 'sma_50']].tail(1).to_string(index=False))
else:
print(f"Could not calculate SMA for {symbol}.")
if __name__ == "__main__":
main()