Skip to content

Instantly share code, notes, and snippets.

@ChadThackray
ChadThackray / code.py
Last active August 13, 2024 19:25
Get live cryptocurrency prices in python
# Licensed under the MIT License. See comment below for full licence information.
import requests
import json
bob = requests.get("https://api.coingecko.com/api/v3/simple/price?ids=bitcoin&vs_currencies=gbp", headers = {"accept":"application/json"})
print("The price of bitcoin is currently £" + str(bob.json()["bitcoin"]["gbp"]))
@ChadThackray
ChadThackray / video-code.py
Last active June 22, 2024 09:13
Historical bitcoin charts in python (Bitcoin data science #1)
# Licensed under the MIT License. See comment below for full license information.
import matplotlib.pyplot as plt
import pandas as pd
plt.style.use("dark_background")
df = pd.read_csv("Kraken_BTCUSD_d.csv")
@ChadThackray
ChadThackray / yfinance.py
Created June 20, 2024 17:35
Yahoo finance stock data in python with yfinance
# Licensed under the MIT License. See comment below for full license information.
import yfinance as yf
import pandas as pd
import matplotlib.pyplot as plt
stock = yf.Ticker("KO")
print(stock.info['open'])
@ChadThackray
ChadThackray / menus.py
Created June 20, 2024 18:01
Simple menus in python
# Licensed under the MIT License. See comment below for full license information.
from simple_term_menu import TerminalMenu
options = ["[a] Apples","[b] Bannas","[o] Oranges","[q] Quit"]
mainMenu = TerminalMenu(options, title = "main menu")
subMenu = TerminalMenu(["option1","option2"], title = "sub-menu")
quitting = False
@ChadThackray
ChadThackray / gridtraveller.py
Last active June 22, 2024 09:16
Recursion and memoisation in python with the gridTraveller problem
# Licensed under the MIT License. See comment below for full licence information.
def gridtraveller(m,n, dict = {}):
key = str(m) + "," + str(n)
if(m == 1 and n == 1):
return 1
elif(m ==0 or n == 0):
return 0
elif( key in dict.keys()):
# Licensed under the MIT License. See comment below for full licence information.
import pandas as pd
import numpy as np
from scipy.optimize import curve_fit
import matplotlib.pyplot as plt
## data processing
df = pd.read_csv("data.csv")
df = df[ df['Value'] > 0]
@ChadThackray
ChadThackray / 200wma.py
Last active May 31, 2025 19:22
Bitcoin 200wk moving average heatmap in python from scratch
# Licensed under the MIT License. See comment below for full licence information.
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.ticker as mtick
## data processing
df = pd.read_csv("data.csv")
df = df.iloc[::-1]
@ChadThackray
ChadThackray / candlesticks.py
Created June 21, 2024 07:26
Candlestick charts in python from scratch with Plotly
# Licensed under the MIT License. See comment below for full licence information.
import plotly.graph_objects as go
import pandas as pd
df = pd.read_csv("Kraken_BTCUSD_d.csv")
df = df.iloc[::-1]
df['date'] = pd.to_datetime(df['date'])
@ChadThackray
ChadThackray / portfolio-backtesting.py
Created June 21, 2024 07:34
Portfolio allocation backtesting in Python from scratch
# Licensed under the MIT License. See comment below for full licence information.
import yfinance as yf
import matplotlib.pyplot as plt
import pandas as pd
weightings1 = {"SPY":"100"}
weightings2 = {"SPY":"95","BTC-USD":"5"}
# Licensed under the MIT License. See comment below for full licence information.
from datetime import datetime
import backtrader as bt
class SmaCross(bt.SignalStrategy):
def __init__(self):
sma = bt.ind.SMA(period=50)
price = self.data
crossover = bt.ind.CrossOver(price, sma)