Skip to main content

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

Project description

oanda-backtest

PyPI License: MIT codecov Build Status PyPI - Python Version

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

Installation

$ pip install oanda-backtest

Usage

#
# basic
#
from oanda_backtest import Backtest

bt = Backtest(token='XXXXXXXXXX')
bt.get("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())).values
bt.buy_exit = bt.sell_entry = ((fast_ma < slow_ma) & (fast_ma.shift() >= slow_ma.shift())).values
bt.run()
bt.plot()

#
# advanced
#
from oanda_backtest import Backtest
import os

bt = Backtest(token='XXXXXXXXXX')
filepath='usd-jpy-h1.csv'
if os.path.exists(filepath):
    df = bt.read_csv(filepath)
else:
    params = {
        "granularity": "H1",  # 1 hour candlesticks
        "count": 5000 # 5000 candlesticks (default=500, maximum=5000)
    }
    df = bt.get("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())).values
bt.sell_entry = ((fast_ma < slow_ma) & (fast_ma.shift() >= slow_ma.shift())).values
bt.buy_exit = ((bt.C < exit_ma) & (bt.C.shift() >= exit_ma.shift())).values
bt.sell_exit = ((bt.C > exit_ma) & (bt.C.shift() <= exit_ma.shift())).values

bt.initial_deposit = 100000 # default=0
bt.point = 0.01 # 1pips (default=0.0001)
bt.lots = 1000 # currency unit (default=10000)
bt.loss_cut = 50 # loss cut pips (default=0)
bt.profit_taking = 100 # profit taking pips (default=0)

print(bt.run())
bt.plot("backtest.png")
total profit            72.00
total trades           188.00
win rate                29.79
profit factor            1.01
maximum drawdown      2781.00
recovery factor          0.03
riskreward ratio         2.36
sharpe ratio             0.00
average return           0.17
loss cut rate            0.53
profit taking rate       0.53

backtest.svg

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

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

oanda-backtest-0.1.2.tar.gz (5.6 kB view details)

Uploaded Source

Built Distribution

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

oanda_backtest-0.1.2-py3-none-any.whl (6.6 kB view details)

Uploaded Python 3

File details

Details for the file oanda-backtest-0.1.2.tar.gz.

File metadata

  • Download URL: oanda-backtest-0.1.2.tar.gz
  • Upload date:
  • Size: 5.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/2.0.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/39.0.1 requests-toolbelt/0.9.1 tqdm/4.36.1 CPython/3.6.8

File hashes

Hashes for oanda-backtest-0.1.2.tar.gz
Algorithm Hash digest
SHA256 a3edc68bef9d0a162d9b48be92f17fb1894a3b9cc6d82c95bed2dec2f733e733
MD5 73ced3742873c918b03343dd99f3dd6c
BLAKE2b-256 0b46df888fd12c769a19ab526ad72e7e63a25430f2b0851d61fb076189cbc0ce

See more details on using hashes here.

File details

Details for the file oanda_backtest-0.1.2-py3-none-any.whl.

File metadata

  • Download URL: oanda_backtest-0.1.2-py3-none-any.whl
  • Upload date:
  • Size: 6.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/2.0.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/39.0.1 requests-toolbelt/0.9.1 tqdm/4.36.1 CPython/3.6.8

File hashes

Hashes for oanda_backtest-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 d289417f7cef3bc32734eaafe347600b7d0a72126c78c4221728a6f710535d0b
MD5 388ba98dd7cca226ff512156ece2b7df
BLAKE2b-256 e69a21637b30db2bf4d94e46e7d0a6cd9dfc1a16b316d0db4b917acfc22573d4

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