Get the App
SLTechnology News&Howtos  ›  Servers  › 

How to implement Stock Analysis Chart by pandas

Shulou Source: shulou.com Published: 2022-05-31 23:11:50 09月28日 Update

This article mainly introduces pandas how to achieve stock analysis chart, the article introduces in great detail, has a certain reference value, interested friends must read it!

Get stock data of APPL,MSFT,GOOG

Stocks = pd.DataFrame ({"Date": apple ["Date"], "AAPL": apple ["Adj Close"], "MSFT": microsoft ["Adj Close"], "GOOG": google ["Adj Close"]}). Set_index ("Date") print (stocks.head () dateAAPLGOOGMSFT2016-01-04102.6123741.84002753.0150322016-01-05100.040792742.58001753.2568892016-01-0698.083025743.619992.2894622016-01-0793.93473726.39001550.4706972016-01-0894.4402214.27467150.625489

Comparison of more than 1 stocks

Make a figure

Stocks.plot (grid = True) plt.show ()

Because the share price of google is relatively high, the volatility of Microsoft and Apple stock becomes smaller. One solution is to use different ticks.

Stocks.plot (secondary_y = ["AAPL", "MSFT"], grid = True)

A better way is to draw a profit chart.

# df.apply (arg) will apply the function parameters to each column of the data box and then return a data box # in this line of code, the x in lambda is a seriesstock_return = stocks.apply (lambda x: X / x [0]) stock_return.head ()

Make a fluctuation chart

Stock_return.plot (grid = True) .axhline (y = 1, color = "black", lw = 2)

Through this chart, we can see the return of each stock relative to the initial price, and we can also see that the fluctuations of these stocks are related.

We can also map the daily changes of stocks.

Stock_change = stocks.apply (lambda x: np.log (x)-np.log (x.shift (1)) # shift moves dates back by 1.

2 average chart of stocks

Stocks ["AAPL"] .plot (label= "APPL") apple ["20d"] = np.round (apple ["Close"] .plot (window = 20, center = False) .mean (), 2) .plot (label= "20Average") apple ["50d"] = np.round (apple ["Close"] .plot (window = 50, center = False) .mean (), 2) .plot (label= "50Average") plt.legend () plt.show ()

The above is all the contents of the article "how to achieve Stock Analysis Chart in pandas". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!

Tags: Stocks data volatility analytical charts analysis content returns methods articles differences codes values prices interests functions scales parameters change charts graphics moving averages Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Xiaomi Huawei Shulou Technology Microsoft macOS