I trying to plot chandlestick with the OHLC data that I have. The data come from 5 minute timeframe resample to 4 hour timeframe, so there will be huge gap on weekends.
# Load data
subdata = pd.read_csv(
'data/M5/EURUSD.csv',
header = None,
skiprows = 0,
sep = '\t',
names = [
'date',
'open',
'high',
'low',
'close',
'volume'
],
)
subdata['date'] = pd.to_datetime(subdata['date'])
subdata.set_index(['date'], inplace = True)
# Resample
subdata = subdata.resample('4H').agg({
'open': 'first',
'high': 'max',
'low': 'min',
'close': 'last',
'volume': 'sum'
}).dropna(axis=0)
After I resampled the data then plotted data use Bokeh, and here comes the problem which is the gap on weekend day. Here the code I used to plot the data and used this concept to solve this problem but still not work.
fig1 = figure(x_axis_type='datetime', height=400, width=900)
# I try to add this code but still not work
fig1.xaxis.major_label_overrides = {
i: date.strftime('%Y-%m-%d %H:%S') for i, date in enumerate(subdata.index)
}
wide = 12*60*60*200
inc = subdata['close'] > subdata['open']
dec = subdata['open'] > subdata['close']
fig1.segment(subdata.index, subdata['high'], subdata.index, subdata['low'], color='black')
fig1.vbar(subdata.index[inc], wide, subdata['open'][inc], subdata.close[inc], fill_color='#D5E1DD', line_color='black')
fig1.vbar(subdata.index[dec], wide, subdata['open'][dec], subdata['close'][dec], fill_color='#F2583E', line_color='black')
show(gridplot([[fig1]]))
Is there something wrong with my code or am I wrong with the concept?
After trial and error, I finally found the root of the problem. When changing xaxis to enumerate(subdata.index)
it means xaxis uses numbers instead of datetime. But i still use datetime to make plots that are supposed to use numbers, and here comes the weird thing. Why is bokeh still receiving xaxis datetime on xaxis numbers, which ends up creating gaps and wrong plots?
To solve this problem, an index number from the row is needed. in my case, index uses datetime so need to create a new column for index number and then create a plot with index number.
# Here code to make number index
subdata['x'] = subdata.reset_index().index
fig1 = figure(x_axis_type='datetime', height=400, width=900)
fig1.xaxis.major_label_overrides = {
i: date.strftime('%Y-%m-%d %H:%S') for i, date in enumerate(subdata.index)
}
wide = 0.5
inc = subdata['close'] > subdata['open']
dec = subdata['open'] > subdata['close']
fig1.segment(subdata['x'], subdata['high'], subdata['x'], subdata['low'], color='black')
fig1.vbar(subdata['x'][inc], wide, subdata['open'][inc], subdata['close'][inc], fill_color='#D5E1DD', line_color='black')
fig1.vbar(subdata['x'][dec], wide, subdata['open'][dec], subdata['close'][dec], fill_color='#F2583E', line_color='black')
show(gridplot([[fig1]]))