> ## Documentation Index
> Fetch the complete documentation index at: https://docs.tqx.ai/llms.txt
> Use this file to discover all available pages before exploring further.

# 因子

> 使用 TQX 公式或 Python 程式碼建立、驗證及分析因子。

因子會將市場或財務資料轉換成數值訊號，供你分析、回測或建立策略。

## 選擇因子模式

### 公式模式

每一行都必須是一個完整表達式。公式模式支援巢狀算子和數學運算。

```text theme={null}
RANK(close / DELAY(close, 20) - 1)
```

欄位名稱使用小寫，算子名稱使用大寫。因子公式不可使用賦值、匯入、Python 屬性或 `FUTURE_RETURNS`。

### Python 模式

當因子需要的邏輯不適合寫成單一表達式時，請使用 Python。定義一個繼承 `Factor` 的類別，並從 `calculate(self, factors)` 回傳 `Series`。

```python theme={null}
class MomentumFactor(Factor):
    def calculate(self, factors):
        close = factors["close"]
        return close.groupby(level="symbol").transform(lambda s: s / s.shift(20) - 1)
```

## 了解更多

* [算子參考](/hk/research/factor/operator-reference)：瀏覽所有支援的公式算子。
* [市場相容性](/hk/research/factor/operator-reference#market-compatibility)：查看欄位和市場限制。

<Warning>
  部分舊版公式參考包含 `FUTURE_RETURNS`，但 TQX 會拒絕在因子中使用它，因為它會讀取未來資料。
</Warning>
