List of helpful shortcuts for faster coding
If you have any other helpful shortcuts, feel free to add in the comments of this gist :)
| import matplotlib.pyplot as plt | |
| plt.style.available | |
| # Style sheets reference | |
| # https://matplotlib.org/stable/gallery/style_sheets/style_sheets_reference.html# | |
| # ['Solarize_Light2', | |
| '_classic_test_patch', | |
| '_mpl-gallery', |
| import pandas as pd | |
| # Option 1: Make naive datetimes timezone-aware | |
| # Add timezone to naive datetime | |
| naive_dt = pd.to_datetime('2024-01-01') | |
| aware_dt = naive_dt.tz_localize('UTC') # or any timezone like 'US/Eastern' | |
| # Option 2: Remove timezone from aware datetime | |
| aware_dt = pd.to_datetime('2024-01-01', utc=True) | |
| naive_dt = aware_dt.tz_localize(None) |
| 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 | |
| from datetime import datetime, date, timedelta | |
| data = yf.download('QQQ', '2020-01-01', date.today().strftime('%Y-%m-%d')) | |
| daily_df = data['Close'] | |
| weekly_df = daily_df.resample('W-Fri').last() | |
| 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: |