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

InstrumentsCandlesFactory not working for monthly granularity #148

Open mpatkinson7 opened 5 years ago

mpatkinson7 commented 5 years ago

Hi

The InstrumentsCandlesFactory is not working for "monthly" granularity. Works fine for weekly and daily granularities.

Using parameters below.

params = {
    "price": "M",
    "granularity": "M",
    "from": "2005-01-01T00:00:00Z",
    "to": "2017-07-01T00:00:00Z",
    "dailyAlignment": 17,
    "alignmentTimezone": "America/New_York",
    "weeklyAlignment": "Friday"
}

Say instrument is "AU200_AUD"

Basically, calling line below: client.request(factories.InstrumentsCandlesFactory(instrument=instrument, params=params))

Running line in Interactive Window in VSCode (using WinPython) yields error below.

AttributeError Traceback (most recent call last) in

----> 1 client.request(factories.InstrumentsCandlesFactory(instrument=instrument, params=params))

C:\WPy64-3720\python-3.7.2.amd64\lib\site-packages\oandapyV20\oandapyV20.py in request(self, endpoint)
    274             V20Error in case of HTTP response code >= 400
    275         """
--> 276         method = endpoint.method
    277         method = method.lower()
    278         params = None

AttributeError: 'generator' object has no attribute 'method'
---------------------------------------------------------------------------

I've just run the script for daily & weekly data - so pretty sure OANDA is working fine.

hootnot commented 5 years ago

Hi,

First of all: you are using it the wrong way.

InstrumentsCandlesFactory is a generator that yields InstrumentsCandles request objects to cover the full range of data specified by from/to. The way to use the factory is:

   for r in InstrumentsCandlesFactory(...):
      rv = client.request(r)
      # and process the data ...

there is an example in the README.rst of the repo ! Check the docs also: https://oanda-api-v20.readthedocs.io/en/latest/contrib/factories/instrumentscandlesfactory.html

I get the impression OANDA added W(eekly) later on and then after that M(onthly). It was april 2018 that I added 'W' to support the weekly data. The function parsing granularity parameter doesn' t support 'M'(onthly) yet.

Easy work-around:

Instead of using InstrumentsCandlesFactory simply use the InstrumentsCandles class to fetch the monthly data. The data of OANDA goes back to 2005. The default count is 500, large enough to get you all monthly history since 2005.

mpatkinson7 commented 5 years ago

Thanks for the response Feit.

Sorry, I should have been clearer. I was using the InstrumentsCandlesFactory as you described in my code, and as described in the documentation. I just didn't use it correctly in that 'line below' I was referring to in the bug! I was hacking around with stuff interactively.

Yeah, you must have read my mind. I have already coded the work around you suggested (i.e. if / else checking granularity) before your reply! That was a great suggestion you made!!! 😊

Finally, just for completeness, do you think the "M" / monthly granularity will be made available at some point in the future in InstrumentsCandlesFactory?

Thanks.

hootnot commented 5 years ago

For completeness it should be there. To be honest: I don't think there is much value in monthly data.

flowtrader2016 commented 2 years ago

Are there any plans to add this enhancement please?