-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathBacktestFunctions.py
More file actions
78 lines (55 loc) · 3.25 KB
/
BacktestFunctions.py
File metadata and controls
78 lines (55 loc) · 3.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
import numpy as np
import pandas as pd
def calculate_s1_returns(df, initial_capital, share_size):
df["Position"] = share_size * np.where(df["Entry/Exit_S1"] > 0.0, 1, (np.where(df["Entry/Exit_S1"] < 0.0, -1, 0)))
# Multiply share price by entry/exit positions and get the cumulatively sum
df["Portfolio_Holdings"] = df["Close"] * df["Position"].cumsum()
# Subtract the initial capital by the portfolio holdings to get the amount of liquid cash in the portfolio
df["Portfolio_Cash"] = (
initial_capital - (df["Close"] * df["Position"]).cumsum()
)
# Get the total portfolio value by adding the cash amount by the portfolio holdings (or investments)
df["Portfolio_Total"] = df["Portfolio_Cash"] + df["Portfolio_Holdings"]
# Calculate the portfolio daily returns
df["Portfolio_Daily_Returns"] = df["Portfolio_Total"].pct_change()
# Calculate the cumulative returns
df["Portfolio Cumulative Returns"] = (1 + df["Portfolio_Daily_Returns"]).cumprod() - 1
return df
def display_returns(df, strategy_name, name: str, list_of_years):
df['Year'] = pd.to_datetime(df.index).to_period("Y")
df['Year'] = df['Year'].dt.strftime('%Y')
df = df[df["Year"].isin(list_of_years) ]
plot = df["Portfolio Cumulative Returns"].hvplot(
title=name + " Portfolio Cumulative Returns of " + strategy_name)#, ylim=ylim)
return plot
def calculate_s2_returns(df, initial_capital, share_size):
df["Position"] = share_size * np.where(df["Entry/Exit_S2"] > 0.0, 1, (np.where(df["Entry/Exit_S2"] <0.0, -1,0)))
# Multiply share price by entry/exit positions and get the cumulatively sum
df["Portfolio_Holdings"] = df["Close"]* df["Position"].cumsum()
# Subtract the initial capital by the portfolio holdings to get the amount of liquid cash in the portfolio
df["Portfolio_Cash"] = (
initial_capital - (df["Close"] * df["Position"]).cumsum()
)
# Get the total portfolio value by adding the cash amount by the portfolio holdings (or investments)
df["Portfolio_Total"] = df["Portfolio_Cash"] + df["Portfolio_Holdings"]
# Calculate the portfolio daily returns
df["Portfolio_Daily_Returns"] = df["Portfolio_Total"].pct_change()
# Calculate the cumulative returns
df["Portfolio Cumulative Returns"] = (1 + df["Portfolio_Daily_Returns"]).cumprod() - 1
return df
#Strategy3 backtest
def calculate_s3_returns(df,initial_capital,share_size):
df["Position"] = share_size * df['Above Range'].diff()
# Multiply share price by entry/exit positions and get the cumulatively sum
df["Portfolio_Holdings"] = df["Close"]* df["Position"].cumsum()
# Subtract the initial capital by the portfolio holdings to get the amount of liquid cash in the portfolio
df["Portfolio_Cash"] = (
initial_capital - (df["Close"] * df["Position"]).cumsum()
)
# Get the total portfolio value by adding the cash amount by the portfolio holdings (or investments)
df["Portfolio_Total"] = df["Portfolio_Cash"] + df["Portfolio_Holdings"]
# Calculate the portfolio daily returns
df["Portfolio_Daily_Returns"] = df["Portfolio_Total"].pct_change()
# Calculate the cumulative returns
df["Portfolio Cumulative Returns"] = (1 + df["Portfolio_Daily_Returns"]).cumprod() - 1
return df