Installation
Create a virtualenv.
pip install mptradelib
**Install pandas_ta as extra.
Create strategy
mpt create <strategy_name>
Backtest
Prepare data (example)
import pandas as pd
import numpy as np
df = pd.read_csv("something.csv")
df.index = pd.to_datetime(df.datetime)
df.index = df.index.tz_convert('Asia/Kolkata')
df.datetime = df.index
Data must have columns
datetime, open, high, low, close
Code
import pandas_ta as ta
import numpy as np
def cross(ema1, ema2):
return (ema1 > ema2) & (ema1.shift(1) < ema2.shift(1))
def compute(df, params):
df['ema_fast'] = ta.ema(df.close, params['fast_ema_len'])
df['ema_slow'] = ta.ema(df.close, params['slow_ema_len'])
df['ema_trend'] = ta.ema(df.close, params['trend_filter_ema_len'])
long_cond = (cross(df.ema_fast, df.ema_slow)) & (df.close > df.ema_trend)
short_cond = (cross(df.ema_slow, df.ema_fast)) & (df.close < df.ema_trend)
df['long'] = np.where(long_cond, 1, 0)
df['long_entries'] = np.where((df.long == 1) & (df.long.shift(1) != 1), 1, 0)
df['short'] = np.where(short_cond, -1, 0)
df['short_entries'] = np.where((df.short == -1) & (df.short.shift(1) != -1), -1, 0)
df['entries'] = df.long_entries + df.short_entries
return df
Compute df['entries'] with 1 for BUY and -1 for SELL.
Run
from mptradelib.vectorised_backtest import Backtest
b = Backtest(df, compute)
result = b.run(ema_fast=20, ema_slow=50, ema_trend=200, sl=1, tp=2)
sl and tp are in percentage and mandatory.
params passed in run can be accessed using params inside compute
Optimize
from mptradelib.vectorised_backtest import Backtest
optimization_params = {
ema_fast: range(1, 20, 1),
ema_slow: range(20, 50, 1),
ema_trend: range(100, 200, 1),
sl: range(0, 1),
tp: range(1, 10),
}
b = Backtest(df, compute)
result = b.optimize(runs=1, **optimization_params)
Live Trading
Code
import redis
from mptradelib.broker.session import FyersSession
from mptradelib.broker.ticker import LiveTicker
from mptradelib.broker.broker import HistoricalV2
from mptradelib.feed import Datas
from mptradelib.livetrading import BaseStrategy, LiveTrading
import threading
import pandas_ta as ta
import datetime as dt
class MyStrategy(BaseStrategy):
ema_fast = 20
ema_slow = 50
ema_trend = 200
sl = 1
tp = 2
def next(self, symbol, data):
ema_fast = ta.ema(data.df.close, self.ema_fast)
ema_slow = ta.ema(data.df.close, self.ema_slow)
ema_trend = ta.ema(data.df.close, self.ema_trend)
self.b.buy()
Run
mpt runlive <strategy_name> --symbols NSE:SBIN-EQ,NSE:CANB-EQ --param {}
Param
Param can be in two formats-
pars = {
"NSE:SBIN-EQ": {
"ema_fast": 20,
"ema_slow": 50,
"ema_trend": 200,
"sl": 1,
"tp": 2
}
}
OR
{
"ema_fast": 20,
"ema_slow": 50,
"ema_trend": 200,
"sl": 1,
"tp": 2
}
In later case, same params are used for all symbols.
Metadata
Release files for mptradelib 0.5.11
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 |
|---|---|---|---|---|
| mptradelib-0.5.11.tar.gz | 29.1 kB | Without provenance | Details for mptradelib-0.5.11.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| mptradelib-0.5.11-py3-none-any.whl | Python 3 | none | any | Details for mptradelib-0.5.11-py3-none-any.whl |
Total release size: 64.0 kB
Release files / mptradelib-0.5.11.tar.gz
| Download URL | mptradelib-0.5.11.tar.gz |
|---|---|
| Size | 29.1 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
8ffa14bfd34523e7b5009a3c453d5eec3547aab8f422a14ea392a23eb6b545b1
|
|
BLAKE2b-256 checksum How to use checksums |
fe454d01fa9c656a30559e510ce9b99fc600283231192cc4dff3e42577cc1f81
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/1.8.2 CPython/3.10.13 Darwin/23.5.0
|
Release files / mptradelib-0.5.11-py3-none-any.whl
| Download URL | mptradelib-0.5.11-py3-none-any.whl |
|---|---|
| Size | 34.9 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
11cbe44697c21705a622220b764b23fa4dce98710c0524f502bea09870f461ea
|
|
BLAKE2b-256 checksum How to use checksums |
8e0686724c5aff33e0df41ebb18b26c80f6c56cb4b832b7e0b2bfde664567570
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/1.8.2 CPython/3.10.13 Darwin/23.5.0
|
Security for mptradelib 0.5.11
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.