technical – Technical indicators

class pyalgotrade.technical.EventWindow(windowSize, dtype=<type 'float'>, skipNone=True)

Bases: object

An EventWindow class is responsible for making calculation over a moving window of values.

Parameters:
  • windowSize (int.) – The size of the window. Must be greater than 0.
  • dtype (data-type.) – The desired data-type for the array.
  • skipNone (boolean.) – True if None values should not be included in the window.

Note

This is a base class and should not be used directly.

getValue()

Override to calculate a value using the values in the window.

getValues()

Returns a numpy.array with the values in the window.

getWindowSize()

Returns the window size.

class pyalgotrade.technical.EventBasedFilter(dataSeries, eventWindow, maxLen=1024)

Bases: pyalgotrade.dataseries.SequenceDataSeries

An EventBasedFilter class is responsible for capturing new values in a pyalgotrade.dataseries.DataSeries and using an EventWindow to calculate new values.

Parameters:
  • dataSeries (pyalgotrade.dataseries.DataSeries.) – The DataSeries instance being filtered.
  • eventWindow (EventWindow.) – The EventWindow instance to use to calculate new values.
  • maxLen (int.) – The maximum number of values to hold. Once a bounded length is full, when new items are added, a corresponding number of items are discarded from the opposite end.

Example

The following example shows how to combine an EventWindow and an EventBasedFilter to build a custom filter:

from pyalgotrade import dataseries
from pyalgotrade import technical


# An EventWindow is responsible for making calculations using a window of values.
class Accumulator(technical.EventWindow):
    def getValue(self):
        ret = None
        if self.windowFull():
            ret = self.getValues().sum()
        return ret

# Build a sequence based DataSeries.
seqDS = dataseries.SequenceDataSeries()
# Wrap it with a filter that will get fed as new values get added to the underlying DataSeries.
accum = technical.EventBasedFilter(seqDS, Accumulator(3))

# Put in some values.
for i in range(0, 50):
    seqDS.append(i)

# Get some values.
print accum[0]  # Not enough values yet.
print accum[1]  # Not enough values yet.
print accum[2]  # Ok, now we should have at least 3 values.
print accum[3]

# Get the last value, which should be equal to 49 + 48 + 47.
print accum[-1]

The output should be:

None
None
3.0
6.0
144.0

Moving Averages

class pyalgotrade.technical.ma.SMA(dataSeries, period, maxLen=1024)

Bases: pyalgotrade.technical.EventBasedFilter

Simple Moving Average filter.

Parameters:
  • dataSeries (pyalgotrade.dataseries.DataSeries.) – The DataSeries instance being filtered.
  • period (int.) – The number of values to use to calculate the SMA.
  • maxLen (int.) – The maximum number of values to hold. Once a bounded length is full, when new items are added, a corresponding number of items are discarded from the opposite end.
class pyalgotrade.technical.ma.EMA(dataSeries, period, maxLen=1024)

Bases: pyalgotrade.technical.EventBasedFilter

Exponential Moving Average filter.

Parameters:
  • dataSeries (pyalgotrade.dataseries.DataSeries.) – The DataSeries instance being filtered.
  • period (int.) – The number of values to use to calculate the EMA. Must be an integer greater than 1.
  • maxLen (int.) – The maximum number of values to hold. Once a bounded length is full, when new items are added, a corresponding number of items are discarded from the opposite end.
class pyalgotrade.technical.ma.WMA(dataSeries, weights, maxLen=1024)

Bases: pyalgotrade.technical.EventBasedFilter

Weighted Moving Average filter.

Parameters:
  • dataSeries (pyalgotrade.dataseries.DataSeries.) – The DataSeries instance being filtered.
  • weights (list.) – A list of int/float with the weights.
  • maxLen (int.) – The maximum number of values to hold. Once a bounded length is full, when new items are added, a corresponding number of items are discarded from the opposite end.
class pyalgotrade.technical.vwap.VWAP(dataSeries, period, useTypicalPrice=False, maxLen=1024)

Bases: pyalgotrade.technical.EventBasedFilter

Volume Weighted Average Price filter.

Parameters:
  • dataSeries (pyalgotrade.dataseries.bards.BarDataSeries.) – The DataSeries instance being filtered.
  • period (int.) – The number of values to use to calculate the VWAP.
  • useTypicalPrice (boolean.) – True if the typical price should be used instead of the closing price.
  • maxLen (int.) – The maximum number of values to hold. Once a bounded length is full, when new items are added, a corresponding number of items are discarded from the opposite end.

Momentum Indicators

class pyalgotrade.technical.macd.MACD(dataSeries, fastEMA, slowEMA, signalEMA, maxLen=1024)

Bases: pyalgotrade.dataseries.SequenceDataSeries

Moving Average Convergence-Divergence indicator as described in http://stockcharts.com/school/doku.php?id=chart_school:technical_indicators:moving_average_conve.

Parameters:
  • dataSeries (pyalgotrade.dataseries.DataSeries.) – The DataSeries instance being filtered.
  • fastEMA (int.) – The number of values to use to calculate the fast EMA.
  • slowEMA (int.) – The number of values to use to calculate the slow EMA.
  • signalEMA (int.) – The number of values to use to calculate the signal EMA.
  • maxLen (int.) – The maximum number of values to hold. Once a bounded length is full, when new items are added, a corresponding number of items are discarded from the opposite end.
getHistogram()

Returns a pyalgotrade.dataseries.DataSeries with the histogram (the difference between the MACD and the Signal).

getSignal()

Returns a pyalgotrade.dataseries.DataSeries with the EMA over the MACD.

class pyalgotrade.technical.rsi.RSI(dataSeries, period, maxLen=1024)

Bases: pyalgotrade.technical.EventBasedFilter

Relative Strength Index filter as described in http://stockcharts.com/school/doku.php?id=chart_school:technical_indicators:relative_strength_index_rsi.

Parameters:
  • dataSeries (pyalgotrade.dataseries.DataSeries.) – The DataSeries instance being filtered.
  • period (int.) – The period. Note that if period is n, then n+1 values are used. Must be > 1.
  • maxLen (int.) – The maximum number of values to hold. Once a bounded length is full, when new items are added, a corresponding number of items are discarded from the opposite end.
class pyalgotrade.technical.stoch.StochasticOscillator(barDataSeries, period, dSMAPeriod=3, useAdjustedValues=False, maxLen=1024)

Bases: pyalgotrade.technical.EventBasedFilter

Stochastic Oscillator filter as described in http://stockcharts.com/school/doku.php?st=stochastic+oscillator&id=chart_school:technical_indicators:stochastic_oscillator_fast_slow_and_full. Note that the value returned by this filter is %K. To access %D use getD().

Parameters:
  • barDataSeries (pyalgotrade.dataseries.bards.BarDataSeries.) – The BarDataSeries instance being filtered.
  • period (int.) – The period. Must be > 1.
  • dSMAPeriod (int.) – The %D SMA period. Must be > 1.
  • useAdjustedValues (boolean.) – True to use adjusted Low/High/Close values.
  • maxLen (int.) – The maximum number of values to hold. Once a bounded length is full, when new items are added, a corresponding number of items are discarded from the opposite end.
getD()

Returns a pyalgotrade.dataseries.DataSeries with the %D values.

class pyalgotrade.technical.roc.RateOfChange(dataSeries, valuesAgo, maxLen=1024)

Bases: pyalgotrade.technical.EventBasedFilter

Rate of change filter as described in http://stockcharts.com/school/doku.php?id=chart_school:technical_indicators:rate_of_change.

Parameters:
  • dataSeries (pyalgotrade.dataseries.DataSeries.) – The DataSeries instance being filtered.
  • valuesAgo (int.) – The number of values back that a given value will compare to. Must be > 0.
  • maxLen (int.) – The maximum number of values to hold. Once a bounded length is full, when new items are added, a corresponding number of items are discarded from the opposite end.

Other Indicators

class pyalgotrade.technical.atr.ATR(barDataSeries, period, useAdjustedValues=False, maxLen=1024)

Bases: pyalgotrade.technical.EventBasedFilter

Average True Range filter as described in http://stockcharts.com/help/doku.php?id=chart_school:technical_indicators:average_true_range_a.

Parameters:
  • barDataSeries (pyalgotrade.dataseries.bards.BarDataSeries.) – The BarDataSeries instance being filtered.
  • period (int.) – The average period. Must be > 1.
  • useAdjustedValues (boolean.) – True to use adjusted Low/High/Close values.
  • maxLen (int.) – The maximum number of values to hold. Once a bounded length is full, when new items are added, a corresponding number of items are discarded from the opposite end.
class pyalgotrade.technical.linreg.LeastSquaresRegression(dataSeries, windowSize, maxLen=1024)

Bases: pyalgotrade.technical.EventBasedFilter

Calculates values based on a least-squares regression.

Parameters:
  • dataSeries (pyalgotrade.dataseries.DataSeries.) – The DataSeries instance being filtered.
  • windowSize (int.) – The number of values to use to calculate the regression.
  • maxLen (int.) – The maximum number of values to hold. Once a bounded length is full, when new items are added, a corresponding number of items are discarded from the opposite end.
getValueAt(dateTime)

Calculates the value at a given time based on the regression line.

Parameters:dateTime (datetime.datetime.) – The datetime to calculate the value at. Will return None if there are not enough values in the underlying DataSeries.
class pyalgotrade.technical.linreg.Slope(dataSeries, period, maxLen=1024)

Bases: pyalgotrade.technical.EventBasedFilter

The Slope filter calculates the slope of a least-squares regression line.

Parameters:
  • dataSeries (pyalgotrade.dataseries.DataSeries.) – The DataSeries instance being filtered.
  • period (int.) – The number of values to use to calculate the slope.
  • maxLen (int.) – The maximum number of values to hold. Once a bounded length is full, when new items are added, a corresponding number of items are discarded from the opposite end.

Note

This filter ignores the time elapsed between the different values.

class pyalgotrade.technical.highlow.High(dataSeries, period, maxLen=1024)

Bases: pyalgotrade.technical.EventBasedFilter

This filter calculates the highest value.

Parameters:
  • dataSeries (pyalgotrade.dataseries.DataSeries.) – The DataSeries instance being filtered.
  • period (int.) – The number of values to use to calculate the highest value.
  • maxLen (int.) – The maximum number of values to hold. Once a bounded length is full, when new items are added, a corresponding number of items are discarded from the opposite end.
class pyalgotrade.technical.highlow.Low(dataSeries, period, maxLen=1024)

Bases: pyalgotrade.technical.EventBasedFilter

This filter calculates the lowest value.

Parameters:
  • dataSeries (pyalgotrade.dataseries.DataSeries.) – The DataSeries instance being filtered.
  • period (int.) – The number of values to use to calculate the lowest value.
  • maxLen (int.) – The maximum number of values to hold. Once a bounded length is full, when new items are added, a corresponding number of items are discarded from the opposite end.
pyalgotrade.technical.cross.cross_above(values1, values2, start=-2, end=None)

Checks for a cross above conditions over the specified period between two DataSeries objects.

It returns the number of times values1 crossed above values2 during the given period.

Parameters:

Note

The default start and end values check for cross above conditions over the last 2 values.

pyalgotrade.technical.cross.cross_below(values1, values2, start=-2, end=None)

Checks for a cross below conditions over the specified period between two DataSeries objects.

It returns the number of times values1 crossed below values2 during the given period.

Parameters:

Note

The default start and end values check for cross below conditions over the last 2 values.

class pyalgotrade.technical.linebreak.Line(low, high, dateTime, white)

Bases: object

A line in a line break chart.

getDateTime()

The datetime.

getHigh()

The high value.

getLow()

The low value.

isBlack()

True if the line is black (falling prices).

isWhite()

True if the line is white (rising prices).

class pyalgotrade.technical.linebreak.LineBreak(barDataSeries, reversalLines, useAdjustedValues=False, maxLen=1024)

Bases: pyalgotrade.dataseries.SequenceDataSeries

Line Break filter as described in http://stockcharts.com/help/doku.php?id=chart_school:chart_analysis:three_line_break . This is a DataSeries of Line instances.

Parameters:
  • barDataSeries (pyalgotrade.dataseries.bards.BarDataSeries.) – The DataSeries instance being filtered.
  • reversalLines (int.) – The number of lines back to check to calculate a reversal. Must be greater than 1.
  • useAdjustedValues (boolean.) – True to use adjusted high/low/close values.
  • maxLen (int.) – The maximum number of values to hold. Once a bounded length is full, when new items are added, a corresponding number of items are discarded from the opposite end. This value can’t be smaller than reversalLines.
class pyalgotrade.technical.stats.StdDev(dataSeries, period, ddof=0, maxLen=1024)

Bases: pyalgotrade.technical.EventBasedFilter

Standard deviation filter.

Parameters:
  • dataSeries (pyalgotrade.dataseries.DataSeries.) – The DataSeries instance being filtered.
  • period (int.) – The number of values to use to calculate the Standard deviation.
  • ddof (int.) – Delta degrees of freedom.
  • maxLen (int.) – The maximum number of values to hold. Once a bounded length is full, when new items are added, a corresponding number of items are discarded from the opposite end.
class pyalgotrade.technical.stats.ZScore(dataSeries, period, ddof=0, maxLen=1024)

Bases: pyalgotrade.technical.EventBasedFilter

Z-Score filter.

Parameters:
  • dataSeries (pyalgotrade.dataseries.DataSeries.) – The DataSeries instance being filtered.
  • period (int.) – The number of values to use to calculate the Z-Score.
  • ddof (int.) – Delta degrees of freedom to use for the standard deviation.
  • maxLen (int.) – The maximum number of values to hold. Once a bounded length is full, when new items are added, a corresponding number of items are discarded from the opposite end.
class pyalgotrade.technical.bollinger.BollingerBands(dataSeries, period, numStdDev, maxLen=1024)

Bases: object

Bollinger Bands filter as described in http://stockcharts.com/school/doku.php?id=chart_school:technical_indicators:bollinger_bands.

Parameters:
  • dataSeries (pyalgotrade.dataseries.DataSeries.) – The DataSeries instance being filtered.
  • period (int.) – The number of values to use in the calculation. Must be > 1.
  • numStdDev (int.) – The number of standard deviations to use for the upper and lower bands.
  • maxLen (int.) – The maximum number of values to hold. Once a bounded length is full, when new items are added, a corresponding number of items are discarded from the opposite end.
getLowerBand()

Returns the lower band as a pyalgotrade.dataseries.DataSeries.

getMiddleBand()

Returns the middle band as a pyalgotrade.dataseries.DataSeries.

getUpperBand()

Returns the upper band as a pyalgotrade.dataseries.DataSeries.

class pyalgotrade.technical.cumret.CumulativeReturn(dataSeries, maxLen=1024)

Bases: pyalgotrade.technical.EventBasedFilter

This filter calculates cumulative returns over another dataseries.

Parameters:
  • dataSeries (pyalgotrade.dataseries.DataSeries.) – The DataSeries instance being filtered.
  • maxLen (int.) – The maximum number of values to hold. Once a bounded length is full, when new items are added, a corresponding number of items are discarded from the opposite end.

Table Of Contents

Previous topic

barfeed – Bar providers

Next topic

broker – Order management classes

This Page