
    6j                     n    d dl Zd dlmc mZ d dlmZ d dlm	Z	 d Z
d Zd Zd Zd Zd	 Zd
 Zd Zd ZdS )    N)Path)	BotConfigc                  
   ddl m}  t          ddd          }g d} | d|d	d
dt                      i d|	  	        }|j        }d}||u }|st          j        d|fd||f          dt          j                    v st          j	        |          rt          j
        |          ndt          j
        |          t          j
        |          dz  }dd|iz  }t          t          j        |                    d x}x}}|j        }d}||k    }|st          j        d|fd||f          dt          j                    v st          j	        |          rt          j
        |          ndt          j
        |          t          j
        |          dz  }dd|iz  }t          t          j        |                    d x}x}}|j        }d}| }	||	k    }|st          j        d|fd||	f          dt          j                    v st          j	        |          rt          j
        |          ndt          j
        |          t          j
        |          dz  }
dd|
iz  }t          t          j        |                    d x}x}x}}	d S )Nr   should_enter_flash_crash      @  <   flash_crash_trigger_pctmin_volume_24hcooldown_minutes)g      ?      Y@)       @g     X@)      @     W@BTCr     r        @	coinhistorycurrent_price
volume_24hbaseline_priceopen_positions	cooldownsnow_tscfgTisz-%(py2)s
{%(py2)s = %(py0)s.enter
} is %(py5)sdecisionpy0py2py5assert %(py7)spy7flash_crash==z.%(py2)s
{%(py2)s = %(py0)s.reason
} == %(py5)s)<=)z1%(py2)s
{%(py2)s = %(py0)s.drop_pct
} <= -%(py5)szassert %(py8)spy8)strategyr   r   setenter
@pytest_ar_call_reprcompare@py_builtinslocals_should_repr_global_name	_safereprAssertionError_format_explanationreasondrop_pct)r   r    r   r$   @py_assert1@py_assert4@py_assert3@py_format6@py_format8@py_assert6@py_format7@py_format9s               N/home/agent/projects/CryptoTradingBot/Crypto_Agent/tests/test_strategy_risk.py3test_flash_crash_entry_signal_allows_qualified_dumprG      s   111111
CXZ
[
[
[C666G''uu
 
 
H >!T!>T!!!!!!!!!!>T!!!!!!!!!!!!8!!!!!8!!!!!!>!!!!T!!!!!!!!!!!!!!!!!!!!!!!!!!!?+m+?m++++++++++?m++++++++++++8+++++8++++++?++++m+++++++++++++++++++++++++++$$$$$$$$$$$$$$$$$$$$$$$$$8$$$$$8$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$    c                     ddl m}  t          ddd          }ddg} | d	|d
ddt                      d	did|	  	        }|j        }d}||u }|st          j        d|fd||f          dt          j                    v st          j	        |          rt          j
        |          ndt          j
        |          t          j
        |          dz  }dd|iz  }t          t          j        |                    d x}x}}|j        }d}||k    }|st          j        d|fd||f          dt          j                    v st          j	        |          rt          j
        |          ndt          j
        |          t          j
        |          dz  }dd|iz  }t          t          j        |                    d x}x}}d S )Nr   r   r   r	   r
   r   r   )r   r   r   r   r   r   g     V@r   r   Fr!   r#   r$   r%   r)   r*   cooldownr,   r.   )r1   r   r   r2   r3   r4   r5   r6   r7   r8   r9   r:   r;   r<   )	r   r    r   r$   r>   r?   r@   rA   rB   s	            rF   4test_flash_crash_entry_signal_blocks_recent_cooldownrK      s0   111111
CXZ
[
[
[C[)G''uu'"
 
 
H >"U">U"""""""""">U""""""""""""8"""""8"""""">""""U"""""""""""""""""""""""""""?(j(?j((((((((((?j((((((((((((8(((((8((((((?((((j(((((((((((((((((((((((((((((rH   c            
         ddl m}  t          ddddd          }g d	} | d
|ddt                      i d|          }|j        }d}||u }|st          j        d|fd||f          dt          j                    v st          j	        |          rt          j
        |          ndt          j
        |          t          j
        |          dz  }dd|iz  }t          t          j        |                    d x}x}}|j        }d}||k    }|st          j        d|fd||f          dt          j                    v st          j	        |          rt          j
        |          ndt          j
        |          t          j
        |          dz  }dd|iz  }t          t          j        |                    d x}x}}|j        }d}||k    }|st          j        d|fd||f          dt          j                    v st          j	        |          rt          j
        |          ndt          j
        |          t          j
        |          dz  }dd|iz  }t          t          j        |                    d x}x}}|j        }d}||k    }|st          j        d|fd||f          dt          j                    v st          j	        |          rt          j
        |          ndt          j
        |          t          j
        |          dz  }dd|iz  }t          t          j        |                    d x}x}}d S )Nr   (should_enter_volatility_squeeze_breakoutvolatility_squeeze_breakout   333333??r	   strategy_familysqueeze_lookback_tickssqueeze_max_band_width_pctsqueeze_breakout_pctr   )r   )r   gfffffY@)r   gX@)      @g33333Y@)      @gX@g      @r   SOLgfffff&Y@r   r   r   r   r   r   r   r   r   r    Tr!   r#   r$   r%   r)   r*   squeeze_breakoutr,   r.   )>)z/%(py2)s
{%(py2)s = %(py0)s.drop_pct
} > %(py5)s)z9%(py2)s
{%(py2)s = %(py0)s.required_drop_pct
} == %(py5)s)r1   rN   r   r2   r3   r4   r5   r6   r7   r8   r9   r:   r;   r<   r=   required_drop_pct)	rN   r    compressed_then_breakoutr$   r>   r?   r@   rA   rB   s	            rF   >test_squeeze_breakout_entry_requires_compression_then_breakoutra   3   s   AAAAAA
5 #&!  C      87(uu	 	 	H >!T!>T!!!!!!!!!!>T!!!!!!!!!!!!8!!!!!8!!!!!!>!!!!T!!!!!!!!!!!!!!!!!!!!!!!!!!!?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08000008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 q q          q            8     8          q                           %--%----------%------------8-----8------%---------------------------------rH   c            
         ddl m}  t          ddddd          }g d	} | d
|ddt                      i d|          }|j        }d}||u }|st          j        d|fd||f          dt          j                    v st          j	        |          rt          j
        |          ndt          j
        |          t          j
        |          dz  }dd|iz  }t          t          j        |                    d x}x}}|j        }d}||k    }|st          j        d|fd||f          dt          j                    v st          j	        |          rt          j
        |          ndt          j
        |          t          j
        |          dz  }dd|iz  }t          t          j        |                    d x}x}}d S )Nr   rM   rO   rP   rQ   rR   r	   rS   )r   )r        Y@)r   g     `X@)rX   g     Y@)rY   g     X@rZ   r[   rc   r   r   r\   Fr!   r#   r$   r%   r)   r*   not_compressedr,   r.   )r1   rN   r   r2   r3   r4   r5   r6   r7   r8   r9   r:   r;   r<   )	rN   r    	wide_chopr$   r>   r?   r@   rA   rB   s	            rF   &test_squeeze_breakout_blocks_wide_choprf   W   s>   AAAAAA
5 #&!  C cbbI77uu	 	 	H >"U">U"""""""""">U""""""""""""8"""""8"""""">""""U"""""""""""""""""""""""""""?...?...........?.............8.....8......?..................................rH   c                     ddl m} m} t          dd          } | ddd	          } |dd
d|d|          }|j        }d}||u }|st          j        d|fd||f          dt          j                    v st          j	        |          rt          j
        |          ndt          j
        |          t          j
        |          dz  }dd|iz  }	t          t          j        |	                    d x}x}}|j        }|j        }d}
 ||
          }|sddt          j                    v st          j	        |          rt          j
        |          ndt          j
        |          t          j
        |          t          j
        |
          t          j
        |          dz  }t          t          j        |                    d x}x}x}
}d S )Nr   PositionStateexit_long_position   r   dead_fish_time_limit_minsmax_hard_stop_pct        r   g     @X@entry_tshigh_px	atr_sl_pxg     X@   i  entry_px
current_pxleveragestater   r    Tr!   z-%(py2)s
{%(py2)s = %(py0)s.close
} is %(py5)sr$   r%   r)   r*   z	Dead Fishzhassert %(py8)s
{%(py8)s = %(py4)s
{%(py4)s = %(py2)s
{%(py2)s = %(py0)s.reason
}.startswith
}(%(py6)s)
})r&   r'   py4py6r0   )r1   ri   rj   r   closer4   r5   r6   r7   r8   r9   r:   r;   r<   
startswith)ri   rj   r    ry   r$   r>   r?   r@   rA   rB   @py_assert5@py_assert7rE   s                rF   9test_exit_decision_closes_dead_fish_long_after_time_limitr   r   s'   ::::::::
bC
H
H
HCM3FFFE!!5TAUZcjpstttH>!T!>T!!!!!!!!!!>T!!!!!!!!!!!!8!!!!!8!!!!!!>!!!!T!!!!!!!!!!!!!!!!!!!!!!!!!!!?2?%2k2%%k2222222222222228222228222222?2222%2222k222222222222222222222222222222rH   c                  r   ddl m} m} t          dd          } | ddd	          } |dd
d|d|          }|j        }d}||u }|st          j        d|fd||f          dt          j                    v st          j	        |          rt          j
        |          ndt          j
        |          t          j
        |          dz  }dd|iz  }	t          t          j        |	                    d x}x}}d}
|j        }|
|v }|st          j        d|fd|
|f          t          j
        |
          dt          j                    v st          j	        |          rt          j
        |          ndt          j
        |          dz  }dd|iz  }	t          t          j        |	                    d x}
x}}d S )Nr   rh   rk   r   rl   ro   r   g      T@rp   g9X@rt   r
   ru   Tr!   rz   r$   r%   r)   r*   zSL Hit)in)z.%(py1)s in %(py5)s
{%(py5)s = %(py3)s.reason
})py1py3r(   )r1   ri   rj   r   r}   r4   r5   r6   r7   r8   r9   r:   r;   r<   )ri   rj   r    ry   r$   r>   r?   r@   rA   rB   @py_assert0@py_assert2s               rF   +test_exit_decision_closes_long_on_hard_stopr   ~   s"   ::::::::
bC
H
H
HCM3FFFE!!5TAUZceknoooH>!T!>T!!!!!!!!!!>T!!!!!!!!!!!!8!!!!!8!!!!!!>!!!!T!!!!!!!!!!!!!!!!!!!!!!!!!!!&x&8&&&&&&&&&&8&&&&&8&&&&&&&&&&&x&&&&&x&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&rH   c                 |   ddl m} | dz  }|                    d            |dd|t                                }|j        }d}||u }|st          j        d|fd	||f          d
t          j                    v st          j	        |          rt          j
        |          nd
t          j
        |          t          j
        |          dz  }dd|iz  }t          t          j        |                    d x}x}}|j        }d}||k    }|st          j        d|fd||f          d
t          j                    v st          j	        |          rt          j
        |          nd
t          j
        |          t          j
        |          dz  }dd|iz  }t          t          j        |                    d x}x}}d S )Nr   	risk_gateKILL_SWITCHstopro   daily_pnl_usddaily_trade_countkill_switch_pathr    Fr!   z/%(py2)s
{%(py2)s = %(py0)s.allowed
} is %(py5)sresultr%   r)   r*   kill_switchr,   r.   )riskr   
write_textr   allowedr4   r5   r6   r7   r8   r9   r:   r;   r<   )	tmp_pathr   r   r   r>   r?   r@   rA   rB   s	            rF   -test_risk_blocks_when_kill_switch_file_existsr      s   ]*K6"""YSAP[ajalalmmmF>"U">U"""""""""">U""""""""""""6"""""6"""""">""""U"""""""""""""""""""""""""""=)M)=M))))))))))=M))))))))))))6)))))6))))))=))))M)))))))))))))))))))))))))))))rH   c                  j   ddl m}  t          d          } | ddt          d          |          }|j        }d}||u }|st          j        d	|fd
||f          dt          j                    v st          j	        |          rt          j
        |          ndt          j
        |          t          j
        |          dz  }dd|iz  }t          t          j        |                    d x}x}}|j        }d}||k    }|st          j        d|fd||f          dt          j                    v st          j	        |          rt          j
        |          ndt          j
        |          t          j
        |          dz  }dd|iz  }t          t          j        |                    d x}x}}d S )Nr   r   g      9@)max_daily_loss_usdg(\9z/tmp/not-presentr   Fr!   r   r   r%   r)   r*   daily_loss_limitr,   r.   )r   r   r   r   r   r4   r5   r6   r7   r8   r9   r:   r;   r<   )r   r    r   r>   r?   r@   rA   rB   s           rF   !test_risk_blocks_daily_loss_limitr      s   
t
,
,
,CYVqSWXjSkSkqtuuuF>"U">U"""""""""">U""""""""""""6"""""6"""""">""""U"""""""""""""""""""""""""""=...=...........=.............6.....6......=..................................rH   c                  `   ddl m} m} d} | |          }d}||k    }|st          j        d|fd||f          dt          j                    v st          j        |          rt          j        |          ndt          j        |          dz  }d	d
|iz  }t          t          j
        |                    d x}} |||          }d}	||	u }
|
s&t          j        d|
fd||	f          dt          j                    v st          j        |          rt          j        |          nddt          j                    v st          j        |          rt          j        |          nddt          j                    v st          j        |          rt          j        |          ndt          j        |          t          j        |	          dz  }dd|iz  }t          t          j
        |                    d x}x}
}	d} |||          }d}||u }	|	st          j        d|	fd||f          dt          j                    v st          j        |          rt          j        |          ndt          j        |          dt          j                    v st          j        |          rt          j        |          ndt          j        |          t          j        |          dz  }dd|iz  }t          t          j
        |                    d x}x}x}	}d} |||          }d}||u }	|	st          j        d|	fd||f          dt          j                    v st          j        |          rt          j        |          ndt          j        |          dt          j                    v st          j        |          rt          j        |          ndt          j        |          t          j        |          dz  }dd|iz  }t          t          j
        |                    d x}x}x}	}d S )Nr   )expected_confirmationvalidate_panic_confirmation0x1234567890abcdefzCLOSE ALL cdefr,   )z%(py0)s == %(py3)sexpected)r&   r   zassert %(py5)sr(   Tr!   )z9%(py4)s
{%(py4)s = %(py0)s(%(py1)s, %(py2)s)
} is %(py7)sr   wallet)r&   r   r'   r{   r*   zassert %(py9)spy9z	CLOSE ALLF)z9%(py5)s
{%(py5)s = %(py0)s(%(py2)s, %(py3)s)
} is %(py8)s)r&   r'   r   r(   r0   zassert %(py10)spy10zCLOSE ALL beef)panic_guardr   r   r4   r5   r6   r7   r8   r9   r:   r;   )r   r   r   r   r   r>   @py_format4rA   r@   rC   r   rB   @py_format10r?   r   rE   @py_format11s                    rF   <test_panic_guard_requires_exact_wallet_specific_confirmationr      s   NNNNNNNN!F$$V,,H''8'''''''''''8'''''''''''''8'''''8''''''''''''''''''''''''''''''''&&x88@D@8D@@@@@@@@@@8D@@@@@@@@@@@@&@@@@@&@@@@@@@@@@@@@x@@@@@x@@@@@@@@@@@@@@@@@@@@@@@@8@@@@D@@@@@@@@@@@@@@@@@@@@@@@@@@@'2D&&{F;;DuD;uDDDDDDDDDD;uDDDDDDDDDDDD&DDDDD&DDDDDD{DDDDDDDDDDDFDDDDDFDDDDDD;DDDDuDDDDDDDDDDDDDDDDDDDDDDDDDDDDD'7I&&'7@@IEI@EIIIIIIIIII@EIIIIIIIIIIII&IIIII&IIIIII'7IIIIIIIIIIIIIIIIIIIIII@IIIIEIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIrH   )builtinsr6   _pytest.assertion.rewrite	assertionrewriter4   pathlibr   configr   rG   rK   ra   rf   r   r   r   r   r    rH   rF   <module>r      s                             % % %.) ) ),!. !. !.H/ / /6	3 	3 	3	' 	' 	'	* 	* 	*/ / /	J 	J 	J 	J 	JrH   