
    zj'                         S SK Jr  S SKJr  S SKJr  SrSrSrSr	Sr
    SS
\S\S\S\S\S\S\S\S\S\S\SS	4S jjrSS\S\S\\   S\\   4S jjrSS\\   S\\   4S jjrg	)    )Optional)get_connection)_validate_table_namezSHOW TABLESz4SELECT id, name, type FROM instruments ORDER BY namez>SELECT id, name FROM instruments WHERE type = %s ORDER BY namezZSELECT COUNT(*) FROM information_schema.tables WHERE table_schema = %s AND table_name = %saV  
CREATE TABLE IF NOT EXISTS signal_log (
    id INT AUTO_INCREMENT PRIMARY KEY,
    ts INT NOT NULL,
    ticker VARCHAR(20) NOT NULL,
    `signal` VARCHAR(10) NOT NULL DEFAULT 'NEUTRAL',
    p_long DECIMAL(6,4) DEFAULT 0,
    p_short DECIMAL(6,4) DEFAULT 0,
    threshold_long DECIMAL(4,2) DEFAULT 0,
    threshold_short DECIMAL(4,2) DEFAULT 0,
    confidence DECIMAL(6,4) DEFAULT 0,
    flat_filter VARCHAR(50) DEFAULT NULL,
    close_price DECIMAL(20,8) DEFAULT 0,
    model_type VARCHAR(20) DEFAULT 'moe_v12',
    INDEX idx_ticker_ts (ticker, ts),
    INDEX idx_ts (ts)
) CHARACTER SET utf8mb4;
Ntstickersignalp_longp_shortthreshold_longthreshold_short
confidenceflat_filterclose_price
model_typereturnc                    [        5        nUR                  5       nUR                  [        5        UR                  S[	        U 5      X[        U5      [        U5      [        U5      [        U5      [        U5      U[        U	5      U
45        UR                  5         UR                  5         SSS5        g! , (       d  f       g= f)u  Записывает сигнал в signal_log таблицу (audit 2026-08-03).

Используется для post-hoc анализа качества модели и debugging.
Таблица создаётся автоматически при первом вызове.
zINSERT INTO signal_log (ts, ticker, `signal`, p_long, p_short, threshold_long, threshold_short,  confidence, flat_filter, close_price, model_type) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)N)r   cursorexecuteSIGNAL_LOG_CREATE_SQLintfloatcommitclose)r   r   r   r	   r
   r   r   r   r   r   r   conncurs                +/home/ai/projects/AI_Strategy/db/queries.pyinsert_signal_logr   "   s     
	Tkkm)*B Wf6]E'N>"E/$::;	-
	
 			! 
		s   B!B55
C	timeframelimitc                 *   [        X5      nSU-   S-   nS nU(       a  US-  n[        U5      4n[        5        nUR                  SS9nUR	                  XE5        UR                  5       nUR                  5         UsS S S 5        $ ! , (       d  f       g = f)NzFSELECT timestamp, `Date`, `Time`, Open, High, Low, Close, Volume FROM z ORDER BY timestampz	 LIMIT %sT
dictionary)r   r   r   r   r   fetchallr   )	r   r   r   tablequeryparamsr   r   rowss	            r   load_candlesr(   ?   s     3ETW\\_ttEFe*		T-u%  
		s   AB
Binstrument_typec                    [        5        nUR                  SS9nU (       a  UR                  [        U 45        OUR                  [        5        UR                  5       nUR                  5         UsS S S 5        $ ! , (       d  f       g = f)NTr!   )r   r   r   INSTRUMENTS_BY_TYPE_QUERYINSTRUMENTS_QUERYr#   r   )r)   r   r   r'   s       r   get_instrumentsr-   N   s`    		T-NN46HINN,-  
		s   A%A::
B)r   r   r   r   r   Nr   moe_v12)N)typingr   db.connectionr   data.loaderr   TABLES_QUERYr,   r+   TABLE_EXISTS_QUERYr   r   strr   r   listdictr(   r-        r   <module>r9      s     ( , J \ q 
 * <=KLAEAJ	# s C $38',CH $) <? %*	 <?	 PT	:  Xc] dSWj 	Xc] 	d4j 	r8   