generated from Tauffer-Consulting/domino_pieces_repository_template
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from Tauffer-Consulting/alphavantage
Alphavantage
- Loading branch information
Showing
10 changed files
with
187 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,4 @@ | ||
newsapi-python==0.2.7 | ||
beautifulsoup4==4.12.2 | ||
beautifulsoup4==4.12.2 | ||
pandas==2.1.3 | ||
plotly==5.18.0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
{ | ||
"name": "AlphaVantageDailyPiece", | ||
"description": "This Piece uses the AlphaVantage API to get daily stock data.\n- https://www.alphavantage.co/", | ||
"dependency": { | ||
"requirements_file": "requirements.txt" | ||
}, | ||
"container_resources": { | ||
"requests": { | ||
"cpu": "100m", | ||
"memory": "128Mi" | ||
}, | ||
"limits": { | ||
"cpu": "500m", | ||
"memory": "512Mi" | ||
} | ||
}, | ||
"style": { | ||
"node_label": "AlphaVantage Daily", | ||
"icon_class_name": "ant-design:stock-outlined" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
from pydantic import BaseModel, Field | ||
from enum import Enum | ||
|
||
|
||
class OutputSizeTypes(str, Enum): | ||
compact = "compact" | ||
full = "full" | ||
|
||
|
||
class InputModel(BaseModel): | ||
symbol: str = Field( | ||
default="AAPL", | ||
description="The stock symbol." | ||
) | ||
output_size: OutputSizeTypes = Field( | ||
default=OutputSizeTypes.compact, | ||
description="The size of the output data. Options are `compact` (100 data points) and full (full-length data). Default is `compact`." | ||
) | ||
max_data_points: int = Field( | ||
default=-1, | ||
description="The maximum number of data points to return. Default is -1, which returns all data points." | ||
) | ||
|
||
|
||
class OutputModel(BaseModel): | ||
data_file_path: str = Field( | ||
default="", | ||
description='Path to the data file.' | ||
) | ||
|
||
|
||
class SecretsModel(BaseModel): | ||
ALPHA_VANTAGE_API_KEY: str = Field( | ||
description="API key for Alpha Vantage data API." | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
from domino.base_piece import BasePiece | ||
from .models import InputModel, OutputModel, SecretsModel | ||
import pandas as pd | ||
import requests | ||
import json | ||
import plotly | ||
import plotly.graph_objects as go | ||
|
||
|
||
def make_plotly_fig(df: pd.DataFrame, symbol: str): | ||
fig = go.Figure() | ||
trace_candlestick = go.Candlestick( | ||
x=df['Date'], | ||
open=df['Open'], | ||
high=df['High'], | ||
low=df['Low'], | ||
close=df['Close'], | ||
name='Candlestick' | ||
) | ||
fig.add_trace(trace_candlestick) | ||
fig.update_layout( | ||
xaxis=dict( | ||
rangeslider=dict(visible=True), | ||
showgrid=True, # Show gridlines for the x-axis | ||
gridcolor='lightgray' # Set the grid color to gray | ||
), | ||
yaxis=dict( | ||
showgrid=True, | ||
gridcolor='lightgray', | ||
fixedrange=False | ||
), | ||
title=f'{symbol} Daily Stock Data', | ||
xaxis_title='Date', | ||
yaxis_title='Price', | ||
height=600, | ||
width=800, | ||
plot_bgcolor='white', | ||
) | ||
return fig | ||
|
||
|
||
class AlphaVantageDailyPiece(BasePiece): | ||
""" | ||
This Piece uses the AlphaVantage API to get daily stock data. | ||
- https://www.alphavantage.co/ | ||
""" | ||
def piece_function(self, input_data: InputModel, secrets_data: SecretsModel): | ||
|
||
api_key = secrets_data.ALPHA_VANTAGE_API_KEY | ||
if not api_key: | ||
raise Exception("Alpha Vantage API key is required.") | ||
|
||
symbol = input_data.symbol | ||
output_size = input_data.output_size.value | ||
number_of_points = input_data.max_data_points | ||
|
||
endpoint = 'https://www.alphavantage.co/query' | ||
params = { | ||
'function': 'TIME_SERIES_DAILY', | ||
'symbol': symbol, | ||
'outputsize': output_size, | ||
'apikey': api_key | ||
} | ||
|
||
data_file_path = f"{self.results_path}/results_daily_data.csv" | ||
|
||
try: | ||
response = requests.get(endpoint, params=params) | ||
if response.status_code == 200: | ||
data = response.json() | ||
daily_data = data['Time Series (Daily)'] | ||
df = pd.DataFrame(daily_data).T | ||
if number_of_points > 0 and number_of_points < len(df): | ||
df = df[:number_of_points] | ||
df.columns = ['Open', 'High', 'Low', 'Close', 'Volume'] | ||
df.index = pd.to_datetime(df.index) | ||
df.reset_index(inplace=True) | ||
df.rename(columns={'index': 'Date'}, inplace=True) | ||
df.to_csv(data_file_path, index=False) | ||
else: | ||
raise Exception(f'Error: {response.status_code} - {response.text}') | ||
except Exception as e: | ||
raise Exception(f'Error: {e}') | ||
|
||
# Save image to results | ||
fig = make_plotly_fig(df, symbol) | ||
fig_json = json.dumps(fig, cls=plotly.utils.PlotlyJSONEncoder) | ||
with open(f"{self.results_path}/results_plotly.json", "w") as f: | ||
f.write(fig_json) | ||
self.display_result = { | ||
"file_type": "plotly-json", | ||
"file_path": f"{self.results_path}/results_plotly.json", | ||
} | ||
|
||
return OutputModel( | ||
data_file_path=data_file_path | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
from domino.testing import piece_dry_run | ||
from pathlib import Path | ||
import os | ||
|
||
|
||
def test_alphavantage(): | ||
ALPHA_VANTAGE_API_KEY = os.environ.get("ALPHA_VANTAGE_API_KEY", None) | ||
if not ALPHA_VANTAGE_API_KEY: | ||
raise Exception("ALPHA_VANTAGE_API_KEY environment variable is required.") | ||
input_data = dict( | ||
symbol="AAPL", | ||
output_size="compact", | ||
max_data_points=-1 | ||
) | ||
piece_output = piece_dry_run( | ||
piece_name="AlphaVantageDailyPiece", | ||
input_data=input_data, | ||
secrets_data=dict( | ||
ALPHA_VANTAGE_API_KEY=ALPHA_VANTAGE_API_KEY | ||
), | ||
) | ||
assert Path(piece_output["data_file_path"]).exists() |