Tutorial 08: Using TCP/IP to Obtain Stock Quotes

April 19, 2008

© NS BASIC Corporation. All rights reserved.

Contributed by Tom Newman, Mission Peak Designs


Contents:

  1. Introduction
  2. How is it Done?
  3. Creating the Form
  4. Adding the Code
  5. Testing the Program

Introduction

The purpose of this tutorial is to demonstrate adding networking functionality to your NS Basic/Desktop programs. You should have completed Tutorial #1 before beginning this tutorial.

The program to be developed uses the NewObjects ActiveX Net Stream control to open a socket and obtain stock quotes from Yahoo.com. The NewObjects Storage and File control is used to send and received data from the Yahoo server.

How is it Done?

Including network support in your application requires adding the NewObjects ActiveX control to the desktop. The control is automatically installed when you installed NS Basic/Desktop but you will need to install it on systems where NS Basic/Desktop is not installed.

(For a complete reference on the methods above, make sure you read the Tech Note 06: Communications - TCP/IP and IRDA and Tech Note 08: File I/O Control - SFStream.)

Working with TCP/IP protocols is similar to reading/writing files from the file system. There are basically three steps used to talk to a server:

  1. First you have to open it (socket),
  2. Then you send commands and read data to/from it,
  3. and finally you close it (socket).

Because you are dealing with a remote system and not a local file, you have a "timeout" feature. It simply means that if your application does not hear from the remote machine for a specified period of time it will give up.

You must also specify to which service you want to connect in that remote machine, and that's the concept behind "port". Different ports provides different services, for instance,

Creating the Form

Start a new "Standard" project and save it as HttpStocks.nsd

Create the following controls on the form:

The form should look like the following:

Adding the Code

In this project we will simply connect to the Yahoo server and send a request to return the latest information on the stock symbol entered (Apple is the default case).

Enter the following code in the Code Window (you can copy and paste this code):

Now let's analyze the code, line by line:

Line number 34 deserves a little explanation:

    conn.WriteText = "GET /d/quotes.csv?f=sd1t1l1c1&e=.cvs&s=" & Symbol & vbCrLf

That message will cause the host (Yahoo) to give the symbol, the date, the time of last trade, the latest price, and the change in dollars from the opening price for Apple Computer. These are respectively coded with s, d1, t1, l1 and c1. (I'm not sure where Yahoo officially publishes these codes but this article from the Motley Fool shows many of them: http://boards.fool.com/Message.asp?mid=18825035)

Testing the Program

Press F5 to save and start the program. Clicking on Fetch should show something similar to the image below. The program defaults to return the Apple stock price (AAPL) but you can enter your own stock symbol in the upper text box.