Refactor main.py to remove commented-out cycle detection code and streamline trend plotting. Update TrendDetectorSimple by eliminating unnecessary logging statements for improved clarity during trend detection.
This commit is contained in:
parent
f316571a3c
commit
7c4db08b1b
38
main.py
38
main.py
@ -17,40 +17,4 @@ print(f"Number of data points: {len(daily_data)}")
|
|||||||
|
|
||||||
trend_detector = TrendDetectorSimple(daily_data, verbose=True)
|
trend_detector = TrendDetectorSimple(daily_data, verbose=True)
|
||||||
trends, analysis_results = trend_detector.detect_trends()
|
trends, analysis_results = trend_detector.detect_trends()
|
||||||
trend_detector.plot_trends(trends, analysis_results, "supertrend")
|
trend_detector.plot_trends(trends, analysis_results, "supertrend")
|
||||||
|
|
||||||
#trend_detector = TrendDetectorMACD(daily_data, True)
|
|
||||||
#trends = trend_detector.detect_trends_MACD_signal()
|
|
||||||
#trend_detector.plot_trends(trends)
|
|
||||||
|
|
||||||
# # Cycle detection (new code)
|
|
||||||
# print("\n===== CYCLE DETECTION =====")
|
|
||||||
|
|
||||||
# # Daily cycles
|
|
||||||
# daily_detector = CycleDetector(daily_data, timeframe='daily')
|
|
||||||
# daily_avg_cycle = daily_detector.get_average_cycle_length()
|
|
||||||
# daily_window = daily_detector.get_cycle_window()
|
|
||||||
|
|
||||||
# print(f"Daily Timeframe: Average Cycle Length = {daily_avg_cycle:.2f} days")
|
|
||||||
# if daily_window:
|
|
||||||
# print(f"Daily Cycle Window: {daily_window[0]:.2f} to {daily_window[2]:.2f} days")
|
|
||||||
|
|
||||||
# weekly_detector = CycleDetector(daily_data, timeframe='weekly')
|
|
||||||
# weekly_avg_cycle = weekly_detector.get_average_cycle_length()
|
|
||||||
# weekly_window = weekly_detector.get_cycle_window()
|
|
||||||
|
|
||||||
# print(f"\nWeekly Timeframe: Average Cycle Length = {weekly_avg_cycle:.2f} weeks")
|
|
||||||
# if weekly_window:
|
|
||||||
# print(f"Weekly Cycle Window: {weekly_window[0]:.2f} to {weekly_window[2]:.2f} weeks")
|
|
||||||
|
|
||||||
# # Detect patterns
|
|
||||||
# two_drives = daily_detector.detect_two_drives_pattern()
|
|
||||||
# v_shapes = daily_detector.detect_v_shaped_lows()
|
|
||||||
|
|
||||||
# print(f"\nDetected {len(two_drives)} 'Two Drives' patterns in daily data")
|
|
||||||
# print(f"Detected {len(v_shapes)} 'V-Shaped' lows in daily data")
|
|
||||||
|
|
||||||
# # Plot cycles with detected patterns
|
|
||||||
# print("\nPlotting cycles and patterns...")
|
|
||||||
# daily_detector.plot_cycles(pattern_detection=['two_drives', 'v_shape'], title_suffix='(with Two Drives Pattern)')
|
|
||||||
# weekly_detector.plot_cycles(title_suffix='(Weekly View)')
|
|
||||||
@ -90,10 +90,8 @@ class TrendDetectorSimple:
|
|||||||
# Convert data to pandas DataFrame if it's not already
|
# Convert data to pandas DataFrame if it's not already
|
||||||
if not isinstance(self.data, pd.DataFrame):
|
if not isinstance(self.data, pd.DataFrame):
|
||||||
if isinstance(self.data, list):
|
if isinstance(self.data, list):
|
||||||
self.logger.info("Converting list to DataFrame")
|
|
||||||
self.data = pd.DataFrame({'close': self.data})
|
self.data = pd.DataFrame({'close': self.data})
|
||||||
else:
|
else:
|
||||||
self.logger.error("Invalid data format provided")
|
|
||||||
raise ValueError("Data must be a pandas DataFrame or a list")
|
raise ValueError("Data must be a pandas DataFrame or a list")
|
||||||
|
|
||||||
self.logger.info(f"Initialized TrendDetectorSimple with {len(self.data)} data points")
|
self.logger.info(f"Initialized TrendDetectorSimple with {len(self.data)} data points")
|
||||||
@ -171,16 +169,12 @@ class TrendDetectorSimple:
|
|||||||
- DataFrame with columns for timestamps, prices, and trend indicators
|
- DataFrame with columns for timestamps, prices, and trend indicators
|
||||||
- Dictionary containing analysis results including linear regression, SMAs, and SuperTrend indicators
|
- Dictionary containing analysis results including linear regression, SMAs, and SuperTrend indicators
|
||||||
"""
|
"""
|
||||||
self.logger.info(f"Detecting trends")
|
|
||||||
|
|
||||||
df = self.data.copy()
|
df = self.data.copy()
|
||||||
close_prices = df['close'].values
|
close_prices = df['close'].values
|
||||||
|
|
||||||
max_peaks, _ = find_peaks(close_prices)
|
max_peaks, _ = find_peaks(close_prices)
|
||||||
min_peaks, _ = find_peaks(-close_prices)
|
min_peaks, _ = find_peaks(-close_prices)
|
||||||
|
|
||||||
self.logger.info(f"Found {len(min_peaks)} local minima and {len(max_peaks)} local maxima")
|
|
||||||
|
|
||||||
df['is_min'] = False
|
df['is_min'] = False
|
||||||
df['is_max'] = False
|
df['is_max'] = False
|
||||||
|
|
||||||
@ -192,7 +186,6 @@ class TrendDetectorSimple:
|
|||||||
result = df[['datetime', 'close', 'is_min', 'is_max']].copy()
|
result = df[['datetime', 'close', 'is_min', 'is_max']].copy()
|
||||||
|
|
||||||
# Perform linear regression on min_peaks and max_peaks
|
# Perform linear regression on min_peaks and max_peaks
|
||||||
self.logger.info("Performing linear regression on min and max peaks")
|
|
||||||
min_prices = df['close'].iloc[min_peaks].values
|
min_prices = df['close'].iloc[min_peaks].values
|
||||||
max_prices = df['close'].iloc[max_peaks].values
|
max_prices = df['close'].iloc[max_peaks].values
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user