Claude
Skills
Sign in
Back

technical-indicators

Included with Lifetime
$97 forever

Technical analysis with TA-Lib - Moving averages, RSI, MACD, Bollinger Bands, Stochastic, ATR, candlestick patterns, and strategy signals using OpenAlgo market data

Generalscripts

What this skill does


# OpenAlgo Technical Indicators

Perform technical analysis using TA-Lib with OpenAlgo market data. Build trading strategies based on indicators, generate signals, and backtest ideas.

## Environment Setup

```bash
# Install TA-Lib (requires system library)
# macOS
brew install ta-lib
pip install TA-Lib

# Ubuntu/Debian
sudo apt-get install libta-lib-dev
pip install TA-Lib

# Windows
# Download from https://www.lfd.uci.edu/~gohlke/pythonlibs/#ta-lib
pip install TA_Lib‑0.4.28‑cp311‑cp311‑win_amd64.whl
```

```python
from openalgo import api
import talib
import pandas as pd
import numpy as np

client = api(
    api_key='your_api_key_here',
    host='http://127.0.0.1:5000'
)
```

## Quick Start Scripts

### Calculate Indicators
```bash
python scripts/indicators.py --symbol SBIN --exchange NSE --interval 5m --days 5
```

### Generate Signals
```bash
python scripts/signals.py --symbol NIFTY --exchange NSE_INDEX --strategy rsi_oversold
```

### Scan for Patterns
```bash
python scripts/scanner.py --symbols RELIANCE,TCS,INFY,SBIN --exchange NSE --pattern bullish
```

---

## Fetching Data for Analysis

```python
from openalgo import api
import pandas as pd

client = api(api_key='your_key', host='http://127.0.0.1:5000')

# Fetch historical data
df = client.history(
    symbol="SBIN",
    exchange="NSE",
    interval="5m",
    start_date="2025-01-01",
    end_date="2025-01-15"
)

# TA-Lib requires numpy arrays
open_prices = df['open'].values
high_prices = df['high'].values
low_prices = df['low'].values
close_prices = df['close'].values
volume = df['volume'].values
```

---

## Overlap Studies (Trend Indicators)

### Simple Moving Average (SMA)

```python
import talib

# Calculate SMA
sma_20 = talib.SMA(close_prices, timeperiod=20)
sma_50 = talib.SMA(close_prices, timeperiod=50)
sma_200 = talib.SMA(close_prices, timeperiod=200)

# Add to DataFrame
df['SMA_20'] = sma_20
df['SMA_50'] = sma_50
df['SMA_200'] = sma_200

# Crossover signal
df['SMA_Cross'] = np.where(df['SMA_20'] > df['SMA_50'], 1, -1)
```

### Exponential Moving Average (EMA)

```python
ema_9 = talib.EMA(close_prices, timeperiod=9)
ema_21 = talib.EMA(close_prices, timeperiod=21)

df['EMA_9'] = ema_9
df['EMA_21'] = ema_21

# EMA crossover
df['EMA_Signal'] = np.where(df['EMA_9'] > df['EMA_21'], 'BUY', 'SELL')
```

### Bollinger Bands

```python
upper, middle, lower = talib.BBANDS(
    close_prices,
    timeperiod=20,
    nbdevup=2,
    nbdevdn=2,
    matype=0  # SMA
)

df['BB_Upper'] = upper
df['BB_Middle'] = middle
df['BB_Lower'] = lower
df['BB_Width'] = (upper - lower) / middle * 100  # Bandwidth %

# Bollinger Band signals
df['BB_Signal'] = np.where(close_prices < lower, 'BUY',
                   np.where(close_prices > upper, 'SELL', 'HOLD'))
```

### SuperTrend (Custom Implementation)

```python
def supertrend(df, period=10, multiplier=3):
    """Calculate SuperTrend indicator."""
    hl2 = (df['high'] + df['low']) / 2
    atr = talib.ATR(df['high'].values, df['low'].values, df['close'].values, timeperiod=period)

    upperband = hl2 + (multiplier * atr)
    lowerband = hl2 - (multiplier * atr)

    supertrend = pd.Series(index=df.index, dtype=float)
    direction = pd.Series(index=df.index, dtype=int)

    for i in range(period, len(df)):
        if df['close'].iloc[i] > upperband[i-1]:
            supertrend.iloc[i] = lowerband[i]
            direction.iloc[i] = 1  # Bullish
        elif df['close'].iloc[i] < lowerband[i-1]:
            supertrend.iloc[i] = upperband[i]
            direction.iloc[i] = -1  # Bearish
        else:
            supertrend.iloc[i] = supertrend.iloc[i-1]
            direction.iloc[i] = direction.iloc[i-1]

    return supertrend, direction

df['SuperTrend'], df['ST_Direction'] = supertrend(df)
```

---

## Momentum Indicators

### Relative Strength Index (RSI)

```python
rsi = talib.RSI(close_prices, timeperiod=14)
df['RSI'] = rsi

# RSI signals
df['RSI_Signal'] = np.where(df['RSI'] < 30, 'OVERSOLD',
                    np.where(df['RSI'] > 70, 'OVERBOUGHT', 'NEUTRAL'))

# RSI divergence detection
def detect_rsi_divergence(df, lookback=14):
    """Detect bullish/bearish RSI divergence."""
    price_low = df['close'].rolling(lookback).min()
    price_high = df['close'].rolling(lookback).max()
    rsi_low = df['RSI'].rolling(lookback).min()
    rsi_high = df['RSI'].rolling(lookback).max()

    # Bullish divergence: price makes lower low, RSI makes higher low
    bullish = (df['close'] == price_low) & (df['RSI'] > rsi_low.shift(lookback))

    # Bearish divergence: price makes higher high, RSI makes lower high
    bearish = (df['close'] == price_high) & (df['RSI'] < rsi_high.shift(lookback))

    return bullish, bearish

df['Bullish_Div'], df['Bearish_Div'] = detect_rsi_divergence(df)
```

### MACD (Moving Average Convergence Divergence)

```python
macd, signal, hist = talib.MACD(
    close_prices,
    fastperiod=12,
    slowperiod=26,
    signalperiod=9
)

df['MACD'] = macd
df['MACD_Signal'] = signal
df['MACD_Hist'] = hist

# MACD crossover signals
df['MACD_Cross'] = np.where(
    (df['MACD'] > df['MACD_Signal']) & (df['MACD'].shift(1) <= df['MACD_Signal'].shift(1)),
    'BUY',
    np.where(
        (df['MACD'] < df['MACD_Signal']) & (df['MACD'].shift(1) >= df['MACD_Signal'].shift(1)),
        'SELL',
        'HOLD'
    )
)
```

### Stochastic Oscillator

```python
slowk, slowd = talib.STOCH(
    high_prices,
    low_prices,
    close_prices,
    fastk_period=14,
    slowk_period=3,
    slowk_matype=0,
    slowd_period=3,
    slowd_matype=0
)

df['Stoch_K'] = slowk
df['Stoch_D'] = slowd

# Stochastic signals
df['Stoch_Signal'] = np.where(
    (df['Stoch_K'] < 20) & (df['Stoch_K'] > df['Stoch_D']),
    'BUY',
    np.where(
        (df['Stoch_K'] > 80) & (df['Stoch_K'] < df['Stoch_D']),
        'SELL',
        'HOLD'
    )
)
```

### ADX (Average Directional Index)

```python
adx = talib.ADX(high_prices, low_prices, close_prices, timeperiod=14)
plus_di = talib.PLUS_DI(high_prices, low_prices, close_prices, timeperiod=14)
minus_di = talib.MINUS_DI(high_prices, low_prices, close_prices, timeperiod=14)

df['ADX'] = adx
df['Plus_DI'] = plus_di
df['Minus_DI'] = minus_di

# Trend strength
df['Trend_Strength'] = np.where(df['ADX'] > 25, 'STRONG',
                        np.where(df['ADX'] > 20, 'MODERATE', 'WEAK'))

# DI crossover with trend filter
df['ADX_Signal'] = np.where(
    (df['Plus_DI'] > df['Minus_DI']) & (df['ADX'] > 25),
    'STRONG_BUY',
    np.where(
        (df['Minus_DI'] > df['Plus_DI']) & (df['ADX'] > 25),
        'STRONG_SELL',
        'NEUTRAL'
    )
)
```

### Williams %R

```python
willr = talib.WILLR(high_prices, low_prices, close_prices, timeperiod=14)
df['Williams_R'] = willr

df['WillR_Signal'] = np.where(df['Williams_R'] < -80, 'OVERSOLD',
                     np.where(df['Williams_R'] > -20, 'OVERBOUGHT', 'NEUTRAL'))
```

### CCI (Commodity Channel Index)

```python
cci = talib.CCI(high_prices, low_prices, close_prices, timeperiod=20)
df['CCI'] = cci

df['CCI_Signal'] = np.where(df['CCI'] < -100, 'OVERSOLD',
                   np.where(df['CCI'] > 100, 'OVERBOUGHT', 'NEUTRAL'))
```

---

## Volatility Indicators

### ATR (Average True Range)

```python
atr = talib.ATR(high_prices, low_prices, close_prices, timeperiod=14)
df['ATR'] = atr
df['ATR_Percent'] = (atr / close_prices) * 100

# Volatility classification
df['Volatility'] = np.where(df['ATR_Percent'] > 2, 'HIGH',
                   np.where(df['ATR_Percent'] > 1, 'MEDIUM', 'LOW'))

# ATR-based stop loss
df['Stop_Loss'] = df['close'] - (2 * df['ATR'])  # 2x ATR trailing stop
```

### Keltner Channel

```python
def keltner_channel(df, ema_period=20, atr_period=10, multiplier=2):
    """Calculate Keltner Channel."""
    ema = talib.EMA(df['close'].values, timeperiod=ema_period)
    atr = talib.ATR(df['high'].values, df['low'].values, df['close'].values, timeperiod=atr_period)

    upper = ema + (multiplier * atr)
    lower = ema - (multiplier * atr)

    return upper, ema, lower

df['KC_Upper'], 

Related in General