Category Archives: lesson

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.