Skip to main content

No project description provided

Project description

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()

r = redis.Redis(
    host='127.0.0.1',
    port=6379,
    decode_responses=True # <-- this will ensure that binary data is decoded
)


f = FyersSession()
feed = LiveTicker(f, r, "ticks")

end_date = dt.datetime.now()
start_date = end_date - dt.timedelta(days=7)
h = HistoricalV2(f)
hd = h.historical("MCX", "CRUDEOILM24MAYFUT", 1, start_date, end_date)

def producer():
    feed.run()

    if feed.is_live:
        feed.subscribe(["MCX:CRUDEOILM24MAYFUT"])

def consumer():
    datas = Datas(r)
    datas.load("MCX:CRUDEOILM24MAYFUT", hd.to_dict('records'))
    l = LiveTrading(MyStrategy)
    l.set_datas(datas)
    l.run(ema_fast=20, ema_slow=50, ema_trend=200, sl=1, tp=2)
    

t = threading.Thread(target=producer)
t.start()

consumer()

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

mptradelib-0.1.1.tar.gz (15.9 kB view details)

Uploaded Source

Built Distribution

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

mptradelib-0.1.1-py3-none-any.whl (17.4 kB view details)

Uploaded Python 3

File details

Details for the file mptradelib-0.1.1.tar.gz.

File metadata

  • Download URL: mptradelib-0.1.1.tar.gz
  • Upload date:
  • Size: 15.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.2 CPython/3.10.13 Darwin/23.4.0

File hashes

Hashes for mptradelib-0.1.1.tar.gz
Algorithm Hash digest
SHA256 3c602113ef4fd2f862b6c7fef97de5a207fba5080517b7c6009e566ca90e2abb
MD5 9d0156da4a1057721d1c18136f2d3c2a
BLAKE2b-256 9580a585a0300c31d7a20bff9bc33c21a8dacbe7d27b45ddfe220edd3ed33cbe

See more details on using hashes here.

File details

Details for the file mptradelib-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: mptradelib-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 17.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.2 CPython/3.10.13 Darwin/23.4.0

File hashes

Hashes for mptradelib-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 79c89320fe1b4af29f570d99e370b4f26a7331b92ce15d32a8f1c0bc71376cd3
MD5 54956ab99e15a1e1d11d10a560f117c8
BLAKE2b-256 56c7cf83a331156fafc012293c4bf92908d535f8bded64c5ec9c42a8d712c6b9

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