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
398 stars 107 forks source link

Historical data #75

Closed iMashhadi closed 7 years ago

iMashhadi commented 7 years ago

Hi How can i set datetime in params and get historical data from X to Y ?

hootnot commented 7 years ago

Hi,

It is likely that you used the wrong format, maybe only forgot the 'Z' in the date spec. It must be RFC3339 (or unix time), so short version:

import json
import oandapyV20
import oandapyV20.endpoints.instruments as instruments
client = oandapyV20.API(access_token=...)

params = {
  "count": 5,
  "granularity": "H4",
  "from": "2017-06-23T12:00:00Z",
}
r = instruments.InstrumentsCandles(instrument="DE30_EUR", params=params)
client.request(r)
print(json.dumps(r.response, indent=2))

{
  "instrument": "DE30_EUR", 
  "candles": [
    {
      "volume": 20214, 
      "mid": {
        "h": "12768.4", 
        "c": "12715.0", 
        "l": "12679.0", 
        "o": "12756.8"
      }, 
      "complete": true, 
      "time": "2017-06-23T09:00:00.000000000Z"
    }, 
    {
      "volume": 20012, 
      "mid": {
        "h": "12753.3", 
        "c": "12726.5", 
        "l": "12674.9", 
        "o": "12714.8"
      }, 
      "complete": true, 
      "time": "2017-06-23T13:00:00.000000000Z"
    }, 
    {
      "volume": 2982, 
      "mid": {
        "h": "12732.0", 
        "c": "12731.8", 
        "l": "12708.8", 
        "o": "12726.3"
      }, 
      "complete": true, 
      "time": "2017-06-23T17:00:00.000000000Z"
    }
  ], 
  "granularity": "H4"
}

Most endpoints are covered with examples in the https://github.com/hootnot/oandapyV20-examples repo. ( I've just added a check on the dates to raise an error in case the date does not qualify)

Check the candle-data.py example. If you run:

python src/candle-data.py --price MBA --start "2016-01-01T00:00:00Z" --gran M5 --instruments EUR_USD --nice >/tmp/EUR_USD.hist.M5

you will get 500 records, with Mid/Bid/Ask. You can control that number by using --count N and set it up to 5000

 "instrument": "EUR_USD", 
 "candles": [
  {
   "complete": true, 
   "bid": {
    "h": "1.08699", 
    "c": "1.08676", 
    "l": "1.08674", 
    "o": "1.08699"
   }, 
   "mid": {
    "h": "1.08746", 
    "c": "1.08726", 
    "l": "1.08724", 
    "o": "1.08743"
   }, 
   "volume": 6, 
   "time": "2016-01-03T22:00:00.000000000Z", 
   "ask": {
    "h": "1.08796", 
    "c": "1.08776", 
    "l": "1.08774", 
    "o": "1.08787"
   }
  }, 
  ...
]

Check the curl examples also that are in the docs at http://developer.oanda.com/rest-live-v20/instrument-ep/

hootnot commented 7 years ago

@torisoft : did not hear from you anymore, guess your problem is solved using the right date spec

dexhunter commented 7 years ago

Hi! I tried to access data from 2016-01-01 to 2016-02-01 with granularity="M5" but failed. Is there a restriction on the datetime oanda api can date back to? Thanks.

Edit: I tried different granularities and works. Maybe is the M5 data missing. The error is 500, "Internal Server Error"

hootnot commented 7 years ago

If you get a 500 error it is always the server side, with the server down or other serious trouble. Check the health-page at developer.oanda.com for issues on their side (and they have lately). I've downloaded data myself this week and had not errors, Do those other granularities work the same time you try M5 ? Which instrument ?

dexhunter commented 7 years ago

Do those other granularities work the same time you try M5 ? Which instrument ?

The instrument is EUR_USD. The minimum granularity I tested working is M30. I have sent an email regarding the issue to api@oanda.com, hope they will fix this soon.

hootnot commented 7 years ago

curl works fine:

curl \ -H "Content-Type: application/json" \ -H "Authorization: Bearer ${TOK}" \ "https://api-fxpractice.oanda.com/v3/instruments/EUR_USD/candles?from=2016-01-01T00:00:00Z&to=2016-02-01T00:00:00Z&price=M&granularity=M5"

Do you use oandapyV20 ? then please show me the code

hootnot commented 7 years ago

@DexHunter

The piece of code I mentioned as first answer in this discussion works fine too with date(s) formatted as in the example.

It was this discussion that I rewrote the src/candle-data.py example to accept from/to instead of start/end. I also added a check for the date to that. That datecheck fails a return value in case the date is OK. So I patched that example from the oandapyV20-examples repo

If you do:

python src/candle-data.py --instruments EUR_USD --gran M5 --price MBA --from 2016-01-01T00:00:00Z --to 2016-02-01T00:00:00Z

you have your history over the requested period.

If you need more than 5000 records: use InstrumentsCandlesFactory from oandapyV20.contrib.factories

(But the 500 error was an OANDA thing)

dexhunter commented 7 years ago

Thank you for reply! I found the problem was on server side. I added a database system to my trading framework and now it should be okay.

JTInfinite commented 6 years ago

Hi hootnot,

thanks for the brilliant wrapper. I have been enjoying my time playing around with it. I have an issue with the candles factory though and that is it never returns candles up until 'now' - more often than not it gives data up until a week ago or more. For instance, if I pass the followng params:

now = datetime.datetime.utcnow().isoformat('T')+'Z'
now = str(now[:-8] + 'Z')

params = {
            "from": "2017-04-01T00:00:00Z",
            "to": now,
            "price": "M",
            "granularity": "M30"
            }

This is the last chunk of data I receive (put into a dataframe) is:

2017-09-14 23:00:00+00:00 699 1.33982 1.34037 1.33975 1.34012

[5686 rows x 5 columns]

Why has it stopped on the 14th? Why not up until 22/09/2017?

Thanks for any clarity

hootnot commented 6 years ago

@4Eye thanks for the compliment, I will have a look... Ok, it needs a fix, will open a new issue

JTInfinite commented 6 years ago

Thanks hootnot!

Let me know if I can do anything. Of course, more than happy to run tests etc.

hootnot commented 6 years ago

Hi, just triggered the release 0.4.4. I tested it on your request, it fills fully now. It will take some time before the new version pops up in pypi. So, if you are in a hurry upgrade from github. Otherwise give it some time and then upgrade using pip:

pip install oandapyV20 --upgrade

JTInfinite commented 6 years ago

perfect, thanks!

hootnot commented 6 years ago

You're welcome. Enjoy the use of the library and the InstrumentsCandlesFactory. When retrieving bulk history it is very conveniant

JTInfinite commented 6 years ago

tested and working perfectly. You're a legend!