pyharmonics.marketdata package

Submodules

pyharmonics.marketdata.alpaca module

class pyharmonics.marketdata.alpaca.AlpacaCandleData(key, schema=None, time_zone='Europe/Dublin', df_index='dts')[source]

Bases: CandleData

If you want to get market data or prices for crypto currencies, you can use this class. This class is a subclass of CandleData and is used to get candle data from Alpaca.

>>> m = BinanceCandleData() # 200 1 hour candles of BTCUSDT price history
>>> m.get_candles('BTCUSDT', '1h', num_candles=1000) # 1000 1 hour candles of BTCUSDT price history
>>> m.get_candles('BTCUSDT', '1h', num_candles=1000, end=datetime.datetime(2020, 3, 21, 14, 0, 15))
# 1000 1 hour candles of BTCUSDT price history leading up to 21st march 2020
>>> m.get_candles('BTCUSDT', '1h', num_candles=1000, start=datetime.datetime(2020, 3, 21, 14, 0, 15))
# 1000 candle data from 21st of march 2020 until present
>>> m.get_candles('BTCUSDT', '1h', start=datetime.datetime(2020, 3, 21, 14, 0, 15))
# All candle data from 21st of march 2020 until present
INTERVALS = {'15m': <alpaca_trade_api.rest.TimeFrame object>, '1M': <alpaca_trade_api.rest.TimeFrame object>, '1d': <alpaca_trade_api.rest.TimeFrame object>, '1h': <alpaca_trade_api.rest.TimeFrame object>, '1m': <alpaca_trade_api.rest.TimeFrame object>, '1w': <alpaca_trade_api.rest.TimeFrame object>, '2h': <alpaca_trade_api.rest.TimeFrame object>, '30m': <alpaca_trade_api.rest.TimeFrame object>, '3m': <alpaca_trade_api.rest.TimeFrame object>, '45m': <alpaca_trade_api.rest.TimeFrame object>, '4h': <alpaca_trade_api.rest.TimeFrame object>, '5m': <alpaca_trade_api.rest.TimeFrame object>, '8h': <alpaca_trade_api.rest.TimeFrame object>}
MAX_CANDLES = 1000
SOURCE = 'Alpaca'
TIME_DELTA = {'15m': datetime.timedelta(seconds=900), '1M': datetime.timedelta(days=30), '1d': datetime.timedelta(days=1), '1h': datetime.timedelta(seconds=3600), '1m': datetime.timedelta(seconds=60), '1w': datetime.timedelta(days=7), '2h': datetime.timedelta(seconds=7200), '30m': datetime.timedelta(seconds=1800), '3m': datetime.timedelta(seconds=180), '45m': datetime.timedelta(seconds=2700), '4h': datetime.timedelta(seconds=14400), '5m': datetime.timedelta(seconds=240), '8h': datetime.timedelta(seconds=28800)}
get_candles(symbol: str, interval: TimeFrame, num_candles=None, start=None, end=None)[source]

If start and end are defined all candles between those time ranges will be pulled and stored in self.df. This is done using multiple calls. This is done in blocks of 1000 candles.

If only start or end or both are None, then a single call is made. If num_candles is greater than 1000 then multiple calls are made to get the data.

>>> a.get_candles('BTCUSDT', TimeFrame(1, TimeFrameUnit.Hour), num_candles=1000)
>>> a.get_candles('BTCUSDT', TimeFrame(1, TimeFrameUnit.Hour), start=datetime.datetime(2020, 3, 21, 14, 0, 15))
>>> a.get_candles('BTCUSDT', TimeFrame(1, TimeFrameUnit.Hour), end=datetime.datetime(2020, 3, 21, 14, 0, 15))
Parameters:
  • symbol – The symbol to fetch.

  • interval – The interval to fetch.

  • num_candles – The number of candles to fetch.

  • start – The start time for a range of candles.

  • end – The end time for a range of candles.

pyharmonics.marketdata.binance_data module

class pyharmonics.marketdata.binance_data.BinanceCandleData(schema=None, time_zone='Europe/Dublin', df_index='dts')[source]

Bases: CandleData

If you want to get market data or prices for crypto currencies, you can use this class. This class is a subclass of CandleData and is used to get candle data from Binance.

>>> m = BinanceCandleData() # create a new instance of BinanceCandleData
>>> m.get_candles('BTCUSDT', '1h', num_candles=1000) # 1000 1 hour candles of BTCUSDT price history
>>> m.get_candles('BTCUSDT', '1h', num_candles=1000, end=datetime.datetime(2020, 3, 21, 14, 0, 15))
# 1000 1 hour candles of BTCUSDT price history leading up to 21st march 2020
>>> m.get_candles('BTCUSDT', '1h', num_candles=1000, start=datetime.datetime(2020, 3, 21, 14, 0, 15))
# 1000 candle data from 21st of march 2020 until present
>>> m.get_candles('BTCUSDT', '1h', start=datetime.datetime(2020, 3, 21, 14, 0, 15))
# All candle data from 21st of march 2020 until present
INTERVALS = {'15m': '15m', '1M': '1M', '1d': '1d', '1h': '1h', '1m': '1m', '1w': '1w', '2h': '2h', '30m': '30m', '3d': '3d', '3m': '3m', '45m': '45m', '4h': '4h', '5m': '5m', '8h': '8h'}
MAX_CANDLES = 1000
SOURCE = 'Binance'
get_candles(symbol, interval, num_candles=None, start=None, end=None)[source]

If start and end are defined all candles between those time ranges will be pulled and stored in self.df. This is done using multiple calls. This is done in blocks of 1000 candles.

If only start or end or both are None, then a single call is made. If num_candles is greater than 1000 then multiple calls are made to get the data.

>>> m.get_candles('BTCUSDT', '1h', num_candles=1000)
>>> m.get_candles('BTCUSDT', '1h', num_candles=1000, end=datetime.datetime(2020, 3, 21, 14, 0, 15))
>>> m.get_candles('BTCUSDT', '1h', num_candles=1000, start=datetime.datetime(2020, 3, 21, 14, 0, 15))
>>> m.get_candles('BTCUSDT', '1h', start=datetime.datetime(2020, 3, 21, 14, 0, 15))
Parameters:
  • symbol – The symbol to fetch.

  • interval – The interval to fetch.

  • num_candles – The number of candles to fetch.

  • start – The start time for a range of candles.

  • end – The end time for a range of candles.

pyharmonics.marketdata.candle_base module

Created on Mon Nov 1 17:02:46 2021

@author: xual

class pyharmonics.marketdata.candle_base.CandleData[source]

Bases: ABC

ALL data apis will convert Kline/candle/trend data into a pandas dataframe. This dataframe uses DateTime as the index and [OPEN, HIGH, LOW, CLOSE, VOLUME] as column headers.

CLOSE = 'close'
CLOSE_TIME = 'close_time'
COLUMNS = ['open', 'high', 'low', 'close', 'volume', 'close_time', 'dts']
DAY_1 = '1d'
DAY_3 = '3d'
DAY_5 = '5d'
DTS = 'dts'
HIGH = 'high'
HOUR_1 = '1h'
HOUR_2 = '2h'
HOUR_4 = '4h'
HOUR_8 = '8h'
INDEX = 'index'
LOW = 'low'
MIN_1 = '1m'
MIN_10 = '10m'
MIN_15 = '15m'
MIN_3 = '3m'
MIN_30 = '30m'
MIN_45 = '45m'
MIN_5 = '5m'
MONTH_1 = '1M'
MONTH_3 = '3M'
MONTH_6 = '6M'
OPEN = 'open'
SOURCE = None
VOLUME = 'volume'
WEEK_1 = '1w'
abstractmethod get_candles()[source]

Set paramaters and convert dates ( especially tricky with binance epoch microseconds. )

num_candles start end Expect 100 None None 100 candles (finishing at now, end is now) 100 None time 100 candles ( finishing at end ) 100 time None 100 candles ( starting from start ) 100 time time candles from start until end ( num_candles is ignored log warning) None None None default candles (finishing at now, end is now) None None time default candles ( finishing at end ) None time None default candles ( starting from start ) None time time candles from start until end ( default is ignored)

>>> bc.get_candles('BTCUSDT', BinanceCandleData.HOUR_1, num_candles=100)
>>> bc.get_candles('BTCUSDT', BinanceCandleData.HOUR_1, start=datetime.datetime(2021, 1, 1))
>>> bc.get_candles('BTCUSDT', BinanceCandleData.HOUR_1, end=datetime.datetime(2021, 1, 1))
Parameters:
  • symbol – The symbol to fetch.

  • interval – The interval to fetch.

  • num_candles – The number of candles to fetch.

  • start – The start time to fetch.

  • end – The end time to fetch.

reset_index(index=None)[source]

Reset the index to the default index or the index specified.

>>> bc.reset_index()
>>> bc.reset_index(index=BinanceCandleData.CLOSE_TIME)
>>> bc.reset_index(index=BinanceCandleData.DTS)
Parameters:

index – The index to reset to. If None, the default index is used.

exception pyharmonics.marketdata.candle_base.InvalidTimeframe[source]

Bases: Exception

pyharmonics.marketdata.yahoo module

class pyharmonics.marketdata.yahoo.YahooCandleData(schema=None, time_zone='Europe/Dublin', df_index='dts')[source]

Bases: CandleData

YahooCandleData is a class for fetching candle data from Yahoo Finance. It is a subclass of CandleData and inherits all of its methods and attributes.

>>> m = YahooCandleData() # 200 1 hour candles of BTCUSDT price history
>>> m.get_candles('MSFT', '1h', num_candles=1000) # 1000 1 hour candles of BTCUSDT price history
>>> m.get_candles('MSFT', '1h', num_candles=1000, end=datetime.datetime(2020, 3, 21, 14, 0, 15))
# 1000 1 hour candles of MSFT price history leading up to 21st march 2020
>>> m.get_candles('MSFT', '1h', num_candles=1000, start=datetime.datetime(2020, 3, 21, 14, 0, 15))
# 1000 candle data from 21st of march 2020 until present
>>> m.get_candles('MSFT', '1h', start=datetime.datetime(2020, 3, 21, 14, 0, 15))
# All candle data from 21st of march 2020 until present
INTERVALS = {'15m': '15m', '1M': '1mo', '1d': '1d', '1h': '60m', '1m': '1m', '1w': '1wk', '30m': '15m', '3M': '3mo', '5d': '5d', '5m': '5m'}
LIMITS = {'15m': 'max', '1M': 'max', '1d': 'max', '1h': 'max', '1m': 'max', '1w': 'max', '3M': 'max', '5m': 'max'}
LONG_INTERVALS = {'M': 1, 'w': 4}
MAX_CANDLES = 10000
SHORT_INTERVALS = {'d': 1, 'h': 24, 'm': 1440}
SOURCE = 'Yahoo'
get_candles(symbol, interval, num_candles=None, start=None, end=None)[source]

Get the candle data from Yahoo Finance for the given asset and interval.

>>> y = Yahoo()
>>> y.get_candles('MSFT', '1d')
Parameters:
  • symbol – The symbol to fetch.

  • interval – The interval to fetch.

  • num_candles – The number of candles to fetch.

  • start – The start time for a range of candles.

  • end – The end time for a range of candles.

class pyharmonics.marketdata.yahoo.YahooOptionChain(option_chain, top=30, trend='openInterest')[source]

Bases: object

YahooOptionChain is a class for analyzing options data from Yahoo Finance.

>>> y = YahooOptionChain(option_chain)
>>> y = YahooOptionChain(option_chain, top=30, trend='openInterest')
class pyharmonics.marketdata.yahoo.YahooOptionData(symbol)[source]

Bases: object

YahooOptionData is a class for analyzing options data from Yahoo Finance.

>>> y = YahooOptionData('AAPL')
>>> y = YahooOptionData('AAPL').analyse_options(top=30, trend='volume')
>>> y = YahooOptionData('AAPL').analyse_options(top=30, trend='openInterest')
analyse_options(top=30, trend='openInterest')[source]

Analyze the options data for the given asset.

>>> y = YahooOptionData('AAPL').analyse_options(top=30, trend='volume')
>>> y = YahooOptionData('AAPL').analyse_options(top=30, trend='openInterest')
Params int top:

the top 30 options, ranked by openInterest, are analyzed by default.

Module contents

class pyharmonics.marketdata.AlpacaCandleData(key, schema=None, time_zone='Europe/Dublin', df_index='dts')[source]

Bases: CandleData

If you want to get market data or prices for crypto currencies, you can use this class. This class is a subclass of CandleData and is used to get candle data from Alpaca.

>>> m = BinanceCandleData() # 200 1 hour candles of BTCUSDT price history
>>> m.get_candles('BTCUSDT', '1h', num_candles=1000) # 1000 1 hour candles of BTCUSDT price history
>>> m.get_candles('BTCUSDT', '1h', num_candles=1000, end=datetime.datetime(2020, 3, 21, 14, 0, 15))
# 1000 1 hour candles of BTCUSDT price history leading up to 21st march 2020
>>> m.get_candles('BTCUSDT', '1h', num_candles=1000, start=datetime.datetime(2020, 3, 21, 14, 0, 15))
# 1000 candle data from 21st of march 2020 until present
>>> m.get_candles('BTCUSDT', '1h', start=datetime.datetime(2020, 3, 21, 14, 0, 15))
# All candle data from 21st of march 2020 until present
INTERVALS = {'15m': <alpaca_trade_api.rest.TimeFrame object>, '1M': <alpaca_trade_api.rest.TimeFrame object>, '1d': <alpaca_trade_api.rest.TimeFrame object>, '1h': <alpaca_trade_api.rest.TimeFrame object>, '1m': <alpaca_trade_api.rest.TimeFrame object>, '1w': <alpaca_trade_api.rest.TimeFrame object>, '2h': <alpaca_trade_api.rest.TimeFrame object>, '30m': <alpaca_trade_api.rest.TimeFrame object>, '3m': <alpaca_trade_api.rest.TimeFrame object>, '45m': <alpaca_trade_api.rest.TimeFrame object>, '4h': <alpaca_trade_api.rest.TimeFrame object>, '5m': <alpaca_trade_api.rest.TimeFrame object>, '8h': <alpaca_trade_api.rest.TimeFrame object>}
MAX_CANDLES = 1000
SOURCE = 'Alpaca'
TIME_DELTA = {'15m': datetime.timedelta(seconds=900), '1M': datetime.timedelta(days=30), '1d': datetime.timedelta(days=1), '1h': datetime.timedelta(seconds=3600), '1m': datetime.timedelta(seconds=60), '1w': datetime.timedelta(days=7), '2h': datetime.timedelta(seconds=7200), '30m': datetime.timedelta(seconds=1800), '3m': datetime.timedelta(seconds=180), '45m': datetime.timedelta(seconds=2700), '4h': datetime.timedelta(seconds=14400), '5m': datetime.timedelta(seconds=240), '8h': datetime.timedelta(seconds=28800)}
get_candles(symbol: str, interval: TimeFrame, num_candles=None, start=None, end=None)[source]

If start and end are defined all candles between those time ranges will be pulled and stored in self.df. This is done using multiple calls. This is done in blocks of 1000 candles.

If only start or end or both are None, then a single call is made. If num_candles is greater than 1000 then multiple calls are made to get the data.

>>> a.get_candles('BTCUSDT', TimeFrame(1, TimeFrameUnit.Hour), num_candles=1000)
>>> a.get_candles('BTCUSDT', TimeFrame(1, TimeFrameUnit.Hour), start=datetime.datetime(2020, 3, 21, 14, 0, 15))
>>> a.get_candles('BTCUSDT', TimeFrame(1, TimeFrameUnit.Hour), end=datetime.datetime(2020, 3, 21, 14, 0, 15))
Parameters:
  • symbol – The symbol to fetch.

  • interval – The interval to fetch.

  • num_candles – The number of candles to fetch.

  • start – The start time for a range of candles.

  • end – The end time for a range of candles.

class pyharmonics.marketdata.BinanceCandleData(schema=None, time_zone='Europe/Dublin', df_index='dts')[source]

Bases: CandleData

If you want to get market data or prices for crypto currencies, you can use this class. This class is a subclass of CandleData and is used to get candle data from Binance.

>>> m = BinanceCandleData() # create a new instance of BinanceCandleData
>>> m.get_candles('BTCUSDT', '1h', num_candles=1000) # 1000 1 hour candles of BTCUSDT price history
>>> m.get_candles('BTCUSDT', '1h', num_candles=1000, end=datetime.datetime(2020, 3, 21, 14, 0, 15))
# 1000 1 hour candles of BTCUSDT price history leading up to 21st march 2020
>>> m.get_candles('BTCUSDT', '1h', num_candles=1000, start=datetime.datetime(2020, 3, 21, 14, 0, 15))
# 1000 candle data from 21st of march 2020 until present
>>> m.get_candles('BTCUSDT', '1h', start=datetime.datetime(2020, 3, 21, 14, 0, 15))
# All candle data from 21st of march 2020 until present
INTERVALS = {'15m': '15m', '1M': '1M', '1d': '1d', '1h': '1h', '1m': '1m', '1w': '1w', '2h': '2h', '30m': '30m', '3d': '3d', '3m': '3m', '45m': '45m', '4h': '4h', '5m': '5m', '8h': '8h'}
MAX_CANDLES = 1000
SOURCE = 'Binance'
get_candles(symbol, interval, num_candles=None, start=None, end=None)[source]

If start and end are defined all candles between those time ranges will be pulled and stored in self.df. This is done using multiple calls. This is done in blocks of 1000 candles.

If only start or end or both are None, then a single call is made. If num_candles is greater than 1000 then multiple calls are made to get the data.

>>> m.get_candles('BTCUSDT', '1h', num_candles=1000)
>>> m.get_candles('BTCUSDT', '1h', num_candles=1000, end=datetime.datetime(2020, 3, 21, 14, 0, 15))
>>> m.get_candles('BTCUSDT', '1h', num_candles=1000, start=datetime.datetime(2020, 3, 21, 14, 0, 15))
>>> m.get_candles('BTCUSDT', '1h', start=datetime.datetime(2020, 3, 21, 14, 0, 15))
Parameters:
  • symbol – The symbol to fetch.

  • interval – The interval to fetch.

  • num_candles – The number of candles to fetch.

  • start – The start time for a range of candles.

  • end – The end time for a range of candles.

class pyharmonics.marketdata.YahooCandleData(schema=None, time_zone='Europe/Dublin', df_index='dts')[source]

Bases: CandleData

YahooCandleData is a class for fetching candle data from Yahoo Finance. It is a subclass of CandleData and inherits all of its methods and attributes.

>>> m = YahooCandleData() # 200 1 hour candles of BTCUSDT price history
>>> m.get_candles('MSFT', '1h', num_candles=1000) # 1000 1 hour candles of BTCUSDT price history
>>> m.get_candles('MSFT', '1h', num_candles=1000, end=datetime.datetime(2020, 3, 21, 14, 0, 15))
# 1000 1 hour candles of MSFT price history leading up to 21st march 2020
>>> m.get_candles('MSFT', '1h', num_candles=1000, start=datetime.datetime(2020, 3, 21, 14, 0, 15))
# 1000 candle data from 21st of march 2020 until present
>>> m.get_candles('MSFT', '1h', start=datetime.datetime(2020, 3, 21, 14, 0, 15))
# All candle data from 21st of march 2020 until present
INTERVALS = {'15m': '15m', '1M': '1mo', '1d': '1d', '1h': '60m', '1m': '1m', '1w': '1wk', '30m': '15m', '3M': '3mo', '5d': '5d', '5m': '5m'}
LIMITS = {'15m': 'max', '1M': 'max', '1d': 'max', '1h': 'max', '1m': 'max', '1w': 'max', '3M': 'max', '5m': 'max'}
LONG_INTERVALS = {'M': 1, 'w': 4}
MAX_CANDLES = 10000
SHORT_INTERVALS = {'d': 1, 'h': 24, 'm': 1440}
SOURCE = 'Yahoo'
get_candles(symbol, interval, num_candles=None, start=None, end=None)[source]

Get the candle data from Yahoo Finance for the given asset and interval.

>>> y = Yahoo()
>>> y.get_candles('MSFT', '1d')
Parameters:
  • symbol – The symbol to fetch.

  • interval – The interval to fetch.

  • num_candles – The number of candles to fetch.

  • start – The start time for a range of candles.

  • end – The end time for a range of candles.

class pyharmonics.marketdata.YahooOptionData(symbol)[source]

Bases: object

YahooOptionData is a class for analyzing options data from Yahoo Finance.

>>> y = YahooOptionData('AAPL')
>>> y = YahooOptionData('AAPL').analyse_options(top=30, trend='volume')
>>> y = YahooOptionData('AAPL').analyse_options(top=30, trend='openInterest')
analyse_options(top=30, trend='openInterest')[source]

Analyze the options data for the given asset.

>>> y = YahooOptionData('AAPL').analyse_options(top=30, trend='volume')
>>> y = YahooOptionData('AAPL').analyse_options(top=30, trend='openInterest')
Params int top:

the top 30 options, ranked by openInterest, are analyzed by default.