API Reference

Subpackages

Submodules

pyharmonics.constants module

pyharmonics.patterns module

class pyharmonics.patterns.ABCDPattern(symbol, interval, x: tuple, y: tuple, name: str, retraces: dict, formed: bool, bullish: bool)[source]

Bases: ABCPattern

An extension of the ABCPattern class for ABCD patterns.

ABCD patterns are a subset of harmonic patterns that have a 3 swing structure. A price moves in an up or down swing and retraces to a fibonacci level before continuing in the opposite direction.

class pyharmonics.patterns.ABCPattern(symbol, interval, x: tuple, y: tuple, name: str, retraces: dict, formed: bool, bullish: bool)[source]

Bases: HarmonicPattern

An extension of the HarmonicPattern class for ABC patterns.

ABC patterns are a subset of harmonic patterns that have a 2 swing structure. A price moves in an up or down swing and retraces to a fibonacci level before continuing in the original direction.

.382, .5, .618, .786, .886, 1.13, 1.141, 1.618 are common retracement levels.

to_dict()[source]

Return a dictionary representation of the pattern.

class pyharmonics.patterns.Divergence(indicator: str, name: str, x: tuple, y: tuple, ind_x: tuple, ind_y: tuple, bullish: bool)[source]

Bases: object

A class to represent a divergence pattern in the market.

A divergence is a disagreement between the price and an indicator. While the price makes a new high or low, the indicator does not.

to_dict()[source]

Return a dictionary representation of the divergence.

class pyharmonics.patterns.HarmonicPattern[source]

Bases: ABC

A base class for all harmonic patterns.

A harmonic pattern is one from price swings that retrace to fibonacci levels.

ABCD, XABCD ( Gartley, Bat, Butterfly, Crab, Cypher, Shark, 5-0, etc. )

abstractmethod to_dict()[source]
class pyharmonics.patterns.XABCDPattern(symbol, interval, x: tuple, y: tuple, name: str, retraces: dict, formed: bool, bullish: bool)[source]

Bases: ABCPattern

An extension of the ABCPattern class for XABCD patterns.

XABCD patterns are a subset of harmonic patterns that have a 4 swing structure. A price moves in an up or down swing and retraces to a fibonacci level before continuing in the opposite direction.

pyharmonics.positions module

class pyharmonics.positions.Position(pattern, strike, dollar_amount)[source]

Bases: object

to_dict()[source]

Return the position as a dictionary.

pyharmonics.quick module

pyharmonics.quick.play_position(hs, pattern, strike, dollar_amount)[source]
pyharmonics.quick.whats_forming(cd, limit_to=10, percent_complete=0.8)[source]

Search for forming harmonic patterns and divergences in the given data.

Parameters:
  • cd – The CandleData object to search.

  • limit_to – Limit the results to patterns that complete in that last n candles.

  • percent_complete – The percentage of the pattern that must be complete.

Returns:

The HarmonicSearch object containing the results.

pyharmonics.quick.whats_forming_binance(symbol, interval, limit_to=10, percent_complete=0.8, candles=1000)[source]

Search for forming harmonic patterns and divergences in the given Binance data.

>>> harmonic_search = whats_forming_binance('BTCUSDT', BinanceCandleData.HOUR_1, limit_to=10, percent_complete=0.8, candles=1000)
>>> harmonic_search.get_patterns()
>>> harmonic_search = whats_forming_binance('BTCUSDT', '1d', candles=1000)
>>> harmonic_search.get_patterns()
Parameters:
  • symbol – The symbol to search.

  • interval – The timeframe or interval to search on.

  • limit_to – Limit the results to patterns that complete in that last n candles.

  • percent_complete – The percentage of the pattern that must be complete.

  • candles – The number of candles to fetch.

Returns:

The HarmonicSearch object containing the results.

pyharmonics.quick.whats_forming_yahoo(symbol, interval, limit_to=10, percent_complete=0.8, candles=1000)[source]

Search for forming harmonic patterns and divergences in the given Yahoo data.

>>> harmonic_search = whats_forming_yahoo('AAPL', YahooCandleData.DAY_1, limit_to=10, percent_complete=0.8, candles=1000)
>>> harmonic_search.get_patterns()
>>> harmonic_search = whats_forming_yahoo('AAPL', '1d', candles=1000)
>>> harmonic_search.get_patterns()
Parameters:
  • symbol – The symbol to search.

  • interval – The timeframe or interval to search on.

  • limit_to – Limit the results to patterns that complete in that last n candles.

  • percent_complete – The percentage of the pattern that must be complete.

  • candles – The number of candles to fetch.

Returns:

The HarmonicSearch object containing the results.

pyharmonics.quick.whats_new(cd, limit_to=-1)[source]

Search for new harmonic patterns and divergences in the given data.

Parameters:
  • cd – The CandleData object to search.

  • limit_to – Limit the results to patterns that complete in that last n candles.

Returns:

The HarmonicSearch object containing the results.

pyharmonics.quick.whats_new_binance(symbol, interval, limit_to=-1, candles=1000)[source]

Search for new harmonic patterns and divergences in the given Binance data.

>>> harmonic_search = whats_new_binance('BTCUSDT', BinanceCandleData.HOUR_1, limit_to=10, candles=1000)
>>> harmonic_search.get_patterns()
>>> harmonic_search = whats_new_binance('BTCUSDT', '1d', candles=1000)
>>> harmonic_search.get_patterns()
Parameters:
  • symbol – The symbol to search.

  • limit_to – Limit the results to patterns that complete in that last n candles.

  • candles – The number of candles to fetch.

Returns:

The HarmonicSearch object containing the results.

pyharmonics.quick.whats_new_yahoo(symbol, interval, limit_to=-1, candles=1000)[source]

Search for new harmonic patterns and divergences in the given Yahoo data.

>>> harmonic_search = whats_new_yahoo('AAPL', YahooCandleData.DAY_1, limit_to=10, candles=1000)
>>> harmonic_search.get_patterns()
>>> harmonic_search = whats_new_yahoo('AAPL', '1d', candles=1000)
>>> harmonic_search.get_patterns()
Parameters:
  • symbol – The symbol to search.

  • interval – The timeframe or interval to search on.

  • limit_to – Limit the results to patterns that complete in that last n candles.

  • candles – The number of candles to fetch.

Returns:

The HarmonicSearch object containing the results.

pyharmonics.quick.whats_options_interest(symbol)[source]

Displays the options open interest for the given symbol. The open interest is the number of contracts that are open. A plot illustrates the point of minimum losses for the market maker.

Parameters:

symbol – The symbol to search.

Returns:

The YahooOptionData object containing the results.

pyharmonics.quick.whats_options_volume(symbol)[source]

Displays the options volume for the given symbol. The volume is the number of contracts traded for the day. A plot illustrates the point of minimum losses for the market maker.

Parameters:

symbol – The symbol to search.

Returns:

The YahooOptionData object containing the results.

pyharmonics.technicals module

class pyharmonics.technicals.OHLCTechnicals(df, symbol, interval, indicator_config=None, sma_config=None, ema_config=None, peak_spacing=10)[source]

Bases: TechnicalsBase

An extension of TechnicalsBase for OHLC data.

>>> t = OHLCTechnicals(df, symbol, time_frame)
get_peak_x_y(peak_type)[source]

Given the indexs of a pattern ( not a dataframe ) found in this technical data, return the time and prices at those indexes.

>>> t.get_peak_x_y(t.PRICE_PEAKS)
Parameters:

peak_type – str The series containing True or False where True marks a peak on this trend

Returns:

tuple

class pyharmonics.technicals.Technicals(df, symbol, interval, indicator_config=None, sma_config=None, ema_config=None, peak_spacing=10)[source]

Bases: TechnicalsBase

An extension of TechnicalsBase for data that tracks only one trend.

get_peak_x_y(peak_type)[source]

Given the indexs of a pattern ( not a dataframe ) found in this technical data, return the time and prices at those indexes.

>>> t.get_peak_x_y(t.PRICE_PEAKS)
Parameters:

peak_type – str The series containing True or False where True marks a peak on this trend

Returns:

tuple

class pyharmonics.technicals.TechnicalsBase(df, indicator_config=None, sma_config=None, ema_config=None, peak_spacing=10)[source]

Bases: ABC

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

Every pattern that indicates a bullish or bearish entry ( buy or sell ) Is based on both the price, volume and indicator analyses.

Peaks, indicators and fibonacci matrix are extended on to candle data. Candle data is a parameter to this constructor

ADX = 'adx'
BBP = 'bb%'
CCI = 'cci'
DIVERGENCE = 'divergence'
DIm = 'di-'
DIp = 'di+'
EMA_13 = 'ema_13'
EMA_21 = 'ema 21'
EMA_34 = 'ema 34'
EMA_5 = 'ema 5'
EMA_55 = 'ema 55'
EMA_8 = 'ema 8'
EXEGGERATED = 'exeggerated'
HIDDEN = 'hidden'
HIGHS = 'highs'
LOWS = 'lows'
MACD = 'macd'
MACD_DIPS = 'macd_dips'
MACD_PEAKS = 'macd_peaks'
MFI = 'mfi'
OBS = 'obs'
PEAK = 'peak'
PEAK_INDEX = 0
PEAK_PRICE = 1
PEAK_TYPE = 2
PRICE_DIPS = 'price_dips'
PRICE_PEAKS = 'price_peaks'
REGULAR = 'regular'
RSI = 'rsi'
RSI_DIPS = 'rsi_dips'
RSI_PEAKS = 'rsi_peaks'
SMA_100 = 'sma 100'
SMA_150 = 'sma 150'
SMA_200 = 'sma 200'
SMA_50 = 'sma 50'
STOCH_RSI = 'stoch_rsi'
STOCH_RSI_DIPS = 'stoch_rsi_dips'
STOCH_RSI_PEAKS = 'stoch_rsi_peaks'
TREND = 'trend'
filter_peak_data(lows=False)[source]

Extract either the highs or the lows from peaks.

>>> t.filter_peak_data()
[(9, 30485.0, 1), (42, 28000.0, 1), (57, 30036.0, 1), (81, 29969.39, 1), ...]
>>> t.filter_peak_data(lows=True)
[(29, 27125.0, 0), (46, 26942.82, 0), (89, 27666.95, 0), (131, 27262.0, 0), ...]
Parameters:

lows – bool If True, return the lows, otherwise return the highs.

Returns:

list

get_index_x(x)[source]

given the index of a pattern found in this technical data, return the time at those indexes.

>>> t = OHLCTechnicals(df, symbol, time_frame)
>>> t.get_index_x([1, 2, 3])
[Timestamp('2023-04-17 08:59:59+0100', tz='Europe/Dublin'),
 Timestamp('2023-04-17 12:59:59+0100', tz='Europe/Dublin'),
 Timestamp('2023-04-17 16:59:59+0100', tz='Europe/Dublin')]
Parameters:

x – list The indexes within technical_data.peak_indexes and technical_data.peak_prices that form the pattern.

Returns:

list

get_pattern_x_y(peak_indexes)[source]

Given the indexs of a pattern ( not a dataframe ) found in this technical data, return the time and prices at those indexes.

>>> t.get_pattern_x_y([1, 2, 3])
([29, 42, 46], [27125.0, 28000.0, 26942.82])
Parameters:

peak_indexes – list The indexes within technical_data.peak_indexes and technical_data.peak_prices that form the pattern.

Returns:

list

abstractmethod get_peak_x_y()[source]
get_series_x_y(series_indexes, series)[source]

Given the indexs of a pattern found in this technical data, return the time and indicator readings at those indexes.

>>> t.get_series_x_y([100, 200, 300], t.MACD)
([100, 200, 300], [5.503533503855266, -11.21857793005239, -160.57022744782142])
Parameters:
  • series_indexes – list The indexes within technical_data.peak_indexes and technical_data.peak_prices that form the pattern.

  • series – str The series to extract the data from.

Returns:

list

pyharmonics.utils module

pyharmonics.utils.find_peaks(data, comparator, axis=0, order=1, mode='clip')[source]

Calculate the relative extrema of data. Relative extrema are calculated by finding locations where comparator(data[n], data[n+1:n+order+1]) is True.

>>> import numpy as np
>>> testdata = np.array([1,2,3,2,1])
>>> self.find_peaks(testdata, np.greater, axis=0)
array([False, False,  True, False, False], dtype=bool)
Parameters:
  • data – The data to search for peaks.

  • comparator – The comparison function.

  • axis – The axis to calculate along.

  • order – The order of the peak.

  • mode – The mode to use.

Returns:

The relative extrema.

pyharmonics.utils.get_candle_span(candle_time, candle_gap: int, num_gaps: int) list[source]

Get the span of candles around the given candle time.

>>> utils.get_candle_span(10, 1, 3)
[7, 8, 9, 10, 11, 12, 13]
>>> utils.get_candle_span(100, 10, 3)
[70, 80, 90, 100, 110, 120, 130]
Parameters:
  • candle_time – The time index to span.

  • candle_gap – The gap between candles.

  • num_gaps – The number of gaps to span.

Returns:

The list of time indexes

pyharmonics.utils.get_pattern_definition(tolerance: float, patterns: dict) dict[source]

No pattern forms exactly. There is always some tolerance in the pattern formation. This function adjusts the pattern retraces to account for this.

>>> utils.get_pattern_definition(0.05, {'ABCD': {'ABCD': {'min': 0.618, 'max': 0.786}}})
{'ABCD': {'ABCD': {'min': 0.5871, 'max': 0.8259}}}
Parameters:
  • tolerance – The tolerance to apply to the pattern retraces.

  • patterns – The patterns to adjust.

pyharmonics.utils.get_pattern_direction(prices: list) str[source]

All patterns forming a final retrace where the price is moving down are indicating a reversal to the upside is iminent. This is bullish.

All patterns forming a final retrace where the price is moving up are indicating a reversal to the downside is iminent. This is bearish.

>>> utils.get_pattern_direction([1.0, 2.0, 3.0])
'bullish'
>>> utils.get_pattern_direction([3.0, 2.0, 1.0])
'bearish'
Parameters:

prices – The prices of the pattern.

Returns:

The direction of the pattern

pyharmonics.utils.get_pattern_retraces(prices: list) dict[source]

Calculate the retraces for the given pattern.

>>> utils.get_pattern_retraces([1.0, 2.0, 3.0])
{'ABC': 1.0}
>>> utils.get_pattern_retraces([1.0, 2.0, 3.0, 4.0])
{'ABC': 1.0, 'BCD': 1.0, 'ABCD': 1.0}
>>> utils.get_pattern_retraces([1.0, 2.0, 3.0, 4.0, 5.0])
{'XAB': 1.0, 'ABC': 1.0, 'BCD': 1.0, 'XABCD': 1.0}
Parameters:

prices – The prices of the pattern.

Returns:

The retraces for the pattern.

pyharmonics.utils.is_pattern_formed(name: str, retrace: float, patterns: dict) bool[source]

Do the pattern retraces reach the required levels for the pattern to be formed?

>>> utils.is_pattern_formed('ABCD', 0.618, {'ABCD': {'ABCD': {'min': 0.618, 'max': 0.786}}})
True
>>> utils.is_pattern_formed('ABCD', 0.786, {'ABCD': {'ABCD': {'min': 0.618, 'max': 0.786}}})
True
>>> utils.is_pattern_formed('ABCD', 0.5, {'ABCD': {'ABCD': {'min': 0.618, 'max': 0.786}}})
False
Params name:

The name of the pattern.

Params retrace:

The retrace of the pattern.

Params patterns:

The patterns to check against.

Returns:

True if the pattern is formed, False otherwise

pyharmonics.utils.line_slope(y2: float, y1: float, x2: int, x1: int) float[source]

Calculate the slope of a line.

>>> utils.line_slope(2, 1, 2, 1)
1.0
>>> utils.line_slope(2, 1, 3, 1)
0.5
Parameters:
  • y2 – The y2 value.

  • y1 – The y1 value.

  • x2 – The x2 value.

  • x1 – The x1 value.

Returns:

The slope of the line.

pyharmonics.utils.match_peaks(indicator_peaks: list, price_peaks: list, index_span: int) list[source]

Match the indicator peaks to the price peaks.

>>> indicator_peaks = [45, 62, 99, 134, 157, 176, 211, 243, 258, 296, 311, 333, 348, 391, 422, 447, 474,
                        524, 540, 581, 617, 635, 647, 664, 719, 737, 766, 786, 817, 862, 910, 932, 970, 998]
>>> price_peaks = [0, 20, 45, 49, 74, 89, 98, 112, 121, 134, 145, 156, 183, 197, 216, 233, 243, 255, 258,
                    278, 296, 301, 310, 314, 359, 377, 391, 423, 427, 456, 470, 478, 507, 519, 521, 537, 543,
                    553, 567, 580, 600, 611, 626, 635, 647, 648, 664, 681, 694, 712, 723, 737, 743, 759, 766,
                    784, 789, 807, 817, 833, 853, 862, 881, 927, 949, 970, 989, 997, 999]
>>> utils.match_peaks(indicator_peaks, price_peaks, 3)
[(45, 45), (99, 98), (134, 134), (157, 156), (243, 243), (258, 258), (296, 296), (311, 310), (391, 391), (422, 423),
 (581, 580), (635, 635), (647, 647), (664, 664), (737, 737), (766, 766), (786, 784), (817, 817), (862, 862), (970, 970), (998, 997)]
Parameters:
  • indicator_peaks – The indicator peaks.

  • price_peaks – The price peaks.

  • index_span – The span to match the peaks.

Returns:

The matched peaks.

Module contents

class pyharmonics.ABCDPattern(symbol, interval, x: tuple, y: tuple, name: str, retraces: dict, formed: bool, bullish: bool)[source]

Bases: ABCPattern

An extension of the ABCPattern class for ABCD patterns.

ABCD patterns are a subset of harmonic patterns that have a 3 swing structure. A price moves in an up or down swing and retraces to a fibonacci level before continuing in the opposite direction.

class pyharmonics.ABCPattern(symbol, interval, x: tuple, y: tuple, name: str, retraces: dict, formed: bool, bullish: bool)[source]

Bases: HarmonicPattern

An extension of the HarmonicPattern class for ABC patterns.

ABC patterns are a subset of harmonic patterns that have a 2 swing structure. A price moves in an up or down swing and retraces to a fibonacci level before continuing in the original direction.

.382, .5, .618, .786, .886, 1.13, 1.141, 1.618 are common retracement levels.

to_dict()[source]

Return a dictionary representation of the pattern.

class pyharmonics.HarmonicPlotter(technicals: OHLCTechnicals, row_map=None, colors=None, plot_ema=False, plot_sma=True)[source]

Bases: PlotterBase

An extension of the PlotterBase class for plotting harmonic patterns.

set_sub_plots()[source]

Set the sub plots for the main plot. The measurements for subplots are optimized purely for pyharmonics.

class pyharmonics.OHLCTechnicals(df, symbol, interval, indicator_config=None, sma_config=None, ema_config=None, peak_spacing=10)[source]

Bases: TechnicalsBase

An extension of TechnicalsBase for OHLC data.

>>> t = OHLCTechnicals(df, symbol, time_frame)
get_peak_x_y(peak_type)[source]

Given the indexs of a pattern ( not a dataframe ) found in this technical data, return the time and prices at those indexes.

>>> t.get_peak_x_y(t.PRICE_PEAKS)
Parameters:

peak_type – str The series containing True or False where True marks a peak on this trend

Returns:

tuple

class pyharmonics.Position(pattern, strike, dollar_amount)[source]

Bases: object

to_dict()[source]

Return the position as a dictionary.

class pyharmonics.PositionPlotter(technicals, position, row_map=None, colors=None, plot_ema=False, plot_sma=True)[source]

Bases: PlotterBase

An extension of the PlotterBase class for plotting positions. Position plotting is more complex than harmonic plotting. Position plotting requires plotting the position outcomes. Position plotting requires plotting the position targets.

pad_right(final_candle, num_candles=120)[source]

Pad the right side of the plot with empty candles. This is useful for plotting the future.

set_sub_plots()[source]

Set the sub plots for the main plot. The measurements for subplots are optimized for trading positions.

class pyharmonics.Technicals(df, symbol, interval, indicator_config=None, sma_config=None, ema_config=None, peak_spacing=10)[source]

Bases: TechnicalsBase

An extension of TechnicalsBase for data that tracks only one trend.

get_peak_x_y(peak_type)[source]

Given the indexs of a pattern ( not a dataframe ) found in this technical data, return the time and prices at those indexes.

>>> t.get_peak_x_y(t.PRICE_PEAKS)
Parameters:

peak_type – str The series containing True or False where True marks a peak on this trend

Returns:

tuple

class pyharmonics.XABCDPattern(symbol, interval, x: tuple, y: tuple, name: str, retraces: dict, formed: bool, bullish: bool)[source]

Bases: ABCPattern

An extension of the ABCPattern class for XABCD patterns.

XABCD patterns are a subset of harmonic patterns that have a 4 swing structure. A price moves in an up or down swing and retraces to a fibonacci level before continuing in the opposite direction.