Skip to content

Commit

Permalink
Merge branch 'master' of github.com:effinggames/financial-chart
Browse files Browse the repository at this point in the history
  • Loading branch information
robgraeber committed Jun 28, 2016
2 parents 540cb63 + 4f93668 commit de87085
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 16 deletions.
9 changes: 7 additions & 2 deletions app/assets/scripts/AllocationChart.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
useHTML: true,
text: `
<a class="chartSubtitle"
href="https://philosophicaleconomics.wordpress.com/2013/12/20/the-single-greatest-predictor-of-future-stock-market-returns/"
href="http://www.philosophicaleconomics.com/2013/12/the-single-greatest-predictor-of-future-stock-market-returns/"
target="_blank">
Source: Philosophical Economics
</a>
Expand Down Expand Up @@ -131,6 +131,11 @@
valueSuffix: '%'
},
lineWidth: 2.5,
},
spline: {
marker: {
enabled: false
}
}
},
series: [
Expand All @@ -152,4 +157,4 @@
}
};
});
})();
})();
10 changes: 9 additions & 1 deletion app/assets/styles/main.styl
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,21 @@ a.chartSubtitle {
font-size: 1.45em;
height: 1.2em;
padding: .5em 1em;
margin-bottom: 0.15em;
vertical-align: middle;

&.correlation {
line-height: 0.8em;
}
}
.links {
margin-top: 1.3em;
font-size: 1.1em;
margin-top: 1.3em;
a + a {
margin-left: .7em;
}
}
.disclaimer {
font-size: 0.9em;
margin-top: 0.7em;
}
53 changes: 40 additions & 13 deletions app/controllers/MainController.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@
const Knex = require('../DatabaseHelper').knex;
const Promise = require('bluebird');
const Regression = require('regression');
const Moment = require('moment-timezone');

class MainController {
getHomePage(req, res) {
//query for allocation + spx 10 year return data
const query1 = Knex.raw(`
SELECT b.date, b.percentage, a.return_10
SELECT b.date, b.percentage::FLOAT, a.return_10
FROM analysis.sp_500_annualized_return a, usa.stock_asset_allocation b
WHERE (date_trunc('month', a.date + INTERVAL '1 day') = date_trunc('month', b.date))
WHERE (date_trunc('month', a.date + INTERVAL '1 month') = date_trunc('month', b.date))
AND b.percentage IS NOT NULL
`).then(rsp => rsp.rows);

Expand All @@ -23,15 +24,18 @@ class MainController {
rows1.forEach(i => i.date = i.date.toISOString().slice(0, 10));

//calculate linear regression
const regressionData = rows1.filter(i => !!i.return_10).map(i => [parseFloat(i.percentage), i.return_10]);
const regressionData = rows1.filter(i => !!i.return_10).map(i => [i.percentage, i.return_10]);
const regressionResults = Regression('linear', regressionData);
const latestAllocation = rows1[rows1.length - 1].percentage;
const expectedReturns = regressionResults.equation[0] * latestAllocation + regressionResults.equation[1];

const lastUpdatedDate = Moment(new Date(rows1[rows1.length - 1].date)).tz('GMT').format('M/D/YYYY');

res.render('usa-allocation', {
title: 'Stock Asset Allocation vs SPX 10-Year Return',
correlationSquared: Math.pow(rows2[0].return_10, 2),
expectedReturns,
lastUpdatedDate,
data: {
chartData: rows1
}
Expand All @@ -42,9 +46,9 @@ class MainController {
getEuropeAllocationChart(req, res) {
//query for allocation + spx 10 year return data
const query1 = Knex.raw(`
SELECT b.date, b.percentage, a.return_10
SELECT b.date, b.percentage::FLOAT, a.return_10
FROM analysis.eafe_annualized_return a, europe.stock_asset_allocation b
WHERE (date_trunc('month', a.date + INTERVAL '1 day') = date_trunc('month', b.date))
WHERE (date_trunc('month', a.date + INTERVAL '1 month') = date_trunc('month', b.date))
AND b.percentage IS NOT NULL
`).then(rsp => rsp.rows);

Expand All @@ -58,18 +62,41 @@ class MainController {
rows1.forEach(i => i.date = i.date.toISOString().slice(0, 10));

//calculate linear regression
const regressionData = rows1.filter(i => !!i.return_10).map(i => [parseFloat(i.percentage), i.return_10]);
const regressionData = rows1.filter(i => !!i.return_10).map(i => [i.percentage, i.return_10]);
const regressionResults = Regression('linear', regressionData);
const latestAllocation = rows1[rows1.length - 1].percentage;
const expectedReturns = regressionResults.equation[0] * latestAllocation + regressionResults.equation[1];
const lastUpdatedDate = Moment(new Date(rows1[rows1.length - 1].date)).tz('GMT').format('M/D/YYYY');

res.render('europe-allocation', {
title: 'Stock Asset Allocation vs EAFE 10-Year Return',
correlationSquared: Math.pow(rows2[0].return_10, 2),
expectedReturns,
data: {
chartData: rows1
}
//gets the latest EAFE value
const latestEAFEQuery = Knex('europe.eafe_daily')
.orderBy('date', 'desc')
.limit(1);

//gets the EAFE value for last allocation update
const latestAllocationEAFEQuery = Knex('europe.eafe_daily')
.orderBy('date', 'desc')
.whereRaw(`date <= '${Moment(new Date(rows1[rows1.length - 1].date)).tz('GMT').format('YYYY-MM-DD')}'`)
.limit(1);

Promise.all([latestEAFEQuery, latestAllocationEAFEQuery]).spread((latestEAFERows, latestAllocationEAFERows) => {
const latestEAFEValue = parseFloat(latestEAFERows[0].value);
const latestAllocationEAFEValue = parseFloat(latestAllocationEAFERows[0].value);
const additionalReturns = Math.pow(latestAllocationEAFEValue / latestEAFEValue, 0.1) - 1;
const extrapolatedReturns = expectedReturns + additionalReturns;
const lastExtrapolatedDate = Moment(new Date(latestEAFERows[0].date)).tz('GMT').format('M/D/YYYY');

res.render('europe-allocation', {
title: 'Stock Asset Allocation vs EAFE 10-Year Return',
correlationSquared: Math.pow(rows2[0].return_10, 2),
expectedReturns,
extrapolatedReturns,
lastUpdatedDate,
lastExtrapolatedDate,
data: {
chartData: rows1
}
});
});
});
}
Expand Down
10 changes: 10 additions & 0 deletions app/views/europe-allocation.html
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,15 @@ <h1>Stock Asset Allocation vs Annualized 10-year EAFE Return</h1>
<span class="infoText">
Expected 10-Year EAFE Returns: [[ expectedReturns | round(4) * 100 ]]%
</span>
<span class="infoText">
Last Updated: [[ lastUpdatedDate ]]
</span>
<span class="infoText">
Extrapolated 10-Year EAFE Returns: [[ extrapolatedReturns | round(4) * 100 ]]%*
</span>
<span class="infoText">
Last Updated: [[ lastExtrapolatedDate ]]
</span>
</div>
<div class="disclaimer">* Based on the current price of the EAFE index</div>
{% endblock %}
3 changes: 3 additions & 0 deletions app/views/usa-allocation.html
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,8 @@ <h1>Stock Asset Allocation vs Annualized 10-year SPX Return</h1>
<span class="infoText">
Expected 10-Year SPX Returns: [[ expectedReturns | round(4) * 100 ]]%
</span>
<span class="infoText">
Last Updated: [[ lastUpdatedDate ]]
</span>
</div>
{% endblock %}
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
"gulp-uglify": "^1.1.0",
"gulp-webpack": "^0.3.0",
"knex": "^0.8.6",
"moment": "^2.13.0",
"moment-timezone": "^0.5.4",
"named-router": "^1.0.3",
"nib": "^1.1.0",
"nunjucks": "^1.3.4",
Expand Down

0 comments on commit de87085

Please sign in to comment.