Useful Python Code
Useful Python code for algo-trading.
General Scripts
Convert timestamp to local datetime
from datetime import datetime
from dateutil import tz
def unix_convert(ts):
ts = int(ts/1000)
tdate = datetime.utcfromtimestamp(ts).replace(tzinfo=tz.UTC).astimezone(tz.gettz('America/New_York'))
return tdateor:
import calendar
import time
calendar.timegm(time.strptime('2000-01-01 12:34:00', '%Y-%m-%d %H:%M:%S'))Create List of Trading Holidays
from pandas.tseries.holiday import (AbstractHolidayCalendar, Holiday,
USMartinLutherKingJr, USPresidentsDay,
GoodFriday, USMemorialDay,
USLaborDay, USThanksgivingDay,
nearest_workday)
class USTradingCalendar(AbstractHolidayCalendar):
rules = [
Holiday('NewYearsDay', month=1, day=1, observance=nearest_workday),
USMartinLutherKingJr,
USPresidentsDay,
GoodFriday,
USMemorialDay,
Holiday('USIndependenceDay', month=7, day=4, observance=nearest_workday),
USLaborDay,
USThanksgivingDay,
Holiday('Christmas', month=12, day=25, observance=nearest_workday)
]Getting a list of tickers from CATNMS
Get Last Friday's Date
Saving Options Data to Database
Last updated