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 lower time frame granularity not working #153

Closed yang2lalang closed 4 years ago

yang2lalang commented 4 years ago

The problem is that attempting to request data on low periods such as M1 or S5 or S30 data returns an empty dictionary. I am only able to get data on higher time frames

The data is requested using:

def csvbackfillOanda(self):
        #with open("{}.{}".format(pair, self.params["granularity"]), "w") as OUT:    
        for pair in self.instrument:
            pair_filename  = "/home/franklin/forex/" + pair + ".csv"
            for r in InstrumentsCandlesFactory(instrument=pair, params=self.params):
                rv = self.broker_client.request(r)
                tradeDict = r.response.get('candles')
                if len(tradeDict) <= 0 :
                    print("Oanda server Returned empty Dict:Check that chosen dates don't fall on weekends or public holidays") 
                    return
                newTradesFrame = pd.DataFrame.from_dict(tradeDict, orient='columns')
                midFrame = newTradesFrame['mid'].apply(pd.Series)
                newTradesFrame = newTradesFrame.drop(['mid'], axis=1)
                newTradesFrame = pd.concat([newTradesFrame, midFrame], axis=1)
                cnt = len(tradeDict)
                print("REQUEST: {} {} {}, received: {}".format(r, r.__class__.__name__, r.params, cnt))
                self.tradesFrame = pd.concat([self.tradesFrame, newTradesFrame], axis = 0)
            self.tradesFrame.to_csv(pair_filename)

and called using:

b = BackFillForexDataOanda(pair=['GBPUSD','EURUSD'],days=14,startDate='2019-01-01',endDate='2019-10-30',granularity='M1')
b.csvbackfillOanda()

Please is anyone having any of these issues? Is the data from Oanda credible or do we have to pay to get useful data?

hootnot commented 4 years ago

example code from the README extracts M1 perfectly your instrument names are wrong also, should read: GBP_USD, EUR_USD timestamps should be in format: "2019-01-01T00:00:00Z

  if len(tradeDict) <= 0 :
                    print("Oanda server Returned empty Dict:Check that chosen dates don't fall on weekends or public holidays") 
                    return

You should use continue instead of return.

A generated request can have parameters falling in a weekend or weekend extended with some holiday. The smaller the timeframe the bigger the chance this can happen. The continue will simply process the next request. Using return you terminate the processing the first time it returns no data.

I will close the issue