piccolomo / plotext

plotting on terminal
MIT License
1.7k stars 77 forks source link

Time Format Error in Datetime Plots #81

Closed Bingonemo123 closed 2 years ago

Bingonemo123 commented 2 years ago
plt.datetime.set_datetime_form(date_form= '%d', time_form = '%H:%M')
prices = [x[4] for x in rates] # close prices 
dates = [plt.datetime.datetime_to_string(datetime.fromtimestamp(el[0])) for el in rates] # from timestamp to datetime and then to string
plt.plot_date(dates, prices)
plt.ylabel("$ Stock Price")

plt.show()

Error

Traceback (most recent call last):
  File "C:\Users\user\Documents\repos\Ripple_3.0\TheoreticalExperiments\CursesInterface\Test5.py", line 17, in <module>
    plt.plot_date(dates, prices)
  File "C:\Users\user\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9\LocalCache\local-packages\Python39\site-packages\plotext\_core.py", line 173, in plot_date
    figure.subplot.draw_date(x, y,
  File "C:\Users\user\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9\LocalCache\local-packages\Python39\site-packages\plotext\_subplot.py", line 428, in draw_date
    x = list(map(datetime.string_to_timestamp, t))
  File "C:\Users\user\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9\LocalCache\local-packages\Python39\site-packages\plotext\_datetime.py", line 72, in string_to_timestamp
    return self.datetime_to_timestamp(self.string_to_datetime(string))
  File "C:\Users\user\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9\LocalCache\local-packages\Python39\site-packages\plotext\_datetime.py", line 69, in datetime_to_timestamp
    return datetime.timestamp() - self._timestamp0
OSError: [Errno 22] Invalid argument

if date_format is not '%d/%m/%Y' getting Error

piccolomo commented 2 years ago

Hi @Bingonemo123,

great issue report. I am writing a new version and should deal with this. The only problem is that it is going to take some time (months).

Let me know if it is a urgent issue or related to a bigger project and could temporary patch it.

Thanks ! :-) Savino

Ledjob commented 2 years ago

I got another error while trying the example.

Exception in thread Thread-1: Traceback (most recent call last): File "/usr/lib/python3.6/threading.py", line 916, in _bootstrap_inner self.run() File "/usr/lib/python3.6/threading.py", line 864, in run self._target(*self._args, **self._kwargs) File "/home/Ledjob/.local/lib/python3.6/site-packages/multitasking/__init__.py", line 102, in _run_via_pool return callee(*args, **kwargs) File "/home/Ledjob/.local/lib/python3.6/site-packages/yfinance/multi.py", line 167, in _download_one_threaded actions, period, interval, prepost, proxy, rounding) File "/home/Ledjob/.local/lib/python3.6/site-packages/yfinance/multi.py", line 182, in _download_one rounding=rounding, many=True) File "/home/Ledjob/.local/lib/python3.6/site-packages/yfinance/base.py", line 155, in history data = data.json() File "/home/Ledjob/.local/lib/python3.6/site-packages/requests/models.py", line 898, in json return complexjson.loads(self.text, **kwargs) File "/home/Ledjob/.local/lib/python3.6/site-packages/simplejson/__init__.py", line 525, in loads return _default_decoder.decode(s) File "/home/Ledjob/.local/lib/python3.6/site-packages/simplejson/decoder.py", line 370, in decode obj, end = self.raw_decode(s) File "/home/Ledjob/.local/lib/python3.6/site-packages/simplejson/decoder.py", line 400, in raw_decode return self.scan_once(s, idx=_w(s, idx).end()) simplejson.errors.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

^CTraceback (most recent call last): File "<string>", line 1, in <module> File "/home/Ledjob/.local/lib/python3.6/site-packages/yfinance/multi.py", line 95, in download _time.sleep(0.01) KeyboardInterrupt

piccolomo commented 2 years ago

Hello @Bingonemo123

I am trying to solve the issue, could kindly provide also rates (where it actually comes from) so that I understand better the error: I don't seem to be able to replicate it.

Thanks

Bingonemo123 commented 2 years ago

@piccolomo rates comes from ohlc structured list soprices must be list of float numbers. I don't have exact code, I changed it and it is, somewhere in git archives. If you still need exact code I will reconstruct it.

piccolomo commented 2 years ago

Hi @Bingonemo123, thanks for answer, the thing is that rates seem to contain also datetime information: it would be helpful to understand what kind of structure it has.

Bingonemo123 commented 2 years ago

@piccolomo It was [ timestamp, open, high, low , close, tick_volume spread real_volume] as is mentioned https://www.mql5.com/en/docs/integration/python_metatrader5/mt5copyratesfrom_py. I think, it is original source.

piccolomo commented 2 years ago

Understood, I will try to simulate some data later. I tried the code btw but I was not successful in installing MetaTrader5 on Linux

Bingonemo123 commented 2 years ago

MetaTrader is not available on Linux, it is available on windows but it needs standalone working Software. But I think. it is returning python object, so you can simulate just that.

piccolomo commented 2 years ago

Ok, I will give it a try later, but if you feel like droping a complete code, would be helpful

piccolomo commented 2 years ago

Hi @Bingonemo123,

I think I solved the issue in the new version 4.3 available only on GitHub for now, using:

pip install git+https://github.com/piccolomo/plotext

Here is some working code:

import yfinance as yf
import plotext as plt

plt.datetime.input_date_form(form = 'd/m/Y')
plt.datetime.output_date_form(form = 'd/m')

start = plt.datetime.string_to_datetime("11/07/2020")
end = plt.datetime.today_datetime()
data = yf.download('goog', start, end)

prices = list(data["Close"])
dates = [plt.datetime.datetime_to_string(el, form='d/m/Y') for el in data.index]

plt.plot_date(dates, prices)

plt.title("Google Stock Price")
plt.xlabel("Date")
plt.ylabel("Stock Price $")
plt.show()

plt.clf()

image

The new code comes from next version on which I am still working on, which I have quickly adapted to previous version, and so there may be some mistakes. Any feedback or double test is appreciated. Hopefully I didn't destroy other functionalities.

To clarify: input_date_form() sets the datetime form of any function which takes strings, while outout_date_form() sets the datetime form of any function which outputs strings. If necessary the form of functions like datetime_to_string() could be set manually. Also I removed the % symbols in the datetime forms for simplicity.

Thanks for the great report, Savino

P.S. I close for now, but feel free to reopen, or let me know if you can't.