market-break
a set of utilities for backtesting vectorized trading algorithms, high-frequency trading strategies and investing strategies.
Installation
pip install python-market-break
Connect to a live database for real-time data.
from sqlalchemy import create_engine
engine = create_engine('sqlite:///database/2024-06-14.sqlite')
Extract The data from the database into a DataFrame.
from cryptocore.market.database import extract_dataframe, table_name
EXCHANGE = "binance"
SYMBOL = "ETH/USDT"
df = extract_dataframe(engine, table_name(exchange=EXCHANGE, symbol=SYMBOL))
df = df.iloc[3500:7000].reset_index()
df
.dataframe tbody tr th {
vertical-align: top;
}
.dataframe thead th {
text-align: right;
}
</style>
| index | exchange | symbol | timestamp | datetime | received_datetime | open | high | low | close | bid | ask | bid_volume | ask_volume | side | |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 0 | 3500 | binance | ETH/USDT | 1.718353e+12 | 2024-06-14 11:19:38.208 | 2024-06-14 08:19:38.156 | 3510.78 | 3539.83 | 3428.0 | 3521.99 | 3521.98 | 3521.99 | 59.7590 | 21.0623 | buy |
| 1 | 3501 | binance | ETH/USDT | 1.718353e+12 | 2024-06-14 11:19:39.031 | 2024-06-14 08:19:39.155 | 3510.78 | 3539.83 | 3428.0 | 3521.98 | 3521.98 | 3521.99 | 58.2914 | 28.2213 | sell |
| 2 | 3502 | binance | ETH/USDT | 1.718353e+12 | 2024-06-14 11:19:39.857 | 2024-06-14 08:19:40.158 | 3510.78 | 3539.83 | 3428.0 | 3521.99 | 3521.98 | 3521.99 | 58.2418 | 24.8653 | buy |
| 3 | 3503 | binance | ETH/USDT | 1.718353e+12 | 2024-06-14 11:19:40.574 | 2024-06-14 08:19:41.179 | 3510.78 | 3539.83 | 3428.0 | 3521.99 | 3521.98 | 3521.99 | 63.9093 | 24.8653 | buy |
| 4 | 3504 | binance | ETH/USDT | 1.718353e+12 | 2024-06-14 11:19:42.080 | 2024-06-14 08:19:42.161 | 3510.78 | 3539.83 | 3428.0 | 3521.99 | 3521.98 | 3521.99 | 64.6069 | 14.6805 | buy |
| ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... |
| 3495 | 6995 | binance | ETH/USDT | 1.718357e+12 | 2024-06-14 12:17:55.995 | 2024-06-14 09:17:55.971 | 3493.19 | 3539.83 | 3428.0 | 3518.19 | 3518.19 | 3518.20 | 125.1496 | 5.5759 | sell |
| 3496 | 6996 | binance | ETH/USDT | 1.718357e+12 | 2024-06-14 12:17:56.069 | 2024-06-14 09:17:56.976 | 3493.19 | 3539.83 | 3428.0 | 3518.20 | 3518.19 | 3518.20 | 125.1496 | 5.5728 | buy |
| 3497 | 6997 | binance | ETH/USDT | 1.718357e+12 | 2024-06-14 12:17:57.779 | 2024-06-14 09:17:58.075 | 3492.59 | 3539.83 | 3428.0 | 3518.20 | 3518.19 | 3518.20 | 121.2325 | 7.5728 | buy |
| 3498 | 6998 | binance | ETH/USDT | 1.718357e+12 | 2024-06-14 12:17:58.886 | 2024-06-14 09:17:58.973 | 3492.59 | 3539.83 | 3428.0 | 3518.20 | 3518.19 | 3518.20 | 83.2826 | 7.7234 | buy |
| 3499 | 6999 | binance | ETH/USDT | 1.718357e+12 | 2024-06-14 12:18:00.028 | 2024-06-14 09:17:59.973 | 3492.60 | 3539.83 | 3428.0 | 3518.20 | 3518.19 | 3518.20 | 75.6586 | 9.3705 | buy |
3500 rows × 15 columns
Import column names to handle the data.
from market_break.labels import BID, ASK, DATETIME, ENTRY, EXIT, TYPE, LONG
Import a class of backtesting functions.
from market_break.backtest import Trades
Generate trades record, both short and long as a DataFrame, from definition of up-trends and down-trends.
trades = Trades.generate(
up=df[ASK] > df[ASK].shift(1),
down=df[BID] < df[BID].shift(1),
adjust=True
)
Process the results of the generated trades.
FEE = 0.001
returns = Trades.returns(trades, bid=df[BID], ask=df[ASK], fee=FEE)
Imports a class of Plotting trading results.
from market_break.backtest import Plot
Plot a histogram of the trades returns.
Plot.returns_histogram(returns, bins=35)
Plot a graph of the trades returns.
Plot.returns_signals(returns, index=df[DATETIME].iloc)
Plot pie graphs of the winning and losing trades and their profits and losses.
Plot.returns_pie(returns)
Plot the signals for long and short entries and exits, with the bid-ask spread. Also plotting The balance, profits and losses.
long_trades = trades[trades[TYPE] == LONG]
Plot.price_signals(
bid=df[BID], ask=df[ASK], index=df[DATETIME].iloc,
long=long_trades[ENTRY], short=long_trades[EXIT]
)
Plot.returns_balance(returns, index=df[DATETIME].iloc)
Import a class for generating a report from the results.
from market_break.backtest import Report
Generating and displaying the results with the report.
report = Report.generate(
index=df[DATETIME], returns=returns,
long=trades[EXIT].values, short=trades[EXIT].values
)
print(Report.repr(report))
[Index]
start 2024-06-14 11:19:38.20
end 2024-06-14 12:18:00.02
total duration 0 days 00:58:21.82
min. tick duration 0:00:00.06
max. tick duration 0:00:01.90
avg. tick duration 0:00:01.00
ticks 3500
[Trades]
long trades 125
short trades 125
min. TUW 0:0
max. TUW 0:0
avg. TUW 0:0
[Gains]
[%] min. gain 0.0003
[%] max. gain 0.1482
[%] avg. gain 0.0291
[%] total gains 2.4411
winning trades 84
[Losses]
[%] min. loss -0.0003
[%] max. loss -0.017
[%] avg. loss -0.0063
[%] total losses -0.2383
losing trades 38
[Performance]
PnL factor 10.245
avg. profit factor 1.0002
[%] win rate 67.2
[%] total profit 2.2029
Metadata
Release files for python-market-break 3.3.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| python_market_break-3.3.0.tar.gz | 21.6 kB | Without provenance | Details for python_market_break-3.3.0.tar.gz |
Release files / python_market_break-3.3.0.tar.gz
| Download URL | python_market_break-3.3.0.tar.gz |
|---|---|
| Size | 21.6 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
1b9073903ead6bbb6331d5fa64fb5cbd951072105f2bbe2c2af42b6bb9fd814e
|
|
BLAKE2b-256 checksum How to use checksums |
03903b41e21b7be134d4e816d947558ca6362e2ea5fd11d7532855387f1a640e
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/5.1.0 CPython/3.12.0
|
Security for python-market-break 3.3.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.