-
Notifications
You must be signed in to change notification settings - Fork 18
/
get-data-harian-emiten.py
179 lines (163 loc) · 4.25 KB
/
get-data-harian-emiten.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
import cloudscraper, json
import pandas as pd
from time import sleep
# length 1 untuk 1 hari kerja
# karena kita mau ambil sehari aja
length = 1
# list emiten
emiten = pd.read_csv('data/List Emiten/all.csv')
lq45 = pd.read_csv('data/List Emiten/LQ45.csv')
# get kode-kode emiten
kode_emiten = emiten['code'].values
kode_lq45 = lq45['code'].values
# http client
http = cloudscraper.CloudScraper()
for code in kode_emiten:
# link
link = f"https://idx.co.id/umbraco/Surface/ListedCompany/GetTradingInfoSS?code={code}&length={length}"
# send request
# always try to repeat
# request whenever failed
while True:
try:
# send request
result = http.get(link).text
result = http.get(link).text
result = json.loads(result)
# success, we stop the while loop
break
except:
# error, we sleep for 2 minutes
sleep(2*60)
# ada isinya?
if result["replies"] == []:
# tidak ada, print
print(f"Tidak ada emiten dengan kode {code}")
# loop diloncati
continue
else:
try:
# load data lama
history = pd.read_csv(f"data/Saham/Semua/{code}.csv")
except:
# ga ada data lama, baru IPO
history = pd.DataFrame({
'date': [],
'previous': [],
'open_price': [],
'first_trade': [],
'high': [],
'low': [],
'close': [],
'change': [],
'volume': [],
'value': [],
'frequency': [],
'index_individual': [],
'offer': [],
'offer_volume': [],
'bid': [],
'bid_volume': [],
'listed_shares': [],
'tradeble_shares': [],
'weight_for_index': [],
'foreign_sell': [],
'foreign_buy': [],
'delisting_date': [],
'non_regular_volume': [],
'non_regular_value': [],
'non_regular_frequency': [],
})
# data-data
date = []
previous = []
openPrice = []
firstTrade = []
high = []
low = []
close = []
change = []
volume = []
value = []
frequency = []
indexIndividual = []
offer = []
offerVolume = []
bid = []
bidVolume = []
listedShares = []
tradebleShares = []
weightForIndex = []
foreignSell = []
foreignBuy = []
delistingDate = []
nonRegularVolume = []
nonRegularValue = []
nonRegularFrequency = []
# simpan data-data
for data in result["replies"][::-1]:
# hari kerja?
if data['Date'] not in history.date.values:
# ya
date.append(data['Date'])
previous.append(data['Previous'])
openPrice.append(data['OpenPrice'])
firstTrade.append(data['FirstTrade'])
high.append(data['High'])
low.append(data['Low'])
close.append(data['Close'])
change.append(data['Change'])
volume.append(data['Volume'])
value.append(data['Value'])
frequency.append(data['Frequency'])
indexIndividual.append(data['IndexIndividual'])
offer.append(data['Offer'])
offerVolume.append(data['OfferVolume'])
bid.append(data['Bid'])
bidVolume.append(data['BidVolume'])
listedShares.append(data['ListedShares'])
tradebleShares.append(data['TradebleShares'])
weightForIndex.append(data['WeightForIndex'])
foreignSell.append(data['ForeignSell'])
foreignBuy.append(data['ForeignBuy'])
delistingDate.append(data['DelistingDate'])
nonRegularVolume.append(data['NonRegularVolume'])
nonRegularValue.append(data['NonRegularValue'])
nonRegularFrequency.append(data['NonRegularFrequency'])
# data beres, simpan dalam CSV
hari_ini = pd.DataFrame({
'date': date,
'previous': previous,
'open_price': openPrice,
'first_trade': firstTrade,
'high': high,
'low': low,
'close': close,
'change': change,
'volume': volume,
'value': value,
'frequency': frequency,
'index_individual': indexIndividual,
'offer': offer,
'offer_volume': offerVolume,
'bid': bid,
'bid_volume': bidVolume,
'listed_shares': listedShares,
'tradeble_shares': tradebleShares,
'weight_for_index': weightForIndex,
'foreign_sell': foreignSell,
'foreign_buy': foreignBuy,
'delisting_date': delistingDate,
'non_regular_volume': nonRegularVolume,
'non_regular_value': nonRegularValue,
'non_regular_frequency': nonRegularFrequency,
})
# jadikan satu dgn yang lama
new_data = pd.concat([history, hari_ini], ignore_index=True)
# simpan
new_data.to_csv(f"data/Saham/Semua/{code}.csv", index=False)
# Saham LQ45?
if code in kode_lq45:
new_data.to_csv(f"data/Saham/LQ45/{code}.csv", index=False)
# bobo dulu biar ga kena ban
sleep(15)