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
( | |
agg_flow | |
.reset_index() | |
.style | |
# after this we are not working a a dataframe but a "styler" object | |
.format({'cost': '${:,.2f}', 'datetime': '{:%Y/%m}/01', | |
'percent_quarterly flow': '{:.1%}', | |
'off_goal': '{:+.1%}', | |
**{col: '{:.1f}' for col in ['cfs', 'total_flow', 'quarterly_flow']}}, | |
na_rep='Missing') |
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_max_min(prices, smoothing, window_range): | |
# Get prices | |
smooth_prices = prices['close'].rolling(window=smoothing).mean().dropna() | |
# Get max and min for window | |
local_max = argrelextrema(smooth_prices.values, np.greater)[0] | |
local_min = argrelextrema(smooth_prices.values, np.less)[0] | |
print('local_max, local_min', local_max, local_min) | |
price_local_max_dt = [] | |
# iterate thru points | |
for i in local_max: |
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
# Concat all trades | |
trades_all = pd.DataFrame() | |
for x in results: | |
trades = pd.DataFrame(x['trades']) | |
trades['sym'] = x['ticker'] | |
trades_all = pd.concat([ | |
trades_all, | |
trades | |
], axis=0) | |
trades_all = trades_all[trades_all['change'] != 0].dropna() |
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
# Fade if price closes outside of running 1h % change (w/2h ema)... closes after 15m. | |
close_time = 1 | |
pct_change = 4 | |
smoothing = 8 | |
results = [] | |
for ticker in tqdm(sp): | |
data = pd.read_json(json.load(open('./{}/{}.json'.format(data_path, ticker), 'r'))) |
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 |
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
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 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
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) |
NewerOlder