
    i<F                         d dl mZ d dlmZmZ dZdZdZdZdZ	dZ
d	Zd
Z ed      Z ed      Z ed      Z ed      ZdadedefdZd Zd Zd Zd Zd Zd Zd ZdedefdZy)    )deque)calc_trend_scorecalc_net_biasg      ?g      ?g333333?gffffffֿg     b@g?333333?
   )maxlenNdepth_ratiobest_level_ratioc                 z    | | nd}||nd}t        dt        d|            }t        dt        d|            }||fS )ud   
    קליפ של עומק ובסט כדי לא להרוס את ה-bias עם קיצוניות.
          ?      ?g      @g?g      @)maxmin)r
   r   dbs       /var/www/html/binance/engine.py_sanitize_orderbook_for_biasr   #   sJ     #.CA,8cACS!ACS!Aa4K    c                 |   | j                  dd      }| j                  dd      }| j                  dd      }| j                  dd      }| j                  d	d      }i }|d
k  |d<   |dv|d<   |dk  |d<   |dkD  |d<   |dk\  |d<   |d uxr |dkD  |d<   |d uxr |dk  |d<   |d uxr |dkD  |d<   |d uxr |dk  |d<   |S )N	buy_ratior   cvd        	vol_priceQUIETratio_5mr   price_change_pct_5mr   very_low_activity)r   NORMALMIDchoppy_priceg333333ÿmild_trend_down_5mr   mild_trend_up_5mg
ףp=
?buy_flow_ok   strong_cvd_upistrong_cvd_downr   light_cvd_uplight_cvd_down)get)stater   r   r   r   price_change_5menvs          r   _classify_envr.   /   s	   		+s+I
))E3
C		+w/IyyS)Hii 5s;O
C'$C#+EEC /% 7C /$ 6C"e+C _9rC _:sC _8qC _8qCJr   c                     t         j                  |        t        j                  |       t        j                  |       t        j                  |       y )N)price_historyappenddepth_historybest_historycvd_history)pricer
   r   cvd_vals       r   _update_historiesr7   H   s;    %()wr   c                  f    t        t              dk  ryt        t              t        t              z  S N   r   )lenr2   sum r   r   depth_rollingr>   O   s'    
=A}M 222r   c                  N    t        t              dk  ryt        d   t        d   z
  S N   r   r   )r;   r2   r=   r   r   depth_sloperC   U   s'    
=A}Q///r   c                  B    t               } t               }| dkD  r|dkD  ryy)u   
    עומק אמיתי:
    - ממוצע עומק גבוה
    - מגמה חיובית או לפחות לא מתרסקת
    g333333?r   TF)r>   rC   )rollslopes     r   depth_strengthrG   [   s%     ?DMEczeair   c                  f    t        t              dk  ryt        t              t        t              z  S r9   )r;   r3   r<   r=   r   r   best_rollingrI   i   s'    
<1|s<000r   c                  N    t        t              dk  ryt        d   t        d   z
  S r@   )r;   r3   r=   r   r   
best_sloperK   o   s&    
<1l1o--r   r+   returnc           
         | j                  dd      }| j                  dd      }| j                  dd      }| j                  dd      }| j                  dd      }| j                  d	d
      }| j                  dd
      }| j                  dd      }| j                  di       }	| j                  di       }
| j                  dd      }| j                  dd      }| j                  dd
      }| j                  dd
      }| j                  dd
      }| j                  dd      }| j                  dg d      }t        |      dk7  rg d}|\  }}}| j                  di       }t        |t              si }|j                  di       }|j                  di       }|j                  di       }|j                  d|      }|j                  d|      }|j                  d|      }|ddd d d!S t	        ||||       t
        }||||d"a|rE|j                  d      }|j                  d      }|j                  d	      }|j                  d      } ndx}x}x}} |||z
  nd }!|||z
  nd }"| || z
  nd }#|rt        ||z
        |z  d#z  nd }$t        ||      \  }%}&t        |||||||      }'t        ||||d d d       }(t        ||%|&|||||      })t        ||%|&||d d d       }*|'d }'|)d })dd$|'|(|)|*d%S )&ua   
    V10 – SAFE VERSION
    מתוקן כך שהבוט לא יקרוס אם חסר שדה.
    r5   Nr   r   smoothr   r   whaler
   r   r   r   r   spooficeberghas_positionF	buy_priceratio_1mr   	ratio_15mr   onchain)r   r   r   r:   mf_stats1515WAITzMissing price in stater   )actionreasontrend_scorenet_bias)r5   r   r
   r   g      Y@u#   SAFE fallback – no logic executed)r\   r]   r^   trend_score_baser_   net_bias_base)r*   r;   
isinstancedictr7   _prev_state_for_deltasabsr   r   r   STOP_LOSS_PCTTAKE_PROFIT_PCTCVD_DIVERGENCE_CVD_MINCVD_DIVERGENCE_MAX_ABS_PRICE_5MCVD_DIVERGENCE_MAX_ABS_PNLTP2_PCTr-   TP1_PCTSOFT_DD_PCTrG   )Ar+   r5   r   rN   r   rO   	depth_rawbest_rawr   rP   rQ   has_posrS   rT   r   rU   r,   rV   f_memf_feef_blockmfmf1mf5mf15buy_1mbuy_5mbuy_15mprev
prev_priceprev_cvd
prev_depth	prev_bestcvd_roc	depth_rocbest_rocspeed_1tdepth_sanitizedbest_sanitizedtrend
trend_basebias	bias_basepnl_pct	weakening	soft_weakbe_exit	soft_stopflow_signalflow_reasonreal_flow_okmicro_spikemicro_contracvd_okdepth_okbest_okimpulse_signalimpulse_reasonimpulse_trend_okimpulse_flow_okimpulse_cvd_okimpulse_speed_okimpulse_depth_okimpulse_best_okimpulse_price_oksA                                                                    r   detect_signalr   y   sT    ))GT*E))K-I))Hc*F))E3'C))GS)E))M3/I)).4H))K1I))GR(E))Ir*G))NE2G))K.I))J,H))J,H))K-Iii 5s;Oii	?3G
7|q!#E5' 
:r	"Bb$66#C66#C66$Dggk9-Fggk9-Fhh{I.G }.	
 	
 eY#6 "D $	 XXg&
XXe_XXm,
XX01	9==
=X=
Y #+"6hAG*4*@	J&aI(1(=9$1H  	EJ*,u4  'C9h&W#O^ VYUE5'RE!&)S%AqIJO^UCQVX]_fgDi.%QTVWYZ\]^I }| 7&" r   )collectionsr   
indicatorsr   r   rg   rf   rl   rk   rm   rh   ri   rj   r2   r3   r4   r0   rd   floatr   r.   r7   r>   rC   rG   rI   rK   rc   r   r=   r   r   <module>r      s     6   #( "& "&  R R R R  	e 	u 	2 301.] ]$ ]r   