From 1b146a84d6d56e84a3b60cbd9e48186e40f149e0 Mon Sep 17 00:00:00 2001 From: David Brazda Date: Wed, 23 Oct 2024 16:54:17 +0200 Subject: [PATCH] daily fix --- setup.py | 2 +- ttools/__init__.py | 2 +- ttools/vbtutils.py | 56 ++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 58 insertions(+), 2 deletions(-) diff --git a/setup.py b/setup.py index 2cdbc1d..42c1869 100644 --- a/setup.py +++ b/setup.py @@ -2,7 +2,7 @@ from setuptools import setup, find_packages setup( name='ttools', - version='0.3.6', + version='0.3.7', packages=find_packages(), install_requires=[ 'vectorbtpro', diff --git a/ttools/__init__.py b/ttools/__init__.py index d946c3a..120c6ea 100644 --- a/ttools/__init__.py +++ b/ttools/__init__.py @@ -1,2 +1,2 @@ -from .vbtutils import AnchoredIndicator, create_mask_from_window, isrising, isfalling, isrisingc, isfallingc +from .vbtutils import AnchoredIndicator, create_mask_from_window, isrising, isfalling, isrisingc, isfallingc, trades2entries_exits from .vbtindicators import register_custom_inds \ No newline at end of file diff --git a/ttools/vbtutils.py b/ttools/vbtutils.py index 7eda9c6..101f0aa 100644 --- a/ttools/vbtutils.py +++ b/ttools/vbtutils.py @@ -4,6 +4,62 @@ import pandas_market_calendars as mcal from typing import Any import datetime + +def trades2entries_exits(trades: pd.DataFrame): + """ + Convert trades DataFrame to entries and exits DataFrame for use in lw plot + + Args: + trades (pd.DataFrame): Trades DataFrame from pf.trades.readable. + + Returns: + tuple: (entries DataFrame, exits DataFrame) + """ + # Create the entries DataFrame with 'Entry Index' as the datetime index + trade_entries = trades.set_index('Entry Index') + + # Create the exits DataFrame with 'Exit Index' as the datetime index + trade_exits = trades.set_index('Exit Index') + + cols_to_entries = { + # 'Size': '', + 'Direction': '', + 'Avg Entry Price': '' + } + + cols_to_exits = { + # 'Size': '', + # 'Direction': '', + 'PnL': 'c', + 'Avg Entry Price': '', + 'Return': 'r:' + } + + # Function to handle rounding and concatenation with labels + def format_row(row, columns_with_labels): + formatted_values = [] + for col, label in columns_with_labels.items(): + value = row[col] + # Check if the value is a float and round it to 4 decimals + if isinstance(value, float): + formatted_values.append(f"{label}{round(value, 3)}") + else: + formatted_values.append(f"{label}{value}") + return ', '.join(formatted_values) + + # Add concatenated column to entries DataFrame + trade_entries['text'] = trade_entries.apply(lambda row: format_row(row, cols_to_entries), axis=1) + + # Add concatenated column to exits DataFrame + trade_exits['text'] = trade_exits.apply(lambda row: format_row(row, cols_to_exits), axis=1) + + trade_exits["value"] = True + trade_entries["value"] = True + trade_entries["price"] = trade_entries["Avg Entry Price"] + trade_exits["price"] = trade_exits["Avg Exit Price"] + return trade_entries, trade_exits + + #TBD create NUMBA alternatives def isrising(series: pd.Series, n: int) -> pd.Series: """