How to Fetch Stock Prices for Securities Using CYBOS Plus CpSysDib.StockChart
Guide 1
CpSysDib.StockChart
receives chart data for stocks, sectors, and ELW. If you’re not familiar with using the API, understanding this system can be quite challenging. Let’s run a Python example that fetches the stock price of Seegene Inc. and understand how it works.
SetInputValue()
: Specifically designates the data you want.BlockRequest()
: Requests reception of the designated data.GetHeaderValue()
: Returns the received header data.GetDataValue()
: Returns the received data.
The reason for using these bizarre functions in a peculiar way is because CYBOS Plus is not really a Python library. We’ll cover this in more detail in the Explanation.
Highlights
>>> days = 10
>>> instStockChart = win32com.client.Dispatch("CpSysDib.StockChart")
>>> instStockChart.SetInputValue(0, seegene)
>>> instStockChart.SetInputValue(1, ord('2'))
>>> instStockChart.SetInputValue(4, days)
>>> instStockChart.SetInputValue(5, 5)
>>> instStockChart.SetInputValue(6, ord('D'))
>>> instStockChart.SetInputValue(9, ord('1'))
>>> instStockChart.BlockRequest()
0
>>> numData = instStockChart.GetHeaderValue(3)
>>> for i in range(numData):
... print(instStockChart.GetDataValue(0, i), end = ', ')
...
80700, 80100, 77400, 77200, 82600, 85800, 83800, 88000, 88000, 90500, >>>
As of around 10 AM on July 21, 2021, we can confirm that the stock data has been successfully fetched.
Explanation
instStockChart = win32com.client.Dispatch("CpSysDib.StockChart")
instStockChart.SetInputValue(0, 'A096530')
As mentioned before, CYBOS Plus wasn’t specifically developed for Python. It can be used in any development environment that can utilize the Windows system COM Object library, including but not limited to Visual Studio, Python, and MS Office. The instStockChart
object is created by loading CpSysDib.StockChart
through win32com.client.Dispatch()
, but there is absolutely no reason for its internals to be Pythonic. Hence, a different Interface is used compared to Pure Python.
object.SetInputValue(0, code)
designates the stock code for object
as code
. One might imagine a more Pythonic syntax like instStockChart.code = 'A096530'
where you directly access and assign a value to the code
property of instStockChart
. However, since it’s not Python, we follow the method of CYBOS Plus.
Next is the designation of whether to request data by period or by quantity.
- 1 - Request Type (char)
- ‘1’: By period
- ‘2’: By quantity
instStockChart.SetInputValue(1, ord('2'))
However, the way to designate this is a bit strange. Instead of just ‘2’, ord('2')
is passed, wrapped in the ord()
function. ord()
is a function that returns a Unicode character string as an integer Unicode point, specifically ord('2')
returns the integer 50. The reason why it’s passed in Unicode instead of directly passing the string '2'
aligns with the reasons explained earlier.
But next is the designation of which values to request.
- 5 - Field (long array)
- 5: Closing price
- 8: Volume
- 20: Institutional net purchase
- …
instStockChart.SetInputValue(5, 5)
This means that we’re asking for the closing price data of Seegene. There are a total of 38 types of fields, from 0 to 37, and for detailed usage of each, refer to the official manual. However, it’s unclear why, despite not using integers as inputs, the request type (1) specifically accepts characters, and even more confusingly, why it’s either ‘1’ or ‘2’. It’s one of those things you just have to accept and move on.
Now, let’s comprehensively understand what data the above code demands.
days = 10
instStockChart = win32com.client.Dispatch("CpSysDib.StockChart")
instStockChart.SetInputValue(0, seegene)
instStockChart.SetInputValue(1, ord('2'))
instStockChart.SetInputValue(4, days)
instStockChart.SetInputValue(5, 5)
instStockChart.SetInputValue(6, ord('D'))
instStockChart.SetInputValue(9, ord('1'))
From the top, it is as follows:
- 0 - Stock Code
- Seegene (A096530)
- 1 - Request Type
- ‘2’: By quantity
- 4 - Quantity Requested
- days (10). The data for ten days will be requested.
- 5 - Field
- 5: Closing price. The price at the close of the market.
- 6 - Chart Type
- ‘D’: Daily data
- 9 - Adjusted Price
- ‘1’: Adjusted price. Whether the stock price is adjusted for any reasons like rights issue or stock split to match the current price ratio. Since Seegene had a rights issue in the first half of 2021, it’s appropriate to request the adjusted price.
Thus, when the instStockChart
object requests data, it receives ’the daily closing prices of Seegene as adjusted prices for the most recent 10 days’. It’s like setting the properties of the object.
instStockChart.BlockRequest()
Now, executing the object.BlockRequest()
method will fetch the data as designated. Indeed, this aspect highlights a Pythonic mindset.
numData = instStockChart.GetHeaderValue(3)
for i in range(numData):
print(instStockChart.GetDataValue(0, i), end = ', ')
GetHeaderValue()
: Returns the received header data. The difference from the data seems to be whether it’s kinda metadata or not, but since this varies from module to module, it’s faster to directly consult the official manual and understand.
- 3: Requests the number of data received. In this example, since 10 days’ worth of data was initially requested,
numData
will exactly be for 10 days. If requested by period, the number can vary as it’s based on trading days.
GetDataValue(Type,index)
: Returns the received data.
- Returns the
index
-th value of the type requested byType
. In this example, since only the closing price was requested, it will return thei
-th value of the closing price, which is the 0-th type.