You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

175 lines
6.1 KiB

2 years ago
// Initialize empty stock list
2 years ago
// let stockList = ['2330.TW'];
var stockList = [];
2 years ago
$('#stock-list span').each(function(){
stockList.push($(this).text());
2 years ago
});
console.log(stockList);
2 years ago
let currentList = [];
2 years ago
const layout={'autosize': true, 'markers':true,
2 years ago
'title': {'text': ''},
'xaxis': {'anchor': 'y', 'domain': [0.0, 1.0], 'rangeslider': {'visible': true}},
2 years ago
'yaxis': {'anchor': 'x', 'domain': [0.0, 1.0]},
2 years ago
'legend': {'yanchor': 'top', 'y': 1.1, 'xanchor': 'left', 'x': 0.01, 'orientation':'h'},
'margin': {'l': 25, 'r': 5, 't': 10, 'b': 5},
2 years ago
}
2 years ago
// Cache frequently-used DOM elements
const $stockForm = $('#stock-form');
const $compSelect = $('#competition');
const $stockList = $('#stock-list');
const $submitBtn = $('#submit-btn');
const $addStockBtn = $('#addStockBtn');
const $submitPort = $('#submit-port');
const $sendPort = $('#sendPort');
const $commentPort = $('#commentPort');
// Function to add a new stock item to the list
function addStockItem(stock, text) {
// Add item to array
stockList.push(stock);
// Update HTML list
const $newItem = $(`<li class="list-group-item">
<span class="px-2">${text}</span>
<a class="btn btn-sm btn-danger float-right delete-btn">
<i class="fas fa-trash-alt"></i>
</a>
</li>`);
$stockList.append($newItem);
}
2 years ago
function changeFunc(value) {
console.log(value);
if (value === 'quadratic_utility') {
$('#gamma').css("display", "flex");
} else {
$('#gamma').css("display", "none");
}
}
2 years ago
// Function to delete a stock item from the list
function deleteStockItem(itemIndex) {
// Remove item from array
stockList.splice(itemIndex, 1);
// Update HTML list
$stockList.children().eq(itemIndex).remove();
}
// Event listener for delete button clicks
$stockList.on('click', '.delete-btn', function(){
var itemIndex = $(this).closest('li').index()
deleteStockItem(itemIndex);
// console.log(stockList);
});
// Event listener for submit button click
$addStockBtn.click(function(event) {
event.preventDefault();
2 years ago
// console.log($('input[name=assetSelect]').val())
2 years ago
// Get selected stock from form
2 years ago
var text = $('input[name=assetSelect]').val();
// const selectedStock = text;
// var text = $('#stock-select option:selected').text();
2 years ago
// console.log(text)
2 years ago
if (text != null && text!= '' && stockList.indexOf(text)===-1) {
2 years ago
// Add new item to list
2 years ago
addStockItem(text, text);
2 years ago
// Clear input field
2 years ago
$('#stockAll').val('');
2 years ago
}
2 years ago
// console.log(stockList);
2 years ago
});
// Event listener for submit button click
$submitPort.click(function(event) {
event.preventDefault();
if (stockList.length > 1){
$('#portModal').modal('show');
2 years ago
// console.log('asset confirm');
2 years ago
// $(this).prop('disabled', true);
}
});
// Event listener for submit button click
$sendPort.click(function(event) {
if (stockList.length > 1){
2 years ago
// $('#confirmMes').replaceWith("<span>投資組合已開始建立,請等待完成訊息,或1分鐘後至分析結果區查看!</span>")
// $('#confirmModal').modal('show');
2 years ago
$submitPort.prop('disabled', true);
$.ajax({
url: '/postPort', //todo create_strategy
method: 'POST',
data: {
name: $('input[name=portName]').val(),
2 years ago
ts: Date(Date.now()),
2 years ago
comp: $('#competition').val(),
2 years ago
lookback: $('#lookback').val(),
frequency: $('#opt-frequency').val(),
2 years ago
role: $('#role-select').val(),
2 years ago
gamma: $('#util-gamma').val(),
2 years ago
comment: $commentPort.val(),
stockList: JSON.stringify(stockList)
},
success: function(response) {
2 years ago
// console.log(response);
2 years ago
// var res = JSON.parse(response);
event.preventDefault();
2 years ago
// $('#modalTitle').text('完成建立')
$('#sucMes').html(response);
// <span>投資組合建立時間間隔(或與登入時間間隔)必須大於60秒</span>
$('#confirmModal').modal('show');
2 years ago
$submitPort.prop('disabled', false);
},
error: function(xhr) {
console.log('Error submitting stock list: ' + xhr.responseText);
2 years ago
alert('建立失敗,請確認資產名稱是否正確! 美股代號均為大寫、台股代號為數字後接".TW"或是"TWO"');
2 years ago
}
});
$commentPort.val('');
}
// Get selected stock from form
});
// Event listener for submit button click
$submitBtn.click(function(event) {
// Send stock list to server
// console.log(event.target)
// console.log(stockList)
// console.log(cacheList.value, stockList);
2 years ago
var texts = [];
$('#stock-list span').each(function(){
texts.push($(this).text());
});
// alert(currentList.includes(texts));
if (stockList.length > 0 && JSON.stringify(currentList)!==JSON.stringify(stockList)) {
2 years ago
// cacheList = stockList;
2 years ago
$('#graph').html('<div class="spinner-border" role="status"><span class="visually-hidden">Loading...</span></div>')
2 years ago
2 years ago
$.ajax({
url: '/postStock', //todo create_strategy
method: 'POST',
data: { stockList: JSON.stringify(stockList) },
success: function(response) {
2 years ago
$('#graph').html('')
2 years ago
var graphs = JSON.parse(response);
2 years ago
// console.log(graphs.data);
2 years ago
Plotly.newPlot("graph",
2 years ago
graphs.data, layout, {responsive: true});
// console.log(response.layout);
currentList = stockList.map(obj => obj);
2 years ago
},
error: function(xhr) {
2 years ago
$('#graph').html('<div><span class="badge bg-warning">錯誤</span></div>')
2 years ago
console.log('Error submitting stock list: ' + xhr.responseText);
}
});
}
});
$(document).ready(function(){
$("#search").on("keyup", function() {
var value = $(this).val().toLowerCase();
$("#stock-select option").filter(function() {
$(this).toggle($(this).text().toLowerCase().indexOf(value) > -1)
});
});
});