Did you notice the picture in the introduction to the “Python In Action: Building a Stock Based Quantitative Trading System” brochure:

Next, we will show you how to plot the pictures that mark the ups and downs of the Shanghai Composite Index. Python third-party libraries such as Matplotlib, PANDAS, Tushare, and Numpy are involved.

Firstly, tushare’s ts.get_k_data interface was used to obtain daily trading data of Shanghai Composite Index from 2008 to 2019.

Obtain daily trading data of Shanghai Composite Index from 2008 to 2019
 sh=ts.get_k_data('sh',start='2008-1-1', end='2019-1-1')
printSh. Head () Date Open close high Low Volume code 183 2008-10-06 2267.39 2173.74 2267.39 2172.57 60938100.0 sh 184 2008-10-07 2101.09 2157.84 2183.00 2072.90 56902600.0 sh 185 2008-10-08 2095.91 2092.22 2127.08 2059.09 50759500.0 sh 186 2008-10-09 2125.57 2074.58 2130.87 2063.41 45071000.0 sh 187 2008-10-10 1995.96 2000.57 2027.83 1963.18 54077500.0 shCopy the code

Plot () function of Matplotlib to plot the closing price of Shanghai Stock Index.

sh['close']. The plot (figsize = (16, 8))Copy the code

It can be seen from the chart that the bull market started in 2014 lasted from July 2014 to June 2015, during which the index increased from 2075.48 points to 5178.19 points. We use matplotlib’s annotate () function to annotate the start and end of the bull market in the chart.

plt.annotate('Bull market start',
   xy=('2014-7-1',2054),
   xytext=('2014-3-1',2500),
   bbox = dict(boxstyle = 'round, pad = 0.5'.fc = 'yellow', alpha = 0.5),
   arrowprops=dict(facecolor='red', the shrink = 0.05), fontsize = 12)Copy the code

Next, matplotlib’s axhline() function is used to set the median value of the index to a horizontal, and Matplotlib’s axvline() function is used to set the minimum value of the index to a vertical.

plt.axhline(y=sh['close'].median(), c='r', ls=The '-', lw=2)
plt.axvline(x=sh[sh['close'].values == sh['close'].min()].index, c='g', ls='-', lw=2)
Copy the code

Then, matplotlib’s axhspan() function is used to set the starting trading day of a bull market to the horizontal reference zone parallel to the X-axis, and Matplotlib’s axvspan () function is used to set the starting trading day of a bull market to the vertical reference zone parallel to the Y-axis.

PLT. Axhspan (ymin = 2075.48, ymax = 5178.19, facecolor ='purple'PLT, alpha = 0.3). Axvspan (xmin ='2014-7-1', xmax='2015-6-15', facecolor='g', alpha = 0.3)Copy the code

Next we’ll tweak the style details of the diagram. For example, remove the top and right border of the graph, set the position of the scale line on the coordinate axis, and place the scale line inside the coordinate axis.

# Style adjustment
# spines Sets the left\right\bottom\top border of the chart. Here remove the top and right border of the chart
for spine in plt.gca().spines.keys():
    print(spine)
    if spine == 'top' or spine == 'right':
        plt.gca().spines[spine].set_color('none')

# set the position of the scale on the axis.
plt.gca().xaxis.set_ticks_position('bottom')
plt.gca().yaxis.set_ticks_position('left')

# Place the scale line inside the coordinate axis
plt.tick_params(direction = 'in')

Copy the code

About the complete code can join the pamphlet exchange group to obtain. More quantitative trading content welcome everyone to subscribe to read the booklet!!