## How to add points to timeseries graph to show buy-sell signal – matplotlib

While doing time series analysis, you can show buy/sell signal on the ghraph itself.

Following is the script for doing the same.

While working on this code, I wasted quite a lot of time on below error.

Traceback (most recent call last):
File "timeseries_simple_with_pointer.py", line 35, in <module>
plt.scatter(df.loc[df['SMA20'] >1400.0 , 'Date'],df.loc[df['SMA20'] >1400.0, 'Close Price'], label='skitscat', color='red', s=25, marker="<")
File "/usr/local/lib/python3.6/site-packages/matplotlib/pyplot.py", line 3378, in scatter
edgecolors=edgecolors, data=data, **kwargs)
File "/usr/local/lib/python3.6/site-packages/matplotlib/__init__.py", line 1717, in inner
return func(ax, *args, **kwargs)
File "/usr/local/lib/python3.6/site-packages/matplotlib/axes/_axes.py", line 4023, in scatter
offsets = np.column_stack([x, y])
File "/usr/local/lib/python3.6/site-packages/numpy/lib/shape_base.py", line 369, in column_stack
return _nx.concatenate(arrays, 1)
TypeError: invalid type promotion

On further research I found that matplotlib scatter plot does not support pandas series and the scatter plot output needs to be converted into list so I had to use .values to make it work.

Output of my strategy using SMA 5 and SMAA12

As you can see, in range bound market it creates a lot of whipsaw, however, it was able to capture a very good bull run.

I tried using larger duration SMA to see another strategy. Second time, I tried with SAM20 and SMA100 and here is the output

As expected, longer duration SMA strategy provides lesser number of signals, lesser whipsaw’s.

This result does not mean you need to use higher duration SMA. I am planning to run this logic for approx 200 securities, will update this post with the feedabck later.

## How to plot simple and Candlestick chart using Python – pandas matplotlib

Pandas and Matplotlib can be used to plot various types of graphs. Simple timeseries plot and candlestick are basic graphs used by technical analyst for identifying the trend.

## Simple time Series Chart using Python – pandas matplotlib

Here is the simplest graph. It uses close price of HDFCBANK for last 24 months to plot normal graph

Here is the sample output

## Candlestick chart using Python – pandas matplotlib

As per this link, module matplotlib.finance is deprecated in 2.0 and has been moved to a module called mpl_finance. This is still working when I have motplotlib version 2.1.2. This may stop working in any future releases, however, you can use mpl_finance module to use this feature. Please note that mpl_finance is no longer maintained.

Here is the output

## Candlestick chart with SMA overlay using Python – pandas matplotlib

here is the output