-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpattern_stock.py
155 lines (130 loc) · 7.34 KB
/
pattern_stock.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
import numpy as np
import pandas as pd
import patterns as pt
import analysis as an
from loader import load_data, check_clean_data
class PatternStock:
"""
A class to represent a stock and perform candlestick pattern analysis on it.
This class handles loading stock data, applying candlestick patterns,
calculating metrics, and running simulations for statistical analysis.
"""
def __init__(self, ticker: str):
"""
Initialize the PatternStock object.
:param ticker: The stock ticker symbol
"""
self.ticker = ticker
self.df = None # Main DataFrame to store historical price data
self.info = None # Dictionary to store general stock information
self.support_df = None # DataFrame to store supporting indicators (e.g., ATR)
self.pattern_data = {} # Dictionary to store pattern-specific data and analysis results
def load_data(self):
"""
Load and preprocess the stock data.
This method fetches the stock data with 'load_data'.
It then checks its consistency and cleans it with 'check_clean_data'.
After that, it adds the percentage and log returns to the dataframe and calculates the ATR indicator.
"""
data = load_data(self.ticker)
if not data or self.ticker not in data:
raise ValueError(f"Invalid ticker or no data found for {self.ticker}")
cleaned_data = check_clean_data(data)
if not cleaned_data[self.ticker]['historical_data'].empty:
self.df = cleaned_data[self.ticker]['historical_data']
self.info = cleaned_data[self.ticker]['info']
self.df = an.add_pct_log_returns(self.df)
self.support_df = an.calculate_ATR(self.df)
else:
raise ValueError(f"No historical data available for {self.ticker}")
def apply_pattern(self, pattern_name: str):
"""
Apply a specific candlestick pattern to the stock data.
This method identifies the occurrences of the specified pattern in the stock's price history.
:param pattern_name: The name of the candlestick pattern to apply
"""
if pattern_name not in self.pattern_data:
pattern_info = pt.patterns[pattern_name]
pattern_function = pattern_info['function']
# Combine main DataFrame and support DataFrame for pattern detection
mask = pattern_function(pd.concat([self.df, self.support_df], axis=1))
self.pattern_data[pattern_name] = {
'info': pattern_info,
'mask': mask,
'dim_pattern': mask.sum(), # Number of pattern occurrences
'metrics': None,
'simulation_results': None
}
self.calculate_metrics(pattern_name)
def calculate_metrics(self, pattern_name: str):
"""
Calculate performance metrics for the specified pattern.
This method computes win rate, average return, median return, and standard deviation
of returns following each occurrence of the pattern.
:param pattern_name: The name of the candlestick pattern
"""
if self.pattern_data[pattern_name]['metrics'] is None:
mask = self.pattern_data[pattern_name]['mask']
returns = an.calculate_log_cumReturns_periods(self.df, mask, max_ahead=15)
self.pattern_data[pattern_name]['metrics'] = {
'win_rate': an.calculate_win_rate_from_log(returns),
'average_return': an.calculate_average_return_from_log(returns),
'median_return': an.calculate_median_return_from_log(returns),
'std_return': an.calculate_std_return_from_log(returns),
}
def run_simulation(self, pattern_name: str, n_iterations: int = 1000):
"""
Run n_interations simulations to assess the statistical significance of the pattern.
This method generates random samples, calculates confidence intervals and p-values,
and creates a significance table comparing the pattern's performance to random chance.
:param pattern_name: The name of the candlestick pattern
:param n_iterations: Number of iterations for the Monte Carlo simulation (default: 1000)
"""
if self.pattern_data[pattern_name]['simulation_results'] is None:
mask = self.pattern_data[pattern_name]['mask']
dim_pattern = self.pattern_data[pattern_name]['dim_pattern']
# Generate random samples and calculate their metrics
base_returns = an.generate_random_returns(self.df, mask, dim_pattern, n_iterations, is_log=True, verbose=False)
base_distributions = an.calculate_metrics(base_returns)
# Calculate confidence intervals
nonPar_conf_int = an.calculate_nonParametric_confidence_intervals(base_distributions)
parametric_conf_int = an.calculate_parametric_confidence_intervals(base_distributions)
metrics = self.pattern_data[pattern_name]['metrics']
empirical_pvalues = an.calculate_empirical_pvalues(metrics, base_distributions)
# Transform confidence intervals for easier comparison
param_conf_int, basecase_estimates = an.transform_confidence_intervals(parametric_conf_int)
non_parametric_conf_int, _ = an.transform_confidence_intervals(nonPar_conf_int)
# Create a table showing the statistical significance of the pattern's performance
significance_table = an.create_significance_table(
metrics, basecase_estimates, empirical_pvalues, param_conf_int, non_parametric_conf_int
)
self.pattern_data[pattern_name]['simulation_results'] = {
'base_distributions': base_distributions,
'nonPar_conf_int': nonPar_conf_int,
'parametric_conf_int': parametric_conf_int,
'empirical_pvalues': empirical_pvalues,
'significance_table': significance_table
}
def get_data_for_plotting(self, pattern_name: str):
"""
Retrieve all necessary data for plotting the pattern analysis results.
This method collects the relevant data for creating visualizations of the pattern analysis,
including the stock data, pattern occurrences, metrics, and simulation results.
:param pattern_name: The name of the candlestick pattern
:return: A dictionary containing all the data needed for plotting, or None if the pattern hasn't been analyzed
"""
if pattern_name not in self.pattern_data:
return None
pattern_data = self.pattern_data[pattern_name]
data = {
'df': self.df,
'mask': pattern_data['mask'],
'pattern_name': pattern_name,
'pattern_info': pattern_data['info'],
'pattern_metrics': pattern_data['metrics'],
}
if pattern_data['simulation_results']:
data['confidence_intervals'] = pattern_data['simulation_results']['nonPar_conf_int']
data['par_confidence_intervals'] = pattern_data['simulation_results']['parametric_conf_int']
data['significance_table'] = pattern_data['simulation_results']['significance_table']
return data