Ambil data historis EOD via yahoo, caranya agak berbeda dari yang sebelumnya saya posting di https://adrxvia.com/blog/ambil-data-historis-harga-saham-dengan-python/ hasilnya dalam bentuk *.csv yang tersimpan di desktop (saya pakai mac), csv ini bisa di import dari amibroker, kodenya sudah saya post di gist github https://gist.github.com/andramarkov/0a38e08d6cd5863c80b27e8cffd59869, data tickernya diambil dari mysql

eod

'''ini prinsipnya sama dengan EOD_direct_to_db_merged.py, bedanya ini nonmerged, 
pake ini karena eod model lama sering error 'date' '''

import sys
sys.path.append('/Users/andra/Downloads/adrx_repo/bitbucket/newpystock/reorg')

from datetime import datetime, timedelta 
import pandas as pd
#agar bisa kayak gini maka harus bikin __init__.py di folder
from lib.yfx_fakeuseragent_modami import IntraData as ambil
import pymysql
from sqlalchemy import Table, Column, Integer, String, MetaData, 
ForeignKey, create_engine, inspect
from pathlib import Path
import datetime
import os
import errno
import time
from random import randint


def newdir(d):
	try:
		os.makedirs(d)
		print ('Processing directory')
		print ('Directory created\n' + d)
	except OSError as exception:
		if exception.errno != errno.EEXIST:
			raise
		else:
			print ("Directory already exists\n%s " % d)
	return d


'''periode = str(input("Periode (#opsi: 1d,5d,1mo,3mo,6mo,1y,2y,5y,10y,
ytd,max) ?  [default: 7d] "))
if len(periode) == 0 :
	periode = '7d'

interval = str(input("Interval (#opsi: 1m, 2m, 5m, 15m, 30m, 60m, 90m, 
1h, 1d, 5d, 1wk, 1mo, 3mo? [default: 1m] "))

if len(interval) == 0 :
	interval = '1m' '''

periode = '1d'
interval = '1d' 



#nanti utk hapus .jk dari ticker 
def splito(tck):
	tck, jk = tck.split('.')
	tck = tck.upper()
	return tck

myQuery = '''SELECT kode from tblMasterTicker'''
engine = create_engine("mysql+pymysql://{user}:{pw}@localhost/{db}"
		                       .format(user="root",
		                               pw="root",
		                               db="ombandaroo"))

symbols = pd.read_sql_query(myQuery, engine)

my_list = []
for kode in symbols['kode']:
	my_list.append(kode)


string = '.JK'
my_new_list = [x + string for x in my_list]


hari_ini = datetime.datetime.today()

sekarang = hari_ini.strftime('%Y-%m-%d')


#desktop
dpath = str(Path.home())
dpath = dpath + '/Desktop/stockdata/eod_all_' + sekarang + '/'
print ('Membuat folder di desktop')
dpath = newdir(dpath)


try:
	'''bikin dataframe kosong dulu gan, kalo gak di bikin di awal 
	nanti error kyk tadi
	ValueError: If using all scalar values, you must pass an index'''

	df = pd.DataFrame()
	length = len(my_new_list)
		
	for i in range(length):
		try:
			#print ('Timestamp: ' + str(datetime.datetime.today()))	
			lst = my_new_list[i]
			nextlst = lst
			nextlst  = splito(nextlst)
			cariapa = ambil(lst, berapa_lama = periode, interval = interval, 
			dropna='True')
			tckr = splito(lst) #hapus dot JK dari ticker
			df = cariapa.hasil #result dataframe
					
			saveidx = df.index #index bawaan = tanggal
			idx = 0
			rxx = saveidx.strftime("%Y-%m-%d")
			#insert kolom
			df.insert(loc=idx, column='Ticker', value=tckr)
			df.insert(loc=idx+1, column='Date', value=rxx)
			df.insert(loc=idx+7, column='AdjClose', value=df.Close)
			#hapus index lama 
			df.reset_index(drop=True, inplace=True)
				
			print ('Processing ' + tckr.upper() + ' (' + str(i+1) + ' of ' + 
			str(length) + ')')
			df.to_csv((os.path.join(dpath,'{}.csv')).format(tckr.upper()), 
			index=False, header=True)
			#print ('Sleep@MainLoop')
			time.sleep(randint(0,2))
			#print ('WakeUp@MainLoop')
				
				
		except:
			print ('Processing ' + nextlst + ' (' + str(i+1) + ' of ' + 
			str(length) + ') - 
			error, skipped ' )
			pass #biar tetap lanjut kalo error tickernya
			
	print ('')
	print ('Proses selesai')
	print ('Directory: ' + dpath)	
	print ('Timestamp: ' + str(datetime.datetime.today()))	
			
except Exception as e:
		print (f'Gagal, Kode Error: {e}')