直接在您的终端中使用Python绘制蜡烛图!
项目描述
Python蜡烛图
📈 直接在您的终端中使用Python绘制蜡烛图!
这是一个从伟大的cli-candlestick-chart(由Julien-R44开发,使用Rust编写)移植过来的。您正在查看Python 3.10+版本。
注意:未在macOS或Windows上测试(可能无法渲染颜色)。
目录:
特性
安装
就像这样
$ python -m pip install -U candlestick-chart
演示
from candlestick_chart import Candle, Chart
# Add some candles
candles = [
Candle(open=133.520004, close=133.610001, high=126.760002, low=129.410004),
Candle(open=128.889999, close=131.740005, high=128.429993, low=131.009995),
Candle(open=127.720001, close=131.050003, high=126.379997, low=126.599998),
Candle(open=128.360001, close=131.630005, high=127.860001, low=130.919998),
Candle(open=132.429993, close=132.630005, high=130.229996, low=132.050003),
]
# Create and display the chart
# Optional keyword arguments: title, width, height
chart = Chart(candles, title="Optional title")
# Set the chart title
chart.set_name("BTC/USDT")
# Set customs colors
chart.set_bear_color(1, 205, 254)
chart.set_bull_color(255, 107, 153)
chart.set_vol_bull_color(1, 205, 254)
chart.set_vol_bear_color(255, 107, 153)
# Set custom labels (empty string => label not displayed)
chart.set_label("highest", "ATH")
chart.set_label("lowest", "ATL")
chart.set_label("average", "")
chart.set_label("volume", "")
# Volume pane settings
chart.set_volume_pane_height(6)
chart.set_volume_pane_enabled(False)
# And, it is also responsive!
new_width = 200
new_height = 150
chart.update_size(new_width, new_height)
# By the way, did you know that you can add more candles in real-time?
chart.update_candles(candles[:3])
# Or completely replace current candles
chart.update_candles(candles[:3], reset=True)
# Set a custom color at price 52,348.63
chart.set_highlight(fnum(52_348.63), "red")
chart.set_highlight(fnum(52_348.63), (255, 0, 0))
chart.set_highlight(fnum(52_348.63), "91m")
chart.set_highlight(fnum(52_348.63), "91;47m")
chart.draw()
二进制使用
安装库时,会创建一个可执行文件(candlestick-chart
)
candlestick-chart --help
options:
-h, --help show this help message and exit
-m {stdin,csv-file,json-file}, --mode {stdin,csv-file,json-file}
Select the method for retrieving the candles.
-f FILE, --file FILE [MODE:*-file] File to read candles from.
--chart-name CHART_NAME
Sets the chart name.
--bear-color BEAR_COLOR
Sets the descending candles color in hexadecimal.
--bull-color BULL_COLOR
Sets the ascending candles color in hexadecimal.
--version show program's version number and exit
当请求JSON或stdin模式时,库期望具有以下格式的JSON
[
{
"open": 28994.009766,
"high": 29600.626953,
"low": 28803.585938,
"close": 29374.152344
},
...
]
对于所有请求,这里都有支持的字段
"open": float # mandatory
"close": float # mandatory
"high": float # mandatory
"low": float # mandatory
"volume": float
"timestamp": float
示例
API
- 使用CSV解析的基本示例:使用
$ python examples/basic_from_csv_file.py
运行 - 使用JSON解析的基本示例:使用
$ python examples/basic_from_json_file.py
运行 - 使用stdin解析的基本示例:使用
$ ./examples/basic_from_stdin.sh
运行 - 从Binance获取蜡烛图:使用
$ python examples/fetch_from_binance.py
运行 - 与Rich集成:使用
$ python examples/integrate_with_rich.py
运行 - 使用自定义图表渲染类:使用
$ python examples/custom_renderer_class.py
运行
二进制
从文件读取CSV
candlestick-chart \
--mode=csv-file \
--file='./examples/BTC-USD.csv' \
--chart-name='My BTC Chart' \
--bear-color='#b967ff' \
--bull-color='ff6b99'
从文件读取JSON
candlestick-chart \
--mode=json-file \
--file='./examples/BTC-chart.json' \
--chart-name='My BTC Chart' \
--bear-color='#b967ff' \
--bull-color='ff6b99'
从stdin读取
echo '[
{
"open": 28994.009766,
"high": 29600.626953,
"low": 28803.585938,
"close": 29374.152344
},
{
"open": 29376.455078,
"high": 33155.117188,
"low": 29091.181641,
"close": 32127.267578
}
]' | candlestick-chart \
--mode=stdin \
--chart-name='My BTC Chart' \
--bear-color='#b967ff' \
--bull-color='ff6b99'
开发者
设置
python -m venv venv
. venv/bin/activate
python -m pip install -U pip
安装
python -m pip install -e '.[test]'
测试
python -m pytest