![]() ![]() To make a figure with subplots, we start by declaring a figure with 4 subplots. To access the values of each instance, we need to use methods like macd.macd_signal(), which you will see in a moment. We have only created the instance of MACD and stochastic. !pip install ta from ta.trend import MACD from ta.momentum import StochasticOscillator # MACD macd = MACD(close=df, window_slow=26, window_fast=12, window_sign=9) # stochastic stoch = StochasticOscillator(high=df, close=df, low=df, window=14, smooth_window=3) We can do the calculation by ourselves but the easiest way would be to use a technical analysis library like ta-lib, since the focus of this article is not about creating technical indicators. We already have volume in our main dataframe but we still need the values of MACD and stochastic. In this section, we will try to add volume, MACD, and stochastic as subplots. So far we have plotted 3 traces on the main plot area and sometimes one plot area is not enough and we need some subplots. Add Volume, MACD & Stochastic as subplots Much better now, right? Notice I have also added a figure title. # first declare an empty figure fig = go.Figure() # add OHLC trace fig.add_trace(go.Candlestick(x=df.index, open=df, high=df, low=df, close=df, showlegend=False)) # add moving average traces fig.add_trace(go.Scatter(x=df.index, y=df, opacity=0.7, line=dict(color='blue', width=2), name='MA 5')) fig.add_trace(go.Scatter(x=df.index, y=df, opacity=0.7, line=dict(color='orange', width=2), name='MA 20')) # hide dates with no values fig.update_xaxes(rangebreaks=) # remove rangeslider fig.update_layout(xaxis_rangeslider_visible=False) # add chart title fig.update_layout(title="AAPL") It can be quite troublesome to rename a plot title, so the best practice is to assign a name when adding a new trace.Īlternatively, if you don’t want the name of a plot to be displayed as a legend entry, you can just add showlegend=False to the trace property. You might also notice the OHLC plot has the name trace 0, that’s because we did not name the plot when we created it in the first place. Note that when you click on the plot title at the legend, you can hide/show the plot. A complete OHLC chart without any missing gap in between candles. That might have seen unnecessary but here we go. The above code will remove the weekends but what about the holidays? Well, there’s no straightforward way to do it but there’s a solution to that: # removing all empty dates # build complete timeline from start date to end date dt_all = pd.date_range(start=df.index,end=df.index) # retrieve the dates that ARE in the original datset dt_obs = # define dates with missing values dt_breaks = fig.update_xaxes(rangebreaks=) If you’d like to remove those empty dates, we can do it by using rangebreaks: # hide weekends fig.update_xaxes(rangebreaks=)]) You might also realize there are some gaps between candles, that’s due to the holidays and weekends when the market is not open. If you find it not really useful and want it gone, simply update the figure by: # removing rangeslider fig.update_layout(xaxis_rangeslider_visible=False) Alternatively, you can do that using the little slider below the main chart called rangeslider. Remember how I mentioned plotly being an interactive charting platform? Well, just hover over the plot and you will see how you can zoom in to the chart by sliding over a period of time. And just like that, we get a nice simple candlestick chart.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |