This commit is contained in:
David Brazda
2024-10-23 17:14:33 +02:00
parent 1b146a84d6
commit ac00480d55
2 changed files with 16 additions and 5 deletions

View File

@ -2,7 +2,7 @@ from setuptools import setup, find_packages
setup( setup(
name='ttools', name='ttools',
version='0.3.7', version='0.3.8',
packages=find_packages(), packages=find_packages(),
install_requires=[ install_requires=[
'vectorbtpro', 'vectorbtpro',

View File

@ -5,16 +5,24 @@ from typing import Any
import datetime import datetime
def trades2entries_exits(trades: pd.DataFrame): def trades2entries_exits(pf):
""" """
Convert trades DataFrame to entries and exits DataFrame for use in lw plot Convert trades from Portfolio to entries and exits DataFrame for use in lw plot
For each trade exit type is fetched from orders.
Args: Args:
trades (pd.DataFrame): Trades DataFrame from pf.trades.readable. pf Portfolio object: trades and orders
Returns: Returns:
tuple: (entries DataFrame, exits DataFrame) tuple: (entries DataFrame, exits DataFrame)
""" """
trades = pf.trades.readable
orders = pf.orders.readable
# Merge the dataframes on 'order_id'
trades = trades.merge(orders[['Order Id', 'Stop Type']], left_on='Exit Order Id', right_on='Order Id', how='left').drop(columns=['Order Id'])
# Create the entries DataFrame with 'Entry Index' as the datetime index # Create the entries DataFrame with 'Entry Index' as the datetime index
trade_entries = trades.set_index('Entry Index') trade_entries = trades.set_index('Entry Index')
@ -32,7 +40,8 @@ def trades2entries_exits(trades: pd.DataFrame):
# 'Direction': '', # 'Direction': '',
'PnL': 'c', 'PnL': 'c',
'Avg Entry Price': '', 'Avg Entry Price': '',
'Return': 'r:' 'Return': 'r:',
'Stop Type': '',
} }
# Function to handle rounding and concatenation with labels # Function to handle rounding and concatenation with labels
@ -41,6 +50,8 @@ def trades2entries_exits(trades: pd.DataFrame):
for col, label in columns_with_labels.items(): for col, label in columns_with_labels.items():
value = row[col] value = row[col]
# Check if the value is a float and round it to 4 decimals # Check if the value is a float and round it to 4 decimals
if value is None:
continue
if isinstance(value, float): if isinstance(value, float):
formatted_values.append(f"{label}{round(value, 3)}") formatted_values.append(f"{label}{round(value, 3)}")
else: else: