-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e737534
commit 615f613
Showing
17 changed files
with
167 additions
and
197 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
DEFAULT_DATE_COLUMN = "date" | ||
DEFAULT_SYMBOL_COLUMN = "symbol" | ||
DEFAULT_QUANTITY_COLUMN = "quantity" | ||
DEFAULT_PRICE_COLUMN = "price" |
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,5 +1,3 @@ | ||
import datetime | ||
|
||
import dateutil.parser | ||
|
||
holidays = list(map(lambda x: dateutil.parser.parse(x).date(), [ | ||
|
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,6 +1,6 @@ | ||
from .base import DataSource | ||
from .coinmarketcap import CoinMarketCapDataSource | ||
from .file import RowParquetFileDataSource | ||
from .yahoo import YahooDataSource | ||
from .dataframe import DataFrameDataSource | ||
from .delegate import DelegateDataSource | ||
from .factset import FactsetDataSource | ||
from .factset import FactsetDataSource | ||
from .yahoo import YahooDataSource |
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 |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import numpy | ||
import pandas | ||
|
||
from .base import DataSource | ||
from ... import constants | ||
|
||
|
||
class DataFrameDataSource(DataSource): | ||
|
||
def __init__( | ||
self, | ||
dataframe: pandas.DataFrame, | ||
date_column=constants.DEFAULT_DATE_COLUMN, | ||
symbol_column=constants.DEFAULT_SYMBOL_COLUMN, | ||
price_column=constants.DEFAULT_PRICE_COLUMN, | ||
closeable=True | ||
) -> None: | ||
super().__init__() | ||
|
||
dataframe = dataframe.drop_duplicates( | ||
subset=[symbol_column, date_column], | ||
keep="first" | ||
) | ||
|
||
dataframe = dataframe.pivot( | ||
index=date_column, | ||
columns=symbol_column, | ||
values=price_column | ||
) | ||
|
||
dataframe.index = pandas.to_datetime(dataframe.index) | ||
dataframe.index.name = constants.DEFAULT_DATE_COLUMN | ||
|
||
self.dataframe = dataframe | ||
self.closeable = closeable | ||
|
||
def fetch_prices(self, symbols, start, end): | ||
symbols = set(symbols) | ||
|
||
missings = symbols - set(self.dataframe.columns) | ||
founds = symbols - missings | ||
|
||
prices = None | ||
if len(founds): | ||
start = pandas.to_datetime(start) | ||
end = pandas.to_datetime(end) | ||
|
||
prices = self.dataframe[ | ||
(self.dataframe.index >= start) & | ||
(self.dataframe.index <= end) | ||
][list(founds)].copy() | ||
else: | ||
prices = pandas.DataFrame( | ||
index=pandas.DatetimeIndex( | ||
data=pandas.date_range(start=start, end=end) | ||
) | ||
) | ||
|
||
prices[list(missings)] = numpy.nan | ||
|
||
return prices | ||
|
||
def is_closeable(self): | ||
return self.closeable |
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 was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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,24 +1,17 @@ | ||
import abc | ||
import datetime | ||
import typing | ||
|
||
import pandas | ||
import yfinance | ||
|
||
from .base import DataSource | ||
|
||
|
||
class YahooDataSource(DataSource): | ||
|
||
@abc.abstractmethod | ||
def fetch_prices(self, symbols: typing.Set[str], start: datetime.date, end: datetime.date) -> pandas.DataFrame: | ||
def fetch_prices(self, symbols, start, end): | ||
return yfinance.download( | ||
tickers=symbols, | ||
start=start, | ||
end=end, | ||
show_errors=False | ||
)["Adj Close"] | ||
|
||
@abc.abstractmethod | ||
def is_closeable(self) -> bool: | ||
def is_closeable(self): | ||
return True |
Oops, something went wrong.