2019-05-05 06:03:40 +02:00
|
|
|
/**
|
|
|
|
* Helper functions for determine whether Limit and Stop orders should
|
|
|
|
* be executed (and executing them)
|
|
|
|
*/
|
|
|
|
import {
|
|
|
|
buyStock,
|
|
|
|
sellStock,
|
|
|
|
shortStock,
|
|
|
|
sellShort,
|
|
|
|
} from "./BuyingAndSelling";
|
|
|
|
import { IOrderBook } from "./IOrderBook";
|
|
|
|
import { IStockMarket } from "./IStockMarket";
|
|
|
|
import { Order } from "./Order";
|
|
|
|
import { Stock } from "./Stock";
|
|
|
|
|
|
|
|
import { OrderTypes } from "./data/OrderTypes";
|
|
|
|
import { PositionTypes } from "./data/PositionTypes";
|
|
|
|
|
2019-05-07 03:01:06 +02:00
|
|
|
import { IMap } from "../types";
|
|
|
|
|
2019-05-05 06:03:40 +02:00
|
|
|
import { numeralWrapper } from "../ui/numeralFormat";
|
|
|
|
|
|
|
|
import { dialogBoxCreate } from "../../utils/DialogBox";
|
|
|
|
|
2019-06-10 06:23:48 +02:00
|
|
|
export interface IProcessOrderRefs {
|
2019-05-05 06:03:40 +02:00
|
|
|
rerenderFn: () => void;
|
|
|
|
stockMarket: IStockMarket;
|
2019-05-07 03:01:06 +02:00
|
|
|
symbolToStockMap: IMap<Stock>;
|
2019-05-05 06:03:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Search for all orders of a specific type and execute them if appropriate
|
|
|
|
* @param {Stock} stock - Stock for which orders should be processed
|
|
|
|
* @param {OrderTypes} orderType - Type of order to check (Limit/Stop buy/sell)
|
|
|
|
* @param {PositionTypes} posType - Long or short
|
|
|
|
* @param {IProcessOrderRefs} refs - References to objects/functions that are required for this function
|
|
|
|
*/
|
|
|
|
export function processOrders(stock: Stock, orderType: OrderTypes, posType: PositionTypes, refs: IProcessOrderRefs): void {
|
|
|
|
let orderBook = refs.stockMarket["Orders"];
|
|
|
|
if (orderBook == null) {
|
|
|
|
const orders: IOrderBook = {};
|
|
|
|
for (const name in refs.stockMarket) {
|
|
|
|
const stock = refs.stockMarket[name];
|
|
|
|
if (!(stock instanceof Stock)) { continue; }
|
|
|
|
orders[stock.symbol] = [];
|
|
|
|
}
|
|
|
|
refs.stockMarket["Orders"] = orders;
|
|
|
|
return; // Newly created, so no orders to process
|
|
|
|
}
|
|
|
|
let stockOrders = orderBook[stock.symbol];
|
|
|
|
if (stockOrders == null || !(stockOrders.constructor === Array)) {
|
2019-06-10 06:23:48 +02:00
|
|
|
console.error(`Invalid Order book for ${stock.symbol} in processOrders(): ${stockOrders}`);
|
2019-05-05 06:03:40 +02:00
|
|
|
stockOrders = [];
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (const order of stockOrders) {
|
|
|
|
if (order.type === orderType && order.pos === posType) {
|
|
|
|
switch (order.type) {
|
|
|
|
case OrderTypes.LimitBuy:
|
|
|
|
if (order.pos === PositionTypes.Long && stock.price <= order.price) {
|
2019-05-07 03:01:06 +02:00
|
|
|
executeOrder/*66*/(order, refs);
|
2019-05-05 06:03:40 +02:00
|
|
|
} else if (order.pos === PositionTypes.Short && stock.price >= order.price) {
|
2019-05-07 03:01:06 +02:00
|
|
|
executeOrder/*66*/(order, refs);
|
2019-05-05 06:03:40 +02:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case OrderTypes.LimitSell:
|
|
|
|
if (order.pos === PositionTypes.Long && stock.price >= order.price) {
|
2019-05-07 03:01:06 +02:00
|
|
|
executeOrder/*66*/(order, refs);
|
2019-05-05 06:03:40 +02:00
|
|
|
} else if (order.pos === PositionTypes.Short && stock.price <= order.price) {
|
2019-05-07 03:01:06 +02:00
|
|
|
executeOrder/*66*/(order, refs);
|
2019-05-05 06:03:40 +02:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case OrderTypes.StopBuy:
|
|
|
|
if (order.pos === PositionTypes.Long && stock.price >= order.price) {
|
2019-05-07 03:01:06 +02:00
|
|
|
executeOrder/*66*/(order, refs);
|
2019-05-05 06:03:40 +02:00
|
|
|
} else if (order.pos === PositionTypes.Short && stock.price <= order.price) {
|
2019-05-07 03:01:06 +02:00
|
|
|
executeOrder/*66*/(order, refs);
|
2019-05-05 06:03:40 +02:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case OrderTypes.StopSell:
|
|
|
|
if (order.pos === PositionTypes.Long && stock.price <= order.price) {
|
2019-05-07 03:01:06 +02:00
|
|
|
executeOrder/*66*/(order, refs);
|
2019-05-05 06:03:40 +02:00
|
|
|
} else if (order.pos === PositionTypes.Short && stock.price >= order.price) {
|
2019-05-07 03:01:06 +02:00
|
|
|
executeOrder/*66*/(order, refs);
|
2019-05-05 06:03:40 +02:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
console.warn(`Invalid order type: ${order.type}`);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Execute a Stop or Limit Order.
|
|
|
|
* @param {Order} order - Order being executed
|
2019-06-10 00:12:33 +02:00
|
|
|
* @param {IProcessOrderRefs} refs - References to objects/functions that are required for this function
|
2019-05-05 06:03:40 +02:00
|
|
|
*/
|
2019-05-07 03:01:06 +02:00
|
|
|
function executeOrder(order: Order, refs: IProcessOrderRefs) {
|
|
|
|
const stock = refs.symbolToStockMap[order.stockSymbol];
|
|
|
|
if (!(stock instanceof Stock)) {
|
|
|
|
console.error(`Could not find stock for this order: ${order.stockSymbol}`);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const stockMarket = refs.stockMarket;
|
2019-05-05 06:03:40 +02:00
|
|
|
const orderBook = stockMarket["Orders"];
|
|
|
|
const stockOrders = orderBook[stock.symbol];
|
|
|
|
|
|
|
|
// When orders are executed, the buying and selling functions shouldn't
|
|
|
|
// emit popup dialog boxes. This options object configures the functions for that
|
|
|
|
const opts = {
|
2019-05-07 03:01:06 +02:00
|
|
|
rerenderFn: refs.rerenderFn,
|
2019-05-05 06:03:40 +02:00
|
|
|
suppressDialog: true
|
|
|
|
}
|
|
|
|
|
|
|
|
let res = true;
|
|
|
|
let isBuy = false;
|
|
|
|
switch (order.type) {
|
2019-05-23 04:12:06 +02:00
|
|
|
case OrderTypes.LimitBuy:
|
|
|
|
case OrderTypes.StopBuy:
|
2019-05-05 06:03:40 +02:00
|
|
|
isBuy = true;
|
|
|
|
if (order.pos === PositionTypes.Long) {
|
|
|
|
res = buyStock(stock, order.shares, null, opts) && res;
|
|
|
|
} else if (order.pos === PositionTypes.Short) {
|
|
|
|
res = shortStock(stock, order.shares, null, opts) && res;
|
|
|
|
}
|
|
|
|
break;
|
2019-05-23 04:12:06 +02:00
|
|
|
case OrderTypes.LimitSell:
|
|
|
|
case OrderTypes.StopSell:
|
2019-05-05 06:03:40 +02:00
|
|
|
if (order.pos === PositionTypes.Long) {
|
2019-05-23 04:12:06 +02:00
|
|
|
res = sellStock(stock, order.shares, null, opts) && res;
|
2019-05-05 06:03:40 +02:00
|
|
|
} else if (order.pos === PositionTypes.Short) {
|
2019-05-23 04:12:06 +02:00
|
|
|
res = sellShort(stock, order.shares, null, opts) && res;
|
2019-05-05 06:03:40 +02:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
console.warn(`Invalid order type: ${order.type}`);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Position type, for logging/message purposes
|
|
|
|
const pos = order.pos === PositionTypes.Long ? "Long" : "Short";
|
|
|
|
|
|
|
|
if (res) {
|
|
|
|
for (let i = 0; i < stockOrders.length; ++i) {
|
|
|
|
if (order == stockOrders[i]) {
|
2019-05-23 04:12:06 +02:00
|
|
|
stockOrders.splice(i, 1);
|
|
|
|
dialogBoxCreate(`${order.type} for ${stock.symbol} @ ${numeralWrapper.formatMoney(order.price)} (${pos}) was filled ` +
|
|
|
|
`(${numeralWrapper.formatBigNumber(Math.round(order.shares))} shares)`);
|
2019-05-07 03:01:06 +02:00
|
|
|
refs.rerenderFn();
|
2019-05-05 06:03:40 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
console.error("Could not find the following Order in Order Book: ");
|
|
|
|
console.error(order);
|
|
|
|
} else {
|
|
|
|
if (isBuy) {
|
2019-05-07 03:01:06 +02:00
|
|
|
dialogBoxCreate(`Failed to execute ${order.type} for ${stock.symbol} @ ${numeralWrapper.formatMoney(order.price)} (${pos}). ` +
|
2019-05-05 06:03:40 +02:00
|
|
|
`This is most likely because you do not have enough money or the order would exceed the stock's maximum number of shares`);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|