Skip to content

Instantly share code, notes, and snippets.

@spdrman
Created July 22, 2019 04:25
Show Gist options
  • Save spdrman/0d1efdc574067e3c2fbd2fa37fd0814a to your computer and use it in GitHub Desktop.
Save spdrman/0d1efdc574067e3c2fbd2fa37fd0814a to your computer and use it in GitHub Desktop.
# From: http://www.nuclearphynance.com/Show%20Post.aspx?PostIDKey=93267
import win32com.client
import datetime
import pandas
def csiTicker2Number(csiTicker):
ua=win32com.client.Dispatch("UA.API2")
ua.MarketSymbol = csiTicker
ua.IsStock = 1
csiNumber=ua.FindMarketNumber()
return csiNumber
def csiDate2DateTime(d):
t=str(d)
YYYY=int(t[0:4])
MM=int(t[4:6])
DD=int(t[6:8])
dateTime=datetime.datetime(YYYY,MM,DD)
return dateTime
csiTicker='IBM'
csiNumber=csiTicker2Number(csiTicker)
# set the start date
startDate=datetime.datetime(1970,1,1)
# set the end date
endDate=datetime.datetime.today()
# connect to UA API
ua=win32com.client.Dispatch("UA.API2")
# set the flags
ua.ShowDecimalPoint = 1
ua.IncludeHolidays = 1
ua.detrendMethod = 0
ua.FillInCashPrice = 0
ua.CloseOutOfRangeAdjustmentMethod = 1
# turn off split adjustments
ua.ApplyStockSplitAdjustments=0
# turn off dividend adjustments
ua.ApplyStockDividendAdjustments=0
# fetch the unadjusted data
numberOfDates=ua.RetrieveStock(csiNumber,startDate.strftime('%Y%m%d'),endDate.strftime('%Y%m%d'))
# extract the data to an array
data=ua.CopyRetrievedDataToArray(0)
# set the column names
columnNames=['date', 'dayOfWeek','deliveryYYYYMM','openPrice','highPrice','lowPrice','closePrice','unadjustedClosePrice','cash','volume','OI','totalVolume','totalOI','cash']
# create the data frame
equityData=pandas.DataFrame(zip(*list(data[1:])),columns = columnNames)
# convert the int YYYYMMDD date to a datetime
equityData['date']=equityData['date'].apply(csiDate2DateTime)
number=ua.RetrieveDividendsSplitsAndCapitalGains(csiNumber,1,startDate.strftime('%Y%m%d'),endDate.strftime('%Y%m%d'))
# dividend data
dividendData=pandas.DataFrame(list(number[4]),list(number[3]),columns=['dividends'])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment