Code Monkey home page Code Monkey logo

oanda-backtest-python's Introduction

oanda-backtest

PyPI License: MIT codecov Build Status PyPI - Python Version Downloads

oanda-backtest is a python library for backtest with oanda fx trade rest api on Python 3.6 and above.

Installation

$ pip install oanda-backtest

Usage

basic

from oanda_backtest import Backtest

bt = Backtest(access_token='<your access token>', environment='practice')
bt.candles("EUR_USD")
fast_ma = bt.sma(period=5)
slow_ma = bt.sma(period=25)
bt.sell_exit = bt.buy_entry = (fast_ma > slow_ma) & (fast_ma.shift() <= slow_ma.shift())
bt.buy_exit = bt.sell_entry = (fast_ma < slow_ma) & (fast_ma.shift() >= slow_ma.shift())
bt.run()
bt.plot()

advanced

from oanda_backtest import Backtest

bt = Backtest(access_token='<your access token>', environment='practice')
filepath='usd-jpy-h1.csv'
if bt.exists(filepath):
    bt.read_csv(filepath)
else:
    params = {
        "granularity": "H1",  # 1 hour candlesticks (default=S5)
        "count": 5000 # 5000 candlesticks (default=500, maximum=5000)
    }
    bt.candles("USD_JPY", params)
    bt.to_csv(filepath)

fast_ma = bt.sma(period=10)
slow_ma = bt.sma(period=30)
exit_ma = bt.sma(period=5)
bt.buy_entry = (fast_ma > slow_ma) & (fast_ma.shift() <= slow_ma.shift())
bt.sell_entry = (fast_ma < slow_ma) & (fast_ma.shift() >= slow_ma.shift())
bt.buy_exit = (bt.C < exit_ma) & (bt.C.shift() >= exit_ma.shift())
bt.sell_exit = (bt.C > exit_ma) & (bt.C.shift() <= exit_ma.shift())

bt.initial_deposit = 100000 # default=0
bt.units = 1000 # currency unit (default=10000)
bt.stop_loss = 50 # stop loss pips (default=0)
bt.take_profit = 100 # take profit pips (default=0)

print(bt.run())
bt.plot("backtest.png")
total profit        1989.000
total trades         171.000
win rate              35.088
profit factor          1.198
maximum drawdown    2745.000
recovery factor        0.725
riskreward ratio       2.236
sharpe ratio           0.050
average return        10.666
stop loss              5.000
take profit            5.000

advanced.png

Supported indicators

  • Simple Moving Average 'sma'
  • Exponential Moving Average 'ema'
  • Moving Average Convergence Divergence 'macd'
  • Relative Strenght Index 'rsi'
  • Bollinger Bands 'bband'
  • Stochastic Oscillator 'stoch'
  • Market Momentum 'mom'

Getting started

For help getting started with OANDA REST API, view our online documentation.

Contributing

  1. Fork it
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create new Pull Request

oanda-backtest-python's People

Contributors

10mohi6 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 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.