Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 47 additions & 0 deletions app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
var app = angular.module("app", ['ui.router']);

app.config(function($stateProvider, $urlRouterProvider){
$urlRouterProvider.otherwise('/index/portfolio');

$stateProvider.state('index', {
url: '/index',
views: {
'index': {
templateUrl: 'js/templates/index.html',
controller: 'MainCtrl'
},
'historical': {
templateUrl: 'js/templates/historical.html',
controller: 'historyCtrl'
}
}
})
.state("index.portfolio", {
url: '/portfolio',
views: {
'display@': {
templateUrl: "js/templates/portfolio.html",
controller: "portfolioCtrl"
}
}
})
.state("index.trade", {
url: '/trade/:symbol',
views: {
'display@': {
templateUrl: "js/templates/trade.html",
controller: "tradeCtrl"
}
}
})

.state("index.transaction", {
url: '/transaction',
views: {
'display@': {
templateUrl: "js/templates/transactions.html",
controller: "transactionCtrl"
}
}
})
})
58 changes: 58 additions & 0 deletions index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Fideliguard</title>

<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">



<script src="https://code.jquery.com/jquery-1.11.3.min.js"></script>
<script src="https://code.jquery.com/ui/1.11.4/jquery-ui.min.js"></script>

<!-- Latest compiled and minified JavaScript -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>

<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.5/angular.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular-ui-router/0.2.15/angular-ui-router.min.js"></script>
</head>
<body ng-app="app">
<div class="container">
<div class="row">
<div ui-view="index"></div>
</div>
<div class="row">
<div class="col-md-5">
<div ui-view="historical" class="row"></div>
</div>

<div class="col-md-7">
<div ui-view="display"></div>
</div>
</div>
</div>

<!-- main module -->
<script src="app.js"></script>

<!-- controllers -->
<script src="js/controllers/mainController.js"></script>
<script src="js/controllers/historyController.js"></script>
<script src="js/controllers/tradeController.js"></script>
<script src="js/controllers/portfolioController.js"></script>
<script src="js/controllers/transactionController.js"></script>

<!-- directives -->
<script src="js/directives/stateDropdown.js"></script>

<!-- filters -->
<script src="js/filters/dateFilter.js"></script>
<script src="js/filters/tableFilter.js"></script>

<!-- services -->
<script src="js/services/historicalStock.js"></script>
<script src="js/services/selectedDate.js"></script>
<script src="js/services/transactionService.js"></script>
</body>
</html>
70 changes: 70 additions & 0 deletions js/controllers/historyController.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
app.controller("historyCtrl", ['$scope', '$filter', 'historicalStock', 'selectedDate', function($scope, $filter, historicalStock, selectedDate){
$scope.stockData = historicalStock.getStockData();

$scope.sortCriteria = "";
$scope.reverse = false;

$scope.sortBy = function(criteria){
var columns = {
1: "Symbol",
2: "Close",
3: $scope.getYesterday,
4: $scope.getLastMonth,
5: $scope.getLast6Month,
}
if (!columns[criteria]) return;

if (columns[criteria] == $scope.sortCriteria){
$scope.reverse = !$scope.reverse;
} else {
$scope.sortCriteria = columns[criteria];
}
}



$scope.symbols = function() {
return $scope.stockData.reduce(function(result, current){
if (result.indexOf(current.Symbol) == -1) {
result.push(current.Symbol);
}
return result;
}, [])
}

$scope.selectedDate = selectedDate.getDate;

$scope.getLast = function(offset, entry){
var currentClosing = entry.Close;
var startingDate = new Date(entry.Date);
var targetDays = [];
targetDays.push(startingDate.setDate(startingDate.getDate() - offset))
targetDays.push(startingDate.setDate(startingDate.getDate() - 1))
targetDays.push(startingDate.setDate(startingDate.getDate() - 1))
targetDays.push(startingDate.setDate(startingDate.getDate() - 1))
targetDays.push(startingDate.setDate(startingDate.getDate() - 1))

var filteredData = $scope.stockData.filter(function(el){ return el.Symbol == entry.Symbol })
var results = [];
targetDays.forEach(function(day) {
var result = $filter("dateFilter")(filteredData, day);
if (result.length > 0 && results.length == 0) {
results = result;
}
});

return (results.length < 1) ? "N/A" : currentClosing - results[0].Close
}

$scope.getYesterday = function(entry){
return $scope.getLast(0, entry);
}

$scope.getLastMonth = function(entry){
return $scope.getLast(29, entry);
}

$scope.getLast6Month = function(entry){
return $scope.getLast(179, entry);
}
}]);
7 changes: 7 additions & 0 deletions js/controllers/mainController.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
app.controller("MainCtrl", ['$scope', 'selectedDate', function($scope, selectedDate){
$scope.selectedDate = selectedDate.getDate;
$scope.currentDate = "";
$scope.$watch("currentDate", function(){
selectedDate.setDate($scope.currentDate);
})
}]);
61 changes: 61 additions & 0 deletions js/controllers/portfolioController.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
app.controller("portfolioCtrl", ['$scope',
'transactionService',
'historicalStock',
'selectedDate',
function($scope, transactionService, historicalStock, selectedDate){
$scope.currentDate = selectedDate.getDate;
$scope.portfolio = transactionService.portfolio(selectedDate.getDate())
$scope.money = transactionService.getMoney($scope.currentDate())

$scope.getPrice = historicalStock.getPrice;
$scope.yesterday = historicalStock.getYesterday;
$scope.lastMonth = historicalStock.getLastMonth;
$scope.last6 = historicalStock.getLast6Month;

$scope.portfolioPrevious = function(offset){
var now = new Date($scope.currentDate())
now = new Date(now.setDate(now.getDate() - offset));

console.log(now);
if (now < Date.parse('1/1/2014')) return "N/A"

var previousPortfolio = transactionService.portfolio(now);

return transactionService.getMoney(now) + previousPortfolio.reduce(function(total, el){
total += historicalStock.getPrice(now, el.symbol) * el.quantity;
return total;
}, 0)
}

$scope.portfolioYesterday = function(){
return $scope.portfolioPrevious(1);

}

$scope.portfolioLastMonth = function(){
return $scope.portfolioPrevious(30);
}

$scope.portfolioLast6Month = function(){
return $scope.portfolioPrevious(180);
}

$scope.stockValue = function(){
return $scope.portfolio.reduce(function(total, el){
total += historicalStock.getPrice($scope.currentDate(), el.symbol) * el.quantity;
return total;
}, 0)
}

$scope.totalCost = function(){
return $scope.portfolio.reduce(function(total, el){
total += el.cost
return total;
}, 0)
}

$scope.$watch('currentDate()', function(){
$scope.portfolio = transactionService.portfolio($scope.currentDate());
$scope.money = transactionService.getMoney($scope.currentDate())
});
}]);
43 changes: 43 additions & 0 deletions js/controllers/tradeController.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
app.controller("tradeCtrl", ['$scope', '$stateParams', 'selectedDate', 'historicalStock', 'transactionService',
function($scope, $stateParams, selectedDate, historicalStock, transactionService){
$scope.tradeData = {};
$scope.currentMoney = transactionService.getMoney;
$scope.tradeData.symbol = $stateParams.symbol;
$scope.tradeData.date = selectedDate.getDate;

$scope.displayMoney = function(){
return $scope.currentMoney($scope.tradeData.date());
}

$scope.futureMoney = function(){
return transactionService.getFutureMoney();
}

$scope.calcCost = function(){
return $scope.calcPrice() * Number($scope.tradeData.quantity);
}

$scope.calcPrice = function(){
return historicalStock.getPrice(selectedDate.getDate(), $scope.tradeData.symbol);
}

$scope.createTranscation = function() {
var tradeRecord = JSON.parse(JSON.stringify($scope.tradeData));
tradeRecord.action = $scope.tradeData.action == "true" ? true : false
tradeRecord.date = $scope.tradeData.date();
tradeRecord.cost = Number($scope.calcCost());
tradeRecord.price = Number($scope.calcPrice());
tradeRecord.quantity = Number(tradeRecord.quantity);
transactionService.addTransaction(tradeRecord);
}

$scope.validBuy = function() {
return $scope.tradeData.action == "true" && transactionService.validateBuy($scope.tradeData.date(), $scope.calcCost());
}

$scope.validSell = function() {
return $scope.tradeData.action != "true" && transactionService.validateSale($scope.tradeData.symbol,
$scope.tradeData.date(),
$scope.tradeData.quantity);
}
}]);
24 changes: 24 additions & 0 deletions js/controllers/transactionController.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
app.controller("transactionCtrl", ['$scope', 'transactionService', function($scope, transactionService){
$scope.transactions = transactionService.transactions;

$scope.sortCriteria = "";
$scope.reverse = false;

$scope.sortBy = function(criteria){
var columns = {
1: "date",
2: "symbol",
3: "action",
4: "quantity",
5: "price"
}
if (!columns[criteria]) return;

if (columns[criteria] == $scope.sortCriteria){
$scope.reverse = !$scope.reverse;
} else {
$scope.sortCriteria = columns[criteria];
$scope.reverse = false;
}
}
}]);
11 changes: 11 additions & 0 deletions js/directives/stateDropdown.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<div class="dropdown">
<button class="btn btn-default dropdown-toggle" type="button" id="dropdownMenu1" data-toggle="dropdown" aria-haspopup="true" aria-expanded="true">
Dropdown
<span class="caret"></span>
</button>
<ul class="dropdown-menu" aria-labelledby="dropdownMenu1">
<li><a ui-sref="index.portfolio">Portfolio</a></li>
<li><a ui-sref="index.trade">Trade</a></li>
<li><a ui-sref="index.transaction">Transactions</a></li>
</ul>
</div>
7 changes: 7 additions & 0 deletions js/directives/stateDropdown.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
app.directive("stateDropdown", function(){
return {
templateUrl: "js/directives/stateDropdown.html",
restrict: "E",
scope: {}
}
})
39 changes: 39 additions & 0 deletions js/filters/dateFilter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
app.filter("dateFilter", function(){
return function(collection, date){
return collection.filter(function(el) {
var chosenDate = new Date(el.Date)
targetDate = new Date(date)
chosenDate.setHours(0,0,0,0);
targetDate.setHours(0,0,0,0);
targetDate.setDate(targetDate.getDate() - 1);
return (chosenDate <= targetDate && chosenDate >= targetDate);
});
};
});

app.filter("beforeDateFilter", function(){
return function(collection, date){
return collection.filter(function(el) {
var chosenDate = new Date(el.date)
targetDate = new Date(date)
chosenDate.setHours(0,0,0,0);
targetDate.setHours(0,0,0,0);
targetDate.setDate(targetDate.getDate() - 1);
return (chosenDate <= targetDate);
});
};
});

app.filter("afterDateFilter", function(){
return function(collection, date){
return collection.filter(function(el) {
var chosenDate = new Date(el.date)
targetDate = new Date(date)
chosenDate.setHours(0,0,0,0);
targetDate.setHours(0,0,0,0);
targetDate.setDate(targetDate.getDate() - 1);
return !(chosenDate <= targetDate);
});
};
});

19 changes: 19 additions & 0 deletions js/filters/tableFilter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
app.filter('tableFilter', function(orderByFilter){
return function(collection, expression, reverse){
if (typeof expression == 'string') {
if ( expression == "Close") {
var result = collection.sort(function(a, b){
return Number(a.Close) - Number(b.Close);
});
return reverse ? result.reverse() : result;
} else {
return orderByFilter(collection, expression, reverse)
}
} else {
var result = collection.sort(function(a, b){
return expression(a) - expression(b);
})
return reverse ? result.reverse() : result;
}
}
})
Loading