-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathAlgorithmicTradingStrategies.py
86 lines (64 loc) · 2.77 KB
/
AlgorithmicTradingStrategies.py
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
79
80
81
82
83
84
85
86
### Moving average crossover strategy:
import pandas as pd
# Load historical price data into a DataFrame
df = pd.read_csv('historical_data.csv') # Replace 'historical_data.csv' with your actual data file
# Calculate the moving averages
df['MA_short'] = df['Close'].rolling(window=10).mean() # Short-term moving average
df['MA_long'] = df['Close'].rolling(window=30).mean() # Long-term moving average
# Generate trading signals
df['Signal'] = 0 # Initialize a column for trading signals
df.loc[df['MA_short'] > df['MA_long'], 'Signal'] = 1 # Set signal to 1 for buy signals
df.loc[df['MA_short'] < df['MA_long'], 'Signal'] = -1 # Set signal to -1 for sell signals
# Calculate position changes based on the signals
df['Position'] = df['Signal'].diff()
# Backtest the strategy
initial_capital = 100000 # Initial capital in your account
position = 0 # Initialize position as 0
for index, row in df.iterrows():
if row['Position'] == 1: # Buy signal
if position == 0:
position = initial_capital / row['Close']
elif row['Position'] == -1: # Sell signal
if position > 0:
initial_capital = position * row['Close']
position = 0
# Calculate the final portfolio value
portfolio_value = initial_capital if position == 0 else position * df['Close'].iloc[-1]
print(f"Final Portfolio Value: {portfolio_value:.2f}")
### Mean Reversion Strategy:
import pandas as pd
df = pd.read_csv('historical_data.csv')
# Calculate mean and standard deviation
mean = df['Close'].mean()
std = df['Close'].std()
# Generate trading signals based on deviations from the mean
df['Signal'] = 0
df.loc[df['Close'] > mean + std, 'Signal'] = -1 # Sell signal
df.loc[df['Close'] < mean - std, 'Signal'] = 1 # Buy signal
# Backtest the strategy and calculate portfolio value
# ...
### Breakout Strategy:
import pandas as pd
df = pd.read_csv('historical_data.csv')
# Define breakout threshold and calculate rolling high and low
breakout_threshold = 0.02
df['RollingHigh'] = df['Close'].rolling(window=20).max()
df['RollingLow'] = df['Close'].rolling(window=20).min()
# Generate trading signals based on breakout thresholds
df['Signal'] = 0
df.loc[df['Close'] > df['RollingHigh'] * (1 + breakout_threshold), 'Signal'] = 1 # Buy signal
df.loc[df['Close'] < df['RollingLow'] * (1 - breakout_threshold), 'Signal'] = -1 # Sell signal
# Backtest the strategy and calculate portfolio value
# ...
### MACD Strategy:
import pandas as pd
import talib
df = pd.read_csv('historical_data.csv')
# Calculate MACD and signal line
macd, signal, _ = talib.MACD(df['Close'])
# Generate trading signals based on MACD crossover
df['Signal'] = 0
df.loc[macd > signal, 'Signal'] = 1 # Buy signal
df.loc[macd < signal, 'Signal'] = -1 # Sell signal
# Backtest the strategy and calculate portfolio value
# ...