Skip to content
Open
Show file tree
Hide file tree
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
3 changes: 3 additions & 0 deletions .idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions .idea/backtesting.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions .idea/inspectionProfiles/Project_Default.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/inspectionProfiles/profiles_settings.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file added backtest_v2/__pycache__/backtest.cpython-38.pyc
Binary file not shown.
55 changes: 32 additions & 23 deletions backtest_v2/backtest.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,47 +161,56 @@ def read_data(price_location, view_location):

def backtest(strat_function, starting_value, prices_location, views_location, log, log_name):
prices, views, names = read_data(prices_location, views_location)
#print(prices)
#print(views)
#print(names)
acc = Account(starting_value, log, names, log_name)
strat = Strategy(strat_function)
for ind, (price, view) in enumerate(zip(prices, views)):
for ind, (price, view) in enumerate(zip(prices, views)):
if ind == 0:
prev_weights = list(np.zeros(len(prices[0])))
else:
prev_weights = acc.weights[-1]
acc.update(strat.allocations(prices[0: ind + 1], views[0: ind + 1], prev_weights), price)

acc.update(strat.allocations(prices[0: ind + 1], views[0: ind + 1], prev_weights), price)
sharpe = acc.daily_sharpe()
max_drawdown = acc.max_drawdown()
calmar = acc.calmar()
total_return, percent_return = acc.returns()

if acc.log:
with open(log_name, mode='a', encoding='utf-8', newline='') as f:
writer = csv.writer(f)
[writer.writerow([idx, trade["Value"], trade["Return"], trade["Weights"]]) for idx, trade in enumerate(acc.trades)]

print(f'Sharpe: {sharpe}')
print(f'Max Drawdon: {max_drawdown}')
print(f'Max Drawdown: {max_drawdown}')
print(f'Total Return: {total_return}')
print(f'Percent Return: {percent_return}')
plt.plot(acc.absolute_values)
plt.xlabel = "Time"
plt.ylabel = "Portfolio Value"

plt.figure()


plt.plot(np.array(acc.absolute_values[1:])/np.array(acc.absolute_values[:-1]))
plt.xlabel = "Time"
plt.ylabel = "Returns"

plt.figure()

plt.hist(np.array(acc.absolute_values[1:])/np.array(acc.absolute_values[:-1]), bins=50)
print(f'Max Tick Return: {np.amax(np.array(acc.absolute_values[1:])/np.array(acc.absolute_values[:-1])) - 1}')
print(f'Min Tick Return: {np.amin(np.array(acc.absolute_values[1:])/np.array(acc.absolute_values[:-1])) - 1}')
print(f'Average Tick Return: {np.average(np.array(acc.absolute_values[1:])/np.array(acc.absolute_values[:-1])) - 1}')

if acc.log:
with open(log_name, mode='a', encoding='utf-8', newline='') as f:
writer = csv.writer(f)
[writer.writerow([idx, trade["Value"], trade["Return"], trade["Weights"]]) for idx, trade in enumerate(acc.trades)]

fig, axs = plt.subplots(3, 1, constrained_layout=True)
axs[0].plot(acc.absolute_values)
axs[0].set_title('Portfolio')
axs[0].set_xlabel('Time')
axs[0].set_ylabel('Portfolio Value')
fig.suptitle('Visual Representation of Strategy Performance', fontsize=16)

axs[1].plot(np.array(acc.absolute_values[1:]) / np.array(acc.absolute_values[:-1]))
axs[1].set_xlabel('Time')
axs[1].set_title('Returns (side view)')
axs[1].set_ylabel('Returns')

axs[2].hist(np.array(acc.absolute_values[1:]) / np.array(acc.absolute_values[:-1]), bins=50)
axs[2].set_xlabel('% Return')
axs[2].set_title('Returns (front view)')
axs[2].set_ylabel('Frequency')
plt.show()

# convert array into dataframe
DF = pd.DataFrame(acc.absolute_values)

# save the dataframe as a csv file
DF.to_csv("time_series_data.csv")

28 changes: 18 additions & 10 deletions backtest_v2/demo.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
from backtest import backtest
import numpy as np
import random
import pandas as pd
from scipy.optimize import minimize


Expand All @@ -15,20 +17,26 @@

'''
def strat_function(preds, prices, last_weights):

#print(last_weights)
prices = np.array(prices)
signal = prices[:, -1][-1]
if signal == -1:
return [0, 1, 0]
if signal == 1:
return [0, 0, 0]
if signal == 0:
return last_weights


#print(signal)

return [random.uniform(-1, 1),
random.uniform(-1, 1),
random.uniform(-1, 1),
random.uniform(-1, 1),
random.uniform(-1, 1),
random.uniform(-1, 1),
random.uniform(-1, 1),
random.uniform(-1, 1),
random.uniform(-1, 1)]

'''
Running the backtest - starting portfolio value of 10000, reading in data from these two locations.
'''
backtest(strat_function, 10000, '../test_datasets/SEDG-ENPH-Signal.csv', '../test_datasets/SEDG-ENPH-Signal.csv', True, "log.csv")
backtest(strat_function, 2000,
'C:/Users/jacky/OneDrive/Documents/GitHub/backtesting/test_datasets/Actual.csv',
'C:/Users/jacky/OneDrive/Documents/GitHub/backtesting/test_datasets/Actual.csv',
True, "log.csv")

Loading