Downloading Financial Data

QuantLua currently can access historical stock data from and historical currency data from

Stock Data 

The command to download stock data is getprices. It takes one to three parameters: the stock symbol, an optional start date and an optional end date. If the start date is missing it is assumed to be January 1, 2011. If the end date is missing it is assumed to be today.

The commands

    local sd={2013,1,1}

    local ed={2013,12,31)

    p=getprices(“IBM”, sd, ed)

return the stock prices from January 1, 2013 to December 31 (or the current date if the end date is in the future). The data returned is a table. Let’s continue with the example above. Within this table is the the table values, This is a list of the adjusted stock prices over the time range. Another table is dates. This is the list of dates for the stock prices in the values table. Other components of p are:

  1. name : the name of the company
  2. open : the list of opening prices
  3. close : the list of closing prices
  4. high : the list of high prices
  5. low : the list of low prices
  6. vol : the volume traded that day.

Continuing the example, we can plot this data:

    plot({y=p.values, x=p.dates, dates=1}) 

We can do more with this data since it’s just a list of numbers. For example we can find the maximum and minimum.


return the maximum and minimum values.

Currency data

The command to download currency data is getcurrency. It takes 2 to 4 parameters. The first is the base currency, the second is the currency converted to, The third and fourth are optional start date and optional end date. If the start date is missing it is assumed to be January 1, 2011. If the end date is missing it is assumed to be today.

The commands

    local sd={2013,1,1}

    local ed={2013,12,31)

    p=getcurrency(“USD”, “JPY”, sd, ed)

return a table with the following: dates is a table containing dates and values is a table containing the currency values corresponding to the dates.

In both stocks and currency data downloading the date can be entered in three different but compatible ways.

The first way is as above, a table of the form {year, month, day}. The second way is as a ql.Date object. The third way is as a number corresponding to an Excel date.


The QuantLua Plot command

One of the most useful commands is plot. With this command you can output virtually any graph, including bar graphs and pie charts. 


For those not well versed in Lua here is a crash course in the language.


In its simplest form plot takes a list of numbers and makes a graph with the x-axis from 0 to 1. So the script


produces the following output:


This is interesting but will not take us too far.

A more general plot command takes a list which contains two parameters. An example is:

      plot({x={2,3,5,7,11}, y={1,2,4,-3,2}})

The x parameter takes points on the horizontal axis and the y parameter takes points on the vertical axis. The output here is




This plot command allows the x-axis numbers to not be monotonic, so a command like plot({x={0,1,1,0},y={0,0,1,1}}) will happily draw a box.


At this stage we draw lots of different types of graphs, but we are still using default parameter. In this app we  can control nearly every aspect of a graph. The first thing we’ll learn to change is the color. In the table we can add a parameter c with one integer value ranging from 1 to 14. So plot({x={0,1,1,0},y={0,0,1,1}, c=2}) displays the above plot in green.

The full list of colors is

  1. red
  2. green
  3. blue
  4. cyan
  5. yellow
  6. white
  7. light gray
  8. gray
  9. dark gray
  10. black
  11. magenta
  12. orange
  13. purple
  14. brown

Another aspect we can control is the line width with the parameter w. By default w is 1. So adding w=2 will produce a line twice as thick. Setting w to 0 completely hides the line, this is useful in constructing scatterplots.

The next useful item to change is the plot marker. Its size is controlled by the parameter r. By default r is set to 1. The following plot hides shows the effect of r (and hiding the plot line).

      plot( {x={2,3,5,7,11,13}, y={1,2,4,-3,2,-4},r=8,w=0} )

Its output is


Another aspect we are able to control is the shape of the marker. This is via the parameter s. By default s=1, the ellipse. The full list of shapes available are:

  1. ellipse
  2. triangle
  3. rectangle
  4. pentagon
  5. hexagon
  6. diamond
  7. star
  8. snowflake
  9. dash
  10. plus
  11. cross

Also, the parameter ss controls the ratio of horizontal vs vertical size of the marker. Normally ss=1, making the ellipse look like a circle and the rectangle a square. Changing this parameter allows for more shapes. The parameter sc controls the color of the marker, which by default is the same color as the lines in the graph. sb controls the thickness of the border on the marker, by default it is 0.5.


So far we have done only one plot. Multiple plots on the same axes are a very  easy extension of the single plot case. The plot command can take arbitrarily many parameters, where each is a list as described above. Each of these will be added to the same graph. Let’s make one now.

      plot(  {x={1,2,3,4,5},y={3,2,1,2,-3},r=5,sc=3}, {x={2,4,6,8},y={1,0,-1,-2},s=3,sc=5,r=5} )

Here is the output:


QuantLua is smart enough to select different colors for different plots, but we can override this.


So far we have been plotting lists of numbers, so let’s get a useful list of numbers to plot. The QuantLua function getprices(“goog”) will return google’s stock prices from 2011-01-01 by default. (We’ll cover getprices and its friends in another posting)


returns a table with several parameters. The parameter d.values is the list of stock prices and d.dates is the list of the corresponding dates in numeric form. We want to plot this date so let’s try the following script:



This almost works except that QuantLua has no way of know that the x-axis is dates, not numbers. To fix this we include the parameter dates=1. The second line now looks like this:

plot({x=d.dates,y=d.values, dates=1})

The resulting graph is:




So far we have been plotting list, which are by far the most common. We can also plot functions very easily. Suppose we have a one variable function defined as

     function g(x)

           return 2*math.sin(x)+x


This function can be plotted with the command

      plot({f=g, x={-16,16}})

giving the following plot:



In conclusion, here is the summary about plot.

It takes three forms:

  1. plot(lst) most basic plot, very little control
  2. plot({x=lst1,y=lst2 [,optional parameters]}) this is the most general plot allowing for fine control of the parameters
  3. plot({f=function,x=range [,optional parameters]}) enable plotting functions.

Several of these plots can be put within one plot statement, enabling multiple graphs on one set of axes.

The optional parameters are

  1. c  determines color of plot line, default varies.
  2. w determines line thickness, default is 1.
  3. r determines size of marker, default is 0.5.
  4. s determines marker shape, default is ellipse.
  5. ss marker shape scale, default is 1.
  6. sc marker color, default is c.
  7. sb marker’s black boundary, default is c.
  8. p used in function plots, numper of points selected in the domain, default 100.
  9. dates, determines if x-axis values are date or numbers, default 0.
  10. axis, determines if axes are drawn or not, default 1.
  11. n, plot legend name, default “”
  12. title, plot title, default “”
  13. xtitle, label on x-axis.
  14. ytitle, label on y-axis.

QuantLua now approved

Apple has approved QuantLua. You can buy it from here: View in App Store 


QuantLua is the most comprehensive financial application available for the iPhone. It has these main features:

-A programmable historical stock simulator for select US stocks

-The most comprehensive finance calculator available, incorporating the QuantLib quantitative finance library and the programming language Lua.

-Excellent graphics library for building your own plots. It includes line graphs, bar graphs and pie charts.

The historical stock market simulator is for select US stocks. It enables you to build a portfolio and see how the strategy would have performed over time. It is also fully programmable, enabling the analysis of dynamic strategies.

Also included is a historical stock and currency data feed. This imports stock data from Yahoo Finance and currency data from OANDA directly into the Lua programming engine.

All this is powered by Lua, an intuitive and complete programming language well suited to financial analysis.