1. Computing & Technology

Discuss in my forum

How the C code Bot Works

A Technical Look

By , About.com Guide

These are examples of the input strings
 Prices "1.2|-2|3|4|5|-66.89|7||9|10|11.8||||15|";
 CompanyShares "+1|2|3||5|-666|7||9|10|11|12|13||15|";
 Feed "A+++|B--|C+|D-|E---|F++|";
 Companies "ABC|BF|R|";
 CompanyShares "180|23456|0|56787800|456789|";
 
I've defined a couple of constants MAXCOMPANIES and MAXTIPS to keep the code looking neat. Also two structs, one for each company and one for share tips. these are stored in arrays allcompanies and tips.

My preference but I've defined typedefs to keep the code clean looking.

  • psvstring (short for Pipe seperated values) - a char * containing the iinput (and output) strings.
  • dvalues - a 100 doubles array with pvalues a pointer to it.
  • ivalues. - 100 int array with pivalues a pointer to it.

    Parsing PSV Strings

    Because the different type of values (ints, doubles, strings), I wrote a common parser plus a function ptr which dealt with each value parsed out. The ParseStr( function is called and passed in a psvstring, a function to call and a null pointer ((void *) for the return values.

    This is the function pointer signature

     typedef void (*fn)(int,char *,void *) ; 
    All of the parse functions must match this signature.
     /* Callback function to parse an int */
     void doParseInt(int index,char * value,pivalues retval) {
     (*retval)[index] = atoi(value) ;
     }
     
     
    This converts the passed in value into an int using atoi then stores it in the retval array which is a pointer to an array of ints. the doParseDouble does the same for double values (the Prices arrays). doParseCompanies does the same for company names. As these don't change from minute to minute, this is only parsed once in minute 1 in the InitData function.

    The GetTrades function is where the action takes place. it begins by calling InitData once (on the first minute) then copies cash to a local variable. it then parses the Position into the local array _SharesHeld. Notice the use of & on these parameters. _SharesHeld is of type ivalues (not pivalues) so to treat a variable as a pointer, use & to get the address of it.

    Next the _Tips array is initialized and the Feed parsed into it. The doParseFeed needs both a sector letter (as defined in the Sectors const char array. It has to parse B+++ into B,3 and A-- into A,-2. Next DebugBuffer and Trades return strings are set to a null string and the SetCanBuy() function is called. This looks through all 100 companies, checking if any shares are no longer available to buy.

    This bot doesn't like to hold a position so in the last minute, it issues an order to sell all shares otherwise it calls DoTrade(). this first checks to see if it's worth selling any shares (if the original bought price is 3/4 of the current price). If it sees bad press (from the feed) it sells half.

    Otherwise if there are shars and good press, it works out how miuch it can spend and generates a buy order.

    Finally the GenerateOrders outputs the Buy/sell orders.

©2012 About.com. All rights reserved.

A part of The New York Times Company.