Skip to main content

A set of tools to help create algorithmic trading strategies and test them, in a vectorized way.

Project description

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 market_break.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
<style scoped> .dataframe tbody tr th:only-of-type { vertical-align: middle; }
.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)

png

Plot a graph of the trades returns.

Plot.returns_signals(returns, index=df[DATETIME].iloc)

png

Plot pie graphs of the winning and losing trades and their profits and losses.

Plot.returns_pie(returns)

png

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)

png

png

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

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

python_market_break-3.0.0.tar.gz (20.7 kB view hashes)

Uploaded Source

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