Repository navigation
Expand file tree
/
Copy pathmain_api.py
More file actions
1737 lines (1522 loc) · 104 KB
/
Copy pathmain_api.py
File metadata and controls
1737 lines (1522 loc) · 104 KB
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# -*- coding: utf-8 -*-
import os
import pandas as pd
import numpy as np
import time
import ccxt
# import ccxt.async_support as ccxt_async
import logging
from datetime import datetime
from arch import arch_model
from sklearn.preprocessing import StandardScaler
# Silence TensorFlow warnings BEFORE importing Keras/TF
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3'
logging.getLogger('tensorflow').setLevel(logging.ERROR)
# --- LSTM DISABLED: Comment out TF/Keras imports ---
# import tensorflow as tf
# from tensorflow.keras.models import Sequential
# from tensorflow.keras.layers import LSTM, Dense, Dropout
# from tensorflow.keras.callbacks import EarlyStopping
# from tensorflow.keras.optimizers import Adam
# from tensorflow.keras.layers import Input
# --- END LSTM DISABLED ---
import warnings
import openai
import json
import re
import asyncio
from fastapi import FastAPI, HTTPException, Body, Query
from pydantic import BaseModel, Field, ConfigDict
from typing import Dict, Any, List, Optional
import uvicorn
from dotenv import load_dotenv
import sys
# --- Logging Configuration (Ensure this runs BEFORE any logging happens) ---
# Set level to INFO by default, easy to change to DEBUG if needed
LOG_LEVEL = os.getenv("LOG_LEVEL", "INFO").upper()
# Use a stream handler to force output to console, overriding potential Uvicorn capture
log_formatter = logging.Formatter('%(asctime)s - %(levelname)s - [%(funcName)s] - %(message)s')
log_handler = logging.StreamHandler(sys.stdout) # Force output to stdout
log_handler.setFormatter(log_formatter)
logger = logging.getLogger()
logger.setLevel(LOG_LEVEL)
# Remove existing handlers if any to avoid duplicates
if logger.hasHandlers():
logger.handlers.clear()
logger.addHandler(log_handler)
# --- Test Log Message ---
logger.critical("--- Logging Initialized (Level: %s). Logs should now appear on the console. ---", LOG_LEVEL)
# --- LSTM DISABLED: Remove TF_LOCK or keep if other TF usage exists (GARCH doesn't use TF) ---
# TF_LOCK = asyncio.Lock()
# logging.info("TensorFlow Lock initialized.") # Commented out
# --- Configuration ---
warnings.filterwarnings("ignore", category=DeprecationWarning)
warnings.filterwarnings("ignore", category=FutureWarning)
warnings.filterwarnings("ignore", message="ConvergenceWarning", category=UserWarning)
load_dotenv()
# --- LSTM DISABLED: Configuration no longer needed ---
# LSTM_TIME_STEPS = 60
# LSTM_EPOCHS = 15
# LSTM_BATCH_SIZE = 64
# --- END LSTM DISABLED ---
DEFAULT_MAX_CONCURRENT_TASKS = 5
# --- CCXT Initialization (Unchanged) ---
binance_futures = None
try:
binance_futures = ccxt.binanceusdm({
'enableRateLimit': True,
'options': { 'adjustForTimeDifference': True },
'timeout': 30000,
'rateLimit': 200
})
logger.info("CCXT Binance Futures instance created.")
except Exception as e:
logger.error(f"Error initializing CCXT: {e}", exc_info=True)
binance_futures = None
# --- Load Markets Function (Unchanged) ---
async def load_exchange_markets(exchange):
if not exchange: return False
try:
logger.info(f"Attempting to load markets for {exchange.id}...")
markets = await asyncio.to_thread(exchange.load_markets, True)
if markets:
logger.info(f"Successfully loaded {len(markets)} markets for {exchange.id}.")
return True
else:
logger.warning(f"Market loading returned empty for {exchange.id}.")
return False
except (ccxt.NetworkError, ccxt.ExchangeNotAvailable) as e:
logger.error(f"Failed to load markets for {exchange.id} due to network error: {e}", exc_info=False)
return False
except ccxt.ExchangeError as e:
logger.error(f"Failed to load markets for {exchange.id} due to exchange error: {e}", exc_info=False)
return False
except Exception as e:
logger.error(f"Unexpected error loading markets for {exchange.id}: {e}", exc_info=True)
return False
# --- OpenAI Initialization (Unchanged) ---
openai_client = None
OPENAI_API_KEY = os.getenv("OPENAI_API_KEY") # Use getenv
if not OPENAI_API_KEY:
logger.warning("OPENAI_API_KEY environment variable not found. GPT features will be disabled.")
else:
try:
openai_client = openai.OpenAI(api_key=OPENAI_API_KEY)
logger.info("OpenAI client initialized.")
except Exception as e:
logger.error(f"Error initializing OpenAI: {e}", exc_info=True)
openai_client = None
# --- Test OpenAI Connection (Unchanged) ---
async def test_openai_connection(client):
if not client: return
try:
await asyncio.to_thread(client.models.list)
logger.info("OpenAI connection test successful.")
except Exception as e:
logger.error(f"OpenAI connection test failed: {e}")
# --- FastAPI App (Unchanged) ---
app = FastAPI(
title="Crypto Trading Analysis & Scanning API",
description="API for technical analysis, GPT-driven strategy evaluation, backtesting, and market scanning.",
version="1.3.0 Simplified" # Version bump
)
# --- Pydantic Models ---
# Tickers (Unchanged)
class TickerRequest(BaseModel): pass
class TickersResponse(BaseModel): tickers: List[str]
# AnalysisRequest - LSTM params removed
class AnalysisRequest(BaseModel):
symbol: str = Field(..., example="BTC/USDT:USDT")
timeframe: str = Field(default="1h", example="1h")
lookback: int = Field(default=1000, ge=250)
accountBalance: float = Field(default=1000.0, ge=0)
maxLeverage: float = Field(default=10.0, ge=1)
# --- LSTM DISABLED ---
# lstm_time_steps: int = Field(default=LSTM_TIME_STEPS, ge=10)
# lstm_epochs: int = Field(default=LSTM_EPOCHS, ge=1, le=100)
# lstm_batch_size: int = Field(default=LSTM_BATCH_SIZE, ge=16)
# --- END LSTM DISABLED ---
# IndicatorsData (Unchanged)
class IndicatorsData(BaseModel):
RSI: Optional[float] = None; ATR: Optional[float] = None; SMA_50: Optional[float] = None; SMA_200: Optional[float] = None
EMA_12: Optional[float] = None; EMA_26: Optional[float] = None; MACD: Optional[float] = None; Signal_Line: Optional[float] = None
Bollinger_Upper: Optional[float] = None; Bollinger_Middle: Optional[float] = None; Bollinger_Lower: Optional[float] = None
Momentum: Optional[float] = None; Stochastic_K: Optional[float] = None; Stochastic_D: Optional[float] = None
Williams_R: Optional[float] = Field(None, alias="Williams_%R")
ADX: Optional[float] = None; CCI: Optional[float] = None; OBV: Optional[float] = None; returns: Optional[float] = None
model_config = ConfigDict(populate_by_name=True, extra='allow')
# ModelOutputData - LSTM removed
class ModelOutputData(BaseModel):
# --- LSTM DISABLED ---
# lstmForecast: Optional[float] = None
# --- END LSTM DISABLED ---
garchVolatility: Optional[float] = None
var95: Optional[float] = None
# GptAnalysisText - LSTM analysis removed
class GptAnalysisText(BaseModel):
# --- LSTM DISABLED ---
# lstm_analysis: Optional[str] = None
# --- END LSTM DISABLED ---
technical_analysis: Optional[str] = None # Justification for evaluation
risk_assessment: Optional[str] = None
market_outlook: Optional[str] = None
raw_text: Optional[str] = None
signal_evaluation: Optional[str] = None # Added field for evaluation summary
# GptTradingParams (Unchanged, but interpretation changes)
class GptTradingParams(BaseModel):
optimal_entry: Optional[float] = None; stop_loss: Optional[float] = None; take_profit: Optional[float] = None
trade_direction: Optional[str] = None; leverage: Optional[int] = Field(None, ge=1)
position_size_usd: Optional[float] = Field(None, ge=0); estimated_profit: Optional[float] = None
confidence_score: Optional[float] = Field(None, ge=0, le=1) # Confidence in the *evaluated* technical signal
# Backtest Models (Unchanged)
class BacktestTradeAnalysis(BaseModel):
total_trades: int = 0; winning_trades: int = 0; losing_trades: int = 0; win_rate: Optional[float] = None
avg_profit: Optional[float] = None; avg_loss: Optional[float] = None; profit_factor: Optional[float] = None
total_profit: Optional[float] = None; largest_win: Optional[float] = None; largest_loss: Optional[float] = None
average_trade_duration: Optional[float] = None
class BacktestResultsData(BaseModel):
strategy_score: Optional[float] = Field(None, ge=0, le=1); trade_analysis: Optional[BacktestTradeAnalysis] = None
recommendation: Optional[str] = None; warnings: List[str] = Field([])
# AnalysisResponse - Updated ModelOutput and GptAnalysis
class AnalysisResponse(BaseModel):
symbol: str; timeframe: str; currentPrice: Optional[float] = None
indicators: Optional[IndicatorsData] = None
modelOutput: Optional[ModelOutputData] = None # LSTM removed
gptParams: Optional[GptTradingParams] = None
gptAnalysis: Optional[GptAnalysisText] = None # LSTM removed, evaluation added
backtest: Optional[BacktestResultsData] = None
error: Optional[str] = None
# ScanRequest - LSTM params removed
class ScanRequest(BaseModel):
# Core Scan Params (Match user JSON where applicable)
ticker_start_index: Optional[int] = Field(default=0, ge=0, description="0-based index to start scanning.")
ticker_end_index: Optional[int] = Field(default=None, ge=0, description="0-based index to end scanning (exclusive).")
timeframe: str = Field(default="1m", description="Candle timeframe (e.g., '1m', '5m', '1h').") # Default '1m' from user JSON
max_tickers: Optional[int] = Field(default=100, description="Maximum tickers per run.") # Default 100 from user JSON
top_n: int = Field(default=10, ge=1, description="Number of top results to return.")
# Core Filters (Match user JSON)
min_gpt_confidence: float = Field(default=0.65, ge=0, le=1) # From user JSON
min_backtest_score: float = Field(default=0.60, ge=0, le=1) # From user JSON
trade_direction: Optional[str] = Field(default=None, pattern="^(long|short)$") # null in user JSON -> default None
# --- NEW BTC TREND FILTER ---
filter_by_btc_trend: Optional[bool] = Field(default=True, description="If True, only show LONG signals if BTC is in Uptrend, and SHORT signals if BTC is in Downtrend (based on Price/SMA50/SMA200).")
# --- END NEW BTC TREND FILTER ---
# --- New Backtest Filters (from user JSON) ---
min_backtest_trades: Optional[int] = Field(default=15, ge=0) # From user JSON
min_backtest_win_rate: Optional[float] = Field(default=0.52, ge=0, le=1) # From user JSON
min_backtest_profit_factor: Optional[float] = Field(default=1.5, ge=0) # From user JSON
# --- New GPT/Risk Filter (from user JSON) ---
min_risk_reward_ratio: Optional[float] = Field(default=1.8, ge=0) # From user JSON
# --- New Indicator Filters (from user JSON) ---
min_adx: Optional[float] = Field(default=25.0, ge=0) # From user JSON
require_sma_alignment: Optional[bool] = Field(default=True) # From user JSON
# Analysis Config (Match user JSON)
lookback: int = Field(default=2000, ge=250) # From user JSON
accountBalance: float = Field(default=5000.0, ge=0) # From user JSON
maxLeverage: float = Field(default=20.0, ge=1) # From user JSON
max_concurrent_tasks: int = Field(default=16, ge=1) # From user JSON
# ScanResultItem (Unchanged)
class ScanResultItem(BaseModel):
rank: int; symbol: str; timeframe: str; currentPrice: Optional[float] = None
gptConfidence: Optional[float] = None; backtestScore: Optional[float] = None; combinedScore: Optional[float] = None
tradeDirection: Optional[str] = None; optimalEntry: Optional[float] = None; stopLoss: Optional[float] = None
takeProfit: Optional[float] = None; gptAnalysisSummary: Optional[str] = None # Will now contain evaluation summary
# ScanResponse (Unchanged structure, data reflects changes)
class ScanResponse(BaseModel):
scan_parameters: ScanRequest; total_tickers_attempted: int; total_tickers_succeeded: int
ticker_start_index: Optional[int] = Field(default=0, ge=0); ticker_end_index: Optional[int] = Field(default=None, ge=0)
total_opportunities_found: int; top_opportunities: List[ScanResultItem]
errors: Dict[str, str] = Field(default={})
# --- Helper Functions ---
# --- Data Fetcher (Unchanged) ---
def get_real_time_data(symbol: str, timeframe: str = "1d", limit: int = 1000) -> pd.DataFrame:
"""Fetch OHLCV data. Raises exceptions on failure."""
logger.debug(f"[{symbol}] Attempting to fetch {limit} candles for timeframe {timeframe}")
if binance_futures is None: raise ConnectionError("CCXT exchange instance is not available.")
if not binance_futures.markets:
logger.warning(f"[{symbol}] Markets not loaded, attempting synchronous load...")
try: binance_futures.load_markets(True); logger.info(f"[{symbol}] Markets loaded successfully (sync).")
except Exception as e: raise ConnectionError(f"Failed to load markets synchronously: {e}") from e
try:
ohlcv = binance_futures.fetch_ohlcv(symbol, timeframe=timeframe, limit=limit)
if not ohlcv:
logger.warning(f"[{symbol}] No OHLCV data returned from fetch_ohlcv.")
return pd.DataFrame()
df = pd.DataFrame(ohlcv, columns=['timestamp', 'open', 'high', 'low', 'close', 'volume'])
df['timestamp'] = pd.to_datetime(df['timestamp'], unit='ms'); df.set_index('timestamp', inplace=True)
df = df.apply(pd.to_numeric, errors='coerce')
# Keep rows with valid price/volume, even if some indicators are NaN later
df.dropna(subset=['open', 'high', 'low', 'close', 'volume'], inplace=True)
if df.empty: logger.warning(f"[{symbol}] DataFrame became empty after type conversion/NaN drop.")
else: logger.debug(f"[{symbol}] Fetched {len(df)} valid candles.")
return df
except ccxt.BadSymbol as e:
logger.error(f"[{symbol}] Invalid symbol error: {e}")
raise ValueError(f"Invalid symbol '{symbol}'") from e
except ccxt.RateLimitExceeded as e:
logger.warning(f"[{symbol}] Rate limit exceeded: {e}")
raise ConnectionAbortedError(f"Rate limit exceeded") from e
except ccxt.NetworkError as e:
logger.error(f"[{symbol}] Network error: {e}")
raise ConnectionError(f"Network error fetching {symbol}") from e
except ccxt.AuthenticationError as e:
logger.error(f"[{symbol}] Authentication error: {e}")
raise PermissionError("CCXT Authentication Error") from e
except Exception as e:
logger.error(f"[{symbol}] Unexpected error fetching data: {e}", exc_info=True)
raise RuntimeError(f"Failed to fetch data for {symbol}") from e
# --- Indicator Functions (compute_*, apply_technical_indicators - Unchanged) ---
def compute_rsi(series, window=14):
delta = series.diff(); gain = delta.where(delta > 0, 0.0).fillna(0); loss = -delta.where(delta < 0, 0.0).fillna(0)
avg_gain = gain.ewm(com=window - 1, min_periods=window).mean(); avg_loss = loss.ewm(com=window - 1, min_periods=window).mean()
rs = avg_gain / avg_loss.replace(0, np.nan); rsi = 100.0 - (100.0 / (1.0 + rs)); return rsi.fillna(100) # Fill NaN with 100 (or 0?) maybe fillna(50) better? Let's keep 100 for now.
def compute_atr(df, window=14):
high_low = df['high'] - df['low']; high_close = abs(df['high'] - df['close'].shift()); low_close = abs(df['low'] - df['close'].shift())
tr = pd.concat([high_low, high_close, low_close], axis=1).max(axis=1, skipna=False); atr = tr.ewm(com=window - 1, min_periods=window).mean(); return atr
def compute_bollinger_bands(series, window=20):
sma = series.rolling(window=window, min_periods=window).mean(); std = series.rolling(window=window, min_periods=window).std()
upper_band = sma + 2 * std; lower_band = sma - 2 * std; return upper_band, sma, lower_band
def compute_stochastic_oscillator(df, window=14, smooth_k=3):
lowest_low = df['low'].rolling(window=window, min_periods=window).min(); highest_high = df['high'].rolling(window=window, min_periods=window).max()
range_hh_ll = (highest_high - lowest_low).replace(0, np.nan); k_percent = 100 * ((df['close'] - lowest_low) / range_hh_ll)
d_percent = k_percent.rolling(window=smooth_k, min_periods=smooth_k).mean(); return k_percent, d_percent
def compute_williams_r(df, window=14):
highest_high = df['high'].rolling(window=window, min_periods=window).max(); lowest_low = df['low'].rolling(window=window, min_periods=window).min()
range_ = (highest_high - lowest_low).replace(0, np.nan); williams_r = -100 * ((highest_high - df['close']) / range_); return williams_r
def compute_adx(df, window=14):
df_adx = df.copy(); df_adx['H-L'] = df_adx['high'] - df_adx['low']; df_adx['H-PC'] = abs(df_adx['high'] - df_adx['close'].shift(1))
df_adx['L-PC'] = abs(df_adx['low'] - df_adx['close'].shift(1)); df_adx['TR_calc'] = df_adx[['H-L', 'H-PC', 'L-PC']].max(axis=1)
df_adx['DMplus'] = np.where((df_adx['high'] - df_adx['high'].shift(1)) > (df_adx['low'].shift(1) - df_adx['low']), df_adx['high'] - df_adx['high'].shift(1), 0)
df_adx['DMplus'] = np.where(df_adx['DMplus'] < 0, 0, df_adx['DMplus'])
df_adx['DMminus'] = np.where((df_adx['low'].shift(1) - df_adx['low']) > (df_adx['high'] - df_adx['high'].shift(1)), df_adx['low'].shift(1) - df_adx['low'], 0)
df_adx['DMminus'] = np.where(df_adx['DMminus'] < 0, 0, df_adx['DMminus'])
alpha = 1 / window; TR_smooth = df_adx['TR_calc'].ewm(alpha=alpha, adjust=False, min_periods=window).mean()
DMplus_smooth = df_adx['DMplus'].ewm(alpha=alpha, adjust=False, min_periods=window).mean(); DMminus_smooth = df_adx['DMminus'].ewm(alpha=alpha, adjust=False, min_periods=window).mean()
DIplus = 100 * (DMplus_smooth / TR_smooth.replace(0, np.nan)).fillna(0); DIminus = 100 * (DMminus_smooth / TR_smooth.replace(0, np.nan)).fillna(0)
DIsum = (DIplus + DIminus).replace(0, np.nan); DX = 100 * (abs(DIplus - DIminus) / DIsum); ADX = DX.ewm(alpha=alpha, adjust=False, min_periods=window).mean(); return ADX
def compute_cci(df, window=20):
tp = (df['high'] + df['low'] + df['close']) / 3; sma = tp.rolling(window=window, min_periods=window).mean()
mad = tp.rolling(window=window, min_periods=window).apply(lambda x: np.nanmean(np.abs(x - np.nanmean(x))), raw=True)
cci = (tp - sma) / (0.015 * mad.replace(0, np.nan)); return cci
def compute_obv(df):
obv = (np.sign(df['close'].diff()) * df['volume']).fillna(0).cumsum(); return obv
def apply_technical_indicators(df: pd.DataFrame) -> pd.DataFrame:
"""Apply technical indicators."""
df_copy = df.copy()
# Ensure 'close' is float before calculations
df_copy['close'] = df_copy['close'].astype(float)
df_copy['returns'] = np.log(df_copy['close'] / df_copy['close'].shift(1)).fillna(0)
df_len = len(df_copy)
symbol_log = df_copy.get('symbol', 'Unknown') # Get symbol if added earlier
logger.debug(f"[{symbol_log}] Applying indicators to {df_len} candles.")
def assign_if_enough_data(col_name, min_len_needed, calculation_lambda):
if df_len >= min_len_needed:
try: df_copy[col_name] = calculation_lambda()
except Exception as e: logger.error(f"[{symbol_log}] Error calculating {col_name}: {e}", exc_info=False); df_copy[col_name] = np.nan
else:
logger.debug(f"[{symbol_log}] Skipping {col_name}, need {min_len_needed}, got {df_len}.")
df_copy[col_name] = np.nan
assign_if_enough_data('SMA_50', 50, lambda: df_copy['close'].rolling(window=50, min_periods=50).mean())
assign_if_enough_data('SMA_200', 200, lambda: df_copy['close'].rolling(window=200, min_periods=200).mean())
assign_if_enough_data('EMA_12', 26, lambda: df_copy['close'].ewm(span=12, adjust=False, min_periods=12).mean())
assign_if_enough_data('EMA_26', 26, lambda: df_copy['close'].ewm(span=26, adjust=False, min_periods=26).mean())
# MACD requires EMA_12 and EMA_26
if df_len >= 26 and 'EMA_12' in df_copy and 'EMA_26' in df_copy and df_copy['EMA_12'].notna().any() and df_copy['EMA_26'].notna().any():
df_copy['MACD'] = df_copy['EMA_12'] - df_copy['EMA_26']
# Signal line requires MACD
if df_len >= 35 and 'MACD' in df_copy and df_copy['MACD'].notna().any():
assign_if_enough_data('Signal_Line', 35, lambda: df_copy['MACD'].ewm(span=9, adjust=False, min_periods=9).mean())
else:
logger.debug(f"[{symbol_log}] Skipping Signal_Line (req MACD/35 candles).")
df_copy['Signal_Line'] = np.nan
else:
logger.debug(f"[{symbol_log}] Skipping MACD/Signal_Line (req EMAs/26 candles).")
df_copy['MACD'], df_copy['Signal_Line'] = np.nan, np.nan
assign_if_enough_data('RSI', 15, lambda: compute_rsi(df_copy['close'], window=14))
assign_if_enough_data('ATR', 15, lambda: compute_atr(df_copy, window=14))
if df_len >= 21:
try:
upper, middle, lower = compute_bollinger_bands(df_copy['close'], window=20)
df_copy['Bollinger_Upper'], df_copy['Bollinger_Middle'], df_copy['Bollinger_Lower'] = upper, middle, lower
except Exception as e:
logger.error(f"[{symbol_log}] Error calculating Bollinger Bands: {e}", exc_info=False)
df_copy['Bollinger_Upper'], df_copy['Bollinger_Middle'], df_copy['Bollinger_Lower'] = np.nan, np.nan, np.nan
else:
logger.debug(f"[{symbol_log}] Skipping Bollinger Bands (req 21 candles).")
df_copy['Bollinger_Upper'], df_copy['Bollinger_Middle'], df_copy['Bollinger_Lower'] = np.nan, np.nan, np.nan
assign_if_enough_data('Momentum', 11, lambda: df_copy['close'] - df_copy['close'].shift(10))
if df_len >= 17:
try:
k, d = compute_stochastic_oscillator(df_copy, window=14, smooth_k=3)
df_copy['Stochastic_K'], df_copy['Stochastic_D'] = k, d
except Exception as e:
logger.error(f"[{symbol_log}] Error calculating Stochastic: {e}", exc_info=False)
df_copy['Stochastic_K'], df_copy['Stochastic_D'] = np.nan, np.nan
else:
logger.debug(f"[{symbol_log}] Skipping Stochastic (req 17 candles).")
df_copy['Stochastic_K'], df_copy['Stochastic_D'] = np.nan, np.nan
assign_if_enough_data('Williams_%R', 15, lambda: compute_williams_r(df_copy, window=14))
assign_if_enough_data('ADX', 28, lambda: compute_adx(df_copy, window=14))
assign_if_enough_data('CCI', 21, lambda: compute_cci(df_copy, window=20))
assign_if_enough_data('OBV', 2, lambda: compute_obv(df_copy))
logger.debug(f"[{symbol_log}] Finished applying indicators.")
return df_copy
# --- Statistical Models (GARCH, VaR - LSTM functions removed) ---
def fit_garch_model(returns: pd.Series, symbol_log: str = "Unknown") -> Optional[float]:
"""Fit GARCH(1,1) model. Returns NEXT PERIOD conditional volatility."""
valid_returns = returns.dropna() * 100
logger.debug(f"[{symbol_log}] GARCH input len: {len(valid_returns)}")
if len(valid_returns) < 50:
logger.warning(f"[{symbol_log}] Skipping GARCH, need 50 returns, got {len(valid_returns)}.")
return None
try:
# Model definition uses upgraded GARCH name
am = arch_model(valid_returns, vol='GARCH', p=1, q=1, dist='Normal')
# Suppress convergence warnings during fit
with warnings.catch_warnings():
warnings.simplefilter("ignore")
res = am.fit(update_freq=0, disp='off', show_warning=False)
if res.convergence_flag == 0:
forecasts = res.forecast(horizon=1, reindex=False)
cond_vol_forecast = np.sqrt(forecasts.variance.iloc[-1, 0]) / 100.0 # Convert back from variance %^2
logger.debug(f"[{symbol_log}] GARCH fit successful. Forecast Vol: {cond_vol_forecast:.4f}")
return float(cond_vol_forecast) if np.isfinite(cond_vol_forecast) else None
else:
logger.warning(f"[{symbol_log}] GARCH did not converge (Flag: {res.convergence_flag}).")
return None
except Exception as e:
logger.error(f"[{symbol_log}] GARCH fitting error: {e}", exc_info=False) # Keep logs cleaner
return None
def calculate_var(returns: pd.Series, confidence_level: float = 0.95, symbol_log: str = "Unknown") -> Optional[float]:
"""Calculate Value at Risk (VaR) at specified confidence level."""
valid_returns = returns.dropna()
logger.debug(f"[{symbol_log}] VaR input len: {len(valid_returns)}")
if len(valid_returns) < 20:
logger.warning(f"[{symbol_log}] Skipping VaR, need 20 returns, got {len(valid_returns)}.")
return None
try:
var_value = np.percentile(valid_returns, (1.0 - confidence_level) * 100.0)
logger.debug(f"[{symbol_log}] VaR calculated: {var_value:.4f} at {confidence_level*100:.0f}% confidence.")
# VaR is usually negative, representing loss
return float(var_value) if np.isfinite(var_value) else None
except Exception as e:
logger.error(f"[{symbol_log}] Error calculating VaR: {e}", exc_info=False)
return None
# --- LSTM Functions Removed ---
# def prepare_lstm_data(...)
# def build_lstm_model(...)
# def train_lstm_model(...)
# def forecast_with_lstm(...)
# --- END LSTM REMOVED ---
# --- GPT Integration (Prompt Heavily Modified) ---
def gpt_generate_trading_parameters(
df_with_indicators: pd.DataFrame,
symbol: str,
timeframe: str,
account_balance: float,
max_leverage: float,
garch_volatility: Optional[float],
var95: Optional[float],
technically_derived_direction: str,
min_requested_rr: Optional[float] # NEW: Pass the direction from RSI check
) -> str:
"""Generate trading parameters using GPT to EVALUATE a technical signal."""
log_prefix = f"[{symbol} ({timeframe}) GPT]"
if openai_client is None:
logger.warning(f"{log_prefix} OpenAI client not available.")
return json.dumps({"error": "OpenAI client not available"})
df_valid = df_with_indicators.dropna(subset=['close', 'RSI', 'ATR']) # Ensure core indicators are present
if df_valid.empty:
logger.warning(f"{log_prefix} No valid data (Close/RSI/ATR) for GPT.")
return json.dumps({"error": "Insufficient indicator data for GPT"})
latest_data = df_valid.iloc[-1].to_dict()
technical_indicators = {}
# Extract finite indicators for context
for field_name, model_field in IndicatorsData.model_fields.items():
key_in_df = model_field.alias or field_name
value = latest_data.get(key_in_df)
if pd.notna(value) and np.isfinite(value):
if abs(value) >= 1e4 or (abs(value) < 1e-4 and value != 0):
technical_indicators[key_in_df] = f"{value:.3e}"
else:
technical_indicators[key_in_df] = round(float(value), 4)
current_price = latest_data.get('close')
if current_price is None or not np.isfinite(current_price):
logger.error(f"{log_prefix} Missing current price for GPT context")
return json.dumps({"error": "Missing current price for GPT context"})
current_price = round(float(current_price), 4)
garch_vol_str = f"{garch_volatility:.4%}" if garch_volatility is not None else "N/A"
var95_str = f"{var95:.4%}" if var95 is not None else "N/A"
# Prepare market context for GPT
market_info = {
"symbol": symbol,
"timeframe": timeframe,
"current_price": current_price,
"garch_forecast_volatility": garch_vol_str,
"value_at_risk_95": var95_str,
"key_technical_indicators": technical_indicators,
"potential_signal_direction": technically_derived_direction # Include the signal
}
data_json = json.dumps(market_info, indent=2)
logger.debug(f"{log_prefix} Data prepared for GPT:\n{data_json}")
# --- REVISED GPT PROMPT ---
prompt = f"""You are a cryptocurrency trading analyst evaluating a potential trade setup.
A technical signal (RSI + basic SMA trend filter) suggests a potential trade direction: '{technically_derived_direction}'.
Your task is to EVALUATE this signal using the provided market data and technical indicators, and provide actionable parameters if appropriate.
Market Data & Indicators:
{data_json}
Instructions:
1. **Evaluate Signal:** Assess the provided '{technically_derived_direction}' signal. Look for **confirming** factors (e.g., MACD alignment, price near relevant Bollinger Band, supportive Stochastic/CCI levels) and **strong contradicting** factors (e.g., clear divergence, price hitting major resistance/support against the signal, very low ADX < 15-20 suggesting chop).
2. **Determine Action:**
* If the initial signal has some confirmation OR lacks strong contradictions, **lean towards confirming the `trade_direction`** ('long' or 'short').
* Only output `trade_direction: 'hold'` if there are **significant contradictions** from multiple important indicators OR if the market context (e.g., extreme chop, major news event imminent - though you don't have news data) makes the signal very unreliable.
3. **Refine Parameters (if action suggested):**
* `optimal_entry`: Suggest a tactical entry, considering pullbacks/rallies to support/resistance (SMAs, BBands) or breakout/down levels relative to the signal candle. Justify briefly. Use `current_price` only if no better tactical level is apparent.
* `stop_loss`: Place logically based on volatility (ATR) or structure.
# <<< --- CHANGE THIS LINE --- >>>
* `take_profit`: Aim for R/R >= {min_requested_rr or 1.5}.
# <<< --- END OF CHANGE --- >>>
4. **Provide Confidence:** Assign a `confidence_score` (0.0-1.0) based on the *degree of confirmation* and the *absence of strong contradictions*. A score > 0.6 requires decent confirmation.
5. **Justify:** Explain your reasoning in the `analysis` sections (`signal_evaluation`, `technical_analysis`, `risk_assessment`, `market_outlook`).
Respond ONLY with a single, valid JSON object containing the specified fields.
"""
# --- END OF REVISED PROMPT ---
try:
logger.info(f"{log_prefix} Sending request to GPT to evaluate '{technically_derived_direction}' signal...")
response = openai_client.chat.completions.create(
model="gpt-4o", # Use a capable model
messages=[
{"role": "system", "content": "You are a crypto trading analyst evaluating technical signals provided by a user. Respond in JSON."},
{"role": "user", "content": prompt}
],
response_format={"type": "json_object"},
temperature=0.3, # Lower temperature for more deterministic analysis
max_tokens=1800 # Allow slightly more tokens for detailed justification
)
gpt_output = response.choices[0].message.content
logger.debug(f"{log_prefix} Raw GPT Output received:\n```json\n{gpt_output}\n```") # Log raw output at DEBUG level
return gpt_output or "{}"
except openai.RateLimitError as e:
logger.error(f"{log_prefix} OpenAI Rate Limit Error: {e}")
return json.dumps({"error": "OpenAI API rate limit exceeded", "details": str(e)})
except openai.APIError as e:
logger.error(f"{log_prefix} OpenAI API Error: {e}", exc_info=False)
return json.dumps({"error": "OpenAI API error", "details": str(e)})
except Exception as e:
logger.error(f"{log_prefix} Error querying OpenAI: {e}", exc_info=False)
return json.dumps({"error": "Failed to query OpenAI", "details": str(e)})
# --- Parse GPT Parameters (Modified to handle new 'analysis' structure) ---
def parse_gpt_trading_parameters(gpt_output_str: str, symbol_for_log: str = "") -> Dict[str, Any]:
"""Parse GPT-generated trading parameters (evaluation focused)."""
log_prefix = f"[{symbol_for_log} Parse]"
# Default structure, including new analysis fields
parsed_data = {
'optimal_entry': None, 'stop_loss': None, 'take_profit': None, 'trade_direction': 'hold', # Default to hold
'leverage': None, 'position_size_usd': None, 'estimated_profit': None, 'confidence_score': 0.0, # Default confidence 0
'analysis': {'signal_evaluation': None, 'technical_analysis': None, 'risk_assessment': None, 'market_outlook': None, 'raw_text': gpt_output_str}
}
try:
data = json.loads(gpt_output_str)
if not isinstance(data, dict):
raise json.JSONDecodeError("GPT output was not a JSON object", gpt_output_str, 0)
logger.debug(f"{log_prefix} Successfully decoded JSON from GPT.")
# Helper to safely get float values
def get_float(key):
val = data.get(key)
return float(val) if isinstance(val, (int, float)) and np.isfinite(val) else None
# Get core trade parameters
parsed_data['optimal_entry'] = get_float('optimal_entry')
parsed_data['stop_loss'] = get_float('stop_loss')
parsed_data['take_profit'] = get_float('take_profit')
parsed_data['position_size_usd'] = get_float('position_size_usd')
parsed_data['estimated_profit'] = get_float('estimated_profit')
parsed_data['confidence_score'] = get_float('confidence_score')
# Leverage
leverage_val = data.get('leverage')
if isinstance(leverage_val, int) and leverage_val >= 1:
parsed_data['leverage'] = leverage_val
elif leverage_val is not None:
logger.warning(f"{log_prefix} Invalid leverage value from GPT: {leverage_val}.")
# Trade Direction (Crucial - reflects GPT's evaluation)
direction = data.get('trade_direction')
if direction in ['long', 'short', 'hold']:
parsed_data['trade_direction'] = direction
logger.info(f"{log_prefix} GPT evaluated signal resulted in direction: '{direction}'")
elif direction:
logger.warning(f"{log_prefix} Invalid trade_direction from GPT: '{direction}'. Defaulting to 'hold'.")
parsed_data['trade_direction'] = 'hold'
else:
logger.warning(f"{log_prefix} Missing trade_direction from GPT. Defaulting to 'hold'.")
parsed_data['trade_direction'] = 'hold'
# Validate parameters *if* GPT suggests a trade
if parsed_data['trade_direction'] in ['long', 'short']:
if not all([parsed_data['optimal_entry'], parsed_data['stop_loss'], parsed_data['take_profit'], parsed_data['confidence_score'] is not None]):
logger.warning(f"{log_prefix} GPT suggested '{parsed_data['trade_direction']}' but missing Entry/SL/TP/Confidence. Forcing 'hold'.")
parsed_data['trade_direction'] = 'hold'
# Optional: Check R/R ratio validity here
elif parsed_data['optimal_entry'] and parsed_data['stop_loss'] and parsed_data['take_profit']:
risk = abs(parsed_data['optimal_entry'] - parsed_data['stop_loss'])
reward = abs(parsed_data['take_profit'] - parsed_data['optimal_entry'])
if risk < 1e-9 or reward / risk < 1.0: # Ensure R > 0 and R/R >= 1.0 at least
logger.warning(f"{log_prefix} GPT suggested '{parsed_data['trade_direction']}' with invalid R/R ({reward=}, {risk=}). Forcing 'hold'.")
parsed_data['trade_direction'] = 'hold'
# Process analysis section - handle new structure
analysis_dict = data.get('analysis')
if isinstance(analysis_dict, dict):
for key in ['signal_evaluation', 'technical_analysis', 'risk_assessment', 'market_outlook']:
val = analysis_dict.get(key)
if isinstance(val, str) and val.strip():
parsed_data['analysis'][key] = val.strip()
elif val is not None:
logger.warning(f"{log_prefix} Invalid type or empty value for analysis key '{key}': {type(val)}.")
elif analysis_dict is not None:
logger.warning(f"{log_prefix} Invalid type for 'analysis' section: {type(analysis_dict)}.")
# Add raw text if analysis section was missing/invalid
if parsed_data['analysis']['signal_evaluation'] is None:
parsed_data['analysis']['raw_text'] = gpt_output_str # Keep raw if parsing failed
except json.JSONDecodeError as e:
logger.error(f"{log_prefix} Failed to decode JSON from GPT: {e}. Raw: {gpt_output_str[:300]}...")
parsed_data['trade_direction'] = 'hold'
parsed_data['analysis']['signal_evaluation'] = f"Error: Failed to parse GPT JSON response. {e}"
except Exception as e:
logger.error(f"{log_prefix} Unexpected error parsing GPT response: {e}", exc_info=True)
parsed_data['trade_direction'] = 'hold'
parsed_data['analysis']['signal_evaluation'] = f"Error: Unexpected error parsing GPT response. {e}"
# Clear trade params if final decision is 'hold'
if parsed_data['trade_direction'] == 'hold':
logger.info(f"{log_prefix} Final direction is 'hold', clearing trade parameters.")
parsed_data['optimal_entry'] = None
parsed_data['stop_loss'] = None
parsed_data['take_profit'] = None
parsed_data['leverage'] = None
parsed_data['position_size_usd'] = None
parsed_data['estimated_profit'] = None
# Keep confidence score as it might reflect confidence in *not* trading
logger.debug(f"{log_prefix} Parsed GPT Params: {parsed_data}")
return parsed_data
# --- Backtesting (Unchanged - still uses RSI trigger for historical simulation) ---
def backtest_strategy(df_with_indicators: pd.DataFrame, gpt_params: Dict[str, Any]) -> Dict[str, Any]:
"""Backtest strategy based on *similar historical conditions* (RSI Trigger)."""
symbol_log = df_with_indicators.get('symbol', 'Unknown')
log_prefix = f"[{symbol_log} Backtest]"
results = {'strategy_score': 0.0, 'trade_analysis': {'total_trades': 0, 'winning_trades': 0, 'losing_trades': 0, 'win_rate': None, 'avg_profit': None, 'avg_loss': None, 'profit_factor': None, 'total_profit': None, 'largest_win': None, 'largest_loss': None, 'average_trade_duration': None}, 'recommendation': 'N/A', 'warnings': []}
trade_analysis = results['trade_analysis']
# Backtest uses the direction *evaluated* by GPT for consistency check
direction_evaluated = gpt_params.get('trade_direction')
if direction_evaluated not in ['long', 'short']:
results['recommendation'] = f"Backtest skipped: Evaluated trade direction is '{direction_evaluated}'."
logger.info(f"{log_prefix} {results['recommendation']}")
return results
required_cols = ['close', 'low', 'high', 'ATR', 'RSI'] # Core columns for the simple RSI strategy
df_clean = df_with_indicators[required_cols].dropna()
if len(df_clean) < 50:
results['recommendation'] = "Backtest skipped: Insufficient data."
results['warnings'].append(f"Insufficient data points ({len(df_clean)}) for backtest.")
logger.warning(f"{log_prefix} {results['recommendation']}")
return results
logger.info(f"{log_prefix} Starting backtest for direction '{direction_evaluated}' over {len(df_clean)} bars.")
# Backtest parameters (using the simplified RSI entry logic)
min_rr_ratio_target = 1.5 # Minimum R:R for a simulated trade
rsi_oversold = 35 # Corresponds to the technical trigger
rsi_overbought = 65 # Corresponds to the technical trigger
atr_sl_multiplier = 1.5 # Stop loss based on ATR at signal time
max_trade_duration_bars = 96 # ~4 days on 1h timeframe
min_bars_between_trades = 5 # Cooldown period
trades = []; last_entry_index = -1 - min_bars_between_trades
# Ensure indices are valid and align with df_clean
try:
search_start_index = df_clean.index.min()
search_end_index_ts = df_clean.index[-6] # Stop a few bars before the end
search_indices = df_with_indicators.loc[search_start_index:search_end_index_ts].index
if len(search_indices) < 2: # Need at least start and next bar
raise ValueError("Not enough bars for simulation loop")
logger.debug(f"{log_prefix} Simulation loop range: {search_indices[0]} to {search_indices[-1]} ({len(search_indices)} bars)")
except Exception as e:
results['recommendation'] = "Backtest skipped: Cannot determine simulation range."
results['warnings'].append(f"Error setting up simulation index: {e}")
logger.error(f"{log_prefix} {results['recommendation']} - {e}")
return results
# --- Simulation Loop ---
entry_count = 0
for i_idx, current_ts in enumerate(search_indices[:-1]): # Iterate up to second to last timestamp
signal_row = df_with_indicators.loc[current_ts]
entry_ts = search_indices[i_idx+1] # Timestamp of the next bar
entry_row = df_with_indicators.loc[entry_ts]
# Check cooldown
current_loc = df_with_indicators.index.get_loc(current_ts)
if current_loc <= last_entry_index + min_bars_between_trades:
continue
# Check data validity for signal and entry bars
if pd.isna(signal_row['RSI']) or pd.isna(signal_row['ATR']) or pd.isna(signal_row['close']) or \
pd.isna(entry_row['open']) or signal_row['ATR'] <= 1e-9:
continue
current_rsi, current_atr, signal_close = signal_row['RSI'], signal_row['ATR'], signal_row['close']
entry_price = entry_row['open'] # Enter at open of next bar
setup_found, stop_loss_calc, take_profit_calc = False, 0.0, 0.0
# Check for RSI-based entry signal (matching the backtest logic)
if direction_evaluated == 'long' and current_rsi < rsi_oversold:
stop_loss_calc = signal_close - current_atr * atr_sl_multiplier # SL based on signal bar's close/ATR
risk_amount = abs(entry_price - stop_loss_calc)
if risk_amount > 1e-9:
take_profit_calc = entry_price + risk_amount * min_rr_ratio_target
setup_found = True
logger.debug(f"{log_prefix} Long setup found at {current_ts} (RSI: {current_rsi:.2f}). Entry: {entry_price:.4f}, SL: {stop_loss_calc:.4f}, TP: {take_profit_calc:.4f}")
elif direction_evaluated == 'short' and current_rsi > rsi_overbought:
stop_loss_calc = signal_close + current_atr * atr_sl_multiplier
risk_amount = abs(stop_loss_calc - entry_price)
if risk_amount > 1e-9:
take_profit_calc = entry_price - risk_amount * min_rr_ratio_target
setup_found = True
logger.debug(f"{log_prefix} Short setup found at {current_ts} (RSI: {current_rsi:.2f}). Entry: {entry_price:.4f}, SL: {stop_loss_calc:.4f}, TP: {take_profit_calc:.4f}")
if setup_found:
# Validate levels make sense (SL < Entry < TP for long, TP < Entry < SL for short)
valid_levels = False
if direction_evaluated == 'long' and stop_loss_calc < entry_price < take_profit_calc: valid_levels = True
elif direction_evaluated == 'short' and take_profit_calc < entry_price < stop_loss_calc: valid_levels = True
if valid_levels:
entry_count += 1
outcome, exit_price, exit_idx_loc = None, None, -1
entry_idx_loc = df_with_indicators.index.get_loc(entry_ts)
# Simulate trade progression bar-by-bar
max_exit_loc = min(len(df_with_indicators) - 1, entry_idx_loc + max_trade_duration_bars)
for k_loc in range(entry_idx_loc + 1, max_exit_loc + 1): # Check from bar *after* entry open
current_bar = df_with_indicators.iloc[k_loc]
current_low, current_high = current_bar['low'], current_bar['high']
if direction_evaluated == 'long':
if current_low <= stop_loss_calc: # Stop Loss Hit
outcome, exit_price, exit_idx_loc = 'loss', stop_loss_calc, k_loc
logger.debug(f"{log_prefix} Trade {entry_count} (Long) SL hit at {df_with_indicators.index[k_loc]}")
break
elif current_high >= take_profit_calc: # Take Profit Hit
outcome, exit_price, exit_idx_loc = 'win', take_profit_calc, k_loc
logger.debug(f"{log_prefix} Trade {entry_count} (Long) TP hit at {df_with_indicators.index[k_loc]}")
break
elif direction_evaluated == 'short':
if current_high >= stop_loss_calc: # Stop Loss Hit
outcome, exit_price, exit_idx_loc = 'loss', stop_loss_calc, k_loc
logger.debug(f"{log_prefix} Trade {entry_count} (Short) SL hit at {df_with_indicators.index[k_loc]}")
break
elif current_low <= take_profit_calc: # Take Profit Hit
outcome, exit_price, exit_idx_loc = 'win', take_profit_calc, k_loc
logger.debug(f"{log_prefix} Trade {entry_count} (Short) TP hit at {df_with_indicators.index[k_loc]}")
break
# If neither SL nor TP hit within duration, exit at close of last bar
if outcome is None:
exit_idx_loc = max_exit_loc
exit_bar = df_with_indicators.iloc[exit_idx_loc]
exit_price = exit_bar['close']
# Determine win/loss based on exit price relative to entry
if direction_evaluated == 'long': outcome = 'win' if exit_price > entry_price else 'loss'
else: outcome = 'win' if exit_price < entry_price else 'loss'
logger.debug(f"{log_prefix} Trade {entry_count} exited due to duration at {df_with_indicators.index[exit_idx_loc]} - Outcome: {outcome}")
# Record the trade if an outcome was determined
if outcome and exit_price is not None and exit_idx_loc != -1:
profit_points = (exit_price - entry_price) if direction_evaluated == 'long' else (entry_price - exit_price)
trade_duration = exit_idx_loc - entry_idx_loc # Duration in bars
trades.append({'profit': profit_points, 'duration': trade_duration, 'outcome': outcome})
last_entry_index = entry_idx_loc # Update cooldown index (use location)
else:
logger.debug(f"{log_prefix} Setup found at {current_ts} but levels invalid (Entry:{entry_price:.4f}, SL:{stop_loss_calc:.4f}, TP:{take_profit_calc:.4f})")
# --- Analyze Backtest Results ---
trade_analysis['total_trades'] = len(trades)
logger.info(f"{log_prefix} Backtest simulation completed. Found {len(trades)} historical trades.")
if trades:
# --- Calculations (Unchanged) ---
profits = np.array([t['profit'] for t in trades]); durations = np.array([t['duration'] for t in trades]); outcomes = [t['outcome'] for t in trades]
trade_analysis['winning_trades'] = sum(1 for o in outcomes if o == 'win'); trade_analysis['losing_trades'] = len(trades) - trade_analysis['winning_trades']
if len(trades) > 0: trade_analysis['win_rate'] = round(trade_analysis['winning_trades'] / len(trades), 3)
winning_profits = profits[profits > 0]; losing_profits = profits[profits <= 0]; gross_profit = np.sum(winning_profits); gross_loss = abs(np.sum(losing_profits))
trade_analysis['avg_profit'] = round(np.mean(winning_profits), 4) if len(winning_profits) > 0 else 0.0
trade_analysis['avg_loss'] = round(abs(np.mean(losing_profits)), 4) if len(losing_profits) > 0 else 0.0
if gross_loss > 1e-9: trade_analysis['profit_factor'] = round(gross_profit / gross_loss, 2)
elif gross_profit > 1e-9: trade_analysis['profit_factor'] = float('inf')
else: trade_analysis['profit_factor'] = 0.0
trade_analysis['total_profit'] = round(np.sum(profits), 4); trade_analysis['largest_win'] = round(np.max(winning_profits), 4) if len(winning_profits) > 0 else 0.0
trade_analysis['largest_loss'] = round(np.min(losing_profits), 4) if len(losing_profits) > 0 else 0.0; trade_analysis['average_trade_duration'] = round(np.mean(durations), 1)
# --- Calculate Strategy Score (Unchanged logic, applied to RSI strategy results) ---
score = 0.0; pf = trade_analysis['profit_factor']; wr = trade_analysis['win_rate']; num_trades = len(trades)
avg_w = trade_analysis['avg_profit']; avg_l = trade_analysis['avg_loss']
if pf is not None:
if pf == float('inf') or pf >= 2.5: score += 0.40
elif pf >= 1.7: score += 0.30
elif pf >= 1.3: score += 0.20
elif pf >= 1.0: score += 0.10
else: score -= 0.20
if wr is not None:
if wr >= 0.6: score += 0.30
elif wr >= 0.5: score += 0.20
elif wr >= 0.4: score += 0.10
else: score -= 0.10
if num_trades >= 30: score += 0.20
elif num_trades >= 15: score += 0.10
elif num_trades < 5: score -= 0.15
if avg_w is not None and avg_l is not None and avg_l > 1e-9:
ratio = avg_w / avg_l
if ratio >= 1.5: score += 0.10
elif ratio >= 1.0: score += 0.05
if trade_analysis['total_profit'] is not None and trade_analysis['total_profit'] <= 0: score -= 0.30
results['strategy_score'] = max(0.0, min(1.0, round(score, 2)))
logger.info(f"{log_prefix} Backtest Score: {results['strategy_score']:.2f} (Trades:{num_trades}, WR:{wr:.1%}, PF:{pf:.2f})")
# --- Recommendation and Warnings (Unchanged) ---
if results['strategy_score'] >= 0.75: results['recommendation'] = "Strong historical performance for RSI trigger."
elif results['strategy_score'] >= 0.60: results['recommendation'] = "Good historical performance for RSI trigger."
elif results['strategy_score'] >= 0.45: results['recommendation'] = "Moderate/Mixed historical performance for RSI trigger."
else: results['recommendation'] = "Poor historical performance for RSI trigger."
if wr is not None and wr < 0.40: results['warnings'].append(f"Low Win Rate ({wr:.1%})")
if pf is not None and pf < 1.2: results['warnings'].append(f"Low Profit Factor ({pf:.2f})")
if num_trades < 10: results['warnings'].append(f"Low Trade Count ({num_trades}).")
if trade_analysis['total_profit'] is not None and trade_analysis['total_profit'] <= 0: results['warnings'].append("Overall loss in backtest.")
if avg_l is not None and trade_analysis['largest_loss'] is not None and abs(trade_analysis['largest_loss']) > 3 * avg_l: results['warnings'].append("Largest loss significantly exceeds average loss.")
else:
results['recommendation'] = "No similar historical RSI setups found to backtest."; results['warnings'].append("No qualifying historical trade setups found."); results['strategy_score'] = 0.0
logger.info(f"{log_prefix} {results['recommendation']}")
return results
# --- Core Analysis Function (LSTM Disabled, Technical Trigger Added) ---
# --- Core Analysis Function (perform_single_analysis) ---
async def perform_single_analysis(
symbol: str, timeframe: str, lookback: int, account_balance: float, max_leverage: float, min_requested_rr: Optional[float] = None
) -> AnalysisResponse:
start_time_ticker = time.time()
log_prefix = f"[{symbol} ({timeframe})]"
logger.info(f"{log_prefix} Starting analysis...")
analysis_result = AnalysisResponse(symbol=symbol, timeframe=timeframe)
# ... (Steps 1: Fetch Data, Step 2: Apply Indicators - remain the same) ...
# --- Make sure these steps complete successfully ---
try:
# Step 1: Fetch Data
df_raw = await asyncio.to_thread(get_real_time_data, symbol, timeframe, lookback)
min_req = 200
if df_raw.empty or len(df_raw) < min_req:
analysis_result.error = f"Insufficient data ({len(df_raw)}/{min_req})"; logger.warning(f"{log_prefix} {analysis_result.error}"); return analysis_result
analysis_result.currentPrice = float(df_raw['close'].iloc[-1])
logger.info(f"{log_prefix} Data fetched. Price: {analysis_result.currentPrice:.4f}")
# Step 2: Apply Indicators
df_raw['symbol'] = symbol
df_indicators = await asyncio.to_thread(apply_technical_indicators, df_raw)
latest_indicators_raw = df_indicators.iloc[-1].to_dict()
indicator_payload = {
field_name: float(value)
for field_name, model_field in IndicatorsData.model_fields.items()
if pd.notna(value := latest_indicators_raw.get(model_field.alias or field_name)) and np.isfinite(value)
}
analysis_result.indicators = IndicatorsData(**indicator_payload)
logger.info(f"{log_prefix} Indicators applied.")
except Exception as e:
logger.error(f"{log_prefix} Error during Data/Indicator steps: {e}", exc_info=True)
analysis_result.error = f"Data/Indicator Error: {e}"
return analysis_result # Stop if basic data/indicators fail
# --- Step 2.5: Determine Technical Trigger (MACD Cross + SMA200 Trend + ADX) ---
technical_signal_direction = "hold" # Default
price = analysis_result.currentPrice
sma200 = analysis_result.indicators.SMA_200 if analysis_result.indicators else None
latest_adx = analysis_result.indicators.ADX if analysis_result.indicators else None
macd_line = analysis_result.indicators.MACD if analysis_result.indicators else None
signal_line = analysis_result.indicators.Signal_Line if analysis_result.indicators else None
min_adx_for_trigger = 20 # Minimum ADX for trend strength
# Get previous bar's MACD values for crossover detection
prev_macd_line = None
prev_signal_line = None
if df_indicators is not None and len(df_indicators) > 1:
prev_indicators_raw = df_indicators.iloc[-2].to_dict() # Get second to last row
prev_macd_line = prev_indicators_raw.get('MACD')
prev_signal_line = prev_indicators_raw.get('Signal_Line')
# Pre-calculate conditions
has_data = all(v is not None and np.isfinite(v) for v in [price, sma200, latest_adx, macd_line, signal_line, prev_macd_line, prev_signal_line])
is_trending_up = has_data and price > sma200
is_trending_down = has_data and price < sma200
is_adx_strong = has_data and latest_adx >= min_adx_for_trigger
# MACD Crossover conditions
macd_crossed_up = has_data and prev_macd_line <= prev_signal_line and macd_line > signal_line
macd_crossed_down = has_data and prev_macd_line >= prev_signal_line and macd_line < signal_line
# Determine Signal based on combined conditions
if is_trending_up and macd_crossed_up and is_adx_strong:
technical_signal_direction = "long"
elif is_trending_down and macd_crossed_down and is_adx_strong:
technical_signal_direction = "short"
# All other combinations result in "hold"
# Log the outcome and conditions clearly
macd_cross_log = "MACD Cross: N/A"
if has_data:
if macd_crossed_up: macd_cross_log = f"MACD Cross: UP ({macd_line:.4f} > {signal_line:.4f})"
elif macd_crossed_down: macd_cross_log = f"MACD Cross: DOWN ({macd_line:.4f} < {signal_line:.4f})"
else: macd_cross_log = f"MACD Cross: None ({macd_line:.4f} vs {signal_line:.4f})"
trend_log = f"Trend: {'UP' if is_trending_up else 'DOWN' if is_trending_down else 'CHOPPY/NA'}"
adx_log = f"ADX: {f'{latest_adx:.2f}' if latest_adx is not None else 'N/A'}"
logger.info(f"{log_prefix} Technical signal trigger: {technical_signal_direction.upper()} "
f"({macd_cross_log}, {trend_log}, TrendOK: {is_trending_up or is_trending_down}, "
f"{adx_log}, ADX_OK: {is_adx_strong})")
# --- End of Step 2.5 ---
# Step 3: Statistical Models (GARCH, VaR)
garch_vol, var95 = None, None
try:
if df_indicators is not None and 'returns' in df_indicators:
returns = df_indicators['returns']
# --- Run concurrently for slight speed up ---
garch_task = asyncio.to_thread(fit_garch_model, returns, symbol)
var_task = asyncio.to_thread(calculate_var, returns, 0.95, symbol)
garch_vol, var95 = await asyncio.gather(garch_task, var_task)
# --- End concurrent run ---
analysis_result.modelOutput = ModelOutputData(garchVolatility=garch_vol, var95=var95)
logger.info(f"{log_prefix} GARCH/VaR calculated (Vol: {garch_vol}, VaR95: {var95}).")
else:
logger.warning(f"{log_prefix} Skipping GARCH/VaR (no returns data).")
analysis_result.modelOutput = ModelOutputData() # Ensure modelOutput exists
except Exception as e:
logger.error(f"{log_prefix} Stat model error: {e}", exc_info=True)
analysis_result.error = (analysis_result.error or "") + f"; Stat Model Error: {e}"
analysis_result.modelOutput = ModelOutputData() # Ensure modelOutput exists
# --- Step 4: GPT Evaluation (Conditional Call) ---
gpt_parsed_output = None
if openai_client and technical_signal_direction != 'hold':
logger.info(f"{log_prefix} Technical signal is '{technical_signal_direction}', proceeding with GPT evaluation.")
try:
# <<< --- MODIFY THIS CALL --- >>>
gpt_raw_output = await asyncio.to_thread(
gpt_generate_trading_parameters,
df_indicators, symbol, timeframe, account_balance, max_leverage,
garch_vol, var95, # Pass stat model results
technical_signal_direction, # Pass the RSI-derived signal