Charts #123
Replies: 13 comments
-
The easiest way to plot candlestick charts is to probably use
Some links to check out: To get the raw chart data into a from poloniex import Poloniex
import pandas as pd
from time import time
api = Poloniex(timeout=120, jsonNums=float)
raw = api.returnChartData('BTC_LTC', period=api.MINUTE * 5, start=time() - api.DAY)
df = pd.DataFrame(raw)
# adjust dates format and set dates as index
df['date'] = [pd.to_datetime(c['date'], unit='s') for c in raw]
df.set_index('date', inplace=True)
# show the end of dataframe
print(df.tail())
I'm working on a module that saves chart data in mongodb and returns data in a dataframe with indicators. But I haven't worked in the plotting bit yet. |
Beta Was this translation helpful? Give feedback.
-
This seems like a good start: pandas-dev/pandas#783 (comment) |
Beta Was this translation helpful? Give feedback.
-
Not pretty but it it works: from poloniex import Poloniex
import pandas as pd
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
from mpl_finance import _candlestick
from time import time
api = Poloniex(timeout=120, jsonNums=float)
raw = api.returnChartData('BTC_LTC', period=api.MINUTE * 5, start=time() - api.DAY)
df = pd.DataFrame(raw)
# adjust dates format and set dates as index
df['date'] = [pd.to_datetime(c['date'], unit='s') for c in raw]
df.set_index('date', inplace=True)
# show the end of dataframe
print(df.tail())
df.dropna(inplace=True)
fig, ax = plt.subplots()
idx_name = df.index.name
dat = df.reset_index()[[idx_name, "open", "high",
"low", "close", "volume"]]
print(dat)
dat[df.index.name] = dat[df.index.name].map(mdates.date2num)
ax.xaxis_date()
ax.xaxis.set_major_formatter(mdates.DateFormatter("%Y-%m-%d %H:%M:%S"))
plt.xticks(rotation=45)
_candlestick(ax, dat.values,
width=0.002, colorup='g', colordown='r',
alpha=1.0, ochl=False)
ax.grid('on')
plt.subplots_adjust(left=.09, bottom=.14, right=.94,
top=.95, wspace=.20, hspace=0)
plt.xlabel('Date')
plt.ylabel('Price')
# plt.savefig("candle.png")
plt.show() |
Beta Was this translation helpful? Give feedback.
-
Here is a nicer looking chart using bokeh from math import pi
from time import time
from poloniex import Poloniex
import pandas as pd
from bokeh.plotting import figure, output_file, show
api = Poloniex(timeout=None, jsonNums=float)
period = api.HOUR * 4
raw = api.returnChartData('BTC_LTC', period=period, start=time() - api.DAY*7)
df = pd.DataFrame(raw)
df['date'] = pd.to_datetime(df["date"], unit='s')
df.dropna(inplace=True)
print(df.tail())
w = (period * 1000) - 5000
tools = "pan,wheel_zoom,box_zoom,reset,save"
output_file("BTC_LTC.html", title="BTC_LTC-Poloniex")
p = figure(x_axis_type="datetime", tools=tools, plot_width=1500, title="BTC_LTC")
p.xaxis.major_label_orientation = pi / 4
p.grid.grid_line_alpha = 0.7
inc = df.close > df.open
dec = df.open > df.close
p.segment(df.date, df.high, df.date, df.low, color="black")
p.vbar(df.date[inc], w, df.open[inc], df.close[inc], fill_color="green", line_color="black")
p.vbar(df.date[dec], w, df.open[dec], df.close[dec], fill_color="red", line_color="black")
show(p) Dataframe:
Chart: |
Beta Was this translation helpful? Give feedback.
-
In reply to this message of yours, i have a small query.
But that var altcoins only provides me data of BTC_DASH which is last in the list. Can you think of any solution where data of a particular currencypair goes into its own variable which is created in advance. Do you have easy way out ? |
Beta Was this translation helpful? Give feedback.
-
Maybe store them in a dict? You were just overwriting the the coinlist = ['BTC_ETH',
'BTC_ZEC',
'BTC_XMR',
'BTC_LTC',
'BTC_ETC',
'BTC_BTS',
'BTC_GNT',
'BTC_XRP',
'BTC_FCT',
'BTC_SC',
'BTC_DCR',
'BTC_DASH']
altcoins = {}
for coin in coinlist:
altcoins[coin] = public.returnChartData(coin, period=14400)
for coin in altcoins:
print(altcoins[coin])
# or rather
print(altcoins) |
Beta Was this translation helpful? Give feedback.
-
Thanks a ton.
While running this code I keep getting some 'date' error every now and than and I dont know why. |
Beta Was this translation helpful? Give feedback.
-
It's because you set date as the index, remove that line or reference the index values. |
Beta Was this translation helpful? Give feedback.
-
or rather: for i in data:
data[i]["date"] = pd.to_datetime(data[i]["date"],unit='s')
data[i]=data[i].set_index("date", inplace=True)
data[i]=data[i][['open','high','low','close']] # These are simply to remove unwanted column |
Beta Was this translation helpful? Give feedback.
-
Right!.
|
Beta Was this translation helpful? Give feedback.
-
Added a bokeh plotting example with basic indicators, it also can merge candles into whatever candle period you want using the method discussed at #129. |
Beta Was this translation helpful? Give feedback.
-
Doesnt look live, how do you build candles from websocket |
Beta Was this translation helpful? Give feedback.
-
good project |
Beta Was this translation helpful? Give feedback.
-
Hi
Your python-poloniex is so simple to use that newbie like me can use it!!!
Is it possible to create similar charts that Poloniex website has? If not, do you know any newbie friendly way to create candlestick charts like that?
Thanks
Beta Was this translation helpful? Give feedback.
All reactions