Skip to content

How to get trades dataframe contain last trade that is still active? #1074

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Amirefarani opened this issue Oct 12, 2023 · 2 comments
Closed
Labels
question Not a bug, but a FAQ entry

Comments

@Amirefarani
Copy link

Expected Behavior

Hello, I have a strategy for trade. After trade with backtesting library, I have some trades list in a dataframe called trades. but the problem is closing active trades. I mean when my strategy open one or more trades at the last candles, while they are still active, the library close all of them and append them to trades dataframe. When you comment the code that close the last trades, active trades will not show in dataframe. Is there any way to get active trades in this dataframe or in another variable?

@s-kust
Copy link

s-kust commented Aug 17, 2024

I have recently published a solution based on a backtesting package, which along with stats and stats._trades also provides a last_day_result dictionary containing data for the last day.

This dictionary includes the following:

all_columns = [
        "current_position_num_stocks",
        "all_current_trades",
        "today_special_situation_msg",
        "current_position_size",
        "desired_size",
        "desired_size_msg",
        "today_action",
    ]

See https://github.com/s-kust/python-backtesting-template/blob/main/strategy/run_backtest_for_ticker.py - here you run the backtest

Here - https://github.com/s-kust/python-backtesting-template/blob/main/utils/strategy_exec/last_day.py - the last_day_result dictionary is filled

@kernc
Copy link
Owner

kernc commented Feb 2, 2025

Finalizing open trades on the last bar was made optional in #393 (to be released shortly).

After a backtest run, you can always access open trades via the strategy instance: stats["_strategy"].trades, and append them to the df or something.

@kernc kernc closed this as completed Feb 2, 2025
@kernc kernc added the question Not a bug, but a FAQ entry label Feb 2, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Not a bug, but a FAQ entry
Projects
None yet
Development

No branches or pull requests

3 participants