yhilpisch / tpqoa Goto Github PK
View Code? Open in Web Editor NEWtpqoa is a Python wrapper package for the Oanda REST API v20 for algorithmic trading.
License: MIT License
tpqoa is a Python wrapper package for the Oanda REST API v20 for algorithmic trading.
License: MIT License
Is it possible to stream many instruments at once or do I have to set up a parallel process in which each calls the API to get the current price of an instrument?
Hi can someone help me to install tpqoa in anaconda for jupyter lab use with the excactely string?
I'm a beginner...
Can this library work in Pycharm too?
If yes how can I install it?
Thanks
What version of pandas is used when calling get_history()? It appears to be outdated and how can it be updated? It will not allow the pandas function date_range() to be used.
Hi!
Thanks first of all @yhilpisch for the awesome library! I recently updated my system to python 3.12 and since cannot pip install tpqoa
anymore because of
ERROR: Ignored the following versions that require a different python version: 1.21.2 Requires-Python >=3.7,<3.11; 1.21.3 Requires-Python >=3.7,<3.11; 1.21.4 Requires-Python >=3.7,<3.11; 1.21.5 Requires-Python >=3.7,<3.11; 1.21.6 Requires-Python >=3.7,<3.11
I know I can pip-install it from GitHub directly. But is there any chance to have the pypi packages updated - or is there an actual issue preventing it from working with python 3.12 (>=3.11)?
Kind regards
Not sure if this is the correct place for the issue but any help is appreciated.
This just popped up starting this morning. I cannot get the program to run.
Traceback (most recent call last): File "C:\Users\elect\anaconda3\lib\site-packages\tpqoa\tpqoa.py", line 373, in _stream_data_failsafe_thread self.stream_data(args[0], callback=args[1])
File "C:\Users\elect\anaconda3\lib\site-packages\tpqoa\tpqoa.py", line 346, in stream_data for msg_type, msg in response.parts():
File "C:\Users\elect\anaconda3\lib\site-packages\v20\response.py", line 53, in parts yield parser(line)
File "C:\Users\elect\anaconda3\lib\site-packages\v20\pricing.py", line 783, in __call__ j = json.loads(line.decode('utf-8'))
ValueError: Expected object or value
Dear Yves,
I just wanted to give a suggestion. I believe this would be most useful if it also supports FXCM.
Sincerely,
Error:
Traceback (most recent call last):
File "FXTutorial.py", line 72, in
mt.stream_data('EUR_USD', stop=100)
File "/home/colaya/miniconda3/lib/python3.8/site-packages/tpqoa/tpqoa.py", line 286, in stream_data
float(msg.bids[0].dict()['price']),
TypeError: 'NoneType' object is not subscriptable
How to produce it:
mt = MomentumTrader('oanda.cfg', momentum=5)
mt.stream_data('EUR_USD', stop=100)
I see the stop loss and trailing stop can be specified in terms of distance, which is great and practical as we don't know in advance what the fill price would be for a market order.
Is it possible to also specify the take profit as a distance?
create_order():
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
The returned variable from create_order() doesn't return any information regarding the SL order, as we see in the example in the notebook of this REPO
It would be helpful to know if the SL order was opened, and its id. Any chance to return this order info as well please?
Hi,
I get the below error when attempting to establish a connection to my OANDA Practice account:
api = tpqoa.tpqoa("oanda.cfg")
TypeError Traceback (most recent call last)
in
----> 1 api = tpqoa.tpqoa("oanda.cfg")
~/Library/Python/3.8/lib/python/site-packages/tpqoa/tpqoa.py in init(self, conf_file)
67 self.stream_hostname = 'stream-fxpractice.oanda.com'
68
---> 69 self.ctx = v20.Context(
70 hostname=self.hostname,
71 port=443,
TypeError: init() got an unexpected keyword argument 'token'
My oanda.cfg looks like this:
[oanda]
account_id = 101-004-8861064-001
access_token = ddab9dd1e464036643cb7a4574e83d87-a35a1948575e9825dacfaac49bb90b00
account_type = practice
My pip freeze:
appnope==0.1.2
APScheduler==3.6.3
as==0.1
asgiref==3.2.7
attrs==19.3.0
autopep8==1.5
backcall==0.2.0
black==19.10b0
certifi==2019.11.28
chardet==3.0.4
Click==7.0
coreapi==2.3.3
coreschema==0.0.4
decorator==4.4.2
Django==3.0.5
django-extensions==2.2.8
django-rest-swagger==2.2.0
djangorestframework==3.11.0
entrypoints==0.3
flake8==3.7.9
flake8-black==0.1.1
Flask==1.1.1
idna==2.9
ipykernel==5.4.2
ipython==7.19.0
ipython-genutils==0.2.0
itsdangerous==1.1.0
itypes==1.1.0
jedi==0.17.2
jupyter-client==6.1.7
jupyter-core==4.7.0
mccabe==0.6.1
numpy==1.19.4
openapi-codec==1.3.2
packaging==20.3
pandas==1.2.0
parso==0.7.1
pathspec==0.7.0
pd==0.0.1
pexpect==4.8.0
pickleshare==0.7.5
prompt-toolkit==3.0.8
ptyprocess==0.6.0
pycodestyle==2.5.0
pyflakes==2.1.1
Pygments==2.7.3
python-dateutil==2.8.1
pytz==2020.5
PyYAML==5.3.1
pyzmq==20.0.0
redis==3.4.1
regex==2020.1.8
requests==2.23.0
six==1.15.0
sqlparse==0.3.1
toml==0.10.0
tornado==6.1
tpqoa @ git+https://github.com/yhilpisch/tpqoa@025ee329f21089db44f1da49fb81cdf1e872422d
traitlets==5.0.5
typed-ast==1.4.1
tzlocal==2.0.0
ujson==4.0.1
uritemplate==3.0.1
urllib3==1.25.8
v20==3.0.25.0
wcwidth==0.2.5
Werkzeug==1.0.0
Dear Yves,
you are already aware about the connection error that Oanda raise after a certain amount of time.
I post it here in order to track it.
At the same time, I have sent an email to Oanda and will get back to you if I get any news from their side.
File "/root/miniconda3/lib/python3.8/site-packages/tpqoa/tpqoa.py", line 279, in stream_data
for msg_type, msg in response.parts():
File "/root/miniconda3/lib/python3.8/site-packages/v20/response.py", line 57, in parts
raise V20ConnectionError(self.path)
v20.errors.V20ConnectionError: Connection to v20 REST server at https://stream-fxpractice.oanda.com:443/v3/accounts/xxxxxxx/pricing/stream?instruments=EUR_USD&snapshot=True failed
Hello Yves,
First of all thank you for the wrapper as i am new in python it helped my lot.
I had an issue to get H4 history candles. After trying to understand the issue. I found if the granuality doesnt start with S or M it will add start and end at he default request without spliting the time to chunks, so it will possibly larger then 5000 candles. Maybe it will help someone else new to programing to not strugle to long why it doesnt work with H* cansles :-)
Would be great to have it build in. Thank you
if granularity.startswith('S') or granularity.startswith('M'):
multiplier = float("".join(filter(str.isdigit, granularity)))
if granularity.startswith('S'):
# freq = '1h'
freq = f"{int(MAX_REQUEST_COUNT * multiplier / float(3600))}H"
else:
# freq = 'D'
freq = f"{int(MAX_REQUEST_COUNT * multiplier / float(1440))}D"
data = pd.DataFrame()
dr = pd.date_range(start, end, freq=freq)
for t in range(len(dr)):
batch_start = self.transform_datetime(dr[t])
if t != len(dr) - 1:
batch_end = self.transform_datetime(dr[t + 1])
else:
batch_end = self.transform_datetime(end)
batch = self.retrieve_data(instrument, batch_start, batch_end,
granularity, price)
data = data.append(batch)
else:
start = self.transform_datetime(start)
end = self.transform_datetime(end)
data = self.retrieve_data(instrument, start, end,
granularity, price)
If I have a LOT of transactions, the base function without passing any args only returns transaction 'id' 1-1000.
My account example
get_transactions(13000) returns 'id' 13001 - 14000
Going through the v20 documentation I cannot figure out how to request only the latest transactions. (I'm no expert though)
It's also very time consuming to always pull in blocks of 1000. I really just want to pull the last 50 or so. Any help greatly appreciated.
Back to the connection problem, some exception could be handled in stream_data function like the one below.
Indeed, a loop could be added in stream_data like:
while(True):
try:
..... streaming ....
except ChunkedEncodingError:
..... continue streaming ....
except:
raise
Traceback (most recent call last):
File "/root/miniconda3/lib/python3.9/site-packages/v20/response.py", line 52, in parts
for line in self.lines:
File "/root/miniconda3/lib/python3.9/site-packages/requests/models.py", line 797, in iter_lines
for chunk in self.iter_content(chunk_size=chunk_size, decode_unicode=decode_unicode):
File "/root/miniconda3/lib/python3.9/site-packages/requests/models.py", line 756, in generate
raise ChunkedEncodingError(e)
requests.exceptions.ChunkedEncodingError: ("Connection broken: InvalidChunkLength(got length b'', 0 bytes read)", InvalidChunkLength(got length b'', 0 bytes read))
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/root/notebook/LiveTradingBots.py", line 483, in
bot.stream_data(depl_env.symbol, stop=stop)
File "/root/miniconda3/lib/python3.9/site-packages/tpqoa/tpqoa.py", line 346, in stream_data
for msg_type, msg in response.parts():
File "/root/miniconda3/lib/python3.9/site-packages/v20/response.py", line 57, in parts
raise V20ConnectionError(self.path)
v20.errors.V20ConnectionError: Connection to v20 REST server at XXXXXXXX=True failed
Hi - following instruction however not getting a connection. Please advise.
Steps:
1)pip install --upgrade git+https://github.com/yhilpisch/tpqoa.git
2)import tpqoa
3)api = tpqoa.tpqoa("Desktop/oanda.cfg")
(Using Atom to edit cfg file / Anaconda with Jupiter Lab 1.1.4)
KeyError Traceback (most recent call last)
in
----> 1 api = tpqoa.tpqoa("Desktop/oanda.cfg")
~/opt/anaconda3/lib/python3.7/site-packages/tpqoa/tpqoa.py in init(self, conf_file)
111 self.config = configparser.ConfigParser()
112 self.config.read(conf_file)
--> 113 self.access_token = self.config['oanda']['access_token']
114 self.account_id = self.config['oanda']['account_id']
115 self.account_type = self.config['oanda']['account_type']
~/opt/anaconda3/lib/python3.7/configparser.py in getitem(self, key)
956 def getitem(self, key):
957 if key != self.default_section and not self.has_section(key):
--> 958 raise KeyError(key)
959 return self._proxies[key]
960
KeyError: 'oanda'
--
TX
Hi,
I've been using tpqoa to build an automated trading bot for Oanda. But stream_data freezes after just fetching one tick. Below is a screenshot from my Jupyter Notebook that shows how my AWS server froze for over an hour after fetching just one tick. Why is this happening? And how can I fix it? Thanks!
David
I've been trying to use tpqoa but when I call stream_data, I get this error frequently. Do you know what causes it? And how can I fix it?
I've also attached the source files for this error which are a simple Bollinger Bands testing strategy and a script to run it with some error checking built into it.
BollTrader.txt
trading-bot.txt
Please note that when calling api.get_history with pandas==1.5.1 I get the following warning
/opt/conda/envs/python_forex/lib/python3.10/site-packages/tpqoa/tpqoa.py:161: FutureWarning: The frame.append method is deprecated and will be removed from pandas in a future version. Use pandas.concat instead.
data = data.append(batch)
I have coded and indicator in Python to print out charts for me and a portion of my function call is shown on the image above for the weekly chart. It works also for the Daily charts and Monthly.
However when I try to load any time frame below the daily (eg four hours, 1 hour etc) I get the following error
what is the Granularity input for the 4 hours? is it H4? that doesn't work.
Thanks for the assist
Got this error when calling oanda.stream_data('EUR_USD', stop=7)
. Is there something wrong with v20? Thanks.
Traceback (most recent call last):
File "o.py", line 10, in
oanda.stream_data('EUR_USD')
File "/anaconda3/envs/py36/lib/python3.6/site-packages/tpqoa-0.0.43-py3.6.egg/tpqoa/tpqoa.py", line 206, in stream_data
TypeError: 'NoneType' object is not subscriptable
The same problem as #2. Please help
It is Anaconda, JupytrerLab 3.0.14, Python 3.7. The same problem on Jupyter Notebook and Spyder
Installed correct credentials with no quotes.
content of pyalgo.cft
[oanda]
account_id = KalinStoyanov2
access_token = 8279127d8a1c...
account_type = practice
Resinstalled the token.
Installed Desktop FXPractice
The working folder is archived on MS OneDrive. Available on this device
Here is my dialog.
[1] import tpqoa
[2] api = tpqoa.tpqoa('pyalgo.cfg')
[3] api.get_instruments()[:15]
ResponseNoField Traceback (most recent call last)
in
----> 1 api.get_instruments()[:15]
~\anaconda31\lib\site-packages\tpqoa\tpqoa.py in get_instruments(self)
85 ''' Retrieves and returns all instruments for the given account. '''
86 resp = self.ctx.account.instruments(self.account_id)
---> 87 instruments = resp.get('instruments')
88 instruments = [ins.dict() for ins in instruments]
89 instruments = [(ins['displayName'], ins['name'])
~\anaconda31\lib\site-packages\v20\response.py in get(self, field, status)
33
34 if value is None:
---> 35 raise ResponseNoField(self, field)
36
37 return value
ResponseNoField: 400 response for GET https://api-fxpractice.oanda.com:443/v3/accounts/KalinStoyanov2/instruments does not have field 'instruments' (contains 'errorMessage')
While trying to create an order with a stop-loss-distance or a take-profit-price I get following error: "v20.errors.ResponseNoField: 400 response for POST". With the error message: errorMessage "Insufficient authorization to perform request.".
Once I remove the stop loss distance and the take-profit-price no error is raised.
I've tried to find the 400 error Insufficient authorization on the developer OANDA documentation but I couldn't find it.
Is this some sort of syntax error on my part, or is this a known bug?
Example Code for clarification:
trader = tpqoa.tpqoa("cfgfile.cfg")
trader.create_order("EUR_USD", units=100000, sl_distance=0.0002, tp_price=1.19200, suppress=True, ret=True)
# -> 400 Error: "Insufficient Authorization"
Once I leave out the sl_distance and the tp_price no error is raised.
Has anybody else run into this problem?
Another question rather unrelated to this problem:
Is there any way to create/change a stoploss order after units have been bought/sold?
According to the V20 rest API this should be possible, but if I'm correct it hasn't been implemented in the wrapper.
Is that correct? If so one could implement it oneself, for example with the requests package?
Line 234 in aba4772
Hi, in my jupyter nb a warning message started to appear: the frame.append method will be deprecated exactly, you can see in the line above.
When you call the method e.g: get_history in granularity of S5 the message appears several times and is annoying.
I can solve this problem changing the the 'append' for this:
data = pd.concat([data, batch])
https://pandas.pydata.org/docs/dev/whatsnew/v1.4.0.html#deprecations
Hi - do you have pip install option for tpqge?
Would it be possible to add functionality to cancel an order ? E.g., we might want to cancel a SL order or a limit order
Hi,
I am trying out this package. to start I tried to get the history of the EUR/USD pair
my code is as follows:
import tpqoa
oanda = tpqoa.tpqoa('oanda.cfg')
print(oanda.get_instruments())
data=oanda.get_history(instrument='EUR_USD',start='2021-10-01', end='2021-10-30', granularity='1h', price='A')
The print call is executed just fin and the EUR/USD pair is listed in there.
but the error I get from the package is:
v20.errors.ResponseNoField: 400 response for GET <url> does not have field 'candles' (contains 'errorMessage')
when use the url returned by the package and put it in curl to see what the error message is it returns this:
{
"errorMessage": "Insufficient authorization to perform request."
}
what is going on here? can someone help me here? Thanks in advance!
ins = oanda.get_instruments()
error:
400 response for GET https://api-fxpractice.oanda.com:443/v3/accounts/'...'/instruments does not have field 'instruments' (contains 'errorMessage')
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.