2019-04-21 07:31:19 +02:00
|
|
|
import { IMinMaxRange } from "../types";
|
|
|
|
import {
|
|
|
|
Generic_fromJSON,
|
|
|
|
Generic_toJSON,
|
|
|
|
Reviver
|
|
|
|
} from "../../utils/JSONReviver";
|
2019-01-09 11:06:49 +01:00
|
|
|
import { getRandomInt } from "../../utils/helpers/getRandomInt";
|
|
|
|
|
2019-04-21 07:31:19 +02:00
|
|
|
export interface IConstructorParams {
|
|
|
|
b: boolean;
|
|
|
|
initPrice: number | IMinMaxRange;
|
|
|
|
marketCap: number;
|
|
|
|
mv: number | IMinMaxRange;
|
|
|
|
name: string;
|
|
|
|
otlkMag: number;
|
|
|
|
spreadPerc: number | IMinMaxRange;
|
|
|
|
shareTxForMovement: number | IMinMaxRange;
|
|
|
|
symbol: string;
|
|
|
|
}
|
|
|
|
|
|
|
|
const defaultConstructorParams: IConstructorParams = {
|
|
|
|
b: true,
|
|
|
|
initPrice: 10e3,
|
|
|
|
marketCap: 1e12,
|
|
|
|
mv: 1,
|
|
|
|
name: "",
|
|
|
|
otlkMag: 0,
|
|
|
|
spreadPerc: 0,
|
|
|
|
shareTxForMovement: 1e6,
|
|
|
|
symbol: "",
|
|
|
|
}
|
|
|
|
|
|
|
|
// Helper function that convert a IMinMaxRange to a number
|
|
|
|
function toNumber(n: number | IMinMaxRange): number {
|
|
|
|
let value: number;
|
|
|
|
switch (typeof n) {
|
|
|
|
case "number": {
|
2019-04-23 10:48:15 +02:00
|
|
|
return <number>n;
|
2019-04-21 07:31:19 +02:00
|
|
|
}
|
|
|
|
case "object": {
|
2019-04-23 10:48:15 +02:00
|
|
|
const range = <IMinMaxRange>n;
|
|
|
|
value = getRandomInt(range.min, range.max);
|
2019-04-21 07:31:19 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
throw Error(`Do not know how to convert the type '${typeof n}' to a number`);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (typeof n === "object" && typeof n.divisor === "number") {
|
|
|
|
return value / n.divisor;
|
|
|
|
}
|
|
|
|
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
2019-01-09 11:06:49 +01:00
|
|
|
/**
|
|
|
|
* Represents the valuation of a company in the World Stock Exchange.
|
|
|
|
*/
|
|
|
|
export class Stock {
|
|
|
|
/**
|
|
|
|
* Initializes a Stock from a JSON save state
|
|
|
|
*/
|
|
|
|
static fromJSON(value: any): Stock {
|
|
|
|
return Generic_fromJSON(Stock, value.data);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Bear or bull (more likely to go up or down, based on otlkMag)
|
|
|
|
*/
|
|
|
|
b: boolean;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Maximum price of a stock (per share)
|
|
|
|
*/
|
|
|
|
readonly cap: number;
|
|
|
|
|
2019-04-23 10:23:08 +02:00
|
|
|
/**
|
|
|
|
* Stocks previous share price
|
|
|
|
*/
|
|
|
|
lastPrice: number;
|
|
|
|
|
2019-01-09 11:06:49 +01:00
|
|
|
/**
|
|
|
|
* Maximum number of shares that player can own (both long and short combined)
|
|
|
|
*/
|
|
|
|
readonly maxShares: number;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Maximum volatility
|
|
|
|
*/
|
|
|
|
readonly mv: number;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Name of the company that the stock is for
|
|
|
|
*/
|
|
|
|
readonly name: string;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Outlook magnitude. Represents the stock's forecast and likelihood
|
|
|
|
* of increasing/decreasing (based on whether its in bear or bull mode)
|
|
|
|
*/
|
|
|
|
otlkMag: number;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Average price of stocks that the player owns in the LONG position
|
|
|
|
*/
|
|
|
|
playerAvgPx: number;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Average price of stocks that the player owns in the SHORT position
|
|
|
|
*/
|
|
|
|
playerAvgShortPx: number;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Number of shares the player owns in the LONG position
|
|
|
|
*/
|
|
|
|
playerShares: number;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Number of shares the player owns in the SHORT position
|
|
|
|
*/
|
|
|
|
playerShortShares: number;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Stock's share price
|
|
|
|
*/
|
|
|
|
price: number;
|
|
|
|
|
2019-04-21 07:31:19 +02:00
|
|
|
/**
|
|
|
|
* Percentage by which the stock's price changes for a transaction-induced
|
|
|
|
* price movement.
|
|
|
|
*/
|
|
|
|
readonly priceMovementPerc: number;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* How many shares need to be transacted in order to trigger a price movement
|
|
|
|
*/
|
|
|
|
readonly shareTxForMovement: number;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* How many share transactions remaining until a price movement occurs
|
|
|
|
*/
|
|
|
|
shareTxUntilMovement: number;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Spread percentage. The bid/ask prices for this stock are N% above or below
|
|
|
|
* the "real price" to emulate spread.
|
|
|
|
*/
|
|
|
|
readonly spreadPerc: number;
|
|
|
|
|
2019-01-09 11:06:49 +01:00
|
|
|
/**
|
|
|
|
* The stock's ticker symbol
|
|
|
|
*/
|
|
|
|
readonly symbol: string;
|
|
|
|
|
2019-01-22 05:39:52 +01:00
|
|
|
/**
|
|
|
|
* Total number of shares of this stock
|
|
|
|
* This is different than maxShares, as this is like authorized stock while
|
|
|
|
* maxShares is outstanding stock.
|
|
|
|
*/
|
|
|
|
readonly totalShares: number;
|
|
|
|
|
2019-04-21 07:31:19 +02:00
|
|
|
constructor(p: IConstructorParams = defaultConstructorParams) {
|
|
|
|
this.name = p.name;
|
|
|
|
this.symbol = p.symbol;
|
|
|
|
this.price = toNumber(p.initPrice);
|
2019-04-23 10:23:08 +02:00
|
|
|
this.lastPrice = this.price;
|
2019-04-21 07:31:19 +02:00
|
|
|
this.playerShares = 0;
|
|
|
|
this.playerAvgPx = 0;
|
|
|
|
this.playerShortShares = 0;
|
|
|
|
this.playerAvgShortPx = 0;
|
|
|
|
this.mv = toNumber(p.mv);
|
|
|
|
this.b = p.b;
|
|
|
|
this.otlkMag = p.otlkMag;
|
|
|
|
this.cap = getRandomInt(this.price * 1e3, this.price * 25e3);
|
|
|
|
this.spreadPerc = toNumber(p.spreadPerc);
|
|
|
|
this.priceMovementPerc = this.spreadPerc / (getRandomInt(10, 30) / 10);
|
|
|
|
this.shareTxForMovement = toNumber(p.shareTxForMovement);
|
|
|
|
this.shareTxUntilMovement = this.shareTxForMovement;
|
2019-01-09 11:06:49 +01:00
|
|
|
|
2019-01-29 01:17:04 +01:00
|
|
|
// Total shares is determined by market cap, and is rounded to nearest 100k
|
2019-04-21 07:31:19 +02:00
|
|
|
let totalSharesUnrounded: number = (p.marketCap / this.price);
|
2019-01-29 01:17:04 +01:00
|
|
|
this.totalShares = Math.round(totalSharesUnrounded / 1e5) * 1e5;
|
2019-01-22 05:39:52 +01:00
|
|
|
|
|
|
|
// Max Shares (Outstanding shares) is a percentage of total shares
|
2019-04-21 07:31:19 +02:00
|
|
|
const outstandingSharePercentage: number = 0.15;
|
2019-01-29 01:17:04 +01:00
|
|
|
this.maxShares = Math.round((this.totalShares * outstandingSharePercentage) / 1e5) * 1e5;
|
2019-04-23 10:23:08 +02:00
|
|
|
}
|
2019-01-09 11:06:49 +01:00
|
|
|
|
2019-04-23 10:23:08 +02:00
|
|
|
changePrice(newPrice: number): void {
|
|
|
|
this.lastPrice = this.price;
|
|
|
|
this.price = newPrice;
|
2019-01-09 11:06:49 +01:00
|
|
|
}
|
|
|
|
|
2019-04-21 07:31:19 +02:00
|
|
|
/**
|
|
|
|
* Return the price at which YOUR stock is bought (market ask price). Accounts for spread
|
|
|
|
*/
|
|
|
|
getAskPrice(): number {
|
|
|
|
return this.price * (1 + (this.spreadPerc / 100));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return the price at which YOUR stock is sold (market bid price). Accounts for spread
|
|
|
|
*/
|
|
|
|
getBidPrice(): number {
|
|
|
|
return this.price * (1 - (this.spreadPerc / 100));
|
|
|
|
}
|
|
|
|
|
2019-01-09 11:06:49 +01:00
|
|
|
/**
|
|
|
|
* Serialize the Stock to a JSON save state.
|
|
|
|
*/
|
|
|
|
toJSON(): any {
|
|
|
|
return Generic_toJSON("Stock", this);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Reviver.constructors.Stock = Stock;
|