Obtengo el método de error "la compra no está definida". ¿Qué está mal en el siguiente código? Por favor, ¿puede decirme dónde me estoy equivocando?
var Portfolio = function() { let stockHoldings = new Map(); function buy(trade) { let tradesForTicker = stockHoldings.get(trade.ticker); if(tradesForTicker == null){ stockHoldings.set(trade.ticker,[trade]); }else{ tradesForTicker.push(trade); } } function sell(trade) { let ticker = trade.ticker; let tradesForTicker = stockHoldings.get(ticker); let precision = 5; if(tradesForTicker != null){ let quantityToSell = Number(Number(trade.quantity).toFixed(precision)); while(quantityToSell > 0) { quantityToSell = Number(Number(quantityToSell).toFixed(precision)); if(tradesForTicker.length > 0){ let nextTradeToSell = tradesForTicker[0]; if(nextTradeToSell.quantity == quantityToSell){ quantityToSell = 0; tradesForTicker.splice(0,1); } else if (nextTradeToSell.quantity < quantityToSell){ quantityToSell = quantityToSell - nextTradeToSell.quantity; tradesForTicker.splice(0,1); }else { nextTradeToSell.quantity = nextTradeToSell.quantity - quantityToSell; quantityToSell = 0; } } } if(tradesForTicker.length == 0){ stockHoldings.delete(ticker) } } } this.stockHoldings = function() { return stockHoldings(); } }; function generetePortfolio(tickers, actions, quantities, prices){ let portfolio = new Portfolio(); for(var i=0;i< tickers.length; i++){ let ticker = tickers[i].toString(); let action = actions[i].toString(); let quantity = quantities[i].toString(); let price = prices[i].toString(); let transactionTrade = new Trade(ticker,quantity,price,action); if(transactionTrade.isBuyOrDrip()){ portfolio.buy(transactionTrade); } if(transactionTrade.isSell()){ portfolio.sell(transactionTrade); } } return portfolio(); }
EDITAR más:
//Método agregado para generar cantidad total y precio promedio, PERO ESTO NO FUNCIONA
function myPositions(tickers, actions, quantities, prices){ let portfolio = generetePortfolio(tickers, actions, quantities, prices); let returnArray = []; portfolio.stockHoldings.forEach((value, key) => { let totalQuantity = 0; let avgPrice = 0; let totalCost = 0; value.map( trade => { totalQuantity += trade.quantity ; totalCost += trade.quantity * trade.price; }); avgPrice = totalCost/totalQuantity; returnArray.push([key, totalQuantity,avgPrice]); } ) return returnArray; }
Está intentando utilizar las propiedades de su objeto de portfolio
suponiendo que será un objeto basado en el constructor de Portfolio
, pero no lo es.
Una función constructora implícitamente devuelve this
. Ahora, el único lugar en el que ha usado this
dentro de la función es adjuntarle una propiedad stockHoldings
.
También debe adjuntar las otras propiedades.
Lo que sucede bajo el capó puede considerarse simplemente como: un objeto vacío local se crea implícitamente . Se devuelve al final implícitamente .
var Portfolio = function () { // this = {}; (implicitly) let stockHoldings = new Map(); function buy(trade) { let tradesForTicker = stockHoldings.get(trade.ticker); if(tradesForTicker == null){ stockHoldings.set(trade.ticker,[trade]); }else{ tradesForTicker.push(trade); } } function sell(trade) { let ticker = trade.ticker; let tradesForTicker = stockHoldings.get(ticker); let precision = 5; if(tradesForTicker != null){ let quantityToSell = Number(Number(trade.quantity).toFixed(precision)); while(quantityToSell > 0) { quantityToSell = Number(Number(quantityToSell).toFixed(precision)); if(tradesForTicker.length > 0){ let nextTradeToSell = tradesForTicker[0]; if(nextTradeToSell.quantity == quantityToSell){ quantityToSell = 0; tradesForTicker.splice(0,1); } else if (nextTradeToSell.quantity < quantityToSell){ quantityToSell = quantityToSell - nextTradeToSell.quantity; tradesForTicker.splice(0,1); }else { nextTradeToSell.quantity = nextTradeToSell.quantity - quantityToSell; quantityToSell = 0; } } } if(tradesForTicker.length == 0){ stockHoldings.delete(ticker) } } } this.stockHoldings = stockHoldings; this.buy = buy; this.sell = sell; // return this; (implicitly) }; function generetePortfolio(tickers, actions, quantities, prices){ let portfolio = new Portfolio(); for(var i=0;i< tickers.length; i++){ let ticker = tickers[i].toString(); let action = actions[i].toString(); let quantity = quantities[i].toString(); let price = prices[i].toString(); let transactionTrade = new Trade(ticker,quantity,price,action); if(transactionTrade.isBuyOrDrip()){ portfolio.buy(transactionTrade); } if(transactionTrade.isSell()){ portfolio.sell(transactionTrade); } } return portfolio(); }
Además, la función para stockHoldings no parece correcta. Deberías devolver el mapa correctamente.