kbar


Namekbar JSON
Version 0.1.2 PyPI version JSON
download
home_pageNone
SummaryA lightweight K-line (candlestick) plotting tool with matplotlib and mplfinance.
upload_time2025-08-22 02:50:34
maintainerNone
docs_urlNone
authorTony Y.H. Huang
requires_python>=3.9
license MIT License Copyright (c) [2025] [Tony Y.H.Huang] Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
keywords finance candlestick mplfinance k-line stock
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # 📈 kbar

`kbar` 提供一個簡單的 `KBar` 類別,專門用來繪製金融 K 線圖(candlestick chart)。  
它是 [`mplfinance`](https://github.com/matplotlib/mplfinance) 的輕量級封裝,並額外支援:  

- 🔹 內建成交量副圖(volume)  
- 🔹 自訂技術指標副圖(例如 RSI、MACD)  
- 🔹 自訂疊圖(例如 SMA、均線)  
- 🔹 自動設定中文字型(避免亂碼)  

---

## 🚀 安裝
```bash
pip install kbar
```

---

## 🛠 匯入
```python
from kbar import KBar
# 或
import kbar
```

---

## 📊 使用教學

### 1️⃣ 準備股票價量資料(以 yfinance 為例)
```python
import yfinance as yf
df = yf.download('0050.TW', start='2024-07-01', end='2024-08-21', auto_adjust=False)

# 轉換為單層欄位名稱(避免多層索引)
df.columns = df.columns.map(lambda x: x[0])
```

---

### 2️⃣ 繪製簡單的 K 線圖
```python
kb = KBar(df)
# 或 kb = kbar.KBar(df) 若使用 import kbar
kb.plot()
```

---

### 3️⃣ 繪製含成交量的 K 線圖
```python
kb = KBar(df)
kb.plot(volume=True)  # 成交量副圖會顯示在 panel=1
```

---

### 4️⃣ 添加自訂副圖(例:RSI 指標)
```python
from talib.abstract import RSI

# Ta-Lib 需要欄位小寫
df.columns = [c.lower() for c in df.columns]

rsi = RSI(df)

kb = KBar(df)
kb.addplot(rsi, panel=2, ylabel='RSI')  # RSI 畫在 panel=2
kb.plot(volume=True)
```

---

### 5️⃣ 繪製內建均線(mav)
```python
kb = KBar(df)
kb.plot(mav=5)        # 繪製 5 日均線
# kb.plot(mav=[3,5,7]) # 繪製多條均線
```

---

### 6️⃣ 添加自訂疊圖(例:SMA 均線)
```python
from talib.abstract import SMA

df.columns = [c.lower() for c in df.columns]

sma3 = SMA(df['close'], timeperiod=3)
sma5 = SMA(df['close'], timeperiod=5)
sma7 = SMA(df['close'], timeperiod=7)

kb = KBar(df)
kb.addplot(sma3, panel=0, color='blue', width=1)
kb.addplot(sma5, panel=0, color='orange', width=1)
kb.addplot(sma7, panel=0, color='green', width=1)

kb.plot(volume=True)
```

---

## 📚 API 文件

### `class KBar`

封裝 `mplfinance`,提供簡化的 K 線圖繪製與副圖管理。

#### 🔹 建構子
```python
KBar(df)
```

- **df** (`pandas.DataFrame`):必須包含以下欄位:  
  - `Open`: 開盤價  
  - `High`: 最高價  
  - `Low`: 最低價  
  - `Close`: 收盤價  
  - `Volume`(可選):成交量  

---

#### 🔹 方法:`addplot(data, **kwargs)`
添加自訂副圖或疊圖。  

- **data** (`Series` 或 `ndarray`):要繪製的資料  
- **kwargs**:傳遞給 `mplfinance.make_addplot()` 的參數,例如:  
  - `color`:線條顏色  
  - `width`:線條寬度  
  - `panel`:放置的圖表區(0=主圖,1~9=副圖)  
  - `ylabel`:副圖標籤  

---

#### 🔹 方法:`plot(**kwargs)`
繪製 K 線圖,可同時顯示副圖與疊圖。  

- **kwargs**:傳遞給 `mplfinance.plot()` 的參數,例如:  
  - `volume`:是否顯示成交量副圖  
  - `mav`:均線(整數或整數清單)  
  - `returnfig`:若 `True`,回傳 `(fig, axes)`  

---

## 📘 範例
```python
import yfinance as yf
from kbar import KBar

df = yf.download("0050.TW", start="2024-08-20", end="2025-01-20")
df.columns = df.columns.map(lambda x: x[0])

kb = KBar(df)
kb.addplot(df['Close'].rolling(5).mean(), color='blue', width=1)
kb.plot(volume=True, mav=5)
```

---

## 📦 依賴套件
- `mplfinance`
- `matplotlib`
- `pandas`
- `numpy < 2`
- `pyarrow`(pandas 內部需要)

---

## 📝 授權
MIT License

            

Raw data

            {
    "_id": null,
    "home_page": null,
    "name": "kbar",
    "maintainer": null,
    "docs_url": null,
    "requires_python": ">=3.9",
    "maintainer_email": null,
    "keywords": "finance, candlestick, mplfinance, k-line, stock",
    "author": "Tony Y.H. Huang",
    "author_email": null,
    "download_url": "https://files.pythonhosted.org/packages/13/c3/c0ddf471959990ddae14da6eacd7ca07e897554fbe4ad3fb2380fe59de93/kbar-0.1.2.tar.gz",
    "platform": null,
    "description": "# \ud83d\udcc8 kbar\r\n\r\n`kbar` \u63d0\u4f9b\u4e00\u500b\u7c21\u55ae\u7684 `KBar` \u985e\u5225\uff0c\u5c08\u9580\u7528\u4f86\u7e6a\u88fd\u91d1\u878d K \u7dda\u5716\uff08candlestick chart\uff09\u3002  \r\n\u5b83\u662f [`mplfinance`](https://github.com/matplotlib/mplfinance) \u7684\u8f15\u91cf\u7d1a\u5c01\u88dd\uff0c\u4e26\u984d\u5916\u652f\u63f4\uff1a  \r\n\r\n- \ud83d\udd39 \u5167\u5efa\u6210\u4ea4\u91cf\u526f\u5716\uff08volume\uff09  \r\n- \ud83d\udd39 \u81ea\u8a02\u6280\u8853\u6307\u6a19\u526f\u5716\uff08\u4f8b\u5982 RSI\u3001MACD\uff09  \r\n- \ud83d\udd39 \u81ea\u8a02\u758a\u5716\uff08\u4f8b\u5982 SMA\u3001\u5747\u7dda\uff09  \r\n- \ud83d\udd39 \u81ea\u52d5\u8a2d\u5b9a\u4e2d\u6587\u5b57\u578b\uff08\u907f\u514d\u4e82\u78bc\uff09  \r\n\r\n---\r\n\r\n## \ud83d\ude80 \u5b89\u88dd\r\n```bash\r\npip install kbar\r\n```\r\n\r\n---\r\n\r\n## \ud83d\udee0 \u532f\u5165\r\n```python\r\nfrom kbar import KBar\r\n# \u6216\r\nimport kbar\r\n```\r\n\r\n---\r\n\r\n## \ud83d\udcca \u4f7f\u7528\u6559\u5b78\r\n\r\n### 1\ufe0f\u20e3 \u6e96\u5099\u80a1\u7968\u50f9\u91cf\u8cc7\u6599\uff08\u4ee5 yfinance \u70ba\u4f8b\uff09\r\n```python\r\nimport yfinance as yf\r\ndf = yf.download('0050.TW', start='2024-07-01', end='2024-08-21', auto_adjust=False)\r\n\r\n# \u8f49\u63db\u70ba\u55ae\u5c64\u6b04\u4f4d\u540d\u7a31\uff08\u907f\u514d\u591a\u5c64\u7d22\u5f15\uff09\r\ndf.columns = df.columns.map(lambda x: x[0])\r\n```\r\n\r\n---\r\n\r\n### 2\ufe0f\u20e3 \u7e6a\u88fd\u7c21\u55ae\u7684 K \u7dda\u5716\r\n```python\r\nkb = KBar(df)\r\n# \u6216 kb = kbar.KBar(df) \u82e5\u4f7f\u7528 import kbar\r\nkb.plot()\r\n```\r\n\r\n---\r\n\r\n### 3\ufe0f\u20e3 \u7e6a\u88fd\u542b\u6210\u4ea4\u91cf\u7684 K \u7dda\u5716\r\n```python\r\nkb = KBar(df)\r\nkb.plot(volume=True)  # \u6210\u4ea4\u91cf\u526f\u5716\u6703\u986f\u793a\u5728 panel=1\r\n```\r\n\r\n---\r\n\r\n### 4\ufe0f\u20e3 \u6dfb\u52a0\u81ea\u8a02\u526f\u5716\uff08\u4f8b\uff1aRSI \u6307\u6a19\uff09\r\n```python\r\nfrom talib.abstract import RSI\r\n\r\n# Ta-Lib \u9700\u8981\u6b04\u4f4d\u5c0f\u5beb\r\ndf.columns = [c.lower() for c in df.columns]\r\n\r\nrsi = RSI(df)\r\n\r\nkb = KBar(df)\r\nkb.addplot(rsi, panel=2, ylabel='RSI')  # RSI \u756b\u5728 panel=2\r\nkb.plot(volume=True)\r\n```\r\n\r\n---\r\n\r\n### 5\ufe0f\u20e3 \u7e6a\u88fd\u5167\u5efa\u5747\u7dda\uff08mav\uff09\r\n```python\r\nkb = KBar(df)\r\nkb.plot(mav=5)        # \u7e6a\u88fd 5 \u65e5\u5747\u7dda\r\n# kb.plot(mav=[3,5,7]) # \u7e6a\u88fd\u591a\u689d\u5747\u7dda\r\n```\r\n\r\n---\r\n\r\n### 6\ufe0f\u20e3 \u6dfb\u52a0\u81ea\u8a02\u758a\u5716\uff08\u4f8b\uff1aSMA \u5747\u7dda\uff09\r\n```python\r\nfrom talib.abstract import SMA\r\n\r\ndf.columns = [c.lower() for c in df.columns]\r\n\r\nsma3 = SMA(df['close'], timeperiod=3)\r\nsma5 = SMA(df['close'], timeperiod=5)\r\nsma7 = SMA(df['close'], timeperiod=7)\r\n\r\nkb = KBar(df)\r\nkb.addplot(sma3, panel=0, color='blue', width=1)\r\nkb.addplot(sma5, panel=0, color='orange', width=1)\r\nkb.addplot(sma7, panel=0, color='green', width=1)\r\n\r\nkb.plot(volume=True)\r\n```\r\n\r\n---\r\n\r\n## \ud83d\udcda API \u6587\u4ef6\r\n\r\n### `class KBar`\r\n\r\n\u5c01\u88dd `mplfinance`\uff0c\u63d0\u4f9b\u7c21\u5316\u7684 K \u7dda\u5716\u7e6a\u88fd\u8207\u526f\u5716\u7ba1\u7406\u3002\r\n\r\n#### \ud83d\udd39 \u5efa\u69cb\u5b50\r\n```python\r\nKBar(df)\r\n```\r\n\r\n- **df** (`pandas.DataFrame`)\uff1a\u5fc5\u9808\u5305\u542b\u4ee5\u4e0b\u6b04\u4f4d\uff1a  \r\n  - `Open`: \u958b\u76e4\u50f9  \r\n  - `High`: \u6700\u9ad8\u50f9  \r\n  - `Low`: \u6700\u4f4e\u50f9  \r\n  - `Close`: \u6536\u76e4\u50f9  \r\n  - `Volume`\uff08\u53ef\u9078\uff09\uff1a\u6210\u4ea4\u91cf  \r\n\r\n---\r\n\r\n#### \ud83d\udd39 \u65b9\u6cd5\uff1a`addplot(data, **kwargs)`\r\n\u6dfb\u52a0\u81ea\u8a02\u526f\u5716\u6216\u758a\u5716\u3002  \r\n\r\n- **data** (`Series` \u6216 `ndarray`)\uff1a\u8981\u7e6a\u88fd\u7684\u8cc7\u6599  \r\n- **kwargs**\uff1a\u50b3\u905e\u7d66 `mplfinance.make_addplot()` \u7684\u53c3\u6578\uff0c\u4f8b\u5982\uff1a  \r\n  - `color`\uff1a\u7dda\u689d\u984f\u8272  \r\n  - `width`\uff1a\u7dda\u689d\u5bec\u5ea6  \r\n  - `panel`\uff1a\u653e\u7f6e\u7684\u5716\u8868\u5340\uff080=\u4e3b\u5716\uff0c1~9=\u526f\u5716\uff09  \r\n  - `ylabel`\uff1a\u526f\u5716\u6a19\u7c64  \r\n\r\n---\r\n\r\n#### \ud83d\udd39 \u65b9\u6cd5\uff1a`plot(**kwargs)`\r\n\u7e6a\u88fd K \u7dda\u5716\uff0c\u53ef\u540c\u6642\u986f\u793a\u526f\u5716\u8207\u758a\u5716\u3002  \r\n\r\n- **kwargs**\uff1a\u50b3\u905e\u7d66 `mplfinance.plot()` \u7684\u53c3\u6578\uff0c\u4f8b\u5982\uff1a  \r\n  - `volume`\uff1a\u662f\u5426\u986f\u793a\u6210\u4ea4\u91cf\u526f\u5716  \r\n  - `mav`\uff1a\u5747\u7dda\uff08\u6574\u6578\u6216\u6574\u6578\u6e05\u55ae\uff09  \r\n  - `returnfig`\uff1a\u82e5 `True`\uff0c\u56de\u50b3 `(fig, axes)`  \r\n\r\n---\r\n\r\n## \ud83d\udcd8 \u7bc4\u4f8b\r\n```python\r\nimport yfinance as yf\r\nfrom kbar import KBar\r\n\r\ndf = yf.download(\"0050.TW\", start=\"2024-08-20\", end=\"2025-01-20\")\r\ndf.columns = df.columns.map(lambda x: x[0])\r\n\r\nkb = KBar(df)\r\nkb.addplot(df['Close'].rolling(5).mean(), color='blue', width=1)\r\nkb.plot(volume=True, mav=5)\r\n```\r\n\r\n---\r\n\r\n## \ud83d\udce6 \u4f9d\u8cf4\u5957\u4ef6\r\n- `mplfinance`\r\n- `matplotlib`\r\n- `pandas`\r\n- `numpy < 2`\r\n- `pyarrow`\uff08pandas \u5167\u90e8\u9700\u8981\uff09\r\n\r\n---\r\n\r\n## \ud83d\udcdd \u6388\u6b0a\r\nMIT License\r\n",
    "bugtrack_url": null,
    "license": "\r\n        MIT License\r\n        \r\n        Copyright (c) [2025] [Tony Y.H.Huang]\r\n        \r\n        Permission is hereby granted, free of charge, to any person obtaining a copy\r\n        of this software and associated documentation files (the \"Software\"), to deal\r\n        in the Software without restriction, including without limitation the rights\r\n        to use, copy, modify, merge, publish, distribute, sublicense, and/or sell\r\n        copies of the Software, and to permit persons to whom the Software is\r\n        furnished to do so, subject to the following conditions:\r\n        \r\n        The above copyright notice and this permission notice shall be included in all\r\n        copies or substantial portions of the Software.\r\n        \r\n        THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\r\n        IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\r\n        FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\r\n        AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\r\n        LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\r\n        OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\r\n        SOFTWARE.",
    "summary": "A lightweight K-line (candlestick) plotting tool with matplotlib and mplfinance.",
    "version": "0.1.2",
    "project_urls": {
        "Bug Tracker": "https://github.com/tony1966/kbar/issues",
        "Homepage": "https://github.com/tony1966/kbar/"
    },
    "split_keywords": [
        "finance",
        " candlestick",
        " mplfinance",
        " k-line",
        " stock"
    ],
    "urls": [
        {
            "comment_text": null,
            "digests": {
                "blake2b_256": "c4c29b58de24d8da183612af2758c330fd3c49cd3deb0447dbf8a5e92166c45b",
                "md5": "8a6469313d289b506698b5eab8cbe44f",
                "sha256": "caf9b1036e3d9c373ef12dbba3fe61c69fb38bc4f3e2fb2ca98224287365449b"
            },
            "downloads": -1,
            "filename": "kbar-0.1.2-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "8a6469313d289b506698b5eab8cbe44f",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.9",
            "size": 7825,
            "upload_time": "2025-08-22T02:50:32",
            "upload_time_iso_8601": "2025-08-22T02:50:32.635707Z",
            "url": "https://files.pythonhosted.org/packages/c4/c2/9b58de24d8da183612af2758c330fd3c49cd3deb0447dbf8a5e92166c45b/kbar-0.1.2-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": null,
            "digests": {
                "blake2b_256": "13c3c0ddf471959990ddae14da6eacd7ca07e897554fbe4ad3fb2380fe59de93",
                "md5": "83e90e539bf0dec082ce3079e5706f20",
                "sha256": "f4a3ce65346c8ba22286ffbdc34fc13c87786d98cb5542caa5b0d5f0c0ccfd1d"
            },
            "downloads": -1,
            "filename": "kbar-0.1.2.tar.gz",
            "has_sig": false,
            "md5_digest": "83e90e539bf0dec082ce3079e5706f20",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.9",
            "size": 9859,
            "upload_time": "2025-08-22T02:50:34",
            "upload_time_iso_8601": "2025-08-22T02:50:34.196835Z",
            "url": "https://files.pythonhosted.org/packages/13/c3/c0ddf471959990ddae14da6eacd7ca07e897554fbe4ad3fb2380fe59de93/kbar-0.1.2.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2025-08-22 02:50:34",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "tony1966",
    "github_project": "kbar",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": false,
    "lcname": "kbar"
}
        
Elapsed time: 0.82114s