Simulator Functions


aloans() returns table of loans in the current portfolio
assets() returns list of all assets in the curent portfolio

Given an asset A the following methods can be called
A:price() returns current price
A:
astocks() returns list of stocks in the current portfolio
bins(table,n,[a,b])

Takes a list of numbers T and and positive integer n. returns a table with the bin count from min to max of T (or a to b if given).
borrow(n[,m])

Borrow amount n for m days (default 30 days).
clear()

clears the bottom output text.
cumulativesum(T)

Given a table of numbers T, returns a table of the cumulative sums.
currpf()

Returns the current portfolio. If the simulation is running this is the location of the script, otherwise it is the portfolio visible in the "Portfolio" tab.
displayplot()

Displays the plots. This function is not usually needed.
flatten(T)

Given table T containing possibly other tables of numbers, returns a list of numbers with no subtable structures.
getcurrency(cur1,cur2,date1[,date2])

Get historical currency rates cur1/cur2 starting from date1 and going to today or date2.

The return value is a table containing two subtables, dates and values.
getmaxmin(L)

Returns the max and min of L, a list of numbers
getprices(t,sd,ed)

Get prices for stock symbol. t is the yahoo stock ticker, sd,ed are start date and end date. If ed is not givven it is assumed to be today. The date formats are either {YYYY,MM,DD}, or an Excel serial number, or a ql.Date object.

The return value is a table containing tables values, dates, name, open, close, high, low, vol.
gindex(i)

market index on date i. if no i is supplied it provides index for the current simulator date.
gindexes(s,e)

Returns market data on date range s to e. The return value is a table T with two subtables: T.values and T.dates.
grate(d)

the current simulator interest rate -- need to fix to look back
gtday()

returns simulation date as a number.
gvars()

returns list of global variables.
liststart(T)

Given input list T, returns a list rescaled to start with 1.
lregress(T,V)

Returns linear regression of two lists.
lrsum(L)

The sum of elements in L
lrdot(L1,L2)

The dot product of lists L1 and L2.
mmap1(f,L)

Apply f to each element in L.
mmapn(f,L1,L2,...,Ln)

Apply f (a function taking n parameters) to the tables componentwise....
plot(...)

The plot function has several formats for input.

In its simplest form it takes a table of numeric values and produces a graph with domain [0,1].

More generally, plot can take a table of the form {y=YTABLE,x=XTABLE}, i.e. plot({y={3,4,1,2},x={1,2,3,5}})). Other options permitted in plot are w=1 (line width), r=0.5 (radius of plot point), n="" (plot name), axis=1 (axis=0 hides axes), dates=0 (if dates=1 then interpret x values as dates).
plotbar()

Bar plot.
plotf()

Function plot.
plotpie()

Pie chart
Portfolio operations

Given a portfolio P, there are methods which can be called

P:cash(d) returns amount of cash on date d (defaults to gtday())
P:name() returns name of P
P:rename(s) rename P to name s
P:uparent() returns the ultimate parent
P:sendcash(Q,n) sends amount of n to portfolio Q
P:tcash() returns cash in P and its subportfolios
P:tcashes(sd,ed) returns cash positions from sd to ed.
P:price(d) returns the price at date d(defaults to gtday() if missing).
P:prices(sd,ed) returns list of portfolio values from sd to ed
P:parent() returns parent portfolio
P:newpf(s) creates new subportfolio with name s
P:getpf(s) returns subportfolio with name s
P:delpf(s) deletes portfolio with name s. Moves contents to parent
P+n borrows n>0 dollars into P
P+S adds stock object (defined below) to P.
P/S returns count of how many stock objects are in P

printBox()
range()
run(string) runs the script named string in the current portfolio

run(table) follows a path based on table. The numbers in that table mean go up the directory structure.
running() true if simulation is running.
sendcash(P,Q,n) send amount n from P to Q.
sendcash(Q,n) send amount n from currpf to Q.
sleep(n) sleep for n seconds
stock(s) returns a stock object for the symbol s, a tradeable stock in the simulator.

Arithmetic can be performed with fstock symbols, addition, subtraction, multiplication with integer (they form a Z-module). These operations return a stock object S.

For a stock object S the following methonds exist
S:name() returns list of stock names for items in S
S:price(d) returns the value of S on date d
S:prices(sd,ed) returns list of prices from start date sd to end dated ed
A stock object can be added to a portfolio as P+S. This will add all the objects in S to the portfolio P. This creates an elegant way to trade items in a portfolio:
currpf()+5*stock("ibm") buys 5 shares of ibm.


stop()

Stop the simulator. Used within scripts attached to portfolios. Attaching a script with just the command "stop()" will enable single day stepping over time.
todaysdate() today's date as a table.
transpose()
tscale() linearly rescale table to go into range 0 to 1.
zip()