Skip to main content

Package for backtesting trading strategies and updating required files

Project description

traderbacktesteroptalpha

This Python script provides three main functionalities:

  1. Updating essential files from a server (update_files function).
  2. Utility functions for backtesting and trading strategies (TraderBackTesterUtils class).
  3. Data manipulation functions for backtesting and trading strategies (TraderBackTesterDM class).

Requirements

This script requires the following libraries:

  • pandas_ta==0.3.14b0
  • swifter==1.3.4
  • openpyxl==3.1.2
  • ta==0.10.2
  • pandas==1.5.3
  • numpy==1.23.5

To install these dependencies, use the following command:

pip install pandas_ta==0.3.14b0 swifter==1.3.4 openpyxl==3.1.2 ta==0.10.2 pandas==1.5.3 numpy==1.23.5  

Usage

1. File Updater (update_files function)

The update_files function downloads and saves three files required for backtesting:

  • nse_holidays.xlsx: A list of market holidays.
  • angel_tokens.csv: Token data for various trading instruments.
  • all.csv: A list of available instruments.

To update these files, call the function:

update_files(files_path='path/to/save/files/', file_server_url='http://your_server_url/')

2. TraderBackTesterUtils Class

This class provides multiple utility methods for trading and backtesting. Initialize it with the path where required files are stored.

Initialization

trader_backtester_utils = TraderBackTesterUtils(files_path='path/to/files/')

Methods

get_delta_strike_def(name: str) -> pd.DataFrame

Returns strike price details for a given instrument, calculating the differences between consecutive strikes.

round_to(row: Any, num_column: str = 'open', precision_column_val: Any = .05) -> float

Rounds a given value to the nearest tick size (default: 0.05).

get_strike(x: Any, num_column: str = 'open', precision_column_val: Any = .05) -> Any

Rounding method that applies round_to on a DataFrame using swifter for parallel processing.

exp_cal(row: Any, x_org: pd.DataFrame, dat: str = 'date', nxt_exp: Any = 0, montly: Any = False) -> str

Calculates the next expiry date for an instrument. Determines weekly or monthly expiry based on input parameters.

get_exp(x: Any, x_org: pd.DataFrame, dat: str = 'date', nxt_exp: Any = 0, montly: bool = False) -> Any

Uses exp_cal to calculate expiries for a DataFrame.

get_lot(name: str) -> pd.DataFrame

Fetches lot size for a specified instrument name.

add_n_lot_only(data: pd.DataFrame, ticker_column: str, exp_colummn_name: str, column_name_to_create: str) -> pd.DataFrame

Adds a new column with lot sizes based on the ticker and expiry date.

is_holiday(date: str = '') -> bool

Checks if a given date is a market holiday. Defaults to checking today's date.

Example Usage

# Initializing
trader_backtester_utils = TraderBackTesterUtils(files_path='path/to/files/')

# Getting delta strike definitions
delta_strike = trader_backtester_utils.get_delta_strike_def(name='NIFTY')

# Rounding example
rounded_value = trader_backtester_utils.round_to(12569.67)

# Calculating expiry
expiry_date = trader_backtester_utils.get_exp(x='2022-04-20', x_org=your_dataframe)

# Adding lot sizes to DataFrame
updated_data = trader_backtester_utils.add_n_lot_only(data=your_dataframe, ticker_column='ticker', exp_colummn_name='expiry', column_name_to_create='lot_size')

3. TraderBackTesterDM Class

This class provides multiple data manipulation methods for trading and backtesting

Initialization

trader_backtester_dm = TraderBackTesterDM()

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

traderbacktesteroptalpha-1.4.tar.gz (10.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

traderbacktesteroptalpha-1.4-py3-none-any.whl (10.0 kB view details)

Uploaded Python 3

File details

Details for the file traderbacktesteroptalpha-1.4.tar.gz.

File metadata

  • Download URL: traderbacktesteroptalpha-1.4.tar.gz
  • Upload date:
  • Size: 10.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.9.7

File hashes

Hashes for traderbacktesteroptalpha-1.4.tar.gz
Algorithm Hash digest
SHA256 9ed26e5364fd84aa7328cb15d938fc300d5647f3aa1c22d0f06d370bcba7ed90
MD5 2e74d63e8cf46bb7626268b2f3b75f8f
BLAKE2b-256 d731e6b7b593e1c018b719faf29c4924af846dc1276937a7a948a0da8d08f510

See more details on using hashes here.

File details

Details for the file traderbacktesteroptalpha-1.4-py3-none-any.whl.

File metadata

File hashes

Hashes for traderbacktesteroptalpha-1.4-py3-none-any.whl
Algorithm Hash digest
SHA256 d20457a7ad2b4a59306c749f1910cd1aacd6cb3edeada80863bdac0663f7ac8a
MD5 478327dfac34d38935770ac7974d734b
BLAKE2b-256 909d3b21d16ebbf562614338e36a5774bea010a51a70644ca2b9a9b5ca13d765

See more details on using hashes here.

Supported by

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