Code Monkey home page Code Monkey logo

trading-backtest-lib-python's Introduction

Description

Are you developing algo trading strategies via Python? Are you eager to back test your strategies on past datasets? If so, then this package can help you.

I myself have spent significant amount of time developing trading backtesting frameworks and am eager to share the code with you to help you get started.

With the framework comes a set of utility functions which enables users to express simple trading strategies in a few lines of code and back test them on past data.

Below are instructions how to install and run the package. Reach out to me if you have problems running it or you would like to contribute :)

How to install

git clone https://github.com/TomasDavidYe/trading-backtest-lib-python.git;
pip3 install -r requirements.txt

Initialize a dataset

indices, candle_dict = DatasetProviderService(
    logger=logger
).get_candles_for_backtest(
    file_path='./data/yahoo_data_us.csv',
    start_date='2020-01-01',
    end_date='2020-06-01',
    date_shift=15
)

Initialize algorithm

The code below defines and algorithm which buys a stock (opens a position), when 5 day Simple Moving Average (SMA) is 1% higher than the 15 day SMA and sells it (closes the position) when the 5 day SMA goes back below 15 day SMA

instructions = [
    close_position(on_market_open(TradeActionInstruction(rules=[
        (sma(-1, 5) - sma(-1, 15)) <= const(0)
    ]))),

    open_position(on_market_open(buy(rules=[
        (sma(-1, 5) - sma(-1, 15)) / sma(-1, 15) >= const(0.01)
    ])))
]

algorithm = RuleBasedAlgorithm(
    instructions=instructions,
    logger=logger
)

Instructions are evaluated in a descending priority order with 1 action per symbol each candle.

Run Backtest with Algorithm on dataset

trade_executor = TradeExecutionService(
    candle_dict=candle_dict,
    indices=indices,
    max_open_positions=5,
    commission=0.001,
    starting_capital=5000,
    logger=logger
)

while trade_executor.has_next_step():
    trade_executor.next_step(algorithm=algorithm)

trade_executor.describe_results()

Full Example

Backtest on US Stocks

pyton3 backtest_us_stocks_example.py

Create Your Own Algorithms

In case you want to design other algorithms via the framework, feel free to look at the expression_shortcuts to see what expressions are currently implemented and you can combine them however you want to create a new trading strategy.

If you want to create a new expression, you will need to create a new implementation of the Expression. The key here is to implement the .evaluate(index, df) method which encodes how a value at a given index in the candle time series is evaluated. To get inspired, please have a look at how the simple expressions (found in expression_shortcuts) are implemented.

A nice feature of expressions is that they can be combined via arithmetic operations. This way, you can build very complex expressions from a few simple building blocks. To see this, take a look at how the Simple Moving Average indicator (SMA) is implemented in expression_shortcuts.

Contribute

This library is still very basic and miles away from a full-fledged back-testing framework for trading algorithms. Here are several ways I can see this to grow:

  1. Implement more indicators by overriding the Expression class. For example, EMA or Boilinger bands are great candidates
  2. Optimisation of the TradeExecutionService. Currently, the back-tests take a while when running over a long period.

If you would like to contribute in any of the above (or some other way), ping me and let's discuss it :) I am super happy for any joint cooperation!

trading-backtest-lib-python's People

Contributors

tomasdavidye avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.