Coder Social home page Coder Social logo

trading_backtesting's Introduction

trading_backtesting

Example of implementing a backtesting framework from scratch.

Why?

Implementing backtesting is seen as something very complicated, but actually it is not! My main motivation for writing this backtesting framework from scratch was flexibility and optimization. I found it very difficult to work with existing backtesting for mutli-stocks strategies and they were pretty slow.

Implementation details

This code implements a general purpose trading environment through trading_env in backtesting.py. This trading enviroment has access to the OHLC price dataframe passed during initialization. You interact with it to place buy/sell orders. The enviroment keeps track of all the data during backtesting that is required for computing the final performance metrics.

The trade function in backtesting.py implements my trading strategy which is a multi-stock strategy.

Usage Details

examples.py shows how to use these to run backtesting on my trading strategy and the benchmark strategy.

Some random thoughts

I wouldn't recommend using this code as is for your backtesting purpose. I would definitely try out existing frameworks and if they don't work, only then I would go for writing one from scratch like this. I landed on this piece of code after going through several iterations. I tried out several ways to optimize the code by vectorization and parallelization. But, ultimately the simple idea of iterating through each trading day worked the best. The final piece of code doesn't seem that complicated, but my thought process took a fairly long and winding road to reach there :D.

It was fun though! And that is what matters the most.

trading_backtesting's People

Contributors

abhayspawar avatar

Stargazers

Aman Goel avatar Nicu avatar Nikita Elenberger avatar  avatar ML Wiz avatar Arthur Temporim avatar Akash Gupta avatar Juan Pablo Acosta avatar Hangjian Li avatar Shabbir Hasan avatar  avatar Viswanath  avatar Aniruddha Kalburgi avatar  avatar  avatar

Watchers

Aniruddha Kalburgi avatar  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.