Fortran

Guide To Learn

Reading stock data from files

Now that we have the list of stock symbols that we’ll work on, let’s use this information to load the data from file and store it in our newly declared dynamic arrays. The prototype of our main loop should look like the following listing.

Listing 5.6 Reading stock data from a file using a read_stock subroutine

do n = 1, size(symbols)
  call read_stock(                         &          ❶
    'data/' // trim(symbols(n)) // '.csv', &          ❶
    time, open, high, low, close, adjclose, volume)   ❶
end do

❶ For each symbol, reads stock data from the file and stores it in arrays

However, we haven’t implemented the read_stock subroutine yet! Based on the calling signature, we should pass the file name as the first argument, an array of times as the second, and six real arrays to hold the stock data as the remaining arguments. At this point, we’re passing arrays that haven’t been allocated yet. As we iterate over the stocks, we’ll need to explicitly allocate our arrays before loading data from the files. The declaration of data in our read_stock subroutine prototype may thus be rendered as shown in the following listing.

Listing 5.7 Data declaration in the read_stock subroutine

subroutine read_stock(filename, time, open, high, &
                      low, close, adjclose, volume)
  character(*), intent(in) :: filename                  ❶
  character(:), allocatable, intent(in out) :: time(:)  ❷
  real, allocatable, intent(in out) :: open(:), &       ❸
    high(:), low(:), close(:), adjclose(:), volume(:)   ❸
  ...                                                   ❹
end subroutine read_stock

❶ Assumed-length input character string

❷ Dynamic array of character strings of length 10

❸ Dynamic real arrays for stock data; ampersand marks line continuation

❹ This is where the action will happen.

Let’s look at our arguments in this subroutine definition. filename is declared as character(*). This is an assumed-length character string. It says that whatever the length of the string is that’s passed to the subroutine, this argument will accept and assume that length. This is useful when you want to pass character strings that are of either varying or unpredictable length. time, however, is declared as a character(:) allocatable array to match the declaration in the calling program. Finally, the arrays to hold the actual stock data are declared as real and allocatable.

Recall the intent attribute from section 3.2.1? Here, we’re using intent(in out) for all arrays, which means that they will be passed back and forth between the main program and the subroutine. Notice also that here we’ve matched the data type and allocatable attributes for the stock data with those declared in the main program. (See listing 5.2.)

In the next subsection, I give a detailed explanation of how explicit allocation and deallocation works and how we can implement it in our app.

Reading stock data from files

Leave a Reply

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

Scroll to top