Skip to content

Commit

Permalink
api and stock helper updated
Browse files Browse the repository at this point in the history
  • Loading branch information
YogeshUpdhyay committed May 17, 2021
1 parent 0cef334 commit 27b2cfc
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 85 deletions.
2 changes: 1 addition & 1 deletion app/dashboard/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@
def stock_detail():
ticker = request.form["ticker"]
data = stock.get_data(ticker)
graph_data = stock.parse_data(data, 'line')
graph_data = stock.parse_data(data)
return json.dumps(graph_data)

18 changes: 9 additions & 9 deletions app/utils/stock.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,18 @@ def get_data(self, ticker):
data.reset_index(inplace=True)
return data

def parse_data(self, data, type):
def parse_data(self, data):

parsed_data = list()

if type == "line":
for index, row in data.iterrows():
parsed_data.append({str(row['Date']) : {
'open': row["Open"],
"close": row["Close"],
"high": row["High"],
"low": row["Low"]
}})
for index, row in data.iterrows():
parsed_data.append({
'date': str(row['Date']),
'open': row["Open"],
"close": row["Close"],
"high": row["High"],
"low": row["Low"]
})

return parsed_data

Expand Down
112 changes: 46 additions & 66 deletions static/assets/js/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ window.onbeforeunload = function () {
localStorage.clear();
}

function submitFilter() {
async function submitFilter() {
// ticker field validation
if (document.getElementById("ticker").value == ""){
document.getElementById("ticker").className = "form-control is-invalid"
Expand All @@ -15,30 +15,21 @@ function submitFilter() {
// graph api call
let data = {
ticker: document.getElementById("ticker").value,
startDate: document.getElementById("start-date").value,
endDate: document.getElementById("end-date").value,
indicator: document.getElementById("indicator").value,
intradayMode: document.getElementById("intraday-mode").checked
}

let graphData;
$.post(
await $.post(
"/api/v1/stockdetail",
data,
function(data, status){
graphData = data;
if (status == "success") {
console.log("here")
localStorage.setItem('graphData', data);
}
}
)

graphData = {
labels: ['May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'],
datasets: [{
label: 'Performance',
data: [0, 20, 10, 30, 15, 40, 20, 60, 60]
}]
}

localStorage.setItem('graphData', JSON.stringify(graphData));

// Initializing line chart
onLineTab();
Expand Down Expand Up @@ -72,6 +63,23 @@ var colors = {
transparent: 'transparent',
};

function parseData(type) {
if (type === 'line') {
var parsed_values = [];
data = JSON.parse(localStorage.getItem('graphData'));
console.log(data)
data.forEach(i => {
var temp = {
x: new Date(i.date).getTime(),
y: i.close.toFixed(2)
};
parsed_values.push(temp);
});
console.log(parsed_values);
return parsed_values;
}
}


function onLineTab() {
try {
Expand All @@ -86,18 +94,31 @@ function onLineTab() {
autoScaleYaxis: true
},
toolbar: {
auto
autoSelected: 'zoom'
},
sparkline: {
enabled: true,
},
tooltip: {
enabled: false
}
},
colors: [colors.gray[200]],
dataLabels: {
enabled: false
},
markers: {
size: 0,
},
stroke: {
curve: 'smooth',
},
series: [{
name: 'sales',
data: [30,40,35,50,49,60,70,91,125]
name: 'Close Price',
data: parseData('line')
}],
xaxis: {
categories: [1991,1992,1993,1994,1995,1996,1997, 1998,1999]
type: 'datetime'
}
}

Expand All @@ -108,56 +129,15 @@ function onLineTab() {
console.log(e)
}


}

function onCandleTab() {
var chart = $('#chart-stock-dark');
var barCount = 60;
var initialDateStr = '01 Apr 2017 00:00 Z';

var candlechart = new Chart(chart, {
type: 'candlestick',
data: {
datasets: [{
label: 'CHRT - Chart.js Corporation',
data: getRandomData(initialDateStr, barCount)
}]
}
});

var getRandomInt = function(max) {
return Math.floor(Math.random() * Math.floor(max));
};

function randomNumber(min, max) {
return Math.random() * (max - min) + min;
}

function randomBar(date, lastClose) {
var open = +randomNumber(lastClose * 0.95, lastClose * 1.05).toFixed(2);
var close = +randomNumber(open * 0.95, open * 1.05).toFixed(2);
var high = +randomNumber(Math.max(open, close), Math.max(open, close) * 1.1).toFixed(2);
var low = +randomNumber(Math.min(open, close) * 0.9, Math.min(open, close)).toFixed(2);
return {
x: date.valueOf(),
o: open,
h: high,
l: low,
c: close
};

}

function getRandomData(dateStr, count) {
var date = luxon.DateTime.fromRFC2822(dateStr);
var data = [randomBar(date, 30)];
while (data.length < count) {
date = date.plus({days: 1});
if (date.weekday <= 5) {
data.push(randomBar(date, data[data.length - 1].c));
}
var options = {
chart: {
type: 'candlestick',
},
series: {
data: parseData('candlestick')
}
return data;
}
}
6 changes: 1 addition & 5 deletions templates/includes/graph.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,7 @@
<div class="row align-items-center">
<div class="col">
<h6 class="text-light text-uppercase ls-1 mb-1">Stock Analysis</h6>
{% if company %}
<h5 class="h3 text-white mb-0">{{ company }}</h5>
{% else %}
<h5 class="h3 text-white mb-0">Select a ticker and submit</h5>
{% endif %}
<h5 class="h3 text-white mb-0" id="stock-name">Select a ticker and submit</h5>
</div>
<div class="col">
<ul class="nav nav-pills justify-content-end">
Expand Down
24 changes: 20 additions & 4 deletions templates/includes/header-filter.html
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,23 @@ <h4>Filter</h4>
<label>Ticker</label>
<input class="form-control text-uppercase" type="text" id="ticker" placeholder="Ticker" aria-placeholder="Ticker">
</div>
<div class="col-sm-8">
<div class="col-sm-4">
<label>Indicators</label>
<select name="indicators" id="indicator" class="form-control">
<option value="volvo">Volvo</option>
<option value="saab">Saab</option>
<option value="mercedes">Mercedes</option>
<option value="audi">Audi</option>
</select>
</div>
<div class="col-sm-4">
<div><label>Intraday Mode</label></div>
<label class="custom-toggle">
<input type="checkbox" id="intraday-mode">
<span class="custom-toggle-slider rounded-circle"></span>
</label>
</div>
<!-- <div class="col-sm-8">
<label>Date Range</label>
<div class="input-daterange datepicker row align-items-center">
<div class="col">
Expand All @@ -36,9 +52,9 @@ <h4>Filter</h4>
</div>
</div>
</div>
</div>
</div> -->
</div>
<div class="row mb-2 ml-4 mr-4">
<!-- <div class="row mb-2 ml-4 mr-4">
<div class="col-sm-6">
<label>Indicators</label>
<select name="indicators" id="indicator" class="form-control">
Expand All @@ -55,7 +71,7 @@ <h4>Filter</h4>
<span class="custom-toggle-slider rounded-circle"></span>
</label>
</div>
</div>
</div> -->
<div class="row ml-4 mt-4 mb-4 mr-4">
<div class="col-xl-4">
<button type="button" class="btn btn-default" onclick="submitFilter()">
Expand Down
Binary file modified yfinance.cache
Binary file not shown.

0 comments on commit 27b2cfc

Please sign in to comment.