bitburner-src/src/StockMarket/Order.ts

67 lines
1.5 KiB
TypeScript
Raw Normal View History

/**
* Represents a Limit or Buy Order on the stock market. Does not represent
* a Market Order since those are just executed immediately
*/
import { OrderTypes } from "./data/OrderTypes";
import { PositionTypes } from "./data/PositionTypes";
import {
2021-09-05 01:09:30 +02:00
Generic_fromJSON,
Generic_toJSON,
Reviver,
} from "../../utils/JSONReviver";
export class Order {
2021-09-05 01:09:30 +02:00
readonly pos: PositionTypes;
readonly price: number;
shares: number;
readonly stockSymbol: string;
readonly type: OrderTypes;
2021-09-05 01:09:30 +02:00
constructor(
stockSymbol = "",
shares = 0,
price = 0,
typ: OrderTypes = OrderTypes.LimitBuy,
pos: PositionTypes = PositionTypes.Long,
) {
// Validate arguments
let invalidArgs = false;
if (typeof shares !== "number" || typeof price !== "number") {
invalidArgs = true;
}
2021-09-05 01:09:30 +02:00
if (isNaN(shares) || isNaN(price)) {
invalidArgs = true;
}
2021-09-05 01:09:30 +02:00
if (typeof stockSymbol !== "string") {
invalidArgs = true;
2021-05-01 09:17:31 +02:00
}
2021-09-05 01:09:30 +02:00
if (invalidArgs) {
throw new Error(`Invalid constructor paramters for Order`);
}
this.stockSymbol = stockSymbol;
this.shares = shares;
this.price = price;
this.type = typ;
this.pos = pos;
}
/**
* Serialize the Order to a JSON save state.
*/
toJSON(): any {
return Generic_toJSON("Order", this);
}
/**
* Initializes a Order from a JSON save state
*/
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
static fromJSON(value: any): Order {
return Generic_fromJSON(Order, value.data);
}
}
Reviver.constructors.Order = Order;