Skip to main content

Package to backtest trading strategies (dataframe with positions) with execution costs modeling

Project description

GitHub last commit GitHub license<space><space> PyPI PyPI - Python Version

Short Overview.

positions_backtester is a python package (py>=3.7) to backtest trading strategies (dataframe with positions) with execution costs modeling

This package is trying to solve problem of slow trading with fast data.
Let’s say that you want to update your trading position once in a hour, day, week, …
But you have data with much higher resolution - minutes, seconds, miliseconds
Then you can give the wanted positions dataframe with the tick with which you want to trade
And higher resolution will be used to calculate approximate execution prices
Which will be just mean price over execution time period

Installation via pip:

pip install positions_backtester

How to use it

Create Backtester

from positions_backtester import Backtester

backtester = Backtester(float_percent_const_trading_fees=0.01,)

How to backtest your dataframe with positions

from positions_backtester import Backtester

df_backtest_res = backtester.backtest(
    df_positions_short,
    df_prices_full,
    is_to_neutralize=True,
    td_trading_delay=None,
    td_execution_duration=None,
)

Arguments:

  1. df_positions_short:
    pd.DataFrame
    Positions we want to take with the frequency with which we want to change our positions
  2. df_prices_full:
    pd.DataFrame
    Prices of assets in higher resolution
    Higher resolution needed for better execution evaluation
  3. is_to_neutralize=True,:
    Flag if to have long-short equal positions
  4. td_trading_delay=None:
    datetime.timedelta
    Delay needed to calculate the wanted positions
  5. td_execution_duration:
    datetime.timedelta
    How long should the execution take
    Execution price will be the mean price over execution time period

Inputs:

df_positions_short

df_prices_full

Output: df_backtest_res

Formulas

PNL_before_costs = (previous_position) * (price_change_%)
trading_volume = abs(new_wanted_position - previous_position)
const_trading_fee_pnl = trading_volume * broker_commision
execution_fee_pnl = (new_wanted_position - previous_position) * (execution_price - current_price)
PNL_after_costs = PNL_before_costs - (const_trading_fee_pnl + execution_fee_pnl)
PNL_half_costs = PNL_before_costs - (const_trading_fee_pnl + execution_fee_pnl) / 2.0

Contacts

License

This project is licensed under the MIT License.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

positions_backtester-0.1.2.tar.gz (6.9 kB view hashes)

Uploaded Source

Built Distribution

positions_backtester-0.1.2-py3-none-any.whl (6.1 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page