Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generate Lines in chart.html Using Default Data #29

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 14 additions & 14 deletions gemini/engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def __init__(self, data):

:return: A bactesting simulation
:rtype: backtest
"""
"""
if not isinstance(data, pd.DataFrame):
raise ValueError("Data must be a pandas dataframe")

Expand All @@ -42,9 +42,9 @@ def start(self, initial_capital, logic):
self.tracker = []
self.account = exchange.account(initial_capital)

# Enter backtest ---------------------------------------------
# Enter backtest ---------------------------------------------
for index, today in self.data.iterrows():

date = today['date']
equity = self.account.total_value(today['close'])

Expand All @@ -64,7 +64,7 @@ def start(self, initial_capital, logic):
self.account.equity.append(equity)

# Equity tracking
self.tracker.append({'date': date,
self.tracker.append({'date': date,
'benchmark_equity' : today['close'],
'strategy_equity' : equity})

Expand All @@ -73,7 +73,7 @@ def start(self, initial_capital, logic):
logic(self.account, lookback)

# Cleanup empty positions
self.account.purge_positions()
self.account.purge_positions()
# ------------------------------------------------------------

# For pyfolio
Expand All @@ -84,16 +84,16 @@ def start(self, initial_capital, logic):
del df['date']
return df

def results(self):
"""Print results"""
def results(self):
"""Print results"""
print("-------------- Results ----------------\n")
being_price = self.data.iloc[0]['open']
final_price = self.data.iloc[-1]['close']

pc = helpers.percent_change(being_price, final_price)
print("Buy and Hold : {0}%".format(round(pc*100, 2)))
print("Net Profit : {0}".format(round(helpers.profit(self.account.initial_capital, pc), 2)))

pc = helpers.percent_change(self.account.initial_capital, self.account.total_value(final_price))
print("Strategy : {0}%".format(round(pc*100, 2)))
print("Net Profit : {0}".format(round(helpers.profit(self.account.initial_capital, pc), 2)))
Expand All @@ -110,24 +110,24 @@ def results(self):
print("--------------------")
print("Total Trades : {0}".format(longs + sells + shorts + covers))
print("\n---------------------------------------")

def chart(self, show_trades=False, title="Equity Curve"):
"""Chart results.

:param show_trades: Show trades on plot
:type show_trades: bool
:param title: Plot title
:type title: str
"""
"""
bokeh.plotting.output_file("chart.html", title=title)
p = bokeh.plotting.figure(x_axis_type="datetime", plot_width=1000, plot_height=400, title=title)
p.grid.grid_line_alpha = 0.3
p.xaxis.axis_label = 'Date'
p.yaxis.axis_label = 'Equity'
shares = self.account.initial_capital/self.data.iloc[0]['open']
base_equity = [price*shares for price in self.data['open']]
p.line(self.data['date'], base_equity, color='#CAD8DE', legend='Buy and Hold')
p.line(self.data['date'], self.account.equity, color='#49516F', legend='Strategy')
base_equity = [price*shares for price in self.data['open']]
p.line(pd.to_datetime(self.data['date']), base_equity, color='#CAD8DE', legend='Buy and Hold')
p.line(pd.to_datetime(self.data['date']), self.account.equity, color='#49516F', legend='Strategy')
Copy link
Author

@11 11 Dec 17, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@anfederico I removed some unwanted whitespace in this file -- but line 129 & 130 is the main hack I used to get your example data to work

p.legend.location = "top_left"

if show_trades:
Expand All @@ -148,5 +148,5 @@ def chart(self, show_trades=False, title="Equity Curve"):
elif trade.type == 'short': p.circle(x, y, size=6, color='orange', alpha=0.5)
except:
pass

bokeh.plotting.show(p)