hootnot / oanda-api-v20

OANDA REST-V20 API wrapper. Easy access to OANDA's REST v20 API with oandapyV20 package. Checkout the Jupyter notebooks!
MIT License
397 stars 107 forks source link

Get Instrument Candles in Unix Time #106

Closed silgon closed 6 years ago

silgon commented 6 years ago

Sorry, this is more a question than an Issue, but it's because I do not find the answer. According to the documentation about instrument candles, you can request the time to be in Unix time by setting the header Accept-Datetime-Format as UNIX. Howerver in the implementation of this nice package, I'm unable to find this.

In the documentation the following example is given:

oandapyV20.endpoints.instruments.InstrumentsCandles(instrument, params=None)

The parameters for the query are well accepted but nothing about the header. Could somebody guide me with this?

Note: I can totally convert the data to Unix time myself, but since it is one of the parameters given, I think it would be nice to get it directly from oanda.

silgon commented 6 years ago

Damn. I found it in the end. First I did something silly:

 client.client.headers["Accept-Datetime-Format"]="Unix"

And then I found out I could do it directly with the configuration.

client = oandapyV20.API(access_token=token_id,
                        environment=environement,
                        headers={"Accept-Datetime-Format":"Unix"})

Thanks for the great work!

hootnot commented 6 years ago

Hi, that indeed is the way to set headers. See the requests package also if you want details. Earlier this year this issue: https://github.com/oanda/v20-python/issues/22 on the v20 wrapper of OANDA. They have the issue still open, don't know if it is fixed. So, if you run into issues you might want to check.

Personally I always use regular datetime.

silgon commented 6 years ago

I tested some hours ago and it seemed to be working, with either unix or RFC3339 time format. In another matter, since you use the requests package, you have the elapsed time of the request. Is there a way to get it from your package? In requests package is like a=requests.get(myurl);print(a.elapsed)

hootnot commented 6 years ago

I guess you want line 305 of oandapyV20.py where you find the response. Little hacking and you have access.

silgon commented 6 years ago

Thanks!. I'll check it out.

hootnot commented 6 years ago

Using jupyter notebook (or ipython directly) you can do:

import os
os.chdir('<there-where-you-cloned-the-repo>/oandapyV20-examples')
import json
from oandapyV20 import API
import oandapyV20.endpoints.accounts as accounts
from oandapyV20.exceptions import V20Error
# touch src/__init__.py
from src.exampleauth import exampleAuth
import requests
accountID, token = exampleAuth()

api = API(access_token=token)
params = {"instruments": "EUR_USD"}
r = accounts.AccountInstruments(accountID=accountID, params=params)
# make a first request before the test to get an established session
api.request(r)

%timeit rv = api.request(r)
print(json.dumps(rv, indent=2))

and you get:

10 loops, best of 3: 134 ms per loop

... don't know what your exact goal is. Network is key in the number you get here.

silgon commented 6 years ago

I just want to be able to check latency if I use different computers or virtual machines, not that I'm going to decide something based on that, I just want to be able to do it (for fun, you know). Thanks for the workaround, it's true that I don't need to complicate myself, I could just use a time it and average the response.