Skip to content

BUG: MACD with signalperiod = 1 - data leak #725

Closed

Description

@Egisess

Hello everyone, I've encountered dangerous behavior of library while calculating MACD with signalperiod = 1:

How to reproduce:

np.random.seed(42)
test_data = np.random.randn(2000) * 100 + 1000 
fastperiod = 2 
slowperiod = 32
signalperiod = 1

macd_1, macdsignal_1, macdhist_1 = ta.MACD(
    real=np.append(test_data[:1500],-999999999), fastperiod=fastperiod, slowperiod=slowperiod, signalperiod=signalperiod
)
macd_2, macdsignal_2, macdhist_2 = ta.MACD(
    real=test_data[:1501], fastperiod=fastperiod, slowperiod=slowperiod, signalperiod=signalperiod
)
print(macdsignal_2[-3] - macdsignal_1[-3]) # Equals 606061258.6885501

As I am checking [-3] index in the array - it shouldn't know about the upcoming value in the end, but the calculated delta shows that TA-Lib takes this value into account.

Also I checked the correct parameters:

np.random.seed(42)
test_data = np.random.randn(2000) * 100 + 1000 
fastperiod = 2 
slowperiod = 32
signalperiod = 2

macd_1, macdsignal_1, macdhist_1 = ta.MACD(
    real=np.append(test_data[:1500],-999999999), fastperiod=fastperiod, slowperiod=slowperiod, signalperiod=signalperiod
)
macd_2, macdsignal_2, macdhist_2 = ta.MACD(
    real=test_data[:1501], fastperiod=fastperiod, slowperiod=slowperiod, signalperiod=signalperiod
)
print(macdsignal_2[-3] - macdsignal_1[-3]) # Equals 0.0

I think it is needed either to forbid signalperiod = 1 or to manually fill the macdhist array with zeros in this edge case.

Activity

  1. mrjbq7 commented on Jun 21, 2025

    @mrjbq7
    Member

    Yes, that's true.

    Duplicate of #335.

    I had reported it upstream (TA-Lib/ta-lib#48) but I think it hasn't been addressed yet.

  2. mario4tier commented on Jul 3, 2026

    @mario4tier
    Member

    This is now fixed in release 0.7.1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions