nickmccullum / algorithmic-trading-python

The repository for freeCodeCamp's YouTube course, Algorithmic Trading in Python
2.35k stars 2.37k forks source link

Testing Sandbox Deprecated #47

Open Indianapolis-Jones opened 1 year ago

Indianapolis-Jones commented 1 year ago

I am getting a 403 error when making API call for AAPL around minute 41 of the video. Is this because it is deprecated? Is there a work around?

npomfret commented 1 year ago

The sandbox is on more unfortunately. Also you will find part 3 of the course requires a paid version of the api sadly. But you can do the first two for free, just use base_url = 'https://cloud.iexapis.com'

christianperera commented 1 year ago

@npomfret @nickmccullum Love the course!! Unfortunately even when I use a new base_url = 'https://cloud.iexapis.com' I still get errors shown below. Anything I could try next? image image image This just happened - this section was working two days ago.

npomfret commented 1 year ago

Can you print out the http status code? Change your request to something like:

response = requests.get(api_url)
print(response.status_code).  <-- need to see this
data = response.json()

It should be 200.

npomfret commented 1 year ago

403 means Forbidden. You are making a request that is in one way not authorised. I think it's because you are using the sandbox which isn't allowed any more.

djangovanderplas commented 1 year ago

Found a solution to this issue. I had the same issue also with the 403 code.

It seems like IEX Cloud has abandoned the system of API's mentioned in the video (docs also call it old now). You can find the new documentation at https://iexcloud.io/docs/. What I did was make an account with IEX Cloud (for free). This will give you access for 30 days to their API platform, should be enough for the course. On the upside: the data will be actual data and not the random data like in the old sandbox.

The new api-url looks like this:

api_url = f'https://api.iex.cloud/v1/data/core/quote/{symbol}?token={IEX_CLOUD_API_TOKEN}'

And you have to replace the token in the secrets.py file with the private key from your account you just made.

My final code block looks like this:


api_url = f'https://api.iex.cloud/v1/data/core/quote/{symbol}?token={IEX_CLOUD_API_TOKEN}'
response = requests.get(api_url)
print(response.status_code)
data = response.json()[0]
print(data)```

Now it gives a 200 code for me, hopefully it'll work for you too!
Edit: Also note that the data is now returned in a list, if you place [0] after the .json() you get the normal dictionary like in the code.
itsmarcotime commented 1 year ago

@djangovanderplas have you made it to the Batch API Calls section yet? image I am able to print my batchApiCallUrl but when I run a .status_code on the url the response I get back is a 404 response. did you have this problem? or could there be something wrong with my url?

anupamKhatiwada01 commented 1 year ago

Having issues making batch url. Tried playing around a little. Tried the below as docs mention

Format: GET /data/WORKSPACE/DATASET_A,DATASET_B/KEY_1,KEY_2

Tried doing this

payment_needed

402

which apparently is payment needed!!!

This sucks!!!

Someone suggest a hack if they have.

johnpd commented 1 year ago

Found a solution to this issue. I had the same issue also with the 403 code.

It seems like IEX Cloud has abandoned the system of API's mentioned in the video (docs also call it old now). You can find the new documentation at https://iexcloud.io/docs/. What I did was make an account with IEX Cloud (for free). This will give you access for 30 days to their API platform, should be enough for the course. On the upside: the data will be actual data and not the random data like in the old sandbox.

The new api-url looks like this:

api_url = f'https://api.iex.cloud/v1/data/core/quote/{symbol}?token={IEX_CLOUD_API_TOKEN}'

And you have to replace the token in the secrets.py file with the private key from your account you just made.

My final code block looks like this:

api_url = f'https://api.iex.cloud/v1/data/core/quote/{symbol}?token={IEX_CLOUD_API_TOKEN}'
response = requests.get(api_url)
print(response.status_code)
data = response.json()[0]
print(data)```

Now it gives a 200 code for me, hopefully it'll work for you too!
Edit: Also note that the data is now returned in a list, if you place [0] after the .json() you get the normal dictionary like in the code.

thanks for the tip regarding the json()[0] my code now works with data = requests.get(api_url).json()[0]

inaG17 commented 1 year ago

batch api url?

anupamKhatiwada01 commented 1 year ago

@inaG17 batch api url mentioned in the pic in my comment throws 402 which says payment needed to make the api call

Smogryd commented 1 year ago

For those who are stuck on the batch call, here's my code. (Definitely could be better, but i'm still a beginner)

#Parsing function shown on the video
def chunks(lst, n):
    for i in range(0, len(lst), n):
        yield lst[i:i+n]

#The actual batch call
symbol_groups = list(chunks(stocks['Ticker'], 100))
symbol_strings = []
for i in range(0, len(symbol_groups)):
    symbol_strings.append(','.join(symbol_groups[i]))

final_dataframe = pd.DataFrame(columns = my_columns)      
for symb_str in symbol_strings:
    batch_api_call_url = f'https://api.iex.cloud/v1/data/core/quote/{symb_str}?token={IEX_CLOUD_API_TOKEN}'
    data = requests.get(batch_api_call_url).json()

    for symb in symb_str.split(','):
        for d in data :
            if d['symbol'] == symb:
                new_row = pd.Series([symb, d['latestPrice'], d['marketCap'], 'N/A'], index = my_columns)
        final_dataframe = pd.concat([final_dataframe,new_row.to_frame().T], ignore_index=True)

final_dataframe
Hongyac commented 1 year ago

Found a solution to this issue. I had the same issue also with the 403 code.

It seems like IEX Cloud has abandoned the system of API's mentioned in the video (docs also call it old now). You can find the new documentation at https://iexcloud.io/docs/. What I did was make an account with IEX Cloud (for free). This will give you access for 30 days to their API platform, should be enough for the course. On the upside: the data will be actual data and not the random data like in the old sandbox.

The new api-url looks like this:

api_url = f'https://api.iex.cloud/v1/data/core/quote/{symbol}?token={IEX_CLOUD_API_TOKEN}'

And you have to replace the token in the secrets.py file with the private key from your account you just made.

My final code block looks like this:

api_url = f'https://api.iex.cloud/v1/data/core/quote/{symbol}?token={IEX_CLOUD_API_TOKEN}'
response = requests.get(api_url)
print(response.status_code)
data = response.json()[0]
print(data)```

Now it gives a 200 code for me, hopefully it'll work for you too!
Edit: Also note that the data is now returned in a list, if you place [0] after the .json() you get the normal dictionary like in the code.

Just want to add that Public token of my new account also works.