Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 55 additions & 0 deletions demo_scripts/data_providers/nasdaq_data_provider_demo.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
# Copyright 2016-present CERN – European Organization for Nuclear Research
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import pandas as pd

from demo_scripts.demo_configuration.demo_ioc import container
from qf_lib.common.enums.price_field import PriceField
from qf_lib.common.enums.nasdaq_db_type import NasdaqDBType
from qf_lib.common.tickers.tickers import NasdaqTicker
from qf_lib.common.utils.dateutils.string_to_date import str_to_date
from qf_lib.data_providers.nasdaq.nasdaq_data_provider import NasdaqDataProvider

pd.options.display.max_rows = 100000
pd.options.display.max_columns = 100


def main():
data_provider = container.resolve(NasdaqDataProvider) # type: NasdaqDataProvider
start_date = str_to_date('2016-01-01')
end_date = str_to_date('2017-11-02')

print('Single ticker:')
ticker = NasdaqTicker('IBM', 'WIKI')
data = data_provider.get_history(tickers=ticker, start_date=start_date, end_date=end_date)
print(data)

print('Single ticker: lower case')
ticker = NasdaqTicker('ibm', 'wiki')
data = data_provider.get_history(tickers=ticker, start_date=start_date, end_date=end_date)
print(data)

print('Single ticker:')
ticker = NasdaqTicker('IBM', 'WIKI/PRICES', NasdaqDBType.Table)
data = data_provider.get_history(tickers=ticker, start_date=start_date, end_date=end_date)
print(data)

print('Single ticker, Price')
ticker = NasdaqTicker('IBM', 'WIKI/PRICES', NasdaqDBType.Table)
data = data_provider.get_price(tickers=ticker, fields=PriceField.Close, start_date=start_date, end_date=end_date)
print(data)


if __name__ == '__main__':
main()
2 changes: 2 additions & 0 deletions demo_scripts/demo_configuration/demo_ioc.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ def _get_ioc_container():

from qf_lib.backtesting.trading_session.backtest_trading_session_builder import BacktestTradingSessionBuilder
from qf_lib.data_providers.quandl.quandl_data_provider import QuandlDataProvider
from qf_lib.data_providers.nasdaq.nasdaq_data_provider import NasdaqDataProvider
from qf_lib.common.risk_parity_boxes.risk_parity_boxes import RiskParityBoxesFactory
from qf_lib.common.utils.dateutils.timer import RealTimer
from qf_lib.common.utils.dateutils.timer import Timer
Expand All @@ -48,6 +49,7 @@ def _get_ioc_container():
builder.register_instance(Settings, settings)

builder.register_class(QuandlDataProvider, component_scope=SingleInstance)
builder.register_class(NasdaqDataProvider, component_scope=SingleInstance)

# PUBLISHERS
builder.register_class(EmailPublisher, component_scope=SingleInstance)
Expand Down
36 changes: 36 additions & 0 deletions qf_lib/common/enums/nasdaq_db_type.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# Copyright 2016-present CERN – European Organization for Nuclear Research
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from enum import Enum


class NasdaqDBType(Enum):
Table = "Table"
Timeseries = "Timeseries"

@classmethod
def list_members(cls):
result = []
for key, value in cls.__members__.items():
result.append(str(value))
return result

def __str__(self):
return str(self.value)

def __lt__(self, other):
# method required for grouping
if self.__class__ is other.__class__:
return self.value < other.value
return False
56 changes: 55 additions & 1 deletion qf_lib/common/tickers/tickers.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
from typing import Union, Sequence

from qf_lib.common.enums.quandl_db_type import QuandlDBType
from qf_lib.common.enums.nasdaq_db_type import NasdaqDBType
from qf_lib.common.enums.security_type import SecurityType
from qf_lib.common.utils.logging.qf_parent_logger import qf_logger

Expand Down Expand Up @@ -244,7 +245,6 @@ def to_ticker(ticker_string: str):
else:
return [to_ticker(t) for t in ticker_str]


class QuandlTicker(Ticker):
""" Representation of Quandl tickers.

Expand Down Expand Up @@ -299,6 +299,60 @@ def to_ticker(ticker_string: str):
else:
return [to_ticker(t) for t in ticker_str]

class NasdaqTicker(Ticker):
""" Representation of Nasdaq tickers.

Parameters
------------
ticker: str
string containing series ID within a specific database
database_name: str
name of the database where the series is located
database_type: NasdaqDBType
type of the database
security_type: SecurityType
denotes the type of the security, that the ticker is representing e.g. SecurityType.STOCK for a stock,
SecurityType.FUTURE for a futures contract etc. By default equals SecurityType.STOCK.
point_value: int
size of the contract as given by the ticker's Data Provider. Used mostly by tickers of security_type FUTURE and
by default equals 1.
"""
def __init__(self, ticker: str, database_name: str, database_type: NasdaqDBType = NasdaqDBType.Timeseries,
security_type: SecurityType = SecurityType.STOCK, point_value: int = 1):
super().__init__(ticker, security_type, point_value)
self.database_name = database_name
self.database_type = database_type

def as_string(self) -> str:
if self.database_type == NasdaqDBType.Timeseries:
# returns a string that corresponds to the notation used by Nasdaq Timeseries: db_name/ticker
return self.database_name + '/' + self.ticker
elif self.database_type == NasdaqDBType.Table:
return self.ticker
else:
raise TypeError("Incorrect database type: {}".format(self.database_type))

def field_to_column_name(self, field: str):
return self.as_string() + ' - ' + field

@classmethod
def from_string(cls, ticker_str: Union[str, Sequence[str]], db_type: NasdaqDBType = NasdaqDBType.Timeseries,
security_type: SecurityType = SecurityType.STOCK, point_value: int = 1) \
-> Union["NasdaqTicker", Sequence["NasdaqTicker"]]:
"""
Example: NasdaqTicker.from_string('WIKI/MSFT')
Note: this method supports only the Timeseries tickers at the moment.
"""

def to_ticker(ticker_string: str):
db_name, ticker = ticker_string.rsplit('/', 1)
return NasdaqTicker(ticker, db_name, db_type, security_type=security_type, point_value=point_value)

if isinstance(ticker_str, str):
return to_ticker(ticker_str)
else:
return [to_ticker(t) for t in ticker_str]


class CcyTicker(Ticker):
""" Representation of Cryptocurrency tickers.
Expand Down
13 changes: 13 additions & 0 deletions qf_lib/data_providers/nasdaq/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Copyright 2016-present CERN – European Organization for Nuclear Research
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
Loading