Package untuk membaca data Stock Saham

By | June 26, 2020
Print Friendly, PDF & Email

Saat ini sudah banyak sekali tersedia analisis chart saham yang berbasis online seperti chartbit, investing.com yang bersifat gratisan, namun demikian terkadang kita ingin membuat analisis tersendiri menggunakan tools yang kita kuasai seperti R dan Python. Nah didalam R itu package bernama quantmod, tapi di python belum ada yang gratisan (quandl –  https://www.quandl.com/ masih berbayar). Tapi untuk urusan download kita bisa koq menggunakan pandas-reader yang berfungsi membaca data saham dari yahoo finance dan technical analyst menggunakan ta

Install Package Pandas-Reader

Pandas-reader membutuhkan pandas, bila kalian telah membaca buku saya https://softscients.com/2020/03/28/buku-belajar-mudah-python-dengan-package-open-source/ maka tak perlu repot-repot melakukan instalasi, sedangkan andas_datareader harus install menggunakan pip https://softscients.com/2020/06/18/buku-pemrograman-python-cara-install-modul-di-python/

pip install pandas-datareader

Sesudah kalian install, cek ya menggunakan perintah show pada pip

Download Data Saham di yahoo finance

Misalkan kita ingin mendapatkan harga saham unilever JAKARTA

import pandas_datareader as web
import pandas as pd
from matplotlib import pyplot as plt

stok = web.DataReader('UNVR.JK', 
                    start='01/01/2020',
                    end='22/06/2020',
                    data_source='yahoo')
print(stok.head(10))

hasil

              High     Low    Open   Close      Volume  Adj Close
Date                                                             
2020-01-02  8700.0  8500.0  8500.0  8550.0  11059800.0     8550.0
2020-01-03  8675.0  8550.0  8675.0  8575.0   8071700.0     8575.0
2020-01-06  8600.0  8350.0  8575.0  8475.0   7913300.0     8475.0
2020-01-07  8500.0  8400.0  8475.0  8450.0   5793000.0     8450.0
2020-01-08  8450.0  8275.0  8450.0  8325.0   8261400.0     8325.0
2020-01-09  8425.0  8250.0  8350.0  8350.0   9224300.0     8350.0
2020-01-10  8400.0  8250.0  8400.0  8250.0  14849800.0     8250.0
2020-01-13  8400.0  8275.0  8275.0  8400.0   8904800.0     8400.0
2020-01-14  8500.0  8300.0  8400.0  8475.0  11761100.0     8475.0
2020-01-15  8525.0  8350.0  8475.0  8475.0  14965200.0     8475.0

Menampilkan plot price close

stok['Close'].plot(), plt.grid(),plt.title('emiten Unilever')

Tentu analisis saham tidak hanya diatas saja, cuman ploting namun ada analisis teknikal. Nah untuk hal tersebut kita perlu package technical analyst https://technical-analysis-library-in-python.readthedocs.io/en/latest/index.html. Lakukan install dengan perintah berikut

pip install ta

Kode lengkapnya yaitu

import pandas_datareader as web
import pandas as pd
from matplotlib import pyplot as plt
import ta
import matplotlib.pyplot as plt
import matplotlib.gridspec as gridspec

stok = web.DataReader('UNVR.JK', 
                    start='01/01/2020',
                    end='22/06/2020',
                    data_source='yahoo')
print(stok.head(10))


stok['rsi'] = ta.momentum.rsi(stok['Close'],n=10)


# set up plots and axes for plotting
fig = plt.figure()
gs = gridspec.GridSpec(2, 1)
ax1 = plt.subplot(gs[0, 0])
ax2 = plt.subplot(gs[1, 0])

# fill overbought and oversold regions on RSI plot
ax2.set_ylim([0, 100])
ax2.fill_between(stok.index, 70, 100, color='#a6c64c66')
ax2.fill_between(stok.index, 0, 30, color='#f58f9266')

# display xaxis labels nicely
ax2.set_xticks(stok.index[::30])
fig.add_subplot(ax1)
fig.add_subplot(ax2)

# plot our stock values
stok[['Close']].plot(ax=ax1, title='UNVR close Price')
stok[['rsi']].plot(ax=ax2, color='orange', title='RSI (n=14)')

# show the window
plt.show()

 

Kalian bisa pelajari sendiri di

https://technical-analysis-library-in-python.readthedocs.io/en/latest/index.html

https://github.com/bukosabino/ta

Ada 34 indikator yang bisa kalian gunakan

Volume

  • Accumulation/Distribution Index (ADI)
  • On-Balance Volume (OBV)
  • Chaikin Money Flow (CMF)
  • Force Index (FI)
  • Ease of Movement (EoM, EMV)
  • Volume-price Trend (VPT)
  • Negative Volume Index (NVI)
  • Volume Weighted Average Price (VWAP)

Volatility

  • Average True Range (ATR)
  • Bollinger Bands (BB)
  • Keltner Channel (KC)
  • Donchian Channel (DC)

Trend

  • Moving Average Convergence Divergence (MACD)
  • Average Directional Movement Index (ADX)
  • Vortex Indicator (VI)
  • Trix (TRIX)
  • Mass Index (MI)
  • Commodity Channel Index (CCI)
  • Detrended Price Oscillator (DPO)
  • KST Oscillator (KST)
  • Ichimoku Kinkō Hyō (Ichimoku)
  • Parabolic Stop And Reverse (Parabolic SAR)

Momentum

  • Money Flow Index (MFI)
  • Relative Strength Index (RSI)
  • True strength index (TSI)
  • Ultimate Oscillator (UO)
  • Stochastic Oscillator (SR)
  • Williams %R (WR)
  • Awesome Oscillator (AO)
  • Kaufman’s Adaptive Moving Average (KAMA)
  • Rate of Change (ROC)

Others

  • Daily Return (DR)
  • Daily Log Return (DLR)
  • Cumulative Return (CR)

Download Data Intraday

Selain data bersifat harian, maka kita bisa juga download data intraday menggunakan https://www.alphavantage.co/

pip install alpha_vantage

Tapi kalian harus daftar dulu untuk mendapatkan sebuah API, oiya karena ini bersifat intraday, maka hanya berlaku pada hari itu juga, tidak bisa mendapatkan informasi intraday sesuai dengan hari yang kita tentukan sebelumnya.

# Import TimeSeries class
from alpha_vantage.timeseries import TimeSeries
ALPHA_VANTAGE_API_KEY = 'KN0KF6YX0TXJF8DT' #API key punya kalian
ts = TimeSeries(key=ALPHA_VANTAGE_API_KEY, output_format='pandas')
# Get pandas dataframe with the intraday data and information of the data
intraday_data, data_info = ts.get_intraday('UNVR.JK', outputsize='full', interval='1min')
# Print the information of the data
data_info

hasil

{'1. Information': 'Intraday (1min) open, high, low, close prices and volume',
 '2. Symbol': 'UNVR.JK',
 '3. Last Refreshed': '2020-06-25 04:11:00',
 '4. Interval': '1min',
 '5. Output Size': 'Full size',
 '6. Time Zone': 'US/Eastern'}

Untuk melihat datanya yaitu

intraday_data.head()

hasil

 			1. open 2. high	3. low 	4. close 5. volume
date 					
2020-06-25 04:11:00 	7900.0 	7900.0 	7900.0 	7900.0 	1275.0
2020-06-25 04:10:00 	7900.0 	7900.0 	7900.0 	7900.0 	510.0
2020-06-25 04:09:00 	7900.0 	7900.0 	7900.0 	7900.0 	425.0
2020-06-25 04:08:00 	7900.0 	7900.0 	7900.0 	7900.0 	2125.0
2020-06-25 04:07:00 	7900.0 	7900.0 	7900.0 	7900.0 	382670.0

Sedangkan untuk meanampilkan plot nya yaitu

intraday_data['4. close'].plot(figsize=(10, 7))
# Define the label for the title of the figure
plt.title("Close Price", fontsize=16)
# Define the labels for x-axis and y-axis
plt.ylabel('Price', fontsize=14)
plt.xlabel('Time', fontsize=14)
# Plot the grid lines
plt.grid(which="major", color='k', linestyle='-.', linewidth=0.5)
plt.show()

 

 

 

Leave a Reply

Your email address will not be published. Required fields are marked *