[Python] Breakout trading strategy along with various technical indicators error

Discussão em 'Python' iniciado por Stack, Agosto 22, 2025.

  1. Stack

    Stack Membro Participativo

    Breakout Trading Strategy to say whether there has been a breakout and should we invest our money. I am writing a code for the same.

    My code:

    import pandas as pd
    import pandas_ta as ta
    import math
    import matplotlib.pyplot as plt

    # Parameters
    length = 14
    k = 1.0
    method = 'Atr'
    show = False

    # Data
    # Replace [...] with your price and volume data
    data = pd.read_csv('data.csv')
    close = data['close']
    high = data['high']
    low = data['low']
    src = close

    # Calculate pivot highs and lows

    def find_pivot_highs(data, length):
    pivot_highs = []
    for i in range(length, len(data) - length):
    if (
    data > max(data[i - length : i])
    and data > max(data[i + 1 : i + length + 1])
    ):
    pivot_highs.append(i)
    return pivot_highs


    def find_pivot_lows(data, length):
    pivot_lows = []
    for i in range(length, len(data) - length):
    if (
    data < min(data[i - length : i])
    and data < min(data[i + 1 : i + length + 1])
    ):
    pivot_lows.append(i)
    return pivot_lows

    ph = find_pivot_highs(high, length)
    pl = find_pivot_lows(low, length)

    # Calculate slope
    def calculate_slope():
    if method == 'Atr':
    return ta.atr(high, low, close, length) / length * k
    elif method == 'Stdev':
    return ta.STDDEV(src, length) / length * k
    elif method == 'Linreg':
    sma1 = ta.SMA(src * n, length)
    sma2 = ta.SMA(src, length)
    return math.fabs(sma1 - sma2 * ta.SMA(n, length)) / ta.VAR(n, length) / 2 * k


    slope = calculate_slope()
    slope_ph = [0] * len(close)
    slope_pl = [0] * len(close)
    slope_ph = [slope if i in ph else slope_ph[i-1] for i in range(len(close))]
    slope_pl = [slope if i in pl else slope_pl[i-1] for i in range(len(close))]

    upper = [0] * len(close)
    lower = [0] * len(close)
    upper = [src if i in ph else upper[i-1] - slope_ph for i in range(len(src))]
    lower = [src if i in pl else lower[i-1] + slope_pl for i in range(len(src))]

    # ----
    single_upper = [0] * len(src)
    single_lower = [0] * len(src)

    for i in range(length, len(close)):
    if close > upper[i-length]:
    single_upper = 1
    elif i in ph:
    single_upper = single_upper[i-1]

    if close < lower[i-length]:
    single_lower = 1
    elif i in pl:
    single_lower = single_lower[i-1]

    upper_breakout= [False] * len(src)
    for i in range(len(src)):
    if src > upper:
    upper_breakout = True
    else:
    upper_breakout = False

    lower_breakout= [False] * len(src)
    for i in range(len(src)):
    if src[i] < lower[i]:
    lower_breakout[i] = True
    else:
    lower_breakout[i] = False

    # Trading strategy
    trades = []
    trade_type = None
    entry_price = None
    stop_loss = None
    take_profit = None

    for i in range(len(close)):
    if trade_type is None:
    if upper_breakout[i]:
    trade_type = 'Long'
    entry_price = close[i]
    stop_loss = entry_price - 0.02 * entry_price # Example stop loss calculation
    take_profit = entry_price + 0.03 * entry_price # Example take profit calculation
    elif lower_breakout[i]:
    trade_type = 'Short'
    entry_price = close[i]
    stop_loss = entry_price + 0.02 * entry_price # Example stop loss calculation
    take_profit = entry_price - 0.03 * entry_price # Example take profit calculation
    else:
    if trade_type == 'Long':
    if close[i] <= stop_loss or close[i] >= take_profit:
    trades.append((entry_price, stop_loss, take_profit))
    trade_type = None
    entry_price = None
    stop_loss = None
    take_profit = None
    elif trade_type == 'Short':
    if close[i] >= stop_loss or close[i] <= take_profit:
    trades.append((entry_price, stop_loss, take_profit))
    trade_type = None
    entry_price = None
    stop_loss = None
    take_profit = None

    # Calculate metrics
    total_trades = len(trades)
    positive_trades = sum(1 for t in trades if t[2] > t[0])
    win_rate = positive_trades / total_trades if total_trades > 0 else 0

    cumulative_returns = sum((t[2] - t[0]) / t[0] for t in trades)
    sharpe_ratio = (cumulative_returns - 0.01) / (cumulative_returns.std() + 1e-9)
    sortino_ratio = (cumulative_returns - 0.01) / (cumulative_returns[cumulative_returns < 0].std() + 1e-9)
    profit_factor = sum(t[2] - t[0] for t in trades if t[2] > t[0]) / abs(sum(t[2] - t[0] for t in trades if t[2] < t[0]))

    # Print metrics
    print(f"Total Trades: {total_trades}")
    print(f"Positive Trades: {positive_trades}")
    print(f"Win Rate: {win_rate * 100}%")
    print(f"Cumulative Returns: {cumulative_returns * 100}%")
    print(f"Sharpe Ratio: {sharpe_ratio}")
    print(f"Sortino Ratio: {sortino_ratio}")
    print(f"Profit Factor: {profit_factor}")

    # Plotting
    plt.plot(close[length:], label='Closing Price')
    plt.plot(upper[length:], label='Upper Trendline', color='#26a69a')
    plt.plot(lower[length:], label='Lower Trendline', color='#ef5350')

    for i in range(len(close)):
    if upper_breakout[i]:
    plt.scatter(i, close[i-length], marker='^', color='r', label='Upper Break')
    if lower_breakout[i]:
    plt.scatter(i, close[i-length], marker='v', color='g', label='Lower Break')

    plt.legend()
    plt.show()



    Here if the lower_breakout is True, one must short and vice versa for upper_breakout.

    While running the code: I'm getting an error saying:

    /Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/numpy/core/_methods.py:265: RuntimeWarning: Degrees of freedom <= 0 for slice
    ret = _var(a, axis=axis, dtype=dtype, out=out, ddof=ddof,
    /Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/numpy/core/_methods.py:223: RuntimeWarning: invalid value encountered in divide
    arrmean = um.true_divide(arrmean, div, out=arrmean, casting='unsafe',
    /Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/numpy/core/_methods.py:257: RuntimeWarning: invalid value encountered in double_scalars
    ret = ret.dtype.type(ret / rcount)
    /Users/dev.barbhaya9gmail.com/Library/CloudStorage/OneDrive-IITKanpur/Beyond/BRAINWorks/Internship/Project #304/strategy (ta-lib).py:141: RuntimeWarning: divide by zero encountered in double_scalars
    profit_factor = sum(t[2] - t[0] for t in trades if t[2] > t[0]) / abs(sum(t[2] - t[0] for t in trades if t[2] < t[0]))
    Total Trades: 82
    Positive Trades: 82
    Win Rate: 100.0%
    Cumulative Returns: 246.0%
    Sharpe Ratio: 2450000000.0
    Sortino Ratio: nan
    Profit Factor: inf
    /Users/dev.barbhaya9gmail.com/Library/CloudStorage/OneDrive-IITKanpur/Beyond/BRAINWorks/Internship/Project #304/strategy (ta-lib).py:153: MatplotlibDeprecationWarning: The resize_event function was deprecated in Matplotlib 3.6 and will be removed two minor releases later. Use callbacks.process('resize_event', ResizeEvent(...)) instead.
    plt.plot(close[length:], label='Closing Price')
    Traceback (most recent call last):
    File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/pandas/core/indexes/range.py", line 391, in get_loc
    return self._range.index(new_key)
    ValueError: -14 is not in range

    The above exception was the direct cause of the following exception:

    Traceback (most recent call last):
    File "/Users/dev.barbhaya9gmail.com/Library/CloudStorage/OneDrive-IITKanpur/Beyond/BRAINWorks/Internship/Project #304/strategy (ta-lib).py", line 159, in <module>
    plt.scatter(i, close[i-length], marker='^', color='r', label='Upper Break')
    File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/pandas/core/series.py", line 981, in __getitem__
    return self._get_value(key)
    File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/pandas/core/series.py", line 1089, in _get_value
    loc = self.index.get_loc(label)
    File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/pandas/core/indexes/range.py", line 393, in get_loc
    raise KeyError(key) from err
    KeyError: -14


    I'm trying to understand if the code has any errors, but I can't find any issues.

    I'm very new to python and all your help will be truly appreciated.

    [url="https://stackoverflow.com/questions/76333197/breakout-trading-strategy-along-with-various-technical-indicators-error"]Continue reading...[/url][/i][/i][/i][/i][/i][/i][/i][/i][/i][/i][/i][/i][/i][/i]

Compartilhe esta Página