
    jl                     V    d dl Zd dlmc mZ d dlZd dlmZmZ d Z	d Z
d Zd Zd ZdS )    N)datetimetimezonec                 t    |                      d                    d |D                       dz   d           d S )N
c              3   >   K   | ]}t          j        |          V  d S )N)jsondumps).0es     S/home/agent/projects/CryptoTradingBot/Crypto_Agent/tests/test_signal_correlation.py	<genexpr>z_write_jsonl.<locals>.<genexpr>   s*      <<djmm<<<<<<    zutf-8)encoding)
write_textjoin)patheventss     r   _write_jsonlr      s>    OODII<<V<<<<<tCgOVVVVVr   c                 
   ddl m} | dz  }| dz  }t          |ddddgd	d
diddddddgdd
didddddgdd
didg           t          |dddddddddddd ddd!d"d#g            |||          }|j        }t	          |          }d}||k    }|st          j        d$|fd%||f          d&t          j                    v st          j	        t                    rt          j
        t                    nd&d't          j                    v st          j	        |          rt          j
        |          nd't          j
        |          t          j
        |          t          j
        |          d(z  }	d)d*|	iz  }
t          t          j        |
                    d x}x}x}}|j        d         }|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        }d0}||k    }|st          j        d$|fd1||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d2||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d3||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        }d4}||k    }|st          j        d$|fd5||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 )6Nr   correlate_trades_with_contexttrade_journal.jsonlmarket_context.jsonlmarket_context2026-01-01T10:00:00+00:00BTC
trend_downgffffff?rsi_14-   
event_typetscointagsreliability_score
indicators2026-01-01T10:10:00+00:00trend_upoi_available?>   2026-01-01T10:20:00+00:00ETHrsi_oversold皙?   entryz2026-01-01T10:12:00+00:00d      Fflash_crash)r!   r"   r#   pricesizedry_runreasonexitz2026-01-01T10:40:00+00:00n   TRAIL
   )r!   r"   r#   r5   r6   r7   r8   realized_pnl_usd==)zM%(py5)s
{%(py5)s = %(py0)s(%(py3)s
{%(py3)s = %(py1)s.matches
})
} == %(py8)slenresult)py0py1py3py5py8zassert %(py10)spy10)z,%(py2)s
{%(py2)s = %(py0)s.coin
} == %(py5)smatchrB   py2rE   assert %(py7)spy7)r(   r)   )z4%(py2)s
{%(py2)s = %(py0)s.context_tags
} == %(py5)s)z8%(py2)s
{%(py2)s = %(py0)s.realized_pnl_usd
} == %(py5)s)z3%(py2)s
{%(py2)s = %(py0)s.exit_reason
} == %(py5)s   )z=%(py2)s
{%(py2)s = %(py0)s.time_in_trade_minutes
} == %(py5)s)signal_correlationr   r   matchesr@   
@pytest_ar_call_reprcompare@py_builtinslocals_should_repr_global_name	_safereprAssertionError_format_explanationr#   context_tagsr=   exit_reasontime_in_trade_minutes)tmp_pathr   journalcontextrA   @py_assert2@py_assert4@py_assert7@py_assert6@py_format9@py_format11rH   @py_assert1@py_assert3@py_format6@py_format8s                   r   -test_match_entries_to_previous_market_contextrh   	   s   @@@@@@..G//G'/JTYdpcq  IL  ]e  gi  \j  	k  	k'/JTYdnp~c  WZ  ks  uw  jx  	y  	y'/JTYdrcs  KN  _g  ik  ^l  	m  	m   
 &A5[^hiv{  HU  	V  	V%@%Z]ghuz  GN  df  	g  	g   
 +*7G<<F~#3~#!#!##########!############3#####3#############v#####v######~########!#############################N1E:::55:=!==!===========!=============5=====5==========!============================!'R'!R''''''''''!R''''''''''''5'''''5''''''!''''R'''''''''''''''''''''''''''''''''''''''''''''''''''5'''''5'''''''''''''''''''''''''''''''''''''&,",&",,,,,,,,,,&",,,,,,,,,,,,5,,,,,5,,,,,,&,,,,",,,,,,,,,,,,,,,,,,,,,,,,,,,,,r   c                 h   ddl m} | dz  }| dz  }t          |ddddgd	i d
dddddgdi d
g           t          |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g            |||          }|j        d         }|j        }d}||k    }|st          j        d |fd!||f          t          j        |          t          j        |          t          j        |          d"z  }	d#d$|	iz  }
t          t          j	        |
                    d x}x}x}}|j        d         }|j
        }d%}||k    }|st          j        d |fd&||f          t          j        |          t          j        |          t          j        |          d"z  }	d#d$|	iz  }
t          t          j	        |
                    d x}x}x}}|j        d         }|j        }d%}| }||k    }|st          j        d |fd'||f          t          j        |          t          j        |          t          j        |          d"z  }d(d)|iz  }t          t          j	        |                    d x}x}x}x}}|j        d         }|j        }d}||k    }|st          j        d |fd!||f          t          j        |          t          j        |          t          j        |          d"z  }	d#d$|	iz  }
t          t          j	        |
                    d x}x}x}}|j        d         }|j
        }d*}||k    }|st          j        d |fd&||f          t          j        |          t          j        |          t          j        |          d"z  }	d#d$|	iz  }
t          t          j	        |
                    d x}x}x}}d S )+Nr   r   r   r   r   r   r   r(   r*   r    z2026-01-01T11:00:00+00:00r-   r.   r/   r1   2026-01-01T10:05:00+00:00r2   r3   Fr!   r"   r#   r5   r6   r7   r9   r'   e   BEr!   r"   r#   r5   r6   r7   r=   r8   z2026-01-01T11:05:00+00:00z2026-01-01T11:20:00+00:00b   SL   r>   )z3%(py3)s
{%(py3)s = %(py1)s.sample_size
} == %(py6)s)rC   rD   py6zassert %(py8)srF   g      ?)z0%(py3)s
{%(py3)s = %(py1)s.win_rate
} == %(py6)s)z4%(py3)s
{%(py3)s = %(py1)s.avg_pnl_usd
} == -%(py6)szassert %(py9)spy9g        )rN   r   r   	tag_statssample_sizerP   rQ   rU   rV   rW   win_rateavg_pnl_usd)r[   r   r\   r]   rA   @py_assert0r^   @py_assert5r_   @py_format7rb   r`   rg   @py_format10s                 r   ,test_tag_stats_aggregate_winrate_and_returnsr}   #   sP   @@@@@@..G//G'/JTYdnco  GJ  Z\  	]  	]'/JTYdnp~c  WZ  jl  	m  	m    &A5[^hiv{||%@%Z]ghuz  QR  ^b  	c  	c&A5[^hiv{||%@%Z\fgty  PR  ^b  	c  	c	    +*7G<<FJ'8'38q83q88888888883q88888'888838888q88888888888888888888888888888J'7'07C70C77777777770C77777'777707777C77777777777777777777777777777J';'3;;t;3t;;;;;;;;;;3t;;;;;';;;;3;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;N+<+7<1<71<<<<<<<<<<71<<<<<+<<<<7<<<<1<<<<<<<<<<<<<<<<<<<<<<<<<<<<<N+;+4;;4;;;;;;;;;;4;;;;;+;;;;4;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;r   c                    ddl m}m} | dz  }| dz  }t          |ddddgd	i d
g           t          |ddddddddddddddddg            |||          } ||d          }d}||v }|st	          j        d|fd||f          t	          j        |          dt          j                    v st	          j	        |          rt	          j        |          nddz  }	dd|	iz  }
t          t	          j        |
                    d x}}d }||v }|st	          j        d|fd||f          t	          j        |          dt          j                    v st	          j	        |          rt	          j        |          nddz  }	dd|	iz  }
t          t	          j        |
                    d x}}d}||v }|st	          j        d|fd||f          t	          j        |          dt          j                    v st	          j	        |          rt	          j        |          nddz  }	dd|	iz  }
t          t	          j        |
                    d x}}d S )!Nr   )r   render_correlation_reportr   r   r   r   r   r(   r*   r    r1   rj   r2   r3   Frk   r9   r,   f   rr   r;   rn      )min_sampleszObservation only)in)z%(py1)s in %(py3)sreport)rC   rD   zassert %(py5)srE   u   keine Strategieänderung)rN   r   r   r   rP   rQ   rU   rR   rS   rT   rV   rW   )r[   r   r   r\   r]   rA   r   ry   r^   @py_format4rf   s              r   6test_report_is_observational_until_minimum_sample_sizer   <   sT   [[[[[[[[..G//G*:B]gl  xB  wC  Z]  mo  p  p  q  r  r  r&A5[^hiv{||%@%Z]ghuz  QR  ^e  	f  	f   
 +*7G<<F&&v1===F'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''%/%//////////%/////%///////////////////////////////////////////:::r   c           
      &   ddl m} | dz  }| dz  }t          |ddddgd	i d
g           t          |dddddddg            |||          }|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        }g }||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    r1   rj   r2   r3   Frk   r>   )z:%(py2)s
{%(py2)s = %(py0)s.closed_trade_count
} == %(py5)srA   rI   rK   rL   )z8%(py2)s
{%(py2)s = %(py0)s.open_entry_count
} == %(py5)s)z/%(py2)s
{%(py2)s = %(py0)s.matches
} == %(py5)s)rN   r   r   closed_trade_countrP   rQ   rR   rS   rT   rU   rV   rW   open_entry_countrO   )
r[   r   r\   r]   rA   rd   r_   re   rf   rg   s
             r   6test_open_entries_are_excluded_from_closed_trade_statsr   O   sm   @@@@@@..G//G*:B]gl  xB  wC  Z]  mo  p  p  q  r  r  r'9T^cnq{|  JO  P  P  Q  R  R  R**7G<<F$))$))))))))))$))))))))))))6)))))6))))))$)))))))))))))))))))))))))))))))"'a'"a''''''''''"a''''''''''''6'''''6''''''"''''a'''''''''''''''''''''''''''>R>R>R66>Rr   )builtinsrR   _pytest.assertion.rewrite	assertionrewriterP   r   r   r   r   rh   r}   r   r    r   r   <module>r      s                  ' ' ' ' ' ' ' 'W W W- - -4< < <2     &         r   