Lecture 1: Introduction to Research — [📝Lecture Notebooks] [
Lecture 2: Introduction to Python — [📝Lecture Notebooks] [
Lecture 3: Introduction to NumPy — [📝Lecture Notebooks] [
Lecture 4: Introduction to pandas — [📝Lecture Notebooks] [
Lecture 5: Plotting Data — [📝Lecture Notebooks] [[
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from sklearn.qda import QDA | |
#from sklearn.ensemble import RandomForestRegressor | |
from sklearn import preprocessing | |
import numpy as np | |
import pandas as pd | |
def initialize(context): | |
context.assets = sid(8554) # Trade SPY | |
context.model = QDA() | |
context.lookback = 5 # Look back |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import alpaca_trade_api as tradeapi | |
import time | |
import datetime | |
from datetime import timedelta | |
from pytz import timezone | |
tz = timezone('EST') | |
api = tradeapi.REST('your key', | |
'your secret', | |
'https://paper-api.alpaca.markets') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import alpaca_trade_api as tradeapi | |
import time | |
import datetime | |
from datetime import timedelta | |
from pytz import timezone | |
tz = timezone('EST') | |
import numpy as np | |
import pandas as pd |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import pandas as pd | |
oc = pd.DataFrame([line.strip('\n').split(',') for line in open('../../Downloads/quotedata.dat')]) | |
oc.head() | |
oc.columns = oc.loc[2, :] | |
oc = oc.loc[3:] | |
oc['Calls K'] = oc.apply(lambda x: float(x['Calls'][-6:-2])/10, axis=1) | |
oc['Puts K'] = oc.apply(lambda x: float(x['Puts'][-6:-2])/10, axis=1) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def lookup(vin): | |
m = getManu(vin) | |
try: | |
y = int(getYear(vin)) | |
except Exception as e: | |
y = e | |
# Exceptions here, or Sam may clean up in dict | |
# if m!= None: | |
# if 'General Motors' in m: | |
# print(m) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def get_data(symbol, start, end): | |
results = pd.DataFrame() | |
while True: | |
if start < end: | |
# 15m bars | |
d = api.polygon.historic_agg_v2(symbol, 15, 'minute', _from=start, to=end, unadjusted=False, limit=None).df | |
time.sleep(.2) | |
start = d.index[-1].strftime('%Y-%m-%d') | |
results = pd.concat([results, d], axis=0) | |
else: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
if data_path not in os.listdir(): | |
os.mkdir(data_path) | |
# save as json | |
for ticker in tqdm(sp): | |
data = get_data(ticker, '2020-08-01', '2020-12-15') | |
with open('./{}/{}.json'.format(data_path, ticker), 'w') as j: | |
json.dump(data.to_json(), open('./{}/{}.json'.format(data_path, ticker), 'w')) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def hb_lb(ohlc, n, smoothing): | |
""" | |
Creates a "high band" and "low band", based on the absolute pct movement for a given period. | |
This will trigger the mean reversion trades. | |
'pct': percent change in the underlying, based on period n | |
'abs_vol': the absolute exponential moving average of the pct movement | |
""" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def get_signals(close_time): | |
""" | |
Creates signals for pybacktest. | |
Note pybacktest specifically needs series named "buy", "sell", "short", "cover" to work. | |
Check out tutorial notebook: https://nbviewer.jupyter.org/github/ematvey/pybacktest/blob/master/examples/tutorial.ipynb | |
""" | |
bs = ohlc['C'] > ohlc['hb'] # sell signal |
OlderNewer