Update MLCryptoForecasterAllAssetsTPSL_ParisTime.py
Browse files
MLCryptoForecasterAllAssetsTPSL_ParisTime.py
CHANGED
|
@@ -1,6 +1,7 @@
|
|
| 1 |
import os
|
| 2 |
import pandas as pd
|
| 3 |
import numpy as np
|
|
|
|
| 4 |
from datetime import timedelta
|
| 5 |
from binance.client import Client
|
| 6 |
from sklearn.model_selection import train_test_split
|
|
@@ -9,16 +10,34 @@ from sklearn.metrics import classification_report
|
|
| 9 |
import ta
|
| 10 |
import pytz
|
| 11 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
# Function to log results to both console and file
|
| 13 |
# Blank lines are added after each asset block explicitly
|
| 14 |
-
|
| 15 |
def log_results(message, filename="predictions_results.txt"):
|
| 16 |
print(message)
|
| 17 |
with open(filename, "a") as f:
|
| 18 |
f.write(message + "\n")
|
| 19 |
|
| 20 |
# Convert UTC timestamp to Europe/Paris timezone
|
| 21 |
-
|
| 22 |
def convert_to_paris_time(utc_time):
|
| 23 |
paris_tz = pytz.timezone('Europe/Paris')
|
| 24 |
utc_time = utc_time.replace(tzinfo=pytz.utc)
|
|
@@ -29,8 +48,7 @@ def convert_to_paris_time(utc_time):
|
|
| 29 |
client = Client()
|
| 30 |
|
| 31 |
# Settings
|
| 32 |
-
|
| 33 |
-
result_file = "predictions_results.txt"
|
| 34 |
|
| 35 |
# Delete the results file if it exists for a fresh start
|
| 36 |
if os.path.exists(result_file):
|
|
@@ -67,14 +85,16 @@ def optimize_tp_sl(df, signals, side, pgrid, lgrid):
|
|
| 67 |
|
| 68 |
# Main loop: process each symbol
|
| 69 |
for symbol in symbols:
|
| 70 |
-
log_results(f"=== {symbol} ===", result_file)
|
| 71 |
|
| 72 |
# Load or download historical data
|
| 73 |
-
data_file = f"{symbol}
|
| 74 |
if os.path.exists(data_file):
|
| 75 |
df = pd.read_csv(data_file, index_col=0, parse_dates=True)
|
| 76 |
last_ts = df.index[-1]
|
| 77 |
-
start = (last_ts + timedelta(
|
|
|
|
|
|
|
| 78 |
new = client.get_historical_klines(symbol, interval, start)
|
| 79 |
if new:
|
| 80 |
new_df = pd.DataFrame(new, columns=[
|
|
@@ -120,10 +140,10 @@ for symbol in symbols:
|
|
| 120 |
df.dropna(inplace=True)
|
| 121 |
|
| 122 |
# Label signals based on Ichimoku cloud
|
| 123 |
-
df['signal'] = np.select(
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
|
| 128 |
# Train/test split
|
| 129 |
features = [c for c in df.columns if c not in ['open','high','low','close','volume','signal']]
|
|
@@ -162,4 +182,4 @@ for symbol in symbols:
|
|
| 162 |
f.write("\n")
|
| 163 |
|
| 164 |
# End of processing
|
| 165 |
-
log_results("All assets processed.", result_file)
|
|
|
|
| 1 |
import os
|
| 2 |
import pandas as pd
|
| 3 |
import numpy as np
|
| 4 |
+
import argparse
|
| 5 |
from datetime import timedelta
|
| 6 |
from binance.client import Client
|
| 7 |
from sklearn.model_selection import train_test_split
|
|
|
|
| 10 |
import ta
|
| 11 |
import pytz
|
| 12 |
|
| 13 |
+
# Parse command-line arguments for timeframe
|
| 14 |
+
parser = argparse.ArgumentParser(description="Binance Trend Forecaster with adjustable timeframe")
|
| 15 |
+
parser.add_argument("--interval", type=str, default="4h",
|
| 16 |
+
choices=["1m","3m","5m","15m","30m","1h","4h","1d"],
|
| 17 |
+
help="Time interval for klines (e.g. '1h', '4h', '1d')")
|
| 18 |
+
args = parser.parse_args()
|
| 19 |
+
|
| 20 |
+
# Map user-friendly intervals to Binance API constants
|
| 21 |
+
interval_map = {
|
| 22 |
+
"1m": Client.KLINE_INTERVAL_1MINUTE,
|
| 23 |
+
"3m": Client.KLINE_INTERVAL_3MINUTE,
|
| 24 |
+
"5m": Client.KLINE_INTERVAL_5MINUTE,
|
| 25 |
+
"15m": Client.KLINE_INTERVAL_15MINUTE,
|
| 26 |
+
"30m": Client.KLINE_INTERVAL_30MINUTE,
|
| 27 |
+
"1h": Client.KLINE_INTERVAL_1HOUR,
|
| 28 |
+
"4h": Client.KLINE_INTERVAL_4HOUR,
|
| 29 |
+
"1d": Client.KLINE_INTERVAL_1DAY
|
| 30 |
+
}
|
| 31 |
+
interval = interval_map[args.interval]
|
| 32 |
+
|
| 33 |
# Function to log results to both console and file
|
| 34 |
# Blank lines are added after each asset block explicitly
|
|
|
|
| 35 |
def log_results(message, filename="predictions_results.txt"):
|
| 36 |
print(message)
|
| 37 |
with open(filename, "a") as f:
|
| 38 |
f.write(message + "\n")
|
| 39 |
|
| 40 |
# Convert UTC timestamp to Europe/Paris timezone
|
|
|
|
| 41 |
def convert_to_paris_time(utc_time):
|
| 42 |
paris_tz = pytz.timezone('Europe/Paris')
|
| 43 |
utc_time = utc_time.replace(tzinfo=pytz.utc)
|
|
|
|
| 48 |
client = Client()
|
| 49 |
|
| 50 |
# Settings
|
| 51 |
+
result_file = f"predictions_results_{args.interval}.txt"
|
|
|
|
| 52 |
|
| 53 |
# Delete the results file if it exists for a fresh start
|
| 54 |
if os.path.exists(result_file):
|
|
|
|
| 85 |
|
| 86 |
# Main loop: process each symbol
|
| 87 |
for symbol in symbols:
|
| 88 |
+
log_results(f"=== {symbol} ({args.interval}) ===", result_file)
|
| 89 |
|
| 90 |
# Load or download historical data
|
| 91 |
+
data_file = f"{symbol}_data_{args.interval}_full.csv"
|
| 92 |
if os.path.exists(data_file):
|
| 93 |
df = pd.read_csv(data_file, index_col=0, parse_dates=True)
|
| 94 |
last_ts = df.index[-1]
|
| 95 |
+
start = (last_ts + timedelta(**{
|
| 96 |
+
'minutes':1 if args.interval=='1m' else 3 if args.interval=='3m' else 5 if args.interval=='5m' else 15 if args.interval=='15m' else 30 if args.interval=='30m' else 60 if args.interval=='1h' else 240 if args.interval=='4h' else 1440
|
| 97 |
+
})).strftime("%d %B %Y %H:%M:%S")
|
| 98 |
new = client.get_historical_klines(symbol, interval, start)
|
| 99 |
if new:
|
| 100 |
new_df = pd.DataFrame(new, columns=[
|
|
|
|
| 140 |
df.dropna(inplace=True)
|
| 141 |
|
| 142 |
# Label signals based on Ichimoku cloud
|
| 143 |
+
df['signal'] = np.select([
|
| 144 |
+
(df['close'] > df['span_a']) & (df['close'] > df['span_b']),
|
| 145 |
+
(df['close'] < df['span_a']) & (df['close'] < df['span_b'])
|
| 146 |
+
], [1, 0], default=-1)
|
| 147 |
|
| 148 |
# Train/test split
|
| 149 |
features = [c for c in df.columns if c not in ['open','high','low','close','volume','signal']]
|
|
|
|
| 182 |
f.write("\n")
|
| 183 |
|
| 184 |
# End of processing
|
| 185 |
+
log_results("All assets processed.", result_file)
|