0

I have a dataframe containing financial data. I am using Plotly to create a candlestick layout, and desire to incorporate a few other indicators to assist with diagnosing price range.

Sample dataframe data (df):

                         Open      High       Low    Close      Volume  \
DATE                                                                     
2017-09-25 10:00:00  249.2800  249.3700  249.2800  249.370     19123.0   
2017-09-25 11:00:00  249.3900  249.4500  249.3400  249.400     40853.0   
2017-09-25 12:00:00  249.4500  249.4500  249.1400  249.170    153004.0   
2017-09-25 13:00:00  249.1700  249.3400  249.0500  249.230   3963325.0   
2017-09-25 14:00:00  249.2201  249.5532  248.5700  248.605  10122404.0   
2017-09-25 15:00:00  248.6000  248.8200  248.0800  248.530   9098104.0   
2017-09-25 16:00:00  248.5400  248.7950  248.1600  248.695   5550874.0   
2017-09-25 17:00:00  248.6900  248.8800  248.5600  248.720   3687280.0   
2017-09-25 18:00:00  248.7300  248.8500  248.3800  248.650   5779563.0   
2017-09-25 19:00:00  248.6600  249.0100  248.5300  248.940  10731186.0   
2017-09-25 20:00:00  248.9400  249.3600  248.9300  249.330   5970611.0   
2017-09-25 21:00:00  249.3300  249.3500  248.7074  249.330    125453.0   
2017-09-25 22:00:00  249.3400  249.3700  249.2800  249.300     65436.0   
2017-09-25 23:00:00  249.3000  249.3200  249.1500  249.230     79118.0   
2017-09-26 08:00:00  249.2600  249.2600  249.0500  249.140     18100.0   
2017-09-26 09:00:00  249.1700  249.2300  249.1700  249.230      1000.0   
2017-09-26 10:00:00  249.2200  249.2700  249.2000  249.210     13173.0   
2017-09-26 11:00:00  249.1300  249.2300  249.0200  249.150    102716.0   
2017-09-26 12:00:00  249.1400  249.3900  249.0100  249.370    262505.0   
2017-09-26 13:00:00  249.3600  249.7000  249.3100  249.570   4968021.0   
2017-09-26 14:00:00  249.5800  249.6700  249.1100  249.140   5959137.0   
2017-09-26 15:00:00  249.1400  249.1950  248.8050  249.150   4724855.0   
2017-09-26 16:00:00  249.1500  249.3256  248.9900  249.190   5557655.0   
2017-09-26 17:00:00  249.1800  249.4900  249.1600  249.420   3901895.0   
2017-09-26 18:00:00  249.4200  249.4800  249.1700  249.240   5939215.0   
2017-09-26 19:00:00  249.2400  249.4400  248.9000  249.050  10846907.0   
2017-09-26 20:00:00  249.0700  249.2893  248.9700  249.040   8378370.0   
2017-09-26 21:00:00  249.0100  249.0600  248.9800  249.010     46410.0   
2017-09-26 22:00:00  249.0100  249.0500  248.9600  249.000     30602.0   
2017-09-26 23:00:00  249.0100  249.0800  249.0100  249.080     17352.0   

                            ema  \
DATE                             
2017-09-25 10:00:00         NaN  
2017-09-25 11:00:00         NaN  
2017-09-25 12:00:00         NaN  
2017-09-25 13:00:00         NaN  
2017-09-25 14:00:00         NaN  
2017-09-25 15:00:00         NaN  
2017-09-25 16:00:00         NaN  
2017-09-25 17:00:00         NaN  
2017-09-25 18:00:00         NaN  
2017-09-25 19:00:00         NaN  
2017-09-25 20:00:00         NaN  
2017-09-25 21:00:00         NaN  
2017-09-25 22:00:00         NaN  
2017-09-25 23:00:00         NaN  
2017-09-26 08:00:00         NaN  
2017-09-26 09:00:00         NaN  
2017-09-26 10:00:00         NaN  
2017-09-26 11:00:00         NaN  
2017-09-26 12:00:00         NaN  
2017-09-26 13:00:00  249.108500  
2017-09-26 14:00:00  249.111500  
2017-09-26 15:00:00  249.115167  
2017-09-26 16:00:00  249.122294  
2017-09-26 17:00:00  249.150647  
2017-09-26 18:00:00  249.159156  
2017-09-26 19:00:00  249.148761  
2017-09-26 20:00:00  249.138402  
2017-09-26 21:00:00  249.126174  
2017-09-26 22:00:00  249.114157  
2017-09-26 23:00:00  249.110904 

                            kc_upper  
DATE                             
2017-09-25 10:00:00         NaN  
2017-09-25 11:00:00         NaN  
2017-09-25 12:00:00         NaN  
2017-09-25 13:00:00         NaN  
2017-09-25 14:00:00         NaN  
2017-09-25 15:00:00         NaN  
2017-09-25 16:00:00         NaN  
2017-09-25 17:00:00         NaN  
2017-09-25 18:00:00         NaN  
2017-09-25 19:00:00         NaN  
2017-09-25 20:00:00         NaN  
2017-09-25 21:00:00         NaN  
2017-09-25 22:00:00         NaN  
2017-09-25 23:00:00         NaN  
2017-09-26 08:00:00         NaN  
2017-09-26 09:00:00         NaN  
2017-09-26 10:00:00         NaN  
2017-09-26 11:00:00         NaN  
2017-09-26 12:00:00         NaN  
2017-09-26 13:00:00  249.108500  
2017-09-26 14:00:00  249.111500  
2017-09-26 15:00:00  249.115167  
2017-09-26 16:00:00  249.122294  
2017-09-26 17:00:00  249.150647  
2017-09-26 18:00:00  249.159156  
2017-09-26 19:00:00  249.148761  
2017-09-26 20:00:00  249.138402  
2017-09-26 21:00:00  249.126174  
2017-09-26 22:00:00  249.114157  
2017-09-26 23:00:00  249.110904
 

I then have the following code:

import pandas as pd
import plotly.graph_objs as go
data = [go.Candlestick(x=df.index, open=df['Open'], high=df['High'], low=df['Low'], close=df['Close'])]
fig = go.Figure(data=data)
fig.update_layout(xaxis_rangeslider_visible=False)
fig.add_scatter(x=df.index, y=df['ema'], mode='lines')
fig.add_scatter(x=df.index, y=df['kc_lower'], mode='lines')

fig.update_xaxes(rangebreaks=[
        dict(bounds=["sat", "mon"]),  # hide weekends, apparently?
        dict(bounds=[16, 8], pattern="hour"),
        ]
)
fig.show()

Which produces the following image:

enter image description here

However, the line and data 'jumps'. How do I smooth this line and data to hide all hours that the market is closed, while ensuring the line in the plot is continuous for both the line and candlestick layouts?

Thank you in advance.

nick_halden
  • 113
  • 4

0 Answers0