Tpqoa
tpqoa is a Python wrapper package for the Oanda REST API v20 for algorithmic trading.
Install / Use
/learn @yhilpisch/TpqoaREADME
<img src="http://hilpisch.com/tpq_logo.png" alt="The Python Quants" width="35%" align="right" border="0"><br>
tpqoa
Algorithmic Trading with Oanda
tpqoa is a wrapper class for the Oanda REST API v20 (http://developer.oanda.com/). It makes use of the Python package v20 from Oanda (https://github.com/oanda/v20-python). The package is authored and maintained by The Python Quants GmbH. © Dr. Yves J. Hilpisch. MIT License.
Disclaimer
Trading forex/CFDs on margin carries a high level of risk and may not be suitable for all investors as you could sustain losses in excess of deposits. Leverage can work against you. Due to the certain restrictions imposed by the local law and regulation, German resident retail client(s) could sustain a total loss of deposited funds but are not subject to subsequent payment obligations beyond the deposited funds. Be aware and fully understand all risks associated with the market and trading. Prior to trading any products, carefully consider your financial situation and experience level. Any opinions, news, research, analyses, prices, code examples or other information is provided as general market commentary, and does not constitute investment advice. The Python Quants GmbH will not accept liability for any loss or damage, including without limitation to, any loss of profit, which may arise directly or indirectly from use of or reliance on such information.
The tpqoa package is intended as a technological illustration only. It comes with no warranties or representations, to the extent permitted by applicable law.
Installation
Installing from source via Git and Github:
git clone https://github.com/yhilpisch/tpqoa
cd tpqoa
python setup.py install
Using pip in combination with Github:
pip install git+git://github.com/yhilpisch/tpqoa
Connection
In order to connect to the API, you need to have at least a practice account with Oanda (https://oanda.com/). Once logged in to you account, you can create an API token and can copy your account number. These are expected to be stored in a configuration file, with name oanda.cfg, for instance, as follows:
[oanda]
account_id = XYZ-ABC-...
access_token = ZYXCAB...
account_type = practice (default) or live
You can then set up an API connection by instantiating a connection object.
import tpqoa
oanda = tpqoa.tpqoa('../../oanda.cfg') # adjust path as necessary
Available Instruments
The get_instruments() method retrieves all available instruments.
ins = oanda.get_instruments()
ins[:10]
[('AUD/CAD', 'AUD_CAD'),
('AUD/CHF', 'AUD_CHF'),
('AUD/HKD', 'AUD_HKD'),
('AUD/JPY', 'AUD_JPY'),
('AUD/NZD', 'AUD_NZD'),
('AUD/SGD', 'AUD_SGD'),
('AUD/USD', 'AUD_USD'),
('Australia 200', 'AU200_AUD'),
('Brent Crude Oil', 'BCO_USD'),
('Bund', 'DE10YB_EUR')]
Historical Data
The get_history() method retrieves historical data.
help(oanda.get_history)
Help on method get_history in module tpqoa.tpqoa:
get_history(instrument, start, end, granularity, price, localize=True) method of tpqoa.tpqoa.tpqoa instance
Retrieves historical data for instrument.
Parameters
==========
instrument: string
valid instrument name
start, end: datetime, str
Python datetime or string objects for start and end
granularity: string
a string like 'S5', 'M1' or 'D'
price: string
one of 'A' (ask), 'B' (bid) or 'M' (middle)
Returns
=======
data: pd.DataFrame
pandas DataFrame object with data
# oanda.get_history??
data = oanda.get_history(instrument='EUR_USD',
start='2022-06-15',
end='2023-06-15',
granularity='D',
price='M')
data.info()
<class 'pandas.core.frame.DataFrame'>
DatetimeIndex: 261 entries, 2022-06-14 21:00:00 to 2023-06-14 21:00:00
Data columns (total 6 columns):
# Column Non-Null Count Dtype
--- ------ -------------- -----
0 o 261 non-null float64
1 h 261 non-null float64
2 l 261 non-null float64
3 c 261 non-null float64
4 volume 261 non-null int64
5 complete 261 non-null bool
dtypes: bool(1), float64(4), int64(1)
memory usage: 12.5 KB
print(data.head())
o h l c volume complete
time
2022-06-14 21:00:00 1.04114 1.05078 1.03593 1.04466 204826 True
2022-06-15 21:00:00 1.04444 1.06014 1.03809 1.05524 183417 True
2022-06-16 21:00:00 1.05496 1.05612 1.04445 1.04938 156233 True
2022-06-19 21:00:00 1.04841 1.05460 1.04746 1.05112 85713 True
2022-06-20 21:00:00 1.05088 1.05826 1.05086 1.05348 101517 True
Streaming Data
The method stream_data() allows the streaming of real-time data (bid & ask).
oanda.stream_data('EUR_USD', stop=3)
2023-06-27T06:57:58.204324464Z 1.09299 1.09313
2023-06-27T06:57:58.400409926Z 1.09301 1.09315
2023-06-27T06:58:00.348284643Z 1.093 1.09314
By redefining the on_success() method, you can control what happes with the streaming data.
class myOanda(tpqoa.tpqoa):
def on_success(self, time, bid, ask):
''' Method called when new data is retrieved. '''
print('BID: {:.5f} | ASK: {:.5f}'.format(bid, ask))
my_oanda = myOanda('../../oanda.cfg')
my_oanda.stream_data('EUR_USD', stop=5)
BID: 1.09297 | ASK: 1.09311
BID: 1.09297 | ASK: 1.09310
BID: 1.09297 | ASK: 1.09311
BID: 1.09297 | ASK: 1.09312
BID: 1.09294 | ASK: 1.09309
Placing Orders
help(oanda.create_order)
Help on method create_order in module tpqoa.tpqoa:
create_order(instrument, units, price=None, sl_distance=None, tsl_distance=None, tp_price=None, comment=None, touch=False, suppress=False, ret=False) method of tpqoa.tpqoa.tpqoa instance
Places order with Oanda.
Parameters
==========
instrument: string
valid instrument name
units: int
number of units of instrument to be bought
(positive int, eg 'units=50')
or to be sold (negative int, eg 'units=-100')
price: float
limit order price, touch order price
sl_distance: float
stop loss distance price, mandatory eg in Germany
tsl_distance: float
trailing stop loss distance
tp_price: float
take profit price to be used for the trade
comment: str
string
touch: boolean
market_if_touched order (requires price to be set)
suppress: boolean
whether to suppress print out
ret: boolean
whether to return the order object
# going long 100 units
# sl_distance of 20 pips
oanda.create_order('EUR_USD', units=100, sl_distance=0.002)
{'id': '3608', 'time': '2023-06-27T06:58:16.307275954Z', 'userID': 13834683, 'accountID': '101-004-13834683-001', 'batchID': '3607', 'requestID': '61122547747050135', 'type': 'ORDER_FILL', 'orderID': '3607', 'instrument': 'EUR_USD', 'units': '100.0', 'gainQuoteHomeConversionFactor': '0.910326029089', 'lossQuoteHomeConversionFactor': '0.919475034407', 'price': 1.09309, 'fullVWAP': 1.09309, 'fullPrice': {'type': 'PRICE', 'bids': [{'price': 1.09294, 'liquidity': '10000000'}], 'asks': [{'price': 1.09309, 'liquidity': '10000000'}], 'closeoutBid': 1.09294, 'closeoutAsk': 1.09309}, 'reason': 'MARKET_ORDER', 'pl': '0.0', 'financing': '0.0', 'commission': '0.0', 'guaranteedExecutionFee': '0.0', 'accountBalance': '101295.189', 'tradeOpened': {'tradeID': '3608', 'units': '100.0', 'price': 1.09309, 'guaranteedExecutionFee': '0.0', 'halfSpreadCost': '0.0069', 'initialMarginRequired': '3.33'}, 'halfSpreadCost': '0.0069'}
# closing out the position
oanda.create_order('EUR_USD', units=-100)
{'id': '3611', 'time': '2023-06-27T06:58:16.523945599Z', 'userID': 13834683, 'accountID': '101-004-13834683-001', 'batchID': '3610', 'requestID': '61122547747050332', 'type': 'ORDER_FILL', 'orderID': '3610', 'instrument': 'EUR_USD', 'units': '-100.0', 'gainQuoteHomeConversionFactor': '0.910326029089', 'lossQuoteHomeConversionFactor': '0.919475034407', 'price': 1.09294, 'fullVWAP': 1.09294, 'fullPrice': {'type': 'PRICE', 'bids': [{'price': 1.09294, 'liquidity': '10000000'}], 'asks': [{'price': 1.09309, 'liquidity': '9999900'}], 'closeoutBid': 1.09294, 'closeoutAsk': 1.09309}, 'reason': 'MARKET_ORDER', 'pl': '-0.0138', 'financing': '0.0', 'commission': '0.0', 'guaranteedExecutionFee': '0.0', 'accountBalance': '101295.1752', 'tradesClosed': [{'tradeID': '3608', 'units': '-100.0', 'price': 1.09294, 'realizedPL': '-0.0138', 'financing': '0.0', 'guaranteedExecutionFee': '0.0', 'halfSpreadCost': '0.0069'}], 'halfSpreadCost': '0.0069'}
Canceling Orders
order = oanda.create_order('EUR_USD', units=10000, sl_distance=0.01, ret=True)
{'id': '3614', 'time': '2023-06-27T06:58:33.953341530Z', 'userID': 13834683, 'accountID': '101-004-13834683-001', 'batchID': '3613', 'requestID': '61122547818369950', 'type': 'ORDER_FILL', 'orderID': '3613', 'instrument': 'EUR_USD', 'units': '10000.0', 'gainQuoteHomeConversionFactor': '0.910363508679', 'lossQuoteHomeConversionFactor': '0.919512890676', 'price': 1.09304, 'fullVWAP': 1.09304, 'fullPrice': {'type': 'PRICE', 'bids': [{'price': 1.0929, 'liquidity': '10000000'}], 'asks': [
Related Skills
node-connect
347.6kDiagnose OpenClaw node connection and pairing failures for Android, iOS, and macOS companion apps
frontend-design
108.4kCreate distinctive, production-grade frontend interfaces with high design quality. Use this skill when the user asks to build web components, pages, or applications. Generates creative, polished code that avoids generic AI aesthetics.
openai-whisper-api
347.6kTranscribe audio via OpenAI Audio Transcriptions API (Whisper).
qqbot-media
347.6kQQBot 富媒体收发能力。使用 <qqmedia> 标签,系统根据文件扩展名自动识别类型(图片/语音/视频/文件)。
