Skip to content

Commit 5ed2b69

Browse files
First commit
0 parents  commit 5ed2b69

8 files changed

+147
-0
lines changed

forex_CSV_nolib.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import os
2+
import requests
3+
import pandas as pd
4+
5+
api_key = os.getenv('ALPHAVANTAGE_API_KEY')
6+
7+
base_url = 'https://www.alphavantage.co/query?'
8+
params = {'function': 'FX_INTRADAY',
9+
'from_symbol': 'EUR',
10+
'to_symbol': 'USD',
11+
'interval': '15min',
12+
'datatype': 'csv',
13+
'apikey': api_key}
14+
15+
response = requests.get(base_url, params=params)
16+
17+
#Save CSV to file
18+
with open('eurusd.csv', 'wb') as file:
19+
file.write(response.content)
20+
21+
df = pd.read_csv('eurusd.csv') #Create pandas dataframe
22+
23+
df.set_index('timestamp', inplace=True) #Time-series index
24+
25+
df.to_csv('eurusd.csv') #Save dataframe to csv file

forex_pandas_library.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
from alpha_vantage.foreignexchange import ForeignExchange
2+
3+
app = ForeignExchange(output_format='pandas')
4+
5+
eurusd = app.get_currency_exchange_intraday('EUR', 'USD')
6+
7+
print(eurusd[0])
8+
print(eurusd[0]['2020-04-03']) #Narrow output to specific date

forex_pandas_nolib.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import os
2+
import requests
3+
import pandas as pd
4+
5+
api_key = os.getenv('ALPHAVANTAGE_API_KEY')
6+
7+
base_url = 'https://www.alphavantage.co/query?'
8+
params = {'function': 'FX_INTRADAY',
9+
'from_symbol': 'EUR',
10+
'to_symbol': 'USD',
11+
'interval': '15min',
12+
'apikey': api_key}
13+
14+
response = requests.get(base_url, params=params)
15+
response_dict = response.json()
16+
_, header = response.json()
17+
18+
#Convert to pandas dataframe
19+
df = pd.DataFrame.from_dict(response_dict[header], orient='index')
20+
21+
#Clean up column names
22+
df_cols = [i.split(' ')[1] for i in df.columns]
23+
df.columns = df_cols
24+
25+
print(df)

readme.rst

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
======================================================
2+
Alpha Vantage Introduction Guide - AlgoTrading101 Blog
3+
======================================================
4+
5+
This is the code used in `Alpha Vantage Introduction Guide <https://algotrading101.com/learn/alpha-vantage-guide/>`_ published on the AlgoTrading101 Blog
6+
7+
-----------------
8+
Table of Contents
9+
-----------------
10+
11+
* `What is the Alpha Vantage API? <https://algotrading101.com/learn/alpha-vantage-guide/#what-is-the-alpha-vantage-api>`_
12+
* `Is the Alpha Vantage API free? <https://algotrading101.com/learn/alpha-vantage-guide/#is-the-alpha-vantage-api-free>`_
13+
* `What kind of data does the Alpha Vantage API offer? <https://algotrading101.com/learn/alpha-vantage-guide/#what-data-does-alpha-vantage-offer>`_
14+
* `What are the advantages of using the Alpha Vantage API? <https://algotrading101.com/learn/alpha-vantage-guide/#what-are-advantage-of-using-alpha-vantage>`_
15+
* `Why shouldn’t I use the Alpha Vantage API? <https://algotrading101.com/learn/alpha-vantage-guide/#why-shouldn't-i-use-alpha-vantage-api>`_
16+
* `What are some alternatives to the Alpha Vantage API? <https://algotrading101.com/learn/alpha-vantage-guide/#what-are-alternatives-to-alpha-vantage-api>`_
17+
* `How do I get started with the Alpha Vantage API? <https://algotrading101.com/learn/alpha-vantage-guide/#how-do-i-start-with-alpha-vantage-api>`_
18+
* `Do I need to use a library to access the Alpha Vantage API? <https://algotrading101.com/learn/alpha-vantage-guide/#do-i-need-a-library-for-alpha-vantage-api>`_
19+
* `How do I configure the Alpha Vantage Library? <https://algotrading101.com/learn/alpha-vantage-guide/#how-to-configure-alpha-vantage-library>`_
20+
* `How do I retrieve stock data using the Alpha Vantage library in Python? <https://algotrading101.com/learn/alpha-vantage-guide/#how-to-retrieve-stock-data-with-library>`_
21+
* `How can I get retrieve stock data without using the Alpha Vantage library in Python? <https://algotrading101.com/learn/alpha-vantage-guide/#how-to-retrieve-stock-data-without-library>`_
22+
* `Does the Alpha Vantage API work well with the pandas library? <https://algotrading101.com/learn/alpha-vantage-guide/#does-alpha-vantage-work-well-with-pandas>`_
23+
* `Should I use the CSV or JSON format for my data? <https://algotrading101.com/learn/alpha-vantage-guide/#should-i-use-json-or-csv-for-my-data>`_
24+
* `How can I use technical indicators with the Alpha Vantage library? <https://algotrading101.com/learn/alpha-vantage-guide/#how-to-technical-indicators-alpha-vantage>`_
25+
* `Final Thoughts <https://algotrading101.com/learn/alpha-vantage-guide/#final-thoughts-alpha-vantage-api>`_
26+
27+
------------
28+
Requirements
29+
------------
30+
31+
* `python <https://www.python.org>`_ >= 2.7, 3.4+
32+
* `pandas <https://github.com/pandas-dev/pandas>`_ (tested to work with >= 1.0.3 )
33+
* `requests <https://github.com/psf/requests>`_ (tested to work with >= 2.22.0 )
34+
* `alpha_vantage <https://github.com/RomelTorres/alpha_vantage>`_ (tested to work with >= 2.1.3 )
35+
36+
-----------
37+
Author Info
38+
-----------
39+
40+
:author: Jignesh Davda
41+
:author page: https://algotrading101.com/learn/author/jdavda/
42+
:published: 2020-04-08

requirements.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
pandas==1.0.3
2+
requests==2.22.0
3+
alpha_vantage==2.1.3

retrieve_stock_data_library.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
from alpha_vantage.timeseries import TimeSeries
2+
3+
app = TimeSeries() #Default - JSON format
4+
#Valid options for output_format are 'pandas', 'csv'.
5+
#app = TimeSeries(output_format='pandas')
6+
#app = TimeSeries(output_format='csv')
7+
8+
help(app)
9+
10+
aapl = app.get_daily_adjusted('BTCUSD', outputsize='full')
11+
print(aapl)

retrieve_stock_data_nolib.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import os
2+
import requests
3+
4+
api_key = os.getenv('ALPHAVANTAGE_API_KEY')
5+
print(api_key)
6+
7+
#method 1 - parameters in url
8+
response = requests.get('https://www.alphavantage.co/query?function=TIME_SERIES_DAILY_ADJUSTED&symbol=IBM&apikey=demo')
9+
print(response.json())
10+
11+
#method 2 - using f strings
12+
base_url = 'https://www.alphavantage.co/query?'
13+
function = 'TIME_SERIES_DAILY_ADJUSTED'
14+
symbol = 'IBM'
15+
16+
response = requests.get(f'{base_url}function={function}&symbol={symbol}&apikey={api_key}')
17+
print(response.json())
18+
19+
#method 3 - using a params dictionary ** recommended method
20+
base_url = 'https://www.alphavantage.co/query?'
21+
params = {'function': 'TIME_SERIES_DAILY_ADJUSTED',
22+
'symbol': 'IBM',
23+
'apikey': api_key}
24+
25+
response = requests.get(base_url, params=params)
26+
print(response.json())

tech_indicators.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
from alpha_vantage.techindicators import TechIndicators
2+
3+
app = TechIndicators(output_format='pandas')
4+
#help(app.get_macd)
5+
6+
aapl_macd = app.get_macd('aapl', fastperiod=12, slowperiod=26, signalperiod=9)
7+
print(aapl_macd)

0 commit comments

Comments
 (0)