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.
No comments:
Post a Comment