highfestiva / finplot

Performant and effortless finance plotting for Python
MIT License
911 stars 186 forks source link

Issue plotting OHCL from a CSV. #58

Closed pssolanki111 closed 3 years ago

pssolanki111 commented 3 years ago

HERE is the small CSV file that I'm getting from an API. I tried to follow your docs to plot this but I'm getting all sorts of troubles in doing that. This is the code I tried it with

#import csv, os
#import shutil as sh
import pandas as pd
import finplot as fplt

#ydir = r'C:\BeingProfessional'

#os.chdir(ydir)
symbol = 'IBM'
df = pd.read_csv('data.csv')
df.dropna(how='any')

ax,ax2 = fplt.create_plot(symbol, rows=2)
candles = df[['timestamp','open','close','high','low']]
fplt.candlestick_ochl(candles, ax=ax)

fplt.show()

data.csv being the csv file. It just takes quite long then execution stops without any error message.

I'd appreciate any help on how to get just a simple plot, nothing fancy or additional layout in there with a simple CSV file?

Moreover they do have an API which reckons about sending data in json, HERE but that doesn't work either.

I'd appreciate any help on either of the cases, preferably on the json one, but csv one is also equally appreciable.

Thank You for the amazing tool btw. I stumbled upon one of your answers on SO and here I am :)

highfestiva commented 3 years ago

You need to arrange your timestamps in order (they're reversed in the CSV and JSON):

import finplot as fplt
df = fplt.pd.read_csv('intraday_5min_IBM.csv', parse_dates=['timestamp'], index_col='timestamp').sort_index()
fplt.candlestick_ochl(df[['open','close','high','low']])
fplt.show()

(Note that some 5 minute intervals are missing in your data, but they are simply left out when plotting.)

pssolanki111 commented 3 years ago

@highfestiva Thanks for your response. I'll try the suggestion in a bit and let you know. On an unrelated topic, I tried your games and honestly they were fun :D. I liked that NSAgent & kill cutie the most.

I've got 2 questions now--

  1. Did you check the json one as well? there seems to something wrong with that as well. Although that's not much important, I can work csv instead. But It would be better to have options.

  2. Is is possible to plot more than one finplot graphs for different CSVs, and is there a way to update the graphs every one hour (I know how to schedule events, I'm just asking if finplot allows changing the plot in-place or like in the same graph window ) or do i have to close the current windows with graphs and plot them again with different data. I mean how does that work.

Thank you for the amazing work again :)

pssolanki111 commented 3 years ago

@highfestiva It worked!! It took a bit time to draw but I'll take that considering I've got a potato system..

highfestiva commented 3 years ago

You can redraw your graphs every hour if you wish. Look at the runtime examples to help you get going (example-bfx, examply-bitmex-ws). There are also some closed issue here where someone asks for the same thing.

pssolanki111 commented 3 years ago

Thank you. I'll take a look :). Istimer_callback() an equivalent of .after() of tkinter, which calls a function after a given period of time? I'm looking at example-bfx.pyRN and what I understand is, usetimer_callback()to schedule data updates, and use plot.update_data() to redraw the graph with new data. Is that correct?

if it is, I'll have to figure out a way to make things synchronized in multi-processed environment. But I could manage that, also that has nothing to do with this 'seriously' amazing lib.

on another topic, when I used the code you wrote above, it plots the data fine. But when i write that dfdata back to a csv, it always skips that timestampcolumn. I want to be able to write that timestampcolumn as well.

And if possible, is there is a way to draw finplotgraphs directly from csvfiles, without creating pandas df? like maybe using csv.DictReader() or something?

highfestiva commented 3 years ago

Function defined like so: def timer_callback(update_func, seconds, single_shot=False).

To set timestamp back as a column, as opposed to an index, use df = df.reset_index().

You can supply arrays directly, or use numpy arrays, you don't have to use pandas. Although pandas is always used internally in finplot.

pssolanki111 commented 3 years ago

Thank you for the information, Jonas! appreciate it. Thanks again..

LongNumber5 commented 3 years ago

Can you ask me, I just want each graph display, the default graph will enlarge and display exactly 100 candles and move to the end like pressing the "End" button?