2019-04-21 07:31:19 +02:00
|
|
|
/**
|
|
|
|
* 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 {
|
|
|
|
Generic_fromJSON,
|
|
|
|
Generic_toJSON,
|
2019-05-07 03:01:06 +02:00
|
|
|
Reviver,
|
2019-04-21 07:31:19 +02:00
|
|
|
} from "../../utils/JSONReviver";
|
|
|
|
|
|
|
|
export class Order {
|
|
|
|
/**
|
|
|
|
* Initializes a Order from a JSON save state
|
|
|
|
*/
|
|
|
|
static fromJSON(value: any): Order {
|
|
|
|
return Generic_fromJSON(Order, value.data);
|
|
|
|
}
|
|
|
|
|
|
|
|
readonly pos: PositionTypes;
|
|
|
|
readonly price: number;
|
2019-05-05 06:03:40 +02:00
|
|
|
shares: number;
|
2019-05-07 03:01:06 +02:00
|
|
|
readonly stockSymbol: string;
|
2019-04-21 07:31:19 +02:00
|
|
|
readonly type: OrderTypes;
|
|
|
|
|
2021-04-30 05:52:56 +02:00
|
|
|
constructor(stockSymbol="", shares=0, price=0, typ: OrderTypes=OrderTypes.LimitBuy, pos: PositionTypes=PositionTypes.Long) {
|
2019-05-05 06:03:40 +02:00
|
|
|
// Validate arguments
|
2021-04-30 05:52:56 +02:00
|
|
|
let invalidArgs = false;
|
2019-05-05 06:03:40 +02:00
|
|
|
if (typeof shares !== "number" || typeof price !== "number") {
|
|
|
|
invalidArgs = true;
|
|
|
|
}
|
|
|
|
if (isNaN(shares) || isNaN(price)) {
|
|
|
|
invalidArgs = true;
|
|
|
|
}
|
2019-05-07 03:01:06 +02:00
|
|
|
if (typeof stockSymbol !== "string") {
|
2019-05-05 06:03:40 +02:00
|
|
|
invalidArgs = true;
|
|
|
|
}
|
|
|
|
if (invalidArgs) {
|
|
|
|
throw new Error(`Invalid constructor paramters for Order`);
|
|
|
|
}
|
|
|
|
|
2019-05-07 03:01:06 +02:00
|
|
|
this.stockSymbol = stockSymbol;
|
2019-04-21 07:31:19 +02:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Reviver.constructors.Order = Order;
|