Skip to content
Permalink
Browse files

Sync R scripts and js impl for better debugging.

Historical series of prices and returns are aligned.

It improves the debugging between R and js envs.
  • Loading branch information...
albertosantini committed Jun 27, 2018
1 parent e7816a9 commit 6bbfb185648ef7e828e0ee989c78c40b23702ef3
Showing with 23 additions and 8 deletions.
  1. +11 −3 examples/portfolio.R
  2. +6 −3 lib/portfolio.R
  3. +6 −2 lib/returns.js
@@ -16,16 +16,22 @@ hackZeroes <- function (x) {
x
}

getLogReturns <- function (symbol, start, end) {
getLogReturns <- function (symbol, start, end, skipPrices=-1) {
prices = try(get.hist.quote(symbol, start=start, end=end,
compression="w", quote="Close", quiet=TRUE))

prices <- tail(prices, skipPrices)
# print(prices)

if (class(prices) == "try-error") {
assetReturns = NULL
} else {
assetPrice <- hackZeroes(as.numeric(prices))
assetReturns <- diff(log(assetPrice[1:(length(assetPrice)-1)]))
}

# print(assetReturns)

assetReturns
}

@@ -41,13 +47,13 @@ getReturns <- function (symbol, refDate) {

start = paste(as.numeric(yyRef) - 2, mmRef, ddRef, sep="-")
end = paste(yyRef, mmRef, ddRef, sep="-")
retsBefore = getLogReturns(symbol, start, end)
retsBefore = getLogReturns(symbol, start, end, skipPrices=-1)

retsAfter = NULL
if (ddNow != ddRef || mmNow != mmRef || yyNow != yyRef) {
start = paste(yyRef, mmRef, ddRef, sep="-")
end = paste(yyNow, mmNow, ddNow, sep="-")
retsAfter = getLogReturns(symbol, start, end)
retsAfter = getLogReturns(symbol, start, end, skipPrices=-2)
}

list(beforeRefDate=retsBefore, afterRefDate=retsAfter)
@@ -93,6 +99,8 @@ getOptimalPortfolio <- function (jsonObj) {
}
}

# print(res)

return(toJSON(res))
}

@@ -16,9 +16,12 @@ hackZeroes <- function (x) {
x
}

getLogReturns <- function (symbol, start, end) {
getLogReturns <- function (symbol, start, end, skipPrices=-1) {
prices = try(get.hist.quote(symbol, start=start, end=end,
compression="w", quote="Close", quiet=TRUE))

prices <- tail(prices, skipPrices)

if (class(prices) == "try-error") {
assetReturns = NULL
} else {
@@ -41,13 +44,13 @@ getReturns <- function (symbol, refDate) {

start = paste(as.numeric(yyRef) - 2, mmRef, ddRef, sep="-")
end = paste(yyRef, mmRef, ddRef, sep="-")
retsBefore = getLogReturns(symbol, start, end)
retsBefore = getLogReturns(symbol, start, end, skipPrices=-1)

retsAfter = NULL
if (ddNow != ddRef || mmNow != mmRef || yyNow != yyRef) {
start = paste(yyRef, mmRef, ddRef, sep="-")
end = paste(yyNow, mmNow, ddNow, sep="-")
retsAfter = getLogReturns(symbol, start, end)
retsAfter = getLogReturns(symbol, start, end, skipPrices=-2)
}

list(beforeRefDate=retsBefore, afterRefDate=retsAfter)
@@ -6,7 +6,7 @@ const csv = require("./parse-csv");
function getLogReturns(x) {
const rets = [];

for (let i = 1; i < x.length; i += 1) {
for (let i = 1; i < x.length - 1; i += 1) {
rets[i - 1] = Math.log(x[i]) - Math.log(x[i - 1]);
}

@@ -17,12 +17,16 @@ function getClosePriceReturns(x) {
const closes = csv.parse(x, {
column: 4, // close prices
replaceZeroes: true, // fill zeroes with the previous value
reverse: true, // from oldest to newest
reverse: false, // from newest to oldest
skipHeader: true, // remove the header
skipNRecords: 1 // remove the latest week
}),
rets = getLogReturns(closes);

// 104,360 vs 105, 362
// console.warn(closes[0], closes[closes.length - 1]);
// console.warn(rets[0], rets[1], rets[rets.length - 2], rets[rets.length - 1]);

return rets;
}

0 comments on commit 6bbfb18

Please sign in to comment.
You can’t perform that action at this time.