Skip to content

Instantly share code, notes, and snippets.

@samchaaa
Created December 20, 2020 08:29
Show Gist options
  • Save samchaaa/40d711cdf3b2604f54cc9bd8d9055fb3 to your computer and use it in GitHub Desktop.
Save samchaaa/40d711cdf3b2604f54cc9bd8d9055fb3 to your computer and use it in GitHub Desktop.
# 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')))
# Setting up for pybacktest
ohlc = data.rename(columns={'open': 'O', 'high': 'H', 'low': 'L', 'close': 'C', 'volume': 'V'})
ohlc = hb_lb(ohlc, pct_change, smoothing)
buy, cover, sell, short = get_signals(close_time)
bt = pybacktest.Backtest(locals(), 'MR pct_period: {}, close_time: {}, ticker: {}'.format(pct_change, close_time, ticker))
e = pd.concat([
bt.trades,
bt.equity
], axis=1)
# Allocating capital equally between symbols (using fractional share amounts)
e['shares'] = e.apply(lambda x: capital/len(sp)/x['price'], axis=1)
e['change'] = e[0] * e['shares']
results.append({
'ticker': ticker,
'trades': e
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment