Technical Indicators

Use a Technical Indicator

To use a technical indicator, the first step is to import the class. The code below will import the simple moving average indicator:

from surmount.technical_indicators import SMA

Next, just pass in the ticker, data dictionary, and any additional parameters required for the calculation. As you can see from the class definition for SMA, the only additional required parameter is length, which is the number of days the moving average will be based on. The following run() function will calculate the SMA of Apple over the past 50 days:

    def run(self, data):
        allocation_dict = {i: 1/len(self.tickers) for i in self.tickers}
        ohlcv = data.get("ohlcv")
        ratios = data.get(("insider_trading","AAPL"))
        apple_sma_10days = SMA("AAPL", ohlcv, 10)
        log(str(apple_sma_10days))
        return TargetAllocation(allocation_dict)

The apple_sma_10days variable will be a list in the following format:

[
    177.024, 
    178.393, 
    179.671, 
    181.377, 
    183.421, 
    185.105, 
...]

Technical Indicator Classes

technical_indicators.RSI(ticker, data, length)

Calculate relative strength index

Parameters:
  • ticker (str) –

    a string ticker

  • data (List[Dict[str, Dict[str, float]]]) –

    data as provided from the OHLCV data function

  • length (int) –

    the window

Returns:
  • List[float]

    list with float RSI

technical_indicators.SMA(ticker, data, length)

Calculate simple moving average

Parameters:
  • ticker (str) –

    a string ticker

  • data (List[Dict[str, Dict[str, float]]]) –

    data as provided from the OHLCV data function

  • length (int) –

    the window

Returns:
  • List[float]

    list with float SMA

technical_indicators.EMA(ticker, data, length)

Calculate exponential moving average

Parameters:
  • ticker (str) –

    a string ticker

  • data (List[Dict[str, Dict[str, float]]]) –

    data as provided from the OHLCV data function

  • length (int) –

    the window

Returns:
  • List[float]

    list with float EMA

technical_indicators.MFI(ticker, data, length)

Calculate money flow index

Parameters:
  • ticker (str) –

    a string ticker

  • data (List[Dict[str, Dict[str, float]]]) –

    data as provided from the OHLCV data function

  • length (int) –

    the window

Returns:
  • List[float]

    list with float MFI

technical_indicators.MACD(ticker, data, fast, slow)

Calculate moving average convergence divergence

Parameters:
  • ticker (str) –

    a string ticker

  • data (List[Dict[str, Dict[str, float]]]) –

    data as provided from the OHLCV data function

  • fast (int) –

    the fast (short) window

  • slow (int) –

    the slow (long) window

Returns:
  • Dict[str, List[float]]

    dict with string key specifying MACD details and value list with float prices

technical_indicators.MACD_F(ticker, data, fast, slow)

Calculate moving average convergence divergence, same data as MACD above

Parameters:
  • ticker (str) –

    a string ticker

  • data (List[Dict[str, Dict[str, float]]]) –

    data as provided from the OHLCV data function

  • fast (int) –

    the fast (short) window

  • slow (int) –

    the slow (long) window

Returns:
  • Dict[str, List[float]]

    dict with string key specifying MACD details and value list with float prices

technical_indicators.BB(ticker, data, length, std=1, band='')

Calculate Bollinger bands

Parameters:
  • ticker (str) –

    a string ticker

  • data (List[Dict[str, Dict[str, float]]]) –

    data as provided from the OHLCV data function

  • length (int) –

    the window

  • std (float, default: 1 ) –

    the standard deviation width of the bollinger bands

  • band (str, default: '' ) –

    an optional string that can be one of "upper", "lower", or "middle", returns all bands if empty

Returns:
  • Dict[str, List[float]]

    dict with string keys of upper, lower, and mid matching a list with float band prices

technical_indicators.Slope(ticker, data, length)

Calculate slope

Parameters:
  • ticker (str) –

    a string ticker

  • data (List[Dict[str, Dict[str, float]]]) –

    data as provided from the OHLCV data function

  • length (int) –

    the window

Returns:
  • List[float]

    list with float SMA

technical_indicators.ADX(ticker, data, length)

Calculate ADX (average directional index)

Parameters:
  • ticker (str) –

    a string ticker

  • data (List[Dict[str, Dict[str, float]]]) –

    data as provided from the OHLCV data function

  • length (int) –

    the window

Returns:
  • List[float]

    list with float ADX

technical_indicators.CCI(ticker, data, length)

Calculate CCI

Parameters:
  • ticker (str) –

    a string ticker

  • data (List[Dict[str, Dict[str, float]]]) –

    data as provided from the OHLCV data function

  • length (int) –

    the window

Returns:
  • List[float]

    list with float CCI

technical_indicators.PPO(ticker, data, fast, slow)

Calculate PPO

Parameters:
  • ticker (str) –

    a string ticker

  • data (List[Dict[str, Dict[str, float]]]) –

    data as provided from the OHLCV data function

  • fast (int) –

    the fast (short) window

  • slow (int) –

    the slow (long) windoww

Returns:
  • List[float]

    list with float PPO

technical_indicators.SO(ticker, data)

Calculate SO

Parameters:
  • ticker (str) –

    a string ticker

  • data (List[Dict[str, Dict[str, float]]]) –

    data as provided from the OHLCV data function

Returns:
  • List[float]

    list with float SOs

technical_indicators.WillR(ticker, data, length)

Calculate Will%R

Parameters:
  • ticker (str) –

    a string ticker

  • data (List[Dict[str, Dict[str, float]]]) –

    data as provided from the OHLCV data function

  • length (int) –

    the window

Returns:
  • List[float]

    list with float Will%R

technical_indicators.STDEV(ticker, data, length)

Calculate STDEV

Parameters:
  • ticker (str) –

    a string ticker

  • data (List[Dict[str, Dict[str, float]]]) –

    data as provided from the OHLCV data function

  • length (int) –

    the window

Returns:
  • List[float]

    list with float STDEV

technical_indicators.VWAP(ticker, data, length)

Calculate VWAP

Parameters:
  • ticker (str) –

    a string ticker

  • data (List[Dict[str, Dict[str, float]]]) –

    data as provided from the OHLCV data function

  • length (int) –

    the window

Returns:
  • List[float]

    list with float VWAP

technical_indicators.Momentum(ticker, data, length)

Calculate Momentum

Parameters:
  • ticker (str) –

    a string ticker

  • data (List[Dict[str, Dict[str, float]]]) –

    data as provided from the OHLCV data function

  • length (int) –

    the window

Returns:
  • List[float]

    list with float Momentum

technical_indicators.PSAR(ticker, data)

Calculate PSAR

Parameters:
  • ticker (str) –

    a string ticker

  • data (List[Dict[str, Dict[str, float]]]) –

    data as provided from the OHLCV data function

Returns:
  • List[float]

    list with float PSAR

technical_indicators.OBV(ticker, data, length=0)

Calculate OBV

Parameters:
  • ticker (str) –

    a string ticker

  • data (List[Dict[str, Dict[str, float]]]) –

    data as provided from the OHLCV data function

  • length (int, default: 0 ) –

    the window

Returns:
  • List[float]

    list with float OBV

technical_indicators.ATR(ticker, data, length)

Calculate ATR

Parameters:
  • ticker (str) –

    a string ticker

  • data (List[Dict[str, Dict[str, float]]]) –

    data as provided from the OHLCV data function

  • length (int) –

    the window

Returns:
  • List[float]

    list with float ATR