Giter Site home page Giter Site logo

bobotig / py-candlestick-chart Goto Github PK

View Code? Open in Web Editor NEW
57.0 2.0 4.0 198 KB

Draw candlesticks charts right into your terminal, using Python!

Home Page: https://pypi.org/project/candlestick-chart

License: MIT License

Shell 0.51% Python 99.49%
candle candlestick-chart chart cli console options stock stock-market terminal trading

py-candlestick-chart's Issues

demonstration just renders vertical red line

WSL debian 11;
pasted demonstration to mychart.py and commented out 5 lines containing "fnum".
Does not recognize current height, width of the terminal.
Does not draw 5 candles but one single vertical red line.

cat /etc/issue;echo $TERM; python3 mychart.py | less -SR
Debian GNU/Linux 11 \n \l
xterm-256color
chart

xterm -fs 11 -fa DejaVuSansMono
cat /etc/issue;echo $TERM; python3 mychart.py
xterm
chart1

Cli crashes when no name is provided

HI, nice tool ! Thanks for sharing it.

I've noticed a small bug while using the cli:
If I don't provide a chart name, it crashes without providing much info:

> candlestick-chart -m json-file -f candles.json 
Traceback (most recent call last):
  File "/home/jose/charts/.venv/bin/candlestick-chart", line 8, in <module>
    sys.exit(main())
             ^^^^^^
  File "/home/jose/charts/.venv/lib/python3.11/site-packages/candlestick_chart/__main__.py", line 59, in main
    chart.draw()
  File "/home/jose/charts/.venv/lib/python3.11/site-packages/candlestick_chart/chart.py", line 57, in draw
    print(self._render())
          ^^^^^^^^^^^^^^
  File "/home/jose/charts/.venv/lib/python3.11/site-packages/candlestick_chart/chart.py", line 53, in _render
    return self.renderer.render(self)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/jose/charts/.venv/lib/python3.11/site-packages/candlestick_chart/chart_renderer.py", line 104, in render
    output.append(chart.info_bar.render(chart_data.main_candle_set, chart_data.width))
                  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/jose/charts/.venv/lib/python3.11/site-packages/candlestick_chart/info_bar.py", line 60, in render
    " | ".join(
TypeError: object of type 'NoneType' has no len()
>

It works if a name is provided:

> candlestick-chart -m json-file -f candles.json --chart-name __NAME__

and even with an empty name:

> candlestick-chart -m json-file -f candles.json --chart-name ''

It looks like the default value for the argument should be a string, changing this line seems to fix it

parser.add_argument("--chart-name", help="Sets the chart name.")

    parser.add_argument("--chart-name", help="Sets the chart name.", default="")

I'm using the latest version from PyPI:

> candlestick-chart --version
candlestick-chart 2.6.0

Improve update_candles() behavior

Chart.update_candles() behavior could be improved by adding an optional keyword argument: reset: bool = False.

The initial behavior will change:

  • Chart.update_candles(some_candles) will add some_candles to the existing candle set.
  • Chart.update_candles(some_candles, reset=True) will replace the existing candle set with some_candles.

Feature request: draw EMA

Hi, Dear @BoboTiG ๐Ÿ‘
This is a great project!!! I wonder that does this project support draw EMA lines, like EMA 20...
if NOT, Do you have a plan to support this feature?

  thank you very much for your project 

Prevent a zero division error

Traceback (most recent call last):
  # ...
  File "candlestick_chart/chart.py", line 36, in __rich_console__
    yield Group(*AnsiDecoder().decode(self._render()))
  File "candlestick_chart/chart.py", line 40, in _render
    return self.renderer.render(self)
  File "candlestick_chart/chart_renderer.py", line 82, in render
    output.extend(
  File "candlestick_chart/chart_renderer.py", line 83, in <genexpr>
    self._render_candle(candle, y, chart.y_axis) for candle in candles
  File "candlestick_chart/chart_renderer.py", line 39, in _render_candle
    high_y, low_y, max_y, min_y = y_axis.price_to_heights(candle)
  File "candlestick_chart/y_axis.py", line 27, in price_to_heights
    (candle.high - min_value) / diff * height,  # high_y
ZeroDivisionError: float division by zero

โ•ญโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€ locals โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ•ฎ
โ”‚   24 โ”‚   โ”‚   diff = candle_set.max_price - min_value                                             โ”‚
โ”‚   25 โ”‚   โ”‚                                                                                       โ”‚
โ”‚   26 โ”‚   โ”‚   return (                                                                            โ”‚
โ”‚ โฑ 27 โ”‚   โ”‚   โ”‚   (candle.high - min_value) / diff * height,  # high_y                            โ”‚
โ”‚   28 โ”‚   โ”‚   โ”‚   (candle.low - min_value) / diff * height,  # low_y                              โ”‚
โ”‚   29 โ”‚   โ”‚   โ”‚   (max_open - min_value) / diff * height,  # max_y                                โ”‚
โ”‚   30 โ”‚   โ”‚   โ”‚   (min_open - min_value) / diff * height,  # min_y                                โ”‚
โ”‚                                                                                                  โ”‚
โ”‚ โ•ญโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€ locals โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ•ฎ โ”‚
โ”‚ โ”‚     candle = <candlestick_chart.candle.Candle object at 0x7fa58053d360>                      โ”‚ โ”‚
โ”‚ โ”‚ candle_set = CandleSet(                                                                      โ”‚ โ”‚
โ”‚ โ”‚              โ”‚   candles=[<candlestick_chart.candle.Candle object at 0x7fa58053d360>],       โ”‚ โ”‚
โ”‚ โ”‚              โ”‚   min_price=8.340877714580442e-06,                                            โ”‚ โ”‚
โ”‚ โ”‚              โ”‚   max_price=8.340877714580442e-06,                                            โ”‚ โ”‚
โ”‚ โ”‚              โ”‚   min_volume=21994.401431846,                                                 โ”‚ โ”‚
โ”‚ โ”‚              โ”‚   max_volume=21994.401431846,                                                 โ”‚ โ”‚
โ”‚ โ”‚              โ”‚   variation=0.0,                                                              โ”‚ โ”‚
โ”‚ โ”‚              โ”‚   average=8.340877714580442e-06,                                              โ”‚ โ”‚
โ”‚ โ”‚              โ”‚   last_price=8.340877714580442e-06,                                           โ”‚ โ”‚
โ”‚ โ”‚              โ”‚   cumulative_volume=21994.401431846                                           โ”‚ โ”‚
โ”‚ โ”‚              )                                                                               โ”‚ โ”‚
โ”‚ โ”‚ chart_data = <candlestick_chart.chart_data.ChartData object at 0x7fa580ea3470>               โ”‚ โ”‚
โ”‚ โ”‚       diff = 0.0                                                                             โ”‚ โ”‚
โ”‚ โ”‚     height = 32                                                                              โ”‚ โ”‚
โ”‚ โ”‚   max_open = 8.340877714580442e-06                                                           โ”‚ โ”‚
โ”‚ โ”‚   min_open = 8.340877714580442e-06                                                           โ”‚ โ”‚
โ”‚ โ”‚  min_value = 8.340877714580442e-06                                                           โ”‚ โ”‚
โ”‚ โ”‚       self = YAxis(                                                                          โ”‚ โ”‚
โ”‚ โ”‚              โ”‚   chart_data=<candlestick_chart.chart_data.ChartData object at                โ”‚ โ”‚
โ”‚ โ”‚              0x7fa580ea3470>                                                                 โ”‚ โ”‚
โ”‚ โ”‚              )                                                                               โ”‚ โ”‚
โ”‚ โ•ฐโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ•ฏ โ”‚
โ•ฐโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ•ฏ

demonstration does not find fnum

Copied demonstration code into chart.py and tried to run it:

$ python3 chart.py
Traceback (most recent call last):
  File "/home/helgein/projects_python/python312/candlechart/chart.py", line 1, in <module>
    from candlestick_chart import Candle, Chart, fnum
ImportError: cannot import name 'fnum' from 'candlestick_chart' (/home/sru/.pyenv/versions/3.12.0/lib/python3.12/site-packages/candlestick_chart/__init__.py

Fix values computation in the info bar

There is a mismatch with the Rust version: when displaying the info bar, our version is computing values using the visible candle set. Actually, it should rather use the main candle set.

Our version (incorrect):

def render(self, candle_set: "CandleSet") -> str:

Rust version (correct): https://github.com/Julien-R44/cli-candlestick-chart/blob/9ea785c1a9fe5e2357bfe1411150ad0a7e009135/src/info_bar.rs#L19

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.