oanda / oandapy

Python wrapper for the OANDA REST API
MIT License
321 stars 147 forks source link

JSONDecodeError: Expecting property name enclosed in double quotes #47

Open jeremytanjianle opened 7 years ago

jeremytanjianle commented 7 years ago

These's are the exact codes from which I copy from a popular oanda sample code in some "Mastering Python for Finance" book. But no matter what I do, I keep getting JSON errors!

if __name__ == "__main__":

    key = token
    account_id = account_ID
    system = ForexSystem(environment="practice", access_token=token)
    system.begin(accountId=account_id,
                 instruments="EUR_USD",
                 qty=1000,
                 resample_interval="10s",
                 mean_period_short=5,
                 mean_period_long=20,
                 buy_threshold=1.,
                 sell_threshold=1.)

The Errors responses are the following. Can someone tell me how to fix this? I hear its OANDA's problem


__main__:37: UserWarning: Streamer() supports the use of multiple endpoints use the rates() method instead
Traceback (most recent call last):

  File "<ipython-input-8-06ee8e5a22a1>", line 13, in <module>
    sell_threshold=1.)

  File "<ipython-input-5-43a3e04216f9>", line 37, in begin
    self.start(**params)  # Start streaming prices

  File "/home/vinitrinh/anaconda3/lib/python3.6/site-packages/oandapy/stream/stream.py", line 73, in start
    self.run("v1/prices", params=params)

  File "/home/vinitrinh/anaconda3/lib/python3.6/site-packages/oandapy/stream/stream.py", line 104, in run
    data = json.loads(line.decode("utf-8"))

  File "/home/vinitrinh/anaconda3/lib/python3.6/json/__init__.py", line 354, in loads
    return _default_decoder.decode(s)

  File "/home/vinitrinh/anaconda3/lib/python3.6/json/decoder.py", line 339, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())

  File "/home/vinitrinh/anaconda3/lib/python3.6/json/decoder.py", line 355, in raw_decode
    obj, end = self.scan_once(s, idx)

JSONDecodeError: Expecting property name enclosed in double quotes
anthonyng2 commented 7 years ago

Can't tell from the code you posted whether you have an v1 or v20 API account. The code also referred to a ForexSystem class which seemed to be a custom class created by the author. It's a bit challenging to tell what the problem is.

You might like to refer to some sample codes that I posted on my github to get up and running quickly. It's open sourced.

hootnot commented 7 years ago

Oanda quotes v1 API:

I ran this a moment ago.

import oandapy

access_token=".."

oanda = oandapy.API(environment="practice", access_token=access_token)

account = "XXXXXXX"

class MyStreamer(oandapy.Streamer):
    def __init__(self, count=10, *args, **kwargs):
        super(MyStreamer, self).__init__(*args, **kwargs)
        self.count = count
        self.reccnt = 0

    def on_success(self, data):
        print(data)
        self.reccnt += 1
        if self.reccnt == self.count:
            self.disconnect()

    def on_error(self, data):
        self.disconnect()

stream = MyStreamer(environment="practice", access_token=access_token)
stream.rates(account, instruments="EUR_USD")

Gives JSON decoded output:

{u'tick': {u'ask': 1.09708, u'instrument': u'EUR_USD', u'bid': 1.09697, u'time': u'2017-05-15T11:04:11.936976Z'}}
{u'tick': {u'ask': 1.09708, u'instrument': u'EUR_USD', u'bid': 1.09697, u'time': u'2017-05-15T11:04:11.936976Z'}}
{u'heartbeat': {u'time': u'2017-05-15T11:04:14.275570Z'}}
{u'heartbeat': {u'time': u'2017-05-15T11:04:16.603674Z'}}
{u'heartbeat': {u'time': u'2017-05-15T11:04:19.275622Z'}}

So these are Python dicts. The streamer does JSON decoding of the incoming data. I guess you have to look in the ForexSystem class where it handles the tick responses.

The error you report comes for instance from trying to decode a stringyfied dict:

>>> d = {"a": 10}     # dict
>>> s = str(d) 
>>> print(s)
"{'a': 10}"
>>> json.loads(s)

you get the error you reported