Remove unused GSheetBatchPusher class and xgboost model file to streamline codebase and eliminate deprecated components.

This commit is contained in:
Simon Moisy committed 2025-06-25 13:11:17 +08:00
1 parent 6c5dcc1183
commit be331ed631
2 files changed
-129

No files matched your search

-128
View File
@@ -1,128 +0,0 @@
import threading
import time
import queue
from google.oauth2.service_account import Credentials
import gspread
import math
import numpy as np
from collections import defaultdict
class GSheetBatchPusher(threading.Thread):
def __init__(self, queue, timestamp, spreadsheet_name, interval=60, logging=None):
super().__init__(daemon=True)
self.queue = queue
self.timestamp = timestamp
self.spreadsheet_name = spreadsheet_name
self.interval = interval
self._stop_event = threading.Event()
self.logging = logging
def run(self):
while not self._stop_event.is_set():
self.push_all()
time.sleep(self.interval)
# Final push on stop
self.push_all()
def stop(self):
self._stop_event.set()
def push_all(self):
batch_results = []
batch_trades = []
while True:
try:
results, trades = self.queue.get_nowait()
batch_results.extend(results)
batch_trades.extend(trades)
except queue.Empty:
break
if batch_results or batch_trades:
self.write_results_per_combination_gsheet(batch_results, batch_trades, self.timestamp, self.spreadsheet_name)
def write_results_per_combination_gsheet(self, results_rows, trade_rows, timestamp, spreadsheet_name="GlimBit Backtest Results"):
scopes = [
"https://www.googleapis.com/auth/spreadsheets",
"https://www.googleapis.com/auth/drive"
]
creds = Credentials.from_service_account_file('credentials/service_account.json', scopes=scopes)
gc = gspread.authorize(creds)
sh = gc.open(spreadsheet_name)
try:
worksheet = sh.worksheet("Results")
except gspread.exceptions.WorksheetNotFound:
worksheet = sh.add_worksheet(title="Results", rows="1000", cols="20")
# Clear the worksheet before writing new results
worksheet.clear()
# Updated fieldnames to match your data rows
fieldnames = [
"timeframe", "stop_loss_pct", "n_trades", "n_stop_loss", "win_rate",
"max_drawdown", "avg_trade", "profit_ratio", "initial_usd", "final_usd"
]
def to_native(val):
if isinstance(val, (np.generic, np.ndarray)):
val = val.item()
if hasattr(val, 'isoformat'):
return val.isoformat()
# Handle inf, -inf, nan
if isinstance(val, float):
if math.isinf(val):
return "∞" if val > 0 else "-∞"
if math.isnan(val):
return ""
return val
# Write header if sheet is empty
if len(worksheet.get_all_values()) == 0:
worksheet.append_row(fieldnames)
for row in results_rows:
values = [to_native(row.get(field, "")) for field in fieldnames]
worksheet.append_row(values)
trades_fieldnames = [
"entry_time", "exit_time", "entry_price", "exit_price", "profit_pct", "type"
]
trades_by_combo = defaultdict(list)
for trade in trade_rows:
tf = trade.get("timeframe")
sl = trade.get("stop_loss_pct")
trades_by_combo[(tf, sl)].append(trade)
for (tf, sl), trades in trades_by_combo.items():
sl_percent = int(round(sl * 100))
sheet_name = f"Trades_{tf}_ST{sl_percent}%"
try:
trades_ws = sh.worksheet(sheet_name)
except gspread.exceptions.WorksheetNotFound:
trades_ws = sh.add_worksheet(title=sheet_name, rows="1000", cols="20")
# Clear the trades worksheet before writing new trades
trades_ws.clear()
if len(trades_ws.get_all_values()) == 0:
trades_ws.append_row(trades_fieldnames)
for trade in trades:
trade_row = [to_native(trade.get(field, "")) for field in trades_fieldnames]
try:
trades_ws.append_row(trade_row)
except gspread.exceptions.APIError as e:
if '429' in str(e):
if self.logging is not None:
self.logging.warning(f"Google Sheets API quota exceeded (429). Please wait one minute. Will retry on next batch push. Sheet: {sheet_name}")
# Re-queue the failed batch for retry
self.queue.put((results_rows, trade_rows))
return # Stop pushing for this batch, will retry next interval
else:
raise
-1
View File
@@ -1 +0,0 @@
{"learner":{"attributes":{"best_iteration":"15","best_score":"6.720108134389e-05"},"feature_names":[],"feature_types":[],"gradient_booster":{"model":{"gbtree_model_param":{"num_parallel_tree":"1","num_trees":"26"},"iteration_indptr":[0,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],"tree_info":[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],"trees":[{"base_weights":[-3.8670794E-12,-1.2762184E-3,4.3431539E-4,-3.9894213E-3,-1.0002927E-3,2.0443962E-4,2.399732E-3,-7.335503E-3,-3.3200998E-3,-1.6589444E-3,-7.3369226E-4,-2.359659E-5,7.121429E-4,2.0261202E-3,5.7618795E-3,-6.589136E-3,-1.0167347E-2,-4.0106303E-3,-2.859653E-3,-2.069192E-3,-1.4351582E-3,-9.593777E-4,-5.8022403E-4,-2.2667211E-4,9.283359E-5,5.3504505E-4,1.0043513E-3,1.6946669E-3,2.8878527E-3,4.3291645E-3,7.194028E-3,-6.0462463E-3,-7.301802E-3,-1.4424558E-2,-8.898965E-3,-4.4059707E-3,-1.0844924E-3,-9.4653416E-4,-2.7118041E-3,-2.2472097E-3,-1.89112E-3,-1.5871366E-3,-1.3084923E-3,-1.0704146E-3,-8.606699E-4,-6.775895E-4,-5.037194E-4,-3.216747E-4,-1.440601E-4,1.759575E-5,2.265892E-4,4.384972E-4,6.508992E-4,8.859183E-4,1.149094E-3,1.505428E-3,1.9974257E-3,2.586768E-3,3.3393602E-3,1.2576575E-3,1.3056449E-3,6.3861436E-3,9.752441E-3,-1.7154933E-3,-1.9069613E-3,-2.6050785E-3,-2.0008117E-3,-3.790425E-3,-5.4830397E-3,-2.4164463E-3,-3.2439483E-3,-1.2794831E-3,-1.3290202E-3,-8.4991683E-4,-7.771017E-4,-7.195334E-4,-6.5145025E-4,-5.962644E-4,-5.528489E-4,-5.050309E-4,-4.568691E-4,-4.1120092E-4,-3.7388413E-4,-3.3835875E-4,-3.0388398E-4,-2.7420704E-4,-2.453918E-4,-2.1776417E-4,-1.9120124E-4,-1.6396039E-4,-1.3826939E-4,-1.1084072E-4,-8.216347E-5,-5.614325E-5,-3.1369636E-5,-3.1354841E-6,2.2107035E-5,5.222811E-5,8.493644E-5,1.16575524E-4,1.4652166E-4,1.7919521E-4,2.1363827E-4,2.4925088E-4,2.8560057E-4,3.2536112E-4,3.6893066E-4,4.1865403E-4,4.8459388E-4,5.6614267E-4,6.488298E-4,7.4328127E-4,8.41465E-4,9.352878E-4,1.0682496E-3,1.7701401E-3,2.0759057E-3,2.634289E-3,3.9918087E-3],"categories":[],"categories_nodes":[],"categories_segments":[],"categories_sizes":[],"default_left":[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],"id":0,"left_children":[1,3,5,7,9,11,13,15,17,19,21,23,25,27,29,31,33,35,37,39,41,43,45,47,49,51,53,55,57,59,61,63,65,67,69,71,-1,-1,73,75,77,79,81,83,85,87,89,91,93,95,97,99,101,103,105,107,109,111,113,-1,-1,115,117,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1],"loss_changes":[1.8017862E0,6.1790013E-1,1.0957562E0,1.7060888E-1,1.3155204E-1,2.5138694E-1,3.1899834E-1,2.6773572E-2,2.0175636E-2,1.9815207E-2,1.8471032E-2,3.5427332E-2,3.482634E-2,6.527841E-2,5.208206E-2,3.8453639E-3,1.4164835E-2,3.9566457E-3,1.6555488E-3,2.4109185E-3,2.6868284E-3,2.365008E-3,2.3643076E-3,4.2852517E-3,9.583905E-3,4.686773E-3,4.3524206E-3,9.454727E-3,8.6224675E-3,2.1725893E-5,2.61873E-2,5.4489076E-4,3.7365109E-3,3.9473176E-3,3.2124668E-3,2.4273992E-5,0E0,0E0,3.6671758E-4,4.3095648E-4,1.7373264E-4,3.901869E-4,2.9300153E-4,3.342256E-4,2.5959313E-4,2.7104467E-4,3.2567978E-4,5.800463E-4,4.969351E-4,9.58928E-4,1.0174084E-3,5.6916475E-4,6.244555E-4,5.0763786E-4,5.938709E-4,1.2246817E-3,1.1536181E-3,9.0011954E-4,1.2360811E-3,0E0,0E0,2.461046E-3,1.0398626E-2,0E0,0E0,0E0,0E0,0E0,0E0,0E0,0E0,0E0,0E0,0E0,0E0,0E0,0E0,0E0,0E0,0E0,0E0,0E0,0E0,0E0,0E0,0E0,0E0,0E0,0E0,0E0,0E0,0E0,0E0,0E0,0E0,0E0,0E0,0E0,0E0,0E0,0E0,0E0,0E0,0E0,0E0,0E0,0E0,0E0,0E0,0E0,0E0,0E0,0E0,0E0,0E0,0E0,0E0,0E0,0E0],"parents":[2147483647,0,0,1,1,2,2,3,3,4,4,5,5,6,6,7,7,8,8,9,9,10,10,11,11,12,12,13,13,14,14,15,15,16,16,17,17,18,18,19,19,20,20,21,21,22,22,23,23,24,24,25,25,26,26,27,27,28,28,29,29,30,30,31,31,32,32,33,33,34,34,35,35,38,38,39,39,40,40,41,41,42,42,43,43,44,44,45,45,46,46,47,47,48,48,49,49,50,50,51,51,52,52,53,53,54,54,55,55,56,56,57,57,58,58,61,61,62,62],"right_children":[2,4,6,8,10,12,14,16,18,20,22,24,26,28,30,32,34,36,38,40,42,44,46,48,50,52,54,56,58,60,62,64,66,68,70,72,-1,-1,74,76,78,80,82,84,86,88,90,92,94,96,98,100,102,104,106,108,110,112,114,-1,-1,116,118,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1],"split_conditions":[-4.2027403E-2,-2.4846634E-1,1.3075246E-1,-5.018099E-1,-1.1910506E-1,3.440461E-2,3.869802E-1,5.4798382E-3,-3.3490157E-1,-1.7547187E-1,-7.685892E-2,-6.51732E-3,7.7357925E-2,2.305668E-1,4.965987E-1,3.6002742E-3,-1.8539244E2,-3.914499E-1,-2.9733706E-1,-2.0401648E-1,-1.438832E-1,-9.60541E-2,-5.9275903E-2,-2.3060042E-2,1.2296796E-2,5.435789E-2,1.01490654E-1,1.756651E-1,2.950238E-1,1.9697219E-3,5.4798382E-3,2.8580925E-3,-1.809319E2,4.910954E-3,7.0174255E1,1.9697219E-3,-1.0844924E-3,-9.4653416E-4,-2.6971948E-1,-2.3109923E-1,-1.9319431E-1,-1Line truncated