oanda-bot is a python library for automated trading bot with oanda rest api on Python 3.6 and above.
Project description
oanda-bot
oanda-bot is a python library for automated trading bot with oanda rest api on Python 3.6 and above.
Installation
$ pip install oanda-bot
Usage
basic run
from oanda_bot import Bot
class MyBot(Bot):
def strategy(self):
fast_ma = self.sma(period=5)
slow_ma = self.sma(period=25)
# golden cross
self.sell_exit = self.buy_entry = (fast_ma > slow_ma) & (
fast_ma.shift() <= slow_ma.shift()
)
# dead cross
self.buy_exit = self.sell_entry = (fast_ma < slow_ma) & (
fast_ma.shift() >= slow_ma.shift()
)
MyBot(
account_id='<your practice account id>',
access_token='<your practice access token>',
).run()
basic backtest
from oanda_bot import Bot
class MyBot(Bot):
def strategy(self):
fast_ma = self.sma(period=5)
slow_ma = self.sma(period=25)
# golden cross
self.sell_exit = self.buy_entry = (fast_ma > slow_ma) & (
fast_ma.shift() <= slow_ma.shift()
)
# dead cross
self.buy_exit = self.sell_entry = (fast_ma < slow_ma) & (
fast_ma.shift() >= slow_ma.shift()
)
MyBot(
account_id='<your practice account id>',
access_token='<your practice access token>',
).backtest()
basic report
from oanda_bot import Bot
Bot(
account_id='<your practice account id>',
access_token='<your practice access token>',
).report()
advanced run
from oanda_bot import Bot
class MyBot(Bot):
def strategy(self):
rsi = self.rsi(period=10)
ema = self.ema(period=20)
lower = ema - (ema * 0.001)
upper = ema + (ema * 0.001)
self.buy_entry = (rsi < 30) & (self.df.C < lower)
self.sell_entry = (rsi > 70) & (self.df.C > upper)
self.sell_exit = ema > self.df.C
self.buy_exit = ema < self.df.C
self.units = 1000 # currency unit (default=10000)
self.take_profit = 50 # take profit pips (default=0 take profit none)
self.stop_loss = 20 # stop loss pips (default=0 stop loss none)
MyBot(
account_id='<your practice account id>',
access_token='<your practice access token>',
# trading environment (default=practice)
environment='practice',
# trading currency (default=EUR_USD)
instrument='USD_JPY',
# 1 minute candlesticks (default=D)
granularity='M1',
# trading time (default=Bot.SUMMER_TIME)
trading_time=Bot.WINTER_TIME,
# Slack notification when an error occurs
slack_webhook_url='<your slack webhook url>',
# Line notification when an error occurs
line_notify_token='<your line notify token>',
# Discord notification when an error occurs
discord_webhook_url='<your discord webhook url>',
).run()
advanced backtest
from oanda_bot import Bot
class MyBot(Bot):
def strategy(self):
rsi = self.rsi(period=10)
ema = self.ema(period=20)
lower = ema - (ema * 0.001)
upper = ema + (ema * 0.001)
self.buy_entry = (rsi < 30) & (self.df.C < lower)
self.sell_entry = (rsi > 70) & (self.df.C > upper)
self.sell_exit = ema > self.df.C
self.buy_exit = ema < self.df.C
self.units = 1000 # currency unit (default=10000)
self.take_profit = 50 # take profit pips (default=0 take profit none)
self.stop_loss = 20 # stop loss pips (default=0 stop loss none)
MyBot(
account_id='<your practice account id>',
access_token='<your practice access token>',
instrument='USD_JPY',
granularity='S15', # 15 second candlestick
).backtest(from_date="2020-7-7", to_date="2020-7-13", filename="backtest.png")
total profit 3910.000
total trades 374.000
win rate 59.091
profit factor 1.115
maximum drawdown 4220.000
recovery factor 0.927
riskreward ratio 0.717
sharpe ratio 0.039
average return 9.787
stop loss 0.000
take profit 0.000
advanced report
from oanda_bot import Bot
Bot(
account_id='<your practice account id>',
access_token='<your practice access token>',
instrument='USD_JPY',
granularity='S15', # 15 second candlestick
).report(filename="report.png", days=-7) # from 7 days ago to now
total profit -4960.000
total trades 447.000
win rate 59.284
profit factor -0.887
maximum drawdown 10541.637
recovery factor -0.471
riskreward ratio -0.609
sharpe ratio -0.043
average return -10.319
live run
from oanda_bot import Bot
class MyBot(Bot):
def atr(self, *, period: int = 14, price: str = "C"):
a = (self.df.H - self.df.L).abs()
b = (self.df.H - self.df[price].shift()).abs()
c = (self.df.L - self.df[price].shift()).abs()
df = pd.concat([a, b, c], axis=1).max(axis=1)
return df.ewm(span=period).mean()
def strategy(self):
rsi = self.rsi(period=10)
ema = self.ema(period=20)
atr = self.atr(period=20)
lower = ema - atr
upper = ema + atr
self.buy_entry = (rsi < 30) & (self.df.C < lower)
self.sell_entry = (rsi > 70) & (self.df.C > upper)
self.sell_exit = ema > self.df.C
self.buy_exit = ema < self.df.C
self.units = 1000
MyBot(
account_id='<your live account id>',
access_token='<your live access token>',
environment='live',
instrument='EUR_GBP',
granularity='H12', # 12 hour candlesticks
trading_time=Bot.WINTER_TIME,
slack_webhook_url='<your slack webhook url>',
).run()
Supported indicators
- Simple Moving Average 'sma'
- Exponential Moving Average 'ema'
- Moving Average Convergence Divergence 'macd'
- Relative Strenght Index 'rsi'
- Bollinger Bands 'bbands'
- Market Momentum 'mom'
- Stochastic Oscillator 'stoch'
- Awesome Oscillator 'ao'
Getting started
For help getting started with OANDA REST API, view our online documentation.
Contributing
- Fork it
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Add some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create new Pull Request
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
oanda-bot-0.1.2.tar.gz
(10.7 kB
view details)
Built Distribution
File details
Details for the file oanda-bot-0.1.2.tar.gz
.
File metadata
- Download URL: oanda-bot-0.1.2.tar.gz
- Upload date:
- Size: 10.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.2.0 pkginfo/1.6.1 requests/2.25.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.54.1 CPython/3.8.2
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | fd33ceec081e282691bfb9a09c3ec41a3487095b6c715bd34e6e4026d5f4d188 |
|
MD5 | 4f96f583ccf9505801099769aaffdab3 |
|
BLAKE2b-256 | e0a4c823f4e267dd328fa110f775a390b46bfe104c29d9f9c68d81397355418d |
File details
Details for the file oanda_bot-0.1.2-py3-none-any.whl
.
File metadata
- Download URL: oanda_bot-0.1.2-py3-none-any.whl
- Upload date:
- Size: 9.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.2.0 pkginfo/1.6.1 requests/2.25.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.54.1 CPython/3.8.2
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 10969f405b4896741a70f1a3708cf2f8b6b29b0221516a4789b31b4993c2318e |
|
MD5 | 8cb54422df71483dfb790373474fff43 |
|
BLAKE2b-256 | b7621b033e13b49eb85103c5459521a1f724a4d8757e997f7829b5c8f0bd68f6 |