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 a 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 time 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 are just the mean price over the choosen execution time period)

Installation via pip:

pip install positions_backtester

How to backtest your dataframe with positions

from positions_backtester import run_backtest

df_backtest_res = run_backtest(
    df_positions,
    df_real_exec_prices,
    is_to_neutralize=True,
    const_trading_fees_percent=0.01,
)

!!! df_real_exec_prices should have the same indices as df_positions

Arguments:

  1. df_positions:
    pd.DataFrame
    Positions we want to take with the frequency with which we want to change our positions
  2. df_real_exec_prices:
    pd.DataFrame
    Prices by which we can buy assets at the given moment
  3. is_to_neutralize=True,:
    bool
    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
  6. const_trading_fees_percent:
    float
    Broker trading fees

Input example: df_positions_short

Output: df_backtest_res

How to create execution prices

from positions_backtester import create_df_simple_execution_prices

df_real_exec_prices = create_df_simple_execution_prices(
    df_prices_full,
    data_frequency="30min",  # 30min, 1h, 3h, 6h, 1d
    offset="-3min",  # "-3min"
    td_trading_delay=datetime.timedelta(minutes=3),
    td_execution_duration=datetime.timedelta(minutes=2),
)

Arguments:

  1. df_prices_full:
    pd.DataFrame
    Prices of assets in higher resolution
    Higher resolution needed for more pricise execution evaluation
  2. td_trading_delay=None:
    datetime.timedelta
    Delay (Time) needed to calculate the wanted positions
  3. td_execution_duration:
    datetime.timedelta
    How long should the execution take
    Execution price will be the mean price over execution time period

Inputs:

df_prices_full

Formulas

PNL before_costs = (previous_position) * (price_change_%)
trading_volume = abs(new_wanted_position - previous_position)
PNL const_trading_fee = trading_volume * broker_commision
PNL after_costs = PNL before_costs - PNL const_trading_fee
PNL half_costs = PNL before_costs - PNL const_trading_fee/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.5.tar.gz (10.8 kB view details)

Uploaded Source

Built Distribution

positions_backtester-0.1.5-py3-none-any.whl (10.4 kB view details)

Uploaded Python 3

File details

Details for the file positions_backtester-0.1.5.tar.gz.

File metadata

  • Download URL: positions_backtester-0.1.5.tar.gz
  • Upload date:
  • Size: 10.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.13 CPython/3.9.7 Windows/10

File hashes

Hashes for positions_backtester-0.1.5.tar.gz
Algorithm Hash digest
SHA256 6bbc8de0a6a70c36f58f0c081d4db21da3eaffb6c57ec004168f36a38d288a22
MD5 5992f80c8ecd5ec8fc8f6a8294d22138
BLAKE2b-256 c208ed2d2751dc5f457615f232f3d2292eb41fc9d0160e719b90b2fc12c7057b

See more details on using hashes here.

File details

Details for the file positions_backtester-0.1.5-py3-none-any.whl.

File metadata

File hashes

Hashes for positions_backtester-0.1.5-py3-none-any.whl
Algorithm Hash digest
SHA256 8d371fd65ea26e6df90c428979b957464266e2c9fd4fe98da7b686494945ac4b
MD5 d218b2c161c4cc63a280dd0478a49b91
BLAKE2b-256 7b8a6f91e77ea8d9cb38dcfcb2f103c4d31fbe86bf142a10256d8d2e2c622af0

See more details on using hashes here.

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