Package to backtest trading strategies (dataframe with positions) with execution costs modeling
Project description
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:
- df_positions_short:
- pd.DataFramePositions we want to take with the frequency with which we want to change our positions
- df_prices_full:
- pd.DataFramePrices of assets in higher resolutionHigher resolution needed for better execution evaluation
- is_to_neutralize=True,:
- Flag if to have long-short equal positions
- td_trading_delay=None:
- datetime.timedeltaDelay needed to calculate the wanted positions
- td_execution_duration:
- datetime.timedeltaHow long should the execution takeExecution 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
Links
Project local Links
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
Built Distribution
Close
Hashes for positions_backtester-0.1.2.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | 9721a817b8841c175ed925ebdae85361ed59ea07b49defbd93f1591cdd33a585 |
|
MD5 | 97cba390e058f0970eb1b5105192c6a3 |
|
BLAKE2b-256 | 840e4314ffed70f65b6b4e470f7f65dd1a2a74f89be8c57d137a6044530f8567 |
Close
Hashes for positions_backtester-0.1.2-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 25f764a2a52258e6f4c8af2237f7c536e9d9e3eced1763b3f92edab96f573529 |
|
MD5 | 3e0f9d68a07d654a6659c734aa33aff5 |
|
BLAKE2b-256 | 4ae591913ebfcdf5acf30901adc8612d8575194f00eda4e431acb792536aa3cb |