Update mltechnicalscanner.py
Browse filesOption added : "--compare" to read the results.txt file and compare to current prices <=> to check if the predictions were ok.
10 unmodified lines111213141516179 unmodified lines2728293031323316 unmodified lines505152515354555657585960616263646566676869707172735774757677597879808182838485866687888990919293947295969798991001017710210310410510610722 unmodified lines13013113210613313413513613713813 unmodified lines1521531541261551561571581591601611311621631641651661675 unmodified lines17317417514317617717817918018119 unmodified lines20120220316920420520620720820921021117521221321421521621735 unmodified lines2532542552172562572582592602612622212632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173182272282293193203213223233243253263273283293303313323333343353363373383393403413423433442373453463473483493503513522433533543553563573582 unmodified lines36136236325236436536636736836925637037137237337437537626137737837938026338138238338438538626738738838939039127039239339439539627339739839940040140210 unmodified linesfrom sklearn.metrics import accuracy_scoreimport pickleimport warningsimport re
# Suppress warningswarnings.filterwarnings('ignore')9 unmodified lines self.model = None self.model_file = "technical_ml_model.pkl" self.training_data_file = "training_data.csv" self.results_file = "results.txt" self.min_training_samples = 100 self.load_ml_model() 16 unmodified lines # Training data collection self.training_data = pd.DataFrame(columns=self.feature_columns + ['target']) def init_results_file(self): """Initialize results file only when starting a new scan""" with open(self.results_file, 'w') as f: f.write("Scan Results Log\n") f.write("="*50 + "\n") f.write(f"Scan started at {datetime.now()}\n\n") def log_result(self, message): """Log message to results file""" try: with open(self.results_file, 'a') as f: f.write(message + '\n') except Exception as e: print(f"Error writing to results file: {str(e)}") def load_ml_model(self): """Load trained ML model if exists""" if os.path.exists(self.model_file): with open(self.model_file, 'rb') as f: self.model = pickle.load(f) print("Loaded trained model from file") msg = "Loaded trained model from file" print(msg) self.log_result(msg) else: print("Initializing new model") msg = "Initializing new model" print(msg) self.log_result(msg) self.model = RandomForestClassifier(n_estimators=100, random_state=42) def save_ml_model(self): """Save trained ML model""" with open(self.model_file, 'wb') as f: pickle.dump(self.model, f) print("Saved model to file") msg = "Saved model to file" print(msg) self.log_result(msg) def load_training_data(self): """Load existing training data if available""" if os.path.exists(self.training_data_file): self.training_data = pd.read_csv(self.training_data_file) print(f"Loaded {len(self.training_data)} training samples") msg = f"Loaded {len(self.training_data)} training samples" print(msg) self.log_result(msg) def save_training_data(self): """Save training data to file""" self.training_data.to_csv(self.training_data_file, index=False) print(f"Saved {len(self.training_data)} training samples") msg = f"Saved {len(self.training_data)} training samples" print(msg) self.log_result(msg) def calculate_features(self, df): """Calculate technical indicators"""22 unmodified lines return df except Exception as e: print(f"Error calculating features: {str(e)}") error_msg = f"Error calculating features: {str(e)}" print(error_msg) self.log_result(error_msg) return None def train_initial_model(self):13 unmodified lines # Evaluate model preds = self.model.predict(X_test) accuracy = accuracy_score(y_test, preds) print(f"Initial model trained with accuracy: {accuracy:.2f}") msg = f"Initial model trained with accuracy: {accuracy:.2f}" print(msg) self.log_result(msg) self.save_ml_model() return True else: print(f"Not enough training data ({len(self.training_data)} samples). Need at least {self.min_training_samples}.") msg = f"Not enough training data ({len(self.training_data)} samples). Need at least {self.min_training_samples}." print(msg) self.log_result(msg) return False def predict_direction(self, features):5 unmodified lines features = features[self.feature_columns].values.reshape(1, -1) return self.model.predict(features)[0] except Exception as e: print(f"Prediction error: {str(e)}") error_msg = f"Prediction error: {str(e)}" print(error_msg) self.log_result(error_msg) return 0 def collect_training_sample(self, symbol, exchange, timeframe='1h'):19 unmodified lines new_row = pd.DataFrame([features]) self.training_data = pd.concat([self.training_data, new_row], ignore_index=True) print(f"Collected training sample for {symbol}") msg = f"Collected training sample for {symbol}" print(msg) self.log_result(msg) if len(self.training_data) % 10 == 0: self.save_training_data() except Exception as e: print(f"Error collecting training sample: {str(e)}") error_msg = f"Error collecting training sample: {str(e)}" print(error_msg) self.log_result(error_msg) def scan_symbol(self, symbol, exchange, timeframes): """Scan symbol for trading opportunities"""35 unmodified lines self.alert(symbol, "DOWNTREND", timeframes, price) except Exception as e: print(f"Error scanning {symbol}: {str(e)}") error_msg = f"Error scanning {symbol}: {str(e)}" print(error_msg) self.log_result(error_msg) def alert(self, symbol, trend_type, timeframes, current_price): """Generate alert for detected trend""" timestamp = datetime.now().strftime('%Y-%m-%d %H:%M:%S') message = f"({trend_type}) detected for {symbol} at price {current_price} on {timeframes} at {datetime.now()}" message = f"({trend_type}) detected for {symbol} at price {current_price} on {timeframes} at {timestamp}" print(message) self.log_result(message) def compare_results(self, exchange_name): """Compare previous results with current prices""" try: if not os.path.exists(self.results_file): print("No results file found to compare") return exchange = self.exchanges.get(exchange_name.lower()) if not exchange: print(f"Exchange {exchange_name} not supported") return # Pattern to extract symbol and price from log entries pattern = r"\((.*?)\) detected for (.*?) at price ([\d.]+) on" with open(self.results_file, 'r') as f: lines = f.readlines() print("\n=== Price Comparison Report ===") print(f"Generated at: {datetime.now()}\n") for line in lines: match = re.search(pattern, line) if match: trend_type = match.group(1) symbol = match.group(2) old_price = float(match.group(3)) timestamp = line.split(' at ')[-1].strip() try: ticker = exchange.fetch_ticker(symbol) current_price = ticker['last'] price_change = current_price - old_price percent_change = (price_change / old_price) * 100 print(f"Symbol: {symbol}") print(f"Previous: {trend_type} at {old_price} ({timestamp})") print(f"Current: {current_price} ({datetime.now().strftime('%Y-%m-%d %H:%M:%S')})") print(f"Change: {price_change:.4f} ({percent_change:.2f}%)") print("-" * 50) except Exception as e: print(f"Error fetching current price for {symbol}: {str(e)}") continue print("\n=== End of Report ===") except Exception as e: print(f"Error comparing results: {str(e)}")
# Main executionif __name__ == "__main__": parser = argparse.ArgumentParser() parser.add_argument("-e", "--exchange", help="Exchange name", required=True) parser.add_argument("-f", "--filter", help="Asset filter", required=True) parser.add_argument("-tf", "--timeframes", help="Timeframes to scan (comma separated)", required=True) parser.add_argument("-e", "--exchange", help="Exchange name", required=False) parser.add_argument("-f", "--filter", help="Asset filter", required=False) parser.add_argument("-tf", "--timeframes", help="Timeframes to scan (comma separated)", required=False) parser.add_argument("--train", help="Run in training mode", action="store_true") parser.add_argument("--compare", help="Compare previous results with current prices", action="store_true") args = parser.parse_args() if args.compare: scanner = MLTechnicalScanner() if args.exchange: scanner.compare_results(args.exchange) else: print("Please specify an exchange with -e/--exchange when using --compare") sys.exit(0) if not all([args.exchange, args.filter, args.timeframes]): print("Error: --exchange, --filter, and --timeframes are required when not using --compare") sys.exit(1) scanner = MLTechnicalScanner(training_mode=args.train) # Initialize results file only for actual scans, not comparisons scanner.init_results_file() exchange = scanner.exchanges.get(args.exchange.lower()) if not exchange: print(f"Exchange {args.exchange} not supported") error_msg = f"Exchange {args.exchange} not supported" print(error_msg) scanner.log_result(error_msg) sys.exit(1) try: markets = exchange.fetch_markets() except Exception as e: print(f"Error fetching markets: {str(e)}") error_msg = f"Error fetching markets: {str(e)}" print(error_msg) scanner.log_result(error_msg) sys.exit(1) symbols = [2 unmodified lines ] if not symbols: print(f"No symbols found matching filter {args.filter}") error_msg = f"No symbols found matching filter {args.filter}" print(error_msg) scanner.log_result(error_msg) sys.exit(1) if args.train: print(f"Running in training mode for {len(symbols)} symbols") train_msg = f"Running in training mode for {len(symbols)} symbols" print(train_msg) scanner.log_result(train_msg) for symbol in symbols: scanner.collect_training_sample(symbol, exchange) if scanner.train_initial_model(): print("Training completed successfully") success_msg = "Training completed successfully" print(success_msg) scanner.log_result(success_msg) else: print("Not enough data collected for training") fail_msg = "Not enough data collected for training" print(fail_msg) scanner.log_result(fail_msg) sys.exit(0) if not hasattr(scanner.model, 'classes_'): print("Warning: No trained model available. Running with basic scanning only.") warn_msg = "Warning: No trained model available. Running with basic scanning only." print(warn_msg) scanner.log_result(warn_msg) timeframes = args.timeframes.split(',') print(f"Scanning {len(symbols)} symbols on timeframes {timeframes}") scan_msg = f"Scanning {len(symbols)} symbols on timeframes {timeframes}" print(scan_msg) scanner.log_result(scan_msg) for symbol in symbols: scanner.scan_symbol(symbol, exchange, timeframes) scanner.scan_symbol(symbol, exchange, timeframes) # Add final summary end_msg = f"\nScan completed at {datetime.now()}" print(end_msg) scanner.log_result(end_msg)mltechnicalscanner.py+155-26