
    >Cj                        d dl mZ d dlZd dlmc mZ d dlm	Z	 d dl
mZ d dlmZmZ dddZddZddZddZddZddZddZddZdS )    )annotationsN)Decimal)OrderIntent)PaperCandlesimulate_paper_lifecycle98103stopstrtake_profit
str | Nonereturnr   c                    t          di ddddddddd	d
dddddt          d          dd dd dt          |           d|t          |          nd dddddt          d          dt          d          S )Nstrategy_id#candidate_v76_strict_live_candidatesymbolzBTC/USDC:USDCcoinBTCsidebuyreduce_onlyF
order_typemarkettifIocsizez0.1pricetrigger_price	stop_lossr   client_order_idz
paper-testreasonconfirmed_squeeze_breakoutrisk_usdz0.20estimated_notional_usd10 )r   r   r
   r   s     P/home/agent/projects/CryptoTradingBot/Crypto_Agent/tests/test_paper_lifecycle.py_intentr)   	   s       99 U U	
 E 8 E U^^^ d d $--- -8,CGK((( % ,+    't}}}!     Nonec                    t          t          dd          t          d          t          t          d          t          d          t          d          t          d                    t          t          d          t          d          t          d	          t          d                    gt          d
          t          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        }d}t          |          }||k    }|st          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	        t                    rt          j
        t                    ndt          j
        |          t          j
        |          dz  }dd|iz  }	t          t          j        |	                    d x}x}x}}| j        }d}t          |          }||k    }|st          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	        t                    rt          j
        t                    ndt          j
        |          t          j
        |          dz  }dd|iz  }	t          t          j        |	                    d x}x}x}}| j        }d}t          |          }||k    }|st          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	        t                    rt          j
        t                    ndt          j
        |          t          j
        |          dz  }dd|iz  }	t          t          j        |	                    d x}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 }||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 }||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 S )$Nr   105r'   10010199openhighlowclose100.597.50.001z0.05entry_pricecandlesfee_rateslippage_pctr   ==z3%(py2)s
{%(py2)s = %(py0)s.exit_reason
} == %(py5)sresultpy0py2py5assert %(py7)spy7zP%(py2)s
{%(py2)s = %(py0)s.exit_price
} == %(py8)s
{%(py8)s = %(py4)s(%(py6)s)
}r   rC   rD   py4py6py8assert %(py10)spy10z1.00)zM%(py2)s
{%(py2)s = %(py0)s.mfe_pct
} == %(py8)s
{%(py8)s = %(py4)s(%(py6)s)
}z-2.50)zM%(py2)s
{%(py2)s = %(py0)s.mae_pct
} == %(py8)s
{%(py8)s = %(py4)s(%(py6)s)
}r   <)z2%(py2)s
{%(py2)s = %(py0)s.net_pnl_usd
} < %(py5)sF)is)z:%(py2)s
{%(py2)s = %(py0)s.live_order_allowed
} is %(py5)s)z=%(py2)s
{%(py2)s = %(py0)s.mainnet_signed_action
} is %(py5)s)r   r)   r   r   exit_reason
@pytest_ar_call_reprcompare@py_builtinslocals_should_repr_global_name	_safereprAssertionError_format_explanation
exit_pricemfe_pctmae_pctnet_pnl_usdlive_order_allowedmainnet_signed_action
rA   @py_assert1@py_assert4@py_assert3@py_format6@py_format8@py_assert5@py_assert7@py_format9@py_format11s
             r(   =test_lifecycle_stop_loss_records_mfe_mae_and_never_live_flagsrk      s   %Tu---ENNWU^^'%..gdmm[bch[i[ijjjWU^^''2B2BPV_fgk_l_lmmm
 !!V__	 	 	F ,,,,,,,,,,,,,,,,,,,,,,,,6,,,,,6,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,-------------------------6-----6------------------------------------------------------------->,V,WV__,>_,,,,,,,,,,>_,,,,,,,,,,,,6,,,,,6,,,,,,>,,,,,,,,,,,W,,,,,W,,,,,,V,,,,_,,,,,,,,,,,,,,,,,,,,,,,,,,,,,>-W-WW--->----------->-------------6-----6------>-----------W-----W------W----------------------------------!!!!!!!!!!!!!!!!!!!!!!!!6!!!!!6!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!$--$----------$------------6-----6------$-------------------------------'050'50000000000'50000000000006000006000000'00005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0r*   c                 `   t          t          dd          t          d          t          t          d          t          d          t          d          t          d                    gt          d	          t          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        }d}t          |          }||k    }|st          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	        t                    rt          j
        t                    ndt          j
        |          t          j
        |          dz  }dd|iz  }	t          t          j        |	                    d x}x}x}}| j        }d}t          |          }||k    }|st          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	        t                    rt          j
        t                    ndt          j
        |          t          j
        |          dz  }dd|iz  }	t          t          j        |	                    d x}x}x}}d S )Nr   r	   r'   r.   z103.1z99.5102r1   0r9   r   r>   r@   rA   rB   rF   rG   rH   r   rI   rM   rN   z0.3)zS%(py2)s
{%(py2)s = %(py0)s.gross_pnl_usd
} == %(py8)s
{%(py8)s = %(py4)s(%(py6)s)
})r   r)   r   r   rR   rS   rT   rU   rV   rW   rX   rY   rZ   r[   gross_pnl_usdra   s
             r(   +test_lifecycle_take_profit_before_time_exitrp   3   s   %Tu---ENN'%..ww7G7GWU[__dklqdrdrssstS\\  F ........................6.....6..............................................................6.....6.............................................................15175>>1>1111111111>111111111111611111611111111111111111711111711111151111>1111111111111111111111111111111r*   c                 .   t          t          dd           t          d          t          t          d          t          d          t          d          t          d                    t          t          d          t          d          t          d          t          d	                    gt          d
          t          d          t          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        }d}t          |          }||k    }|st          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	        t                    rt          j
        t                    ndt          j
        |          t          j
        |          dz  }dd|iz  }	t          t          j        |	                    d x}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.   104r	   r1   z103.5z101.5rm   2rn   )r:   r;   trailing_stop_pctr<   r=   trailing_stopr>   r@   rA   rB   rF   rG   z101.92rH   r   rI   rM   rN   r   )>)z2%(py2)s
{%(py2)s = %(py0)s.net_pnl_usd
} > %(py5)s)r   r)   r   r   rR   rS   rT   rU   rV   rW   rX   rY   rZ   r[   r^   ra   s
             r(   /test_lifecycle_trailing_stop_moves_up_and_exitsrw   A   s   %Tt,,,ENNWU^^'%..genn\cdi\j\jkkkWU^^''2B2BPWHXHX`ghm`n`nooo
 "#,,S\\
 
 
F 0000000000000000000000006000006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11 1 11 11111111111 1111111111111611111611111111111111111111111111111111 111111111111111111111111111111!!!!!!!!!!!!!!!!!!!!!!!!6!!!!!6!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!r*   c                 d
   t          t          dd          t          d          t          t          d          t          d          t          d          t          d                    gt          d	          t          d
          t          d          t          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        }d}t          |          }||k    }|st          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	        t                    rt          j
        t                    ndt          j
        |          t          j
        |          dz  }dd|iz  }	t          t          j        |	                    d x}x}x}}| j        }d}t          |          }||k    }|st          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	        t                    rt          j
        t                    ndt          j
        |          t          j
        |          dz  }dd|iz  }	t          t          j        |	                    d x}x}x}}| j        }| j        }| j        }
||
z
  }||k    }|s9t          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
        |          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}x}x}
}d S )N95110r'   r.   r/   r0   r6   r1   rs   z0.01rn   )r:   r;   hold_hours_per_candlefunding_rate_hourly_pctr<   r=   	time_exitr>   r@   rA   rB   rF   rG   rH   r   rI   rM   rN   z0.0020)zV%(py2)s
{%(py2)s = %(py0)s.funding_cost_usd
} == %(py8)s
{%(py8)s = %(py4)s(%(py6)s)
})z%(py2)s
{%(py2)s = %(py0)s.net_pnl_usd
} == (%(py6)s
{%(py6)s = %(py4)s.gross_pnl_usd
} - %(py9)s
{%(py9)s = %(py7)s.funding_cost_usd
}))rC   rD   rJ   rK   rG   py9zassert %(py12)spy12)r   r)   r   r   rR   rS   rT   rU   rV   rW   rX   rY   rZ   r[   funding_cost_usdr^   ro   )rA   rb   rc   rd   re   rf   rg   rh   ri   rj   @py_assert8@py_assert10@py_format13s                r(   )test_lifecycle_time_exit_and_funding_costr   S   sx   %Tu---ENN'%..wu~~7SW==`gho`p`pqqqr%cll 'S\\  F ,,,,,,,,,,,,,,,,,,,,,,,,6,,,,,6,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,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06000006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000000000000000000000000"7h7gh&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76777776777777"77777777777g77777g777777h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77777O!5O8OO!58O!OO!OOOOOOOOOOO!OOOOOOOOOOOOO6OOOOO6OOOOOOOOOOOOOOOOOOOOOOOOOOOO!5OOOOOOOOOOOOOOOOOOOOOO8OOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOr*   c                    t          t          dd          t          d          t          t          d          t          d          t          d          t          d                    gt          d	          t          d
          t          d
                    } | j        }d}t          |          }||k    }|st          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	        t                    rt          j
        t                    ndt          j
        |          t          j
        |          dz  }dd|iz  }t          t          j        |                    d x}x}x}}| j        }d}t          |          }||k    }|st          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	        t                    rt          j
        t                    ndt          j
        |          t          j
        |          dz  }dd|iz  }t          t          j        |                    d x}x}x}}| j        }d}t          |          }||k    }|st          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	        t                    rt          j
        t                    ndt          j
        |          t          j
        |          dz  }dd|iz  }t          t          j        |                    d x}x}x}}| j        }d}t          |          }||k    }|st          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	        t                    rt          j
        t                    ndt          j
        |          t          j
        |          dz  }dd|iz  }t          t          j        |                    d x}x}x}}|                                 }|d         }d}||k    }	|	slt          j        d|	fd||f          t          j
        |          t          j
        |          dz  }
dd|
iz  }t          t          j        |                    d x}x}	}|d         }d}||k    }	|	slt          j        d|	fd||f          t          j
        |          t          j
        |          dz  }
dd|
iz  }t          t          j        |                    d x}x}	}|d          }d}||k    }	|	slt          j        d|	fd||f          t          j
        |          t          j
        |          dz  }
dd|
iz  }t          t          j        |                    d x}x}	}d S )!Nry   rz   r'   r.   r/   r0   r6   r1   4rn   )r:   r;   max_fill_notional_usdr<   r=   z
0.10000000r>   )zT%(py2)s
{%(py2)s = %(py0)s.requested_size
} == %(py8)s
{%(py8)s = %(py4)s(%(py6)s)
}rA   r   rI   rM   rN   z
0.04000000)zQ%(py2)s
{%(py2)s = %(py0)s.filled_size
} == %(py8)s
{%(py8)s = %(py4)s(%(py6)s)
}z
0.06000000)zS%(py2)s
{%(py2)s = %(py0)s.unfilled_size
} == %(py8)s
{%(py8)s = %(py4)s(%(py6)s)
}z0.4000)zP%(py2)s
{%(py2)s = %(py0)s.fill_ratio
} == %(py8)s
{%(py8)s = %(py4)s(%(py6)s)
}requested_size)z%(py1)s == %(py4)s)py1rJ   zassert %(py6)srK   filled_sizeunfilled_size)r   r)   r   r   r   rS   rT   rU   rV   rW   rX   rY   rZ   r   r   
fill_ratioto_journal_row)rA   rb   rg   rh   rd   ri   rj   row@py_assert0@py_assert2@py_format5@py_format7s               r(   Atest_lifecycle_partial_fill_caps_position_size_and_journal_fieldsr   d   s   %Tu---ENN'%..wu~~7SW==`gho`p`pqqqr%cllS\\  F  9L9GL$9$99 $99999999999 $99999999999996999996999999 99999999999G99999G999999L9999$99999999999999999999999999999966!6!66!66666666666!6666666666666666666666666666666666666666666666666666!6666666666666666666666666666668<87<#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86888886888888888888888887888887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8888888888811 1 11 11111111111 1111111111111611111611111111111111111111111111111111 111111111111111111111111111111



!
!C 0L0 L0000000000 L00000 0000L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}------------------------------------------------/</<//////////</////////</////////////////////////////r*   c                    t          t          dd          t          d          t          t          d          t          d          t          d          t          d                    gt          d          t          d	          d
dt          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        }d}t          |          }||k    }|st          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	        t                    rt          j
        t                    ndt          j
        |          t          j
        |          dz  }dd|iz  }	t          t          j        |	                    d x}x}x}}| j        }d}t          |          }||k    }|st          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	        t                    rt          j
        t                    ndt          j
        |          t          j
        |          dz  }dd|iz  }	t          t          j        |	                    d x}x}x}}| j        }d}t          |          }||k    }|st          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	        t                    rt          j
        t                    ndt          j
        |          t          j
        |          dz  }dd|iz  }	t          t          j        |	                    d x}x}x}}| j        }d}t          |          }||k    }|st          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	        t                    rt          j
        t                    ndt          j
        |          t          j
        |          dz  }dd|iz  }	t          t          j        |	                    d x}x}x}}| j        }d}t          |          }||k    }|st          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	        t                    rt          j
        t                    ndt          j
        |          t          j
        |          dz  }dd|iz  }	t          t          j        |	                    d x}x}x}}d S )!Nry   r/   r'   r.   z101.1r0   r1   z0.0001r8   makertakerz0.10)r:   r;   maker_fee_ratetaker_fee_rateentry_liquidityexit_liquiditydepth_slippage_pctr   r>   r@   rA   rB   rF   rG   z
0.00100000)zS%(py2)s
{%(py2)s = %(py0)s.entry_fee_usd
} == %(py8)s
{%(py8)s = %(py4)s(%(py6)s)
}r   rI   rM   rN   z
0.01010000)zR%(py2)s
{%(py2)s = %(py0)s.exit_fee_usd
} == %(py8)s
{%(py8)s = %(py4)s(%(py6)s)
}rn   )zX%(py2)s
{%(py2)s = %(py0)s.entry_slippage_pct
} == %(py8)s
{%(py8)s = %(py4)s(%(py6)s)
})zW%(py2)s
{%(py2)s = %(py0)s.exit_slippage_pct
} == %(py8)s
{%(py8)s = %(py4)s(%(py6)s)
})zW%(py2)s
{%(py2)s = %(py0)s.slippage_cost_usd
} == %(py8)s
{%(py8)s = %(py4)s(%(py6)s)
})r   r)   r   r   rR   rS   rT   rU   rV   rW   rX   rY   rZ   entry_fee_usdexit_fee_usdentry_slippage_pctexit_slippage_pctslippage_cost_usdra   s
             r(   9test_lifecycle_maker_taker_fee_classes_and_depth_slippager   x   s   %Tu---ENN'%..ww7G7GWUY]]bijobpbpqqqrx((w''"6??	 	 	F ........................6.....6.....................................8<87<#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86888886888888888888888887888887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88888888888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76777776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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7777$444$4444444444$4444444444446444446444444$4444444444444444444444444444444444444444444444444444444#6v6wv6#6666666666#6666666666666666666666666#66666666666w66666w666666v666666666666666666666666666666666#<|<w|'<'<<#'<<<<<<<<<<<#'<<<<<<<<<<<<<6<<<<<6<<<<<<#<<<<<<<<<<<w<<<<<w<<<<<<|<<<<'<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<r*   c                 	   t          t          dd          t          d          t          t          d          t          d          t          d          t          d                    gt          d	          t          d	          t          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        }d}t          |          }||k    }|st          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	        t                    rt          j
        t                    ndt          j
        |          t          j
        |          dz  }dd|iz  }	t          t          j        |	                    d x}x}x}}| j        }d
}t          |          }||k    }|st          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	        t                    rt          j
        t                    ndt          j
        |          t          j
        |          dz  }dd|iz  }	t          t          j        |	                    d x}x}x}}| j        }d}t          |          }||k     }|st          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	        t                    rt          j
        t                    ndt          j
        |          t          j
        |          dz  }dd|iz  }	t          t          j        |	                    d x}x}x}}d S )Nr   r-   r'   r.   97r7   96r1   rn   z0.50)r:   r;   r<   r=   stop_gap_slippage_pctstop_gap_slippager>   r@   rA   rB   rF   rG   z96.51500000rH   r   rI   rM   rN   )z[%(py2)s
{%(py2)s = %(py0)s.stop_gap_slippage_pct
} == %(py8)s
{%(py8)s = %(py4)s(%(py6)s)
}z-0.30rO   )zP%(py2)s
{%(py2)s = %(py0)s.net_pnl_usd
} < %(py8)s
{%(py8)s = %(py4)s(%(py6)s)
})r   r)   r   r   rR   rS   rT   rU   rV   rW   rX   rY   rZ   r[   r   r^   ra   s
             r(   1test_lifecycle_stop_gap_slippage_exits_below_stopr      s#   %Tu---ENN'$--gfoo7SW==`ghn`o`opppqS\\%foo  F 4!44!44444444444!444444444444464444464444444444!444444444444444444444444444466 6 66 66666666666 6666666666666666666666666666666666666666666666666666 666666666666666666666666666666':6:76??:'?::::::::::'?::::::::::::6:::::6::::::':::::::::::7:::::7::::::6::::?:::::::::::::::::::::::::::::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06000006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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0r*   )r   r	   )r
   r   r   r   r   r   )r   r+   )
__future__r   builtinsrU   _pytest.assertion.rewrite	assertionrewriterS   decimalr   src.execution.order_intentr   src.execution.paper_lifecycler   r   r)   rk   rp   rw   r   r   r   r   r&   r*   r(   <module>r      s!   " " " " " "                     2 2 2 2 2 2 O O O O O O O O    *1 1 1 1*2 2 2 2" " " "$P P P P"0 0 0 0(= = = =(1 1 1 1 1 1r*   