2019-06-03 08:29:56 +02:00
|
|
|
/**
|
|
|
|
* Stock Market Helper Functions
|
|
|
|
*/
|
2019-04-21 07:31:19 +02:00
|
|
|
import { Stock } from "./Stock";
|
|
|
|
import { PositionTypes } from "./data/PositionTypes";
|
|
|
|
import { CONSTANTS } from "../Constants";
|
|
|
|
|
2019-05-02 00:20:14 +02:00
|
|
|
// Amount by which a stock's forecast changes during each price movement
|
2019-06-04 07:21:36 +02:00
|
|
|
export const forecastChangePerPriceMovement = 0.01;
|
2019-05-02 00:20:14 +02:00
|
|
|
|
2019-04-28 10:23:11 +02:00
|
|
|
/**
|
2019-05-23 04:12:06 +02:00
|
|
|
* Calculate the total cost of a "buy" transaction. This accounts for spread and commission.
|
2019-04-21 07:31:19 +02:00
|
|
|
* @param {Stock} stock - Stock being purchased
|
|
|
|
* @param {number} shares - Number of shares being transacted
|
|
|
|
* @param {PositionTypes} posType - Long or short position
|
|
|
|
* @returns {number | null} Total transaction cost. Returns null for an invalid transaction
|
|
|
|
*/
|
|
|
|
export function getBuyTransactionCost(stock: Stock, shares: number, posType: PositionTypes): number | null {
|
|
|
|
if (isNaN(shares) || shares <= 0 || !(stock instanceof Stock)) { return null; }
|
|
|
|
|
2019-04-28 02:43:38 +02:00
|
|
|
// Cap the 'shares' arg at the stock's maximum shares. This'll prevent
|
|
|
|
// hanging in the case when a really big number is passed in
|
|
|
|
shares = Math.min(shares, stock.maxShares);
|
|
|
|
|
2019-04-21 07:31:19 +02:00
|
|
|
const isLong = (posType === PositionTypes.Long);
|
|
|
|
|
|
|
|
// If the number of shares doesn't trigger a price movement, its a simple calculation
|
2019-05-17 08:55:21 +02:00
|
|
|
if (isLong) {
|
2019-05-23 04:12:06 +02:00
|
|
|
return (shares * stock.getAskPrice()) + CONSTANTS.StockMarketCommission;
|
2019-05-17 08:55:21 +02:00
|
|
|
} else {
|
2019-05-23 04:12:06 +02:00
|
|
|
return (shares * stock.getBidPrice()) + CONSTANTS.StockMarketCommission;
|
2019-05-07 03:01:06 +02:00
|
|
|
}
|
2019-04-28 04:20:51 +02:00
|
|
|
}
|
|
|
|
|
2019-04-21 07:31:19 +02:00
|
|
|
/**
|
|
|
|
* Calculate the TOTAL amount of money gained from a sale (NOT net profit). This accounts
|
2019-05-23 04:12:06 +02:00
|
|
|
* for spread and commission.
|
2019-04-21 07:31:19 +02:00
|
|
|
* @param {Stock} stock - Stock being sold
|
|
|
|
* @param {number} shares - Number of sharse being transacted
|
|
|
|
* @param {PositionTypes} posType - Long or short position
|
|
|
|
* @returns {number | null} Amount of money gained from transaction. Returns null for an invalid transaction
|
|
|
|
*/
|
|
|
|
export function getSellTransactionGain(stock: Stock, shares: number, posType: PositionTypes): number | null {
|
|
|
|
if (isNaN(shares) || shares <= 0 || !(stock instanceof Stock)) { return null; }
|
|
|
|
|
2019-04-28 02:43:38 +02:00
|
|
|
// Cap the 'shares' arg at the stock's maximum shares. This'll prevent
|
|
|
|
// hanging in the case when a really big number is passed in
|
|
|
|
shares = Math.min(shares, stock.maxShares);
|
|
|
|
|
2019-04-21 07:31:19 +02:00
|
|
|
const isLong = (posType === PositionTypes.Long);
|
2019-05-23 04:12:06 +02:00
|
|
|
if (isLong) {
|
|
|
|
return (shares * stock.getBidPrice()) - CONSTANTS.StockMarketCommission;
|
|
|
|
} else {
|
|
|
|
// Calculating gains for a short position requires calculating the profit made
|
|
|
|
const origCost = shares * stock.playerAvgShortPx;
|
|
|
|
const profit = ((stock.playerAvgShortPx - stock.getAskPrice()) * shares) - CONSTANTS.StockMarketCommission;
|
2019-04-28 10:23:11 +02:00
|
|
|
|
2019-05-23 04:12:06 +02:00
|
|
|
return origCost + profit;
|
2019-04-21 07:31:19 +02:00
|
|
|
}
|
|
|
|
}
|
2019-04-28 04:20:51 +02:00
|
|
|
|
|
|
|
/**
|
2019-05-23 04:12:06 +02:00
|
|
|
* Processes a stock's change in forecast whenever it is transacted
|
2019-04-28 04:20:51 +02:00
|
|
|
* @param {Stock} stock - Stock being sold
|
|
|
|
* @param {number} shares - Number of sharse being transacted
|
|
|
|
* @param {PositionTypes} posType - Long or short position
|
|
|
|
*/
|
2019-05-23 04:12:06 +02:00
|
|
|
export function processTransactionForecastMovement(stock: Stock, shares: number): void {
|
2019-04-28 04:20:51 +02:00
|
|
|
if (isNaN(shares) || shares <= 0 || !(stock instanceof Stock)) { return; }
|
|
|
|
|
|
|
|
// Cap the 'shares' arg at the stock's maximum shares. This'll prevent
|
|
|
|
// hanging in the case when a really big number is passed in
|
|
|
|
shares = Math.min(shares, stock.maxShares);
|
|
|
|
|
2019-05-17 08:55:21 +02:00
|
|
|
// If there's only going to be one iteration at most
|
2019-05-23 04:12:06 +02:00
|
|
|
const firstShares = stock.shareTxUntilMovement;
|
2019-05-17 08:55:21 +02:00
|
|
|
if (shares <= firstShares) {
|
2019-05-23 04:12:06 +02:00
|
|
|
stock.shareTxUntilMovement -= shares;
|
|
|
|
if (stock.shareTxUntilMovement <= 0) {
|
|
|
|
stock.shareTxUntilMovement = stock.shareTxForMovement;
|
2019-05-05 06:03:40 +02:00
|
|
|
stock.otlkMag -= (forecastChangePerPriceMovement);
|
|
|
|
}
|
|
|
|
|
2019-04-28 04:20:51 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-05-05 06:03:40 +02:00
|
|
|
// Calculate how many iterations of price changes we need to account for
|
2019-05-17 08:55:21 +02:00
|
|
|
let remainingShares = shares - firstShares;
|
2019-04-28 04:20:51 +02:00
|
|
|
let numIterations = 1 + Math.ceil(remainingShares / stock.shareTxForMovement);
|
|
|
|
|
2019-05-17 08:55:21 +02:00
|
|
|
// If on the offchance we end up perfectly at the next price movement
|
2019-05-23 04:12:06 +02:00
|
|
|
stock.shareTxUntilMovement = stock.shareTxForMovement - ((shares - stock.shareTxUntilMovement) % stock.shareTxForMovement);
|
|
|
|
if (stock.shareTxUntilMovement === stock.shareTxForMovement || stock.shareTxUntilMovement <= 0) {
|
|
|
|
++numIterations;
|
|
|
|
stock.shareTxUntilMovement = stock.shareTxForMovement;
|
2019-05-05 06:03:40 +02:00
|
|
|
}
|
2019-05-17 08:55:21 +02:00
|
|
|
|
2019-05-02 00:20:14 +02:00
|
|
|
|
|
|
|
// Forecast always decreases in magnitude
|
2019-06-04 07:21:36 +02:00
|
|
|
const forecastChange = forecastChangePerPriceMovement * (numIterations - 1);
|
|
|
|
stock.otlkMag = Math.max(6, stock.otlkMag - forecastChange);
|
2019-04-28 04:20:51 +02:00
|
|
|
}
|
2019-04-30 05:54:20 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Calculate the maximum number of shares of a stock that can be purchased.
|
|
|
|
* Handles mid-transaction price movements, both L and S positions, etc.
|
|
|
|
* Used for the "Buy Max" button in the UI
|
|
|
|
* @param {Stock} stock - Stock being purchased
|
|
|
|
* @param {PositionTypes} posType - Long or short position
|
|
|
|
* @param {number} money - Amount of money player has
|
|
|
|
* @returns maximum number of shares that the player can purchase
|
|
|
|
*/
|
|
|
|
export function calculateBuyMaxAmount(stock: Stock, posType: PositionTypes, money: number): number {
|
|
|
|
if (!(stock instanceof Stock)) { return 0; }
|
|
|
|
|
|
|
|
const isLong = (posType === PositionTypes.Long);
|
|
|
|
|
|
|
|
let remainingMoney = money - CONSTANTS.StockMarketCommission;
|
|
|
|
let currPrice = isLong ? stock.getAskPrice() : stock.getBidPrice();
|
|
|
|
|
2019-05-23 04:12:06 +02:00
|
|
|
return Math.floor(remainingMoney / currPrice);
|
2019-04-30 05:54:20 +02:00
|
|
|
}
|