**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:

**name**: the name of the company**open**: the list of opening prices**close**: the list of closing prices**high**: the list of high prices**low**: the list of low prices**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.

**print(getmaxmin(p.values))**

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.

]]>

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

** plot({3,1,2,4,0})**

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

- red
- green
- blue
- cyan
- yellow
- white
- light gray
- gray
- dark gray
- black
- magenta
- orange
- purple
- 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:

- ellipse
- triangle
- rectangle
- pentagon
- hexagon
- diamond
- star
- snowflake
- dash
- plus
- 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)

** d=getprices(“goog”)**

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:

** d=getprices(“goog”)**

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

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**

** end**

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:

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

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

The optional parameters are

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

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.

]]>