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 lines1112131415169 unmodified lines26272829303116 unmodified lines48495051525354555657585960616263646566676869707172737475767778798022 unmodified lines10310410510610710810913 unmodified lines1231241251261271281291301311321331345 unmodified lines14014114214314414514619 unmodified lines16616716816917017117217317417517617717835 unmodified lines2142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462 unmodified lines24925025125225325425525625725825926026126226326426526626726826927027127227310 unmodified linesfrom sklearn.metrics import accuracy_scoreimport pickleimport warnings# Suppress warningswarnings.filterwarnings('ignore')9 unmodified linesself.model = Noneself.model_file = "technical_ml_model.pkl"self.training_data_file = "training_data.csv"self.min_training_samples = 100self.load_ml_model()16 unmodified lines# Training data collectionself.training_data = pd.DataFrame(columns=self.feature_columns + ['target'])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")else:print("Initializing new model")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")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")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")def calculate_features(self, df):"""Calculate technical indicators"""22 unmodified linesreturn dfexcept Exception as e:print(f"Error calculating features: {str(e)}")return Nonedef train_initial_model(self):13 unmodified lines# Evaluate modelpreds = self.model.predict(X_test)accuracy = accuracy_score(y_test, preds)print(f"Initial model trained with accuracy: {accuracy:.2f}")self.save_ml_model()return Trueelse:print(f"Not enough training data ({len(self.training_data)} samples). Need at least {self.min_training_samples}.")return Falsedef predict_direction(self, features):5 unmodified linesfeatures = features[self.feature_columns].values.reshape(1, -1)return self.model.predict(features)[0]except Exception as e:print(f"Prediction error: {str(e)}")return 0def collect_training_sample(self, symbol, exchange, timeframe='1h'):19 unmodified linesnew_row = pd.DataFrame([features])self.training_data = pd.concat([self.training_data, new_row], ignore_index=True)print(f"Collected training sample for {symbol}")if len(self.training_data) % 10 == 0:self.save_training_data()except Exception as e:print(f"Error collecting training sample: {str(e)}")def scan_symbol(self, symbol, exchange, timeframes):"""Scan symbol for trading opportunities"""35 unmodified linesself.alert(symbol, "DOWNTREND", timeframes, price)except Exception as e:print(f"Error scanning {symbol}: {str(e)}")def alert(self, symbol, trend_type, timeframes, current_price):"""Generate alert for detected trend"""message = f"({trend_type}) detected for {symbol} at price {current_price} on {timeframes} at {datetime.now()}"print(message)# 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("--train", help="Run in training mode", action="store_true")args = parser.parse_args()scanner = MLTechnicalScanner(training_mode=args.train)exchange = scanner.exchanges.get(args.exchange.lower())if not exchange:print(f"Exchange {args.exchange} not supported")sys.exit(1)try:markets = exchange.fetch_markets()except Exception as e:print(f"Error fetching markets: {str(e)}")sys.exit(1)symbols = [2 unmodified lines]if not symbols:print(f"No symbols found matching filter {args.filter}")sys.exit(1)if args.train:print(f"Running in training mode for {len(symbols)} symbols")for symbol in symbols:scanner.collect_training_sample(symbol, exchange)if scanner.train_initial_model():print("Training completed successfully")else:print("Not enough data collected for training")sys.exit(0)if not hasattr(scanner.model, 'classes_'):print("Warning: No trained model available. Running with basic scanning only.")timeframes = args.timeframes.split(',')print(f"Scanning {len(symbols)} symbols on timeframes {timeframes}")for symbol in symbols:scanner.scan_symbol(symbol, exchange, timeframes)10 unmodified lines111213141516179 unmodified lines2728293031323316 unmodified lines505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610722 unmodified lines13013113213313413513613713813 unmodified lines1521531541551561571581591601611621631641651661675 unmodified lines17317417517617717817918018119 unmodified lines20120220320420520620720820921021121221321421521621735 unmodified lines2532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573582 unmodified lines36136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140210 unmodified linesfrom sklearn.metrics import accuracy_scoreimport pickleimport warningsimport re# Suppress warningswarnings.filterwarnings('ignore')9 unmodified linesself.model = Noneself.model_file = "technical_ml_model.pkl"self.training_data_file = "training_data.csv"self.results_file = "results.txt"self.min_training_samples = 100self.load_ml_model()16 unmodified lines# Training data collectionself.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)msg = "Loaded trained model from file"print(msg)self.log_result(msg)else: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)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)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)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 linesreturn dfexcept Exception as e:error_msg = f"Error calculating features: {str(e)}"print(error_msg)self.log_result(error_msg)return Nonedef train_initial_model(self):13 unmodified lines# Evaluate modelpreds = self.model.predict(X_test)accuracy = accuracy_score(y_test, preds)msg = f"Initial model trained with accuracy: {accuracy:.2f}"print(msg)self.log_result(msg)self.save_ml_model()return Trueelse: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 Falsedef predict_direction(self, features):5 unmodified linesfeatures = features[self.feature_columns].values.reshape(1, -1)return self.model.predict(features)[0]except Exception as e:error_msg = f"Prediction error: {str(e)}"print(error_msg)self.log_result(error_msg)return 0def collect_training_sample(self, symbol, exchange, timeframe='1h'):19 unmodified linesnew_row = pd.DataFrame([features])self.training_data = pd.concat([self.training_data, new_row], ignore_index=True)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: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 linesself.alert(symbol, "DOWNTREND", timeframes, price)except Exception as 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 {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")returnexchange = 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 entriespattern = 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_pricepercent_change = (price_change / old_price) * 100print(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)}")continueprint("\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=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 comparisonsscanner.init_results_file()exchange = scanner.exchanges.get(args.exchange.lower())if not exchange: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: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:error_msg = f"No symbols found matching filter {args.filter}"print(error_msg)scanner.log_result(error_msg)sys.exit(1)if args.train: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():success_msg = "Training completed successfully"print(success_msg)scanner.log_result(success_msg)else: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_'):warn_msg = "Warning: No trained model available. Running with basic scanning only."print(warn_msg)scanner.log_result(warn_msg)timeframes = args.timeframes.split(',')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)# Add final summaryend_msg = f"\nScan completed at {datetime.now()}"print(end_msg)scanner.log_result(end_msg)
mltechnicalscanner.py+155-26