T
import requests
import pandas as pd
import numpy as np
import talib
def fetch_ohlc(coin_id, vs_currency='usd', days='30'):
url = f"https://api.coingecko.com/api/v3/coins/{coin_id}/ohlc"
params = {'vs_currency': vs_currency, 'days': days}
response = requests.get(url, params=params)
if response.status_code == 200:
data = response.json()
df = pd.DataFrame(data, columns=['timestamp', 'open', 'high', 'low', 'close'])
df['timestamp'] = pd.to_datetime(df['timestamp'], unit='ms')
df.set_index('timestamp', inplace=True)
return df
else:
print("Error fetching data")
return None
def calculate_macd(df):
macd, macdsignal, macdhist = talib.MACD(df['close'])
df['MACD'] = macd
df['MACD_Signal'] = macdsignal
df['MACD_Hist'] = macdhist
def calculate_stochastic(df):
slowk, slowd = talib.STOCH(df['high'], df['low'], df['close'])
df['SlowK'] = slowk
df['SlowD'] = slowd
def main():
coin_id = 'uniswap' # Change to desired coin id from CoinGecko (e.g. 'ethereum', 'dodo')
df = fetch_ohlc(coin_id, days='30')
if df is not None:
calculate_macd(df)
calculate_stochastic(df)
print(df.tail(10)) # print last 10 rows including MACD and Stochastic
if __name__ == '__main__':
main()
Comments
Post a Comment