Skip to content

Instantly share code, notes, and snippets.

@zachlim98
Created December 4, 2020 04:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zachlim98/345e0e4440320c6c57c877594ae694e2 to your computer and use it in GitHub Desktop.
Save zachlim98/345e0e4440320c6c57c877594ae694e2 to your computer and use it in GitHub Desktop.
import bt
import matplotlib.pyplot as plt
import numpy as np
#create strategy
crosses = bt.Strategy('Test1', [bt.algos.WeighTarget(df),
bt.algos.Rebalance()])
#create benchmark of buy-and-hold
long_only = bt.Strategy('Benchmark', [bt.algos.RunOnce(),
bt.algos.SelectAll(),
bt.algos.WeighEqually(),
bt.algos.Rebalance()])
#set backtests and commissions
t = bt.Backtest(crosses,data,commissions=lambda q, p: max(1, abs(q) * 0.05))
s = bt.Backtest(long_only, data,commissions=lambda q, p: max(1, abs(q) * 0.05))
report = bt.run(t,s)
plt.title("Portfolio Returns")
report.plot()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment