-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpivot_points.py
311 lines (269 loc) · 10.9 KB
/
pivot_points.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
from datetime import timedelta
import multiprocessing as mp
from itertools import product
from typing import Tuple, List
plt.style.use("seaborn-v0_8")
class PivotPointStrategy:
def __init__(self, dataframe: pd.DataFrame, window: int, days_high: int, stoploss: float):
"""
Initialize the PivotPointStrategy class with the given parameters.
Args:
dataframe (pd.DataFrame): The input data frame containing the market data.
window (int): The rolling window size for calculating pivot points.
days_high (int): The number of consecutive days a value must be the highest to be considered a pivot point.
stoploss (float): The stop loss percentage to limit losses in the strategy.
"""
self.dataframe = dataframe
self.window = window
self.days_high = days_high
self.stoploss = stoploss
def find_pivs(self) -> pd.DataFrame:
"""
Identify pivot points in the dataframe based on the initialized parameters.
Returns:
pd.DataFrame: The modified dataframe with pivot points established.
"""
data = self.dataframe.copy()
if self.window >= self.days_high:
data["creturns_max"] = (
data["creturns"].rolling(self.window + 1, min_periods=1).max()
)
data["prev_max"] = data["creturns_max"].shift(1)
data["counter"] = (
(data["creturns_max"] == data["prev_max"]).astype(int).cumsum()
)
data["reset_counter"] = (
(data["creturns_max"] != data["prev_max"]).astype(int).cumsum()
)
data["counter"] -= data.groupby("reset_counter")["counter"].transform("min")
data["pivot_points_established"] = np.where(
data["counter"] == self.days_high, data["creturns_max"], np.nan
)
self.dataframe = data
return data
def process_pivot_points(self) -> pd.DataFrame:
"""
Process the established pivot points and update the dataframe with their first occurrence.
Returns:
pd.DataFrame: The updated dataframe with pivot points processed.
"""
pivot_points = self.dataframe["pivot_points_established"].dropna().unique()
index: List[Tuple[float, int]] = [
(pivot, idx)
for pivot in pivot_points
for idx in self.dataframe[self.dataframe.creturns == pivot].index
]
df = pd.DataFrame(index, columns=["pivot_first_hit", "index_value"]).set_index(
"index_value"
)
self.dataframe = self.dataframe.join(df["pivot_first_hit"])
return self.dataframe
def plot_pivots(self, line_length: int = 50) -> None:
"""
Plot the pivot points on a graph.
Args:
line_length (int): The length of the line to extend the pivot point visualization.
"""
dates = []
pivots = []
for datetime, row in self.dataframe.iterrows():
value = row["pivot_first_hit"]
dates.append(datetime)
dates.append(datetime + timedelta(days=line_length))
pivots.append(value)
pivots.append(value)
fig = plt.figure(figsize=(12, 8))
plt.plot(dates, pivots, linestyle="dotted", label="Pivot Points", color="blue")
plt.plot(
self.dataframe.index,
self.dataframe.creturns,
label="creturns",
color="black",
linewidth=0.5,
)
plt.xlabel("Date")
plt.ylabel("Return")
plt.title(
f"BTC - Pivot Points \n\n Window = {self.window}, Days High = {self.days_high}"
)
plt.legend()
plt.show()
def run_strat(self) -> pd.DataFrame:
"""
Execute the trading strategy based on pivot points and stop loss criteria.
Returns:
pd.DataFrame: The dataframe with strategy execution results.
"""
data = self.dataframe.copy()
# Calculate pivot above and below
pivot = []
next_higher_piv = []
next_lower_piv = []
for index, row in data.iterrows():
pivot.append(row["pivot_points_established"])
current_price = row["creturns"]
higher_pivots = [p for p in pivot if p > current_price]
lower_pivots = [p for p in pivot if p < current_price]
next_higher_piv.append(min(higher_pivots, default=np.nan))
next_lower_piv.append(max(lower_pivots, default=np.nan))
data["next_higher_piv"] = next_higher_piv
data["next_lower_piv"] = next_lower_piv
# Position when price crosses pivot
data["position"] = np.where(
data.creturns > data.next_higher_piv.shift(1), 1, np.nan
)
data["position"] = np.where(
data.creturns < data.next_lower_piv.shift(1), -1, data["position"]
)
# Stop loss
if self.stoploss > 0:
data["position_chng"] = (data.position.ffill().diff() == 2) | (
data.position.ffill().diff() == -2
)
data["strategy"] = data["position"].ffill().shift(1) * data["returns"]
data["trade_ret"] = data["strategy"].where(~data["position_chng"], 0)
trade_ret_lst = [0]
for index, row in data.iterrows():
cumsum = trade_ret_lst[-1] + row.strategy
trade_ret_lst.append(cumsum if not row.position_chng else 0)
data["trade_ret"] = trade_ret_lst[1:]
data["position"] = np.where(
data["trade_ret"] < -self.stoploss / 100, 0, data["position"]
)
# Forward fill position
data["position"] = data.position.ffill()
# Calculate cumulative returns
data["creturns"] = data["returns"].cumsum().apply(np.exp)
data["strategy"] = data["position"].shift(1) * data["returns"]
data["cstrategy"] = data["strategy"].cumsum().apply(np.exp)
# Calculate prices for plot
data["long_price"] = data.creturns[
(data.position.diff() == 1) & (data.position > 0)
| (data.position.diff() == 2) & (data.position > 0)
]
data["short_price"] = data.creturns[
(data.position.diff() == -1) & (data.position < 0)
| (data.position.diff() == -2) & (data.position < 0)
]
data["neutral_price"] = data.creturns[
(data.position.diff() != 0) & (data.position == 0)
]
self.dataframe = data
return data
def plot_strat(self) -> None:
"""
Plot the strategy execution results on a graph.
"""
dates = []
pivots = []
for datetime, row in self.dataframe.iterrows():
value = row["pivot_first_hit"]
dates.append(datetime)
dates.append(datetime + timedelta(days=30))
pivots.append(value)
pivots.append(value)
fig, ax1 = plt.subplots()
fig.set_figwidth(12)
fig.set_figheight(8)
ax2 = ax1.twinx()
x = self.dataframe.index
ax1.plot(
x, self.dataframe.creturns, label="creturns", color="black", linewidth=0.5
)
ax1.plot(
x, self.dataframe.cstrategy, label="cstrategy", color="teal", linewidth=0.5
)
ax1.scatter(
self.dataframe.index,
self.dataframe.long_price,
label="Long",
marker="^",
color="green",
alpha=1,
)
ax1.scatter(
self.dataframe.index,
self.dataframe.short_price,
label="Short",
marker="v",
color="red",
alpha=1,
)
ax1.scatter(
self.dataframe.index,
self.dataframe.neutral_price,
label="neutral",
marker="o",
color="blue",
alpha=1,
)
ax1.plot(dates, pivots, linestyle="dotted", label="Pivot Points")
ax1.legend()
ax1.set_xlabel("Date")
ax2.set_ylabel("Position")
ax1.set_ylabel("Return")
ax1.set_title(
f"BTC - Pivot Points Strategy \n\n Window = {self.window}, Days High = {self.days_high}, Stop loss = {self.stoploss}%"
)
plt.show()
def optimize(self, window: int, days_high: int, stoploss: float) -> pd.DataFrame:
"""
Optimize the strategy parameters and return the results.
Args:
window (int): The rolling window size for calculating pivot points.
days_high (int): The number of consecutive days a value must be the highest to be considered a pivot point.
stoploss (float): The stop loss percentage to limit losses in the strategy.
Returns:
pd.DataFrame: A dataframe containing the optimization results.
"""
# Update strategy parameters
self.window = window
self.days_high = days_high
self.stoploss = stoploss
# Calculate pivot points and run strategy
self.find_pivs()
self.run_strat()
# Attempt to extract the last strategy return value
try:
strat_ret = self.dataframe['cstrategy'].iloc[-1]
result = pd.DataFrame({
"window": [window],
"days_high": [days_high],
"stoploss": [stoploss],
"strat_ret": [strat_ret],
})
except IndexError as e:
# Log the error and return an empty DataFrame if there's an issue
print(f"Failed to extract strategy return: {e}")
return pd.DataFrame()
return result
def run_optimization(self, window_range: Tuple[int, int], days_high_range: Tuple[int, int], stoploss_range: Tuple[int, int]) -> pd.DataFrame:
"""
Run optimization over a range of parameters and return the best results.
Args:
window_range (Tuple[int, int]): The range of window sizes to test.
days_high_range (Tuple[int, int]): The range of days high values to test.
stoploss_range (Tuple[int, int]): The range of stop loss percentages to test.
Returns:
pd.DataFrame: A dataframe containing the best optimization results.
"""
try:
cpus = mp.cpu_count()
except NotImplementedError:
cpus = 2 # Default to 2 CPUs if count not available
with mp.Pool(cpus) as pool:
combinations = product(
range(*window_range),
range(*days_high_range),
range(*stoploss_range),
)
results = pool.starmap(self.optimize, combinations)
if results:
df_optimized = pd.concat(results)
if not df_optimized.empty:
df_optimized.sort_values(by="strat_ret", ascending=False, inplace=True)
return df_optimized
return pd.DataFrame()