Base 16  
 Base 16 Corporation May 11, 2008  
Home
Technical Guide
System
Versus Internet
Evaluate
Code Examples
License
Release History
Contact
Yahoo Group



Requires Subscription to
DTN RealTime
 
Code Examples

Real-Time Stock Scans with Visual Basic

If you're new to Visual Basic, here's an online tutorial that will help you learn your way around in Visual Basic 6.0.

This example shows how to use Visual Basic 6.0 to scan the TRAX shared memory for stocks in real-time. This example scans for Top Volume Leaders - stocks trading the highest volume today. The scan filters out stocks trading below $2 and those that have traded less than 100,000 shares today.

Download source code for this example. Here's a screen shot:



Accessing real-time data in the TRAX shared memory areas is one of the primary benefits in using TRAX. TRAX automatically updates the shared memory area where your application programs can access the data instantly. The data records are arranged in a simple memory array structure.

Once you learn the basics of accessing the shared memory, you'll be able write some very powerful real-time scans.

Initialization

Let's start with the Form_Load subroutine. This routine is called when the main form of the example program first loads. We perform some initialization steps here to gain access to the TRAX shared memory and setup the decimal multiplier table.

Gaining access to a shared memory file takes a few simple Windows API calls. The Windows API functions are declared in the example project's ApiStuff.bas file. The Windows API calls that we make use of are CreateFile, CreateFileMapping, MapViewOfFile, UnmapViewOfFile, CloseHandle and CopyMemory. For more details on using Windows API calls from Visual Basic see this page.

Here's the complete Form_Load subroutine:

Private Sub Form_Load()
Dim lpsec As SECURITY_ATTRIBUTES
Dim FileName As String

FileName = "C:\Program Files\Trax\memory\stock"

' Open shared memory file
hFile = CreateFile(FileName, GENERIC_READ, FILE_SHARE_READ Or FILE_SHARE_WRITE Or_
FILE_SHARE_DELETE, lpsec, OPEN_EXISTING, FILE_ATTRIBUTE_READONLY, 0)
If hFile <> INVALID_HANDLE_VALUE Then
  ' Create the file mapping
   hMap = CreateFileMapping(hFile, lpsec, PAGE_READONLY, 0, 0, 0)
   If hMap <> 0 Then
     ' Get pointer to memory
     SharedMem = MapViewOfFile(hMap, FILE_MAP_READ, 0, 0, 0)
   End If
End If


If SharedMem = 0 Then
   lblStatus = "Could not open shared memory file " & FileName
End If

DecimalMultiplier(0) = 1#
DecimalMultiplier(1) = 0.1
DecimalMultiplier(2) = 0.01
DecimalMultiplier(3) = 0.001
DecimalMultiplier(4) = 0.0001
DecimalMultiplier(5) = 0.00001
DecimalMultiplier(6) = 0.000001
DecimalMultiplier(7) = 0.0000001
DecimalMultiplier(8) = 0.00000001

End Sub

Decimal fields in TRAX shared memory records are actually of type Long (4 byte values) that must be multiplied by a decimal multiplier to arrive at the actual decimal value. For instance the LastTradePrice field in the shared memory may have the value 245000 and a DecimalCode of 4. This means that we must multiply the value 245000 by 0.0001 to arrive at $24.50.

The Decimal Multiplier table is an array that makes translating the Decimal fields of the shared memory records very easy. We declare the table as a global array in the example program making it easy to access from any routine in the module:

Dim DecimalMultiplier(8) As Double

The code in the Form_Load subroutine above simply initializes the DecimalMultiplier array for use later in the program.


Real-Time Scanning

Now we're ready to do a scan. In the example we have designed a form with a button to start a scan. When this button is pressed, the routine btnScan_Click will be called.

This routine will start at the beginning of the TRAX shared memory record array, and examine data from each record. If the data is within the bounds of the scan parameters, it will save a pointer to the shared memory record and the Volume in a separate array. This array will be used to sort the records from highest to lowest and put the top 20 stocks into a ListView control for display.

The scanning loop of the btnScan_Click subroutine is shown here:

' Store the volume from each record in an array to be sorted
Do While ListCount < MaxCount
   ' Copy shared memory record to structure
   CopyMemory Record, ByVal MemPointer, RecordLen

   If (Record.Symbol(0) <> 0) Then
     ' Save Volume and pointer to memory record
     Multiplier = DecimalMultiplier(Record.DecimalCode)
     Value = Record.LastTradePrice * Multiplier
     If Value > 2# And Record.Volume > 100000 Then
       RecordArray(ListCount).Value = Record.Volume
       RecordArray(ListCount).Index = MemPointer
       ListCount = ListCount + 1
     End If
   Else
     Exit Do ' end of list when symbol is empty
   End If

   MemPointer = MemPointer + RecordLen
   ScanCount = ScanCount + 1
Loop

Each record of TRAX shared memory is copied into a local record structure using the CopyMemory Windows API function so that the record fields can be easily accessed by Visual Basic.

The loop starts at the beginning of TRAX shared memory and checks for a record where the Symbol field is zero indicating that the end of table has been reached. As long as the end hasn't been reached, the routine gets the Multiplier value from the table and multiplies the LastTradePrice field to obtain the last traded price.

If the price is greater than $2 and the volume is greater than 100,000 shares, the Volume and the pointer to the TRAX shared memory record is saved for later. Caching the pointer to the shared memory record will allow us to access the data once again when populating the ListView control after the result records are sorted.

The rest of the routine (not shown) sorts the results and populates the ListView control with the top 20 volume stocks.

Using this simple example, you can develop your own specialized scans of the 86 available real time TRAX shared memory record fields.



PHP hosting by Modwest PHP