Algorithmic Trading

Lev Peshkov

Lev Peshkov

High T
Joined
Apr 5, 2020
Posts
26,943
Reputation
41,663
heres some code you can feel free to play with to make it better


require(quantmod)

symbol = "^GSPC"

start = "2001-01-01"
end = "2011-12-31"

prices = getSymbols(symbol, from = start, to = end, auto.assign = FALSE)

fastLen = 15
slowLen = 300

close = prices[,6]

fastMA = SMA(close, n = fastLen)
slowMA = SMA(close, n = slowLen)

sig = 0;
sig = ifelse(fastMA > slowMA, 1, -1)
sig = lag(sig, 1)
sig[is.na(sig)] = 0

ret = ROC(close, type="discrete")
ret[1] = 0

eq = cumprod(1 + ret * sig)

lineChart(prices)
addSMA(fastLen, col = "blue")
addSMA(slowLen, col = "red")
addTA(eq, col = "cyan")
 
6181 1
 
  • JFL
Reactions: Lev Peshkov

Users who are viewing this thread

Back
Top