Git Product home page Git Product logo

python-backtest's Introduction

Back Test Trading Strategy

Stock (& Tick)

Stock(SYMBOL) instantiates an object containing historical prices for a SYMBOL since 2001. A Stock object is akin to a list of Tick objects.

>>> goog = Stock('GOOG')

Stock.plot is a versatile instance method allowing you to plot Tick attributes.

Tick attributes you may want to plot:

  • open
  • close
  • high
  • low
  • ma(N)
  • std(N)
  • upper_bb(N, K)
  • lower_bb(N, K)

To plot the close value and the upper and lower Bollinger's band for N=30 and K=1:

>>> goog.plot('close', 'upper_bb(30, 1)', 'lower_bb(30, 1)')

It will save the plot in the file png/GOOG.png.

BackTest

BackTest() instantiates a backtesting object. e.g. backtest = BackTest()

A strategy is a callable object accepting a Tick object as an argument and returning buy, None or sell.

>>> bollinger = Bollinger(30, 1)
>>> monkey = Monkey(30)

To back test the bollinger strategy against the goog stock:

>>> backtest(goog, bollinger)

To plot PNL (net) and position (long/.../short) over time call BackTest.plot:

>>> backtest.plot()

It will save the plot in the file png/GOOG_Bollinger.png

Trading cost

Backtest.cost should be a function taking a trade amount as an argument and returning a cost. By default there are no trading cost using yet it is very easy to change:

>>> backtest.cost = lambda trade: 0.5 * trade / 100

Will factor in 0.5 % trading cost for the net PNL computation.

python-backtest's People

Contributors

3kwa avatar

Watchers

James Cloos avatar river hong 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.