MyTT Documentation

repository·main·Indexed 25 days ago

https://github.com/mpquant/mytt

A high-performance Python library providing implementations of common stock market indicators (such as MACD, RSI, BOLL, ATR, KDJ, CCI, and PSY) optimized for numpy and pandas. It ports technical indicators and syntax from Chinese trading platforms including TongdaXun, Flush, and WenHua MyLanguage for use in technical analysis, automated stock trading, and cryptocurrency quantitative trading.

Tokens
988
Snippets
1
Records
6
Agent score
34%

What's inside MyTT

  1. Overview of MyTT

    main
    MyTT is a lightweight, high-performance Python library designed to port technical indicators from platforms like TongdaXun (通达信), Flush (同花顺), and WenHua MyLanguage (文华麦语言) into Python. It is a 'Swiss Army knife' for quantitative toolboxes, providing a concise implementation of common indicators such as MACD, RSI, BOLL, ATR, KDJ, CCI, and PSY. The core library is built on top of numpy and pandas for efficiency and is suitable for technical analysis, automated stock trading, and cryptocurrency (BTC) quantitative trading.
  2. Install MyTT

    main

    You can install MyTT using standard pip or by manually including the source file in your project.

    Option 1: Manual inclusion (Recommended for lightweight use) Copy MyTT.py directly into your project directory and use from MyTT import * to access all functions.

    Option 2: Standard installation

    pip install MyTT
  3. Translate TongdaXun logic to Python syntax

    main

    When translating TongdaXun formulas to Python using MyTT, note the following syntax changes:

    1. Assignment: Use = instead of :=.
    2. Logical AND: Use & instead of AND.
    3. Logical OR: Use | instead of OR.

    Examples:

    • TongdaXun: VAR1:=(C>REF(C,1) AND C>REF(C,2)); Python: VAR1 = ((CLOSE > REF(CLOSE, 1)) & (CLOSE > REF(CLOSE, 2)));

    • TongdaXun: 收盘价在10日均线上 且10日均线在20日均线上 Python: (CLOSE > MA(CLOSE, 10)) & (MA(CLOSE, 10) > MA(CLOSE, 20))

    • TongdaXun: 收阳线 或 收盘价大于昨收 Python: (CLOSE > OPEN) | (CLOSE > REF(CLOSE, 1))

  4. Use MyTT for technical indicator calculation

    main

    MyTT is a pure Python implementation of technical indicators compatible with TongdaXun and Flush (同花顺) syntax. It uses numpy and pandas for high performance and does not require ta-lib.

    Most functions accept sequences (like pandas Series or numpy arrays) and return sequences. You can pass df.close.values or even a list to the functions.

  5. Implement complex indicators with MyTT

    main

    You can define complex indicators by combining MyTT utility functions. Below are common implementations:

    def MACD(CLOSE, SHORT=12, LONG=26, M=9):
        DIF = EMA(CLOSE, SHORT) - EMA(CLOSE, LONG)
        DEA = EMA(DIF, M)
        MACD = (DIF - DEA) * 2
        return DIF, DEA, MACD
    
    def KDJ(CLOSE, HIGH, LOW, N=9, M1=3, M2=3):
        RSV = (CLOSE - LLV(LOW, N)) / (HHV(HIGH, N) - LLV(LOW, N)) * 100
        K = EMA(RSV, (M1 * 2 - 1))
        D = EMA(K, (M2 * 2 - 1))
        J = K * 3 - D * 2
        return K, D, J
    
    def BOLL(CLOSE, N=20, P=2):
        MID = MA(CLOSE, N)
        UPPER = MID + STD(CLOSE, N) * P
        LOWER = MID - STD(CLOSE, N) * P
        return UPPER, MID, LOWER
  6. Reference MyTT core utility functions

    main

    The following core functions are used to build complex indicators and logic:

    FunctionDescription
    REF(X, N)Returns the value of X from N periods ago.
    MA(X, N)Simple Moving Average of X over N periods.
    EMA(X, N)Exponential Moving Average of X over N periods.
    SMA(X, N)Chinese-style SMA (Weighted Moving Average).
    STD(X, N)Standard deviation of X over N periods.
    AVEDEV(X, N)Average absolute deviation of X over N periods.
    CROSS(A, B)Returns true if A crosses above B.
    MAX(A, B)Returns the maximum of A and B.
    MIN(A, B)Returns the minimum of A and B.
    COUNT(Cond, N)Number of days in the last N periods where Cond is true.
    EVERY(Cond, N)True if Cond has been true for all of the last N periods.
    LAST(Cond, A, B)True if Cond was true from A days ago to B days ago.
    EXIST(Cond, N)True if Cond was true at least once in the last N periods.
    BARSLAST(Cond)Number of periods since Cond was last true.
    SLOPE(X, N)Linear regression slope of X over N periods.
    FORCAST(X, N)Linear regression forecast value for X over N periods.
    HHV(X, N)Highest value of X in the last N periods.
    LLV(X, N)Lowest value of X in the last N periods.
    IF(Cond, A, B)If Cond is true, return A, else return B.