List of helpful shortcuts for faster coding
If you have any other helpful shortcuts, feel free to add in the comments of this gist :)
pandas as pd | |
import numpy as np | |
# Assuming returns is your dataset with returns data | |
def calculate_correlations(returns, symbols, window=50): | |
""" | |
Calculate rolling correlations between ETFs | |
Parameters: | |
returns: numpy array or pandas DataFrame of returns |
import pandas as pd | |
import matplotlib.pyplot as plt | |
import yfinance as yf | |
data = yf.download('QQQ', '2020-01-01', '2023-12-31') | |
daily_df = data['Adj Close'] | |
weekly_df = daily_df.resample('W').mean() | |
monthly_df = daily_df.resample('BME').last() |
import pandas as pd | |
import yfinance as yf | |
etf = 'SPY' | |
# etf = 'QQQ | |
trend = "downtrend" | |
# trend = "uptrend" | |
window = 30 | |
year = 2024 |
######################################################################### | |
# US_stock_bundle | |
############### | |
# ~.zipline/extensions.py | |
#from zipline.data.bundles import register, US_stock_data | |
# | |
#register('US_stock_bundle', US_stock_data.US_stock_data, calendar_name='NYSE') |
# to ingest the etfs | |
import zipline | |
from zipline.data.bundles.core import register, ingest | |
import yfinance as yf | |
import pandas as pd | |
from datetime import datetime | |
import pytz | |
# FOR ALL ASSETS |
import sys | |
if 'ziptrader36' in sys.path[1]: | |
active_kernel = 'ziptrader36' | |
elif 'zipline-reloaded' in sys.path[1]: | |
active_kernel = 'zipline-reloaded' | |
else: | |
active_kernel = 'not ziptrader36 or zipline-reloaded' | |
import talib | |
# routine to get args required for talib function | |
def find_talib_args(func): | |
s = eval('talib.' + func + '.__doc__') | |
try: | |
s = s.splitlines()[0].split('(')[1].split(')')[0].replace('[','').replace(']','').replace('?','') | |
return s.split(',') | |
except: |
from fintools import endpoints | |
# end_points prices (in this case, monthly) | |
end_points = endpoints(period='M', trading_days=prices_d.index) | |
prices_m = prices_d.loc[end_points] | |
prices_m[:2] |
etfs = [ | |
# ----------------------------------------- # | |
# SPDRS/State Street Global Advisors (SSGA) | |
'XLY', # Select SPDR U.S. Consumer Discretionary | |
'XLP', # Select SPDR U.S. Consumer Staples | |
'XLE', # Select SPDR U.S. Energy | |
'XLF', # Select SPDR U.S. Financials | |
'XLV', # Select SPDR U.S. Healthcare | |
'XLI', # Select SPDR U.S. Industrials | |
'XLB', # Select SPDR U.S. Materials |