On building an integrated QuantLib/Lua platform on the world's most popular computer.

Thursday, January 31, 2013

Running two historical simulations

Running two simulations of IBM starting from 1981 going through the 1987 crash.

The code for the first simulation:

local s=stock("ibm")

local p=currpf()

if s:price(-1)>s:price() and p/s<10 then

  _=p+10*s

end


if s:price(-1) <s:price() and p/s>0 then

  _=p-10*s

end

This code is run daily and does the following: Buy 10 IBM stock if yesterday's price is higher than today, otherwise sell if yesterday's price is lower. This is essentially bucking the trend. This is the blue line.

The second simulation does the opposite (this is the red line):

local s=stock("ibm")

local P=currpf()

if s:price()>s:price(-1) and P/s<10 then

  _=P+10*s

end


if s:price()<s:price(-1) and P/s>0 then

  _=P-10*s

end 

The results of the simulation are in the graph below:



A note on the code.

A stock object is created simply by saying 'stock(symbolstring)' e.g. 'stock("ibm")'. Adding or subtracting assets to a portfolio is done by a regular +, so the line

       _=P+10*s

simply adds ten shares of s to the portfolio P.

Dividing a portfolio by a stock give a count of how many shares are in that portfolio.
P/s is the count of the number of share in P.




Latest screenshot:


Sunday, January 27, 2013

Sunday, January 23, 2011

Reading Yahoo/Fed data directly

I can now use Lua to read in Federal-H15 data and Yahoo stock data. directly into QuantLib/Lua. I just wish I could read bond data from somewhere :-(

Thursday, January 6, 2011

Pricing a bond using QuantLib/Lua

Here is an example of pricing a bond using QuantLib/Lua on the iPhone

--Simple bond pricing
--http://tgwena.blogspot.com
ql=QuantLib
Date=ql.Date


local sDD=Date(17,5,1998)
local eDD=Date(17,5,2001)

local r=0.04 --interest rate
local c=0.06 --coupon

ql.Settings_instance():setEvaluationDate(sDD)


sched=ql.Schedule(sDD,eDD,ql.Period(2),ql.NullCalendar(),ql.Unadjusted,ql.Unadjusted,0,false)
ufixQ=ql.SimpleQuote(r)
ufixH=ql.QuoteHandle(ufixQ)
curve=ql.FlatForward(0,ql.NullCalendar(),ufixH,ql.SimpleDayCounter())
flatTS=ql.YieldTermStructureHandle(curve)
bondEngine=ql.DiscountingBondEngine(flatTS)


bond=ql.FixedRateBond(0,100,sched,ql.DoubleVector(1,c),ql.SimpleDayCounter(),ql.Unadjusted, 100, sDD)

bond:setPricingEngine(bondEngine)

print(bond:dirtyPrice())

Followers