Test your own strategies with python and a dataset. A hackclub siege project
Project description
PYTHON STRATEGY BACKTESTING
A cool Python backtesting tool.
The idea
You load CSV chart data into the library, build your strategy on top of it and SIMULATE it! 🥳
Installation
pip install strategy_backtesting
Install it with PIP and then USE IT!!
Keep in mind that all the CSV data need to follow this format:
timestamp,open,high,low,close,volume
where timestamp is in milliseconds and ordered from OLDEST to NEWEST.
Example
To start, let's import the required libraries:
import strategy_backtesting as sb
import pandas as pd
After that we will define the settings for our simulation
timespan - The number of data points (e.g., 1000)
buy_amount - What the simulator will buy if triggered
sell_amount - What the simulator will sell if triggered
settings = sb.Settings(timespan=1000, buy_amount=1, sell_amount=1)
Then we will create our chart object and apply the settings and CSV file (in this case the ETHUSDT chart data)
eth_chart = sb.ChartManager()
eth_chart.set_chart_settings(settings=settings)
eth_chart.set_chart_data(pd.read_csv("ETHUSDT_1h.csv").iloc[::-1])
Then I will add my strategy, for example: Buying it only if the RSI is in cricial area
buy_data = []
sell_data = []
oversold = 30
overbought = 70
for _, row in experiment_chart.iterrows():
if not pd.notna(row["RSI"]):
continue
rsi = row["RSI"]
if rsi < oversold:
buy_data.append([row["timestamp"], row.get("close")])
elif rsi > overbought:
sell_data.append([row["timestamp"], row.get("close")])
Adding the strategy and simulating it
strategy = sb.Strategy(name="RSI Strategy")
strategy.set_strategy_buys(buys=buy_data)
strategy.set_strategy_sells(sells=sell_data)
simulation = sb.Simulation(chart=experiment_chart, settings=settings, strategy=strategy)
portfolio = simulation.simulate()
print("TOTAL PnL: "+str(round((portfolio["balance"].iloc[-1])-settings.default_money, 2))+"$")
simulation.graph(rsi=True, rsi_over=[oversold, overbought], ema=True)
Project details
Release history Release notifications | RSS feed
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
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file strategy_backtesting-0.1.0.tar.gz.
File metadata
- Download URL: strategy_backtesting-0.1.0.tar.gz
- Upload date:
- Size: 4.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.9.16 {"installer":{"name":"uv","version":"0.9.16","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":null,"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
1e18767033156b82293df5547e04f9b9eaf5566c4628aa55c67c11b4563e3217
|
|
| MD5 |
e9db27a60e92fa12f8a9734de52abbfc
|
|
| BLAKE2b-256 |
6a5e6baad5aea32449ebfd9ee7a801aec2690772443ea68ced67788c72404baf
|
File details
Details for the file strategy_backtesting-0.1.0-py3-none-any.whl.
File metadata
- Download URL: strategy_backtesting-0.1.0-py3-none-any.whl
- Upload date:
- Size: 4.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.9.16 {"installer":{"name":"uv","version":"0.9.16","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":null,"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
c947a1b1cac063ff5565957fdecdb317fecac007753cc88fa9d09d20cfa20d7d
|
|
| MD5 |
c1af1dd181d9767a1a00088ca43f5ca3
|
|
| BLAKE2b-256 |
3934cbe00b9bfed526e2a17129a53d2fd77f7aa3301ec7fc5f11b3a0e71fc76b
|