bitburner-src/src/NetscriptFunctions/StockMarket.ts

415 lines
14 KiB
TypeScript
Raw Normal View History

2022-08-09 21:41:47 +02:00
import { Player as player } from "../Player";
2021-10-14 09:22:02 +02:00
import { buyStock, sellStock, shortStock, sellShort } from "../StockMarket/BuyingAndSelling";
import { StockMarket, SymbolToStockMap, placeOrder, cancelOrder, initStockMarket } from "../StockMarket/StockMarket";
2021-10-14 09:22:02 +02:00
import { getBuyTransactionCost, getSellTransactionGain } from "../StockMarket/StockMarketHelpers";
import { OrderTypes } from "../StockMarket/data/OrderTypes";
import { PositionTypes } from "../StockMarket/data/PositionTypes";
import { StockSymbols } from "../StockMarket/data/StockSymbols";
2022-03-19 17:58:18 +01:00
import {
getStockMarket4SDataCost,
getStockMarket4STixApiCost,
getStockMarketWseCost,
getStockMarketTixApiCost,
} from "../StockMarket/StockMarketCosts";
2021-10-14 09:22:02 +02:00
import { Stock } from "../StockMarket/Stock";
import { StockOrder, TIX } from "@nsdefs";
2022-08-08 19:43:41 +02:00
import { InternalAPI, NetscriptContext } from "../Netscript/APIWrapper";
import { helpers } from "../Netscript/NetscriptHelpers";
2021-10-14 09:22:02 +02:00
2022-08-09 21:41:47 +02:00
export function NetscriptStockMarket(): InternalAPI<TIX> {
/** Checks if the player has TIX API access. Throws an error if the player does not */
2022-05-08 01:08:07 +02:00
const checkTixApiAccess = function (ctx: NetscriptContext): void {
2021-10-14 09:22:02 +02:00
if (!player.hasWseAccount) {
2022-08-08 19:43:41 +02:00
throw helpers.makeRuntimeErrorMsg(ctx, `You don't have WSE Access! Cannot use ${ctx.function}()`);
2021-10-14 09:22:02 +02:00
}
if (!player.hasTixApiAccess) {
2022-08-08 19:43:41 +02:00
throw helpers.makeRuntimeErrorMsg(ctx, `You don't have TIX API Access! Cannot use ${ctx.function}()`);
2021-10-14 09:22:02 +02:00
}
};
2022-05-08 01:08:07 +02:00
const getStockFromSymbol = function (ctx: NetscriptContext, symbol: string): Stock {
2021-10-14 09:22:02 +02:00
const stock = SymbolToStockMap[symbol];
if (stock == null) {
2022-08-08 19:43:41 +02:00
throw helpers.makeRuntimeErrorMsg(ctx, `Invalid stock symbol: '${symbol}'`);
2021-10-14 09:22:02 +02:00
}
return stock;
};
2021-10-14 09:22:02 +02:00
return {
hasWSEAccount: () => () => {
2022-07-28 20:35:55 +02:00
return player.hasWseAccount;
},
hasTIXAPIAccess: () => () => {
2022-07-28 20:35:55 +02:00
return player.hasTixApiAccess;
},
has4SData: () => () => {
2022-07-28 20:35:55 +02:00
return player.has4SData;
},
has4SDataTIXAPI: () => () => {
2022-07-28 20:35:55 +02:00
return player.has4SDataTixApi;
},
getSymbols: (ctx) => () => {
2022-05-08 01:08:07 +02:00
checkTixApiAccess(ctx);
2021-10-14 09:22:02 +02:00
return Object.values(StockSymbols);
},
getPrice: (ctx) => (_symbol) => {
const symbol = helpers.string(ctx, "symbol", _symbol);
checkTixApiAccess(ctx);
const stock = getStockFromSymbol(ctx, symbol);
2021-10-14 09:22:02 +02:00
return stock.price;
},
getOrganization: (ctx) => (_symbol) => {
const symbol = helpers.string(ctx, "symbol", _symbol);
checkTixApiAccess(ctx);
const stock = getStockFromSymbol(ctx, symbol);
return stock.name;
},
getAskPrice: (ctx) => (_symbol) => {
const symbol = helpers.string(ctx, "symbol", _symbol);
checkTixApiAccess(ctx);
const stock = getStockFromSymbol(ctx, symbol);
2021-10-14 09:22:02 +02:00
return stock.getAskPrice();
},
getBidPrice: (ctx) => (_symbol) => {
const symbol = helpers.string(ctx, "symbol", _symbol);
checkTixApiAccess(ctx);
const stock = getStockFromSymbol(ctx, symbol);
2021-10-14 09:22:02 +02:00
return stock.getBidPrice();
},
getPosition: (ctx) => (_symbol) => {
const symbol = helpers.string(ctx, "symbol", _symbol);
checkTixApiAccess(ctx);
const stock = SymbolToStockMap[symbol];
if (stock == null) {
throw helpers.makeRuntimeErrorMsg(ctx, `Invalid stock symbol: ${symbol}`);
}
return [stock.playerShares, stock.playerAvgPx, stock.playerShortShares, stock.playerAvgShortPx];
},
getMaxShares: (ctx) => (_symbol) => {
const symbol = helpers.string(ctx, "symbol", _symbol);
checkTixApiAccess(ctx);
const stock = getStockFromSymbol(ctx, symbol);
2021-10-14 09:22:02 +02:00
return stock.maxShares;
},
getPurchaseCost: (ctx) => (_symbol, _shares, _posType) => {
const symbol = helpers.string(ctx, "symbol", _symbol);
let shares = helpers.number(ctx, "shares", _shares);
const posType = helpers.string(ctx, "posType", _posType);
checkTixApiAccess(ctx);
const stock = getStockFromSymbol(ctx, symbol);
shares = Math.round(shares);
let pos;
const sanitizedPosType = posType.toLowerCase();
if (sanitizedPosType.includes("l")) {
pos = PositionTypes.Long;
} else if (sanitizedPosType.includes("s")) {
pos = PositionTypes.Short;
} else {
return Infinity;
}
2021-10-14 09:22:02 +02:00
const res = getBuyTransactionCost(stock, shares, pos);
if (res == null) {
return Infinity;
}
2021-10-14 09:22:02 +02:00
return res;
},
getSaleGain: (ctx) => (_symbol, _shares, _posType) => {
const symbol = helpers.string(ctx, "symbol", _symbol);
let shares = helpers.number(ctx, "shares", _shares);
const posType = helpers.string(ctx, "posType", _posType);
checkTixApiAccess(ctx);
const stock = getStockFromSymbol(ctx, symbol);
shares = Math.round(shares);
let pos;
const sanitizedPosType = posType.toLowerCase();
if (sanitizedPosType.includes("l")) {
pos = PositionTypes.Long;
} else if (sanitizedPosType.includes("s")) {
pos = PositionTypes.Short;
} else {
return 0;
}
const res = getSellTransactionGain(stock, shares, pos);
if (res == null) {
return 0;
}
return res;
},
buyStock: (ctx) => (_symbol, _shares) => {
const symbol = helpers.string(ctx, "symbol", _symbol);
const shares = helpers.number(ctx, "shares", _shares);
checkTixApiAccess(ctx);
const stock = getStockFromSymbol(ctx, symbol);
const res = buyStock(stock, shares, ctx, {});
return res ? stock.getAskPrice() : 0;
},
sellStock: (ctx) => (_symbol, _shares) => {
const symbol = helpers.string(ctx, "symbol", _symbol);
const shares = helpers.number(ctx, "shares", _shares);
checkTixApiAccess(ctx);
const stock = getStockFromSymbol(ctx, symbol);
const res = sellStock(stock, shares, ctx, {});
return res ? stock.getBidPrice() : 0;
},
buyShort: (ctx) => (_symbol, _shares) => {
const symbol = helpers.string(ctx, "symbol", _symbol);
const shares = helpers.number(ctx, "shares", _shares);
checkTixApiAccess(ctx);
if (player.bitNodeN !== 8) {
if (player.sourceFileLvl(8) <= 1) {
throw helpers.makeRuntimeErrorMsg(
ctx,
"You must either be in BitNode-8 or you must have Source-File 8 Level 2.",
);
2022-05-08 01:08:07 +02:00
}
}
const stock = getStockFromSymbol(ctx, symbol);
const res = shortStock(stock, shares, ctx, {});
return res ? stock.getBidPrice() : 0;
},
sellShort: (ctx) => (_symbol, _shares) => {
const symbol = helpers.string(ctx, "symbol", _symbol);
const shares = helpers.number(ctx, "shares", _shares);
checkTixApiAccess(ctx);
if (player.bitNodeN !== 8) {
if (player.sourceFileLvl(8) <= 1) {
2022-08-08 19:43:41 +02:00
throw helpers.makeRuntimeErrorMsg(
ctx,
"You must either be in BitNode-8 or you must have Source-File 8 Level 2.",
2022-08-08 19:43:41 +02:00
);
2022-05-08 01:08:07 +02:00
}
}
const stock = getStockFromSymbol(ctx, symbol);
const res = sellShort(stock, shares, ctx, {});
return res ? stock.getAskPrice() : 0;
},
placeOrder: (ctx) => (_symbol, _shares, _price, _type, _pos) => {
const symbol = helpers.string(ctx, "symbol", _symbol);
const shares = helpers.number(ctx, "shares", _shares);
const price = helpers.number(ctx, "price", _price);
const type = helpers.string(ctx, "type", _type);
const pos = helpers.string(ctx, "pos", _pos);
checkTixApiAccess(ctx);
if (player.bitNodeN !== 8) {
if (player.sourceFileLvl(8) <= 2) {
throw helpers.makeRuntimeErrorMsg(
ctx,
"You must either be in BitNode-8 or you must have Source-File 8 Level 3.",
);
2021-10-14 09:22:02 +02:00
}
}
const stock = getStockFromSymbol(ctx, symbol);
let orderType;
let orderPos;
const ltype = type.toLowerCase();
if (ltype.includes("limit") && ltype.includes("buy")) {
orderType = OrderTypes.LimitBuy;
} else if (ltype.includes("limit") && ltype.includes("sell")) {
orderType = OrderTypes.LimitSell;
} else if (ltype.includes("stop") && ltype.includes("buy")) {
orderType = OrderTypes.StopBuy;
} else if (ltype.includes("stop") && ltype.includes("sell")) {
orderType = OrderTypes.StopSell;
} else {
throw helpers.makeRuntimeErrorMsg(ctx, `Invalid order type: ${type}`);
}
const lpos = pos.toLowerCase();
if (lpos.includes("l")) {
orderPos = PositionTypes.Long;
} else if (lpos.includes("s")) {
orderPos = PositionTypes.Short;
} else {
throw helpers.makeRuntimeErrorMsg(ctx, `Invalid position type: ${pos}`);
}
2021-10-14 09:22:02 +02:00
return placeOrder(stock, shares, price, orderType, orderPos, ctx);
},
cancelOrder: (ctx) => (_symbol, _shares, _price, _type, _pos) => {
const symbol = helpers.string(ctx, "symbol", _symbol);
const shares = helpers.number(ctx, "shares", _shares);
const price = helpers.number(ctx, "price", _price);
const type = helpers.string(ctx, "type", _type);
const pos = helpers.string(ctx, "pos", _pos);
checkTixApiAccess(ctx);
if (player.bitNodeN !== 8) {
if (player.sourceFileLvl(8) <= 2) {
throw helpers.makeRuntimeErrorMsg(
ctx,
"You must either be in BitNode-8 or you must have Source-File 8 Level 3.",
);
2022-05-08 01:08:07 +02:00
}
}
const stock = getStockFromSymbol(ctx, symbol);
if (isNaN(shares) || isNaN(price)) {
throw helpers.makeRuntimeErrorMsg(
ctx,
`Invalid shares or price. Must be numeric. shares=${shares}, price=${price}`,
);
}
let orderType;
let orderPos;
const ltype = type.toLowerCase();
if (ltype.includes("limit") && ltype.includes("buy")) {
orderType = OrderTypes.LimitBuy;
} else if (ltype.includes("limit") && ltype.includes("sell")) {
orderType = OrderTypes.LimitSell;
} else if (ltype.includes("stop") && ltype.includes("buy")) {
orderType = OrderTypes.StopBuy;
} else if (ltype.includes("stop") && ltype.includes("sell")) {
orderType = OrderTypes.StopSell;
} else {
throw helpers.makeRuntimeErrorMsg(ctx, `Invalid order type: ${type}`);
}
const lpos = pos.toLowerCase();
if (lpos.includes("l")) {
orderPos = PositionTypes.Long;
} else if (lpos.includes("s")) {
orderPos = PositionTypes.Short;
} else {
throw helpers.makeRuntimeErrorMsg(ctx, `Invalid position type: ${pos}`);
}
const params = {
stock: stock,
shares: shares,
price: price,
type: orderType,
pos: orderPos,
};
return cancelOrder(params, ctx);
},
getOrders: (ctx) => () => {
2022-05-08 01:08:07 +02:00
checkTixApiAccess(ctx);
2021-10-14 09:22:02 +02:00
if (player.bitNodeN !== 8) {
if (player.sourceFileLvl(8) <= 2) {
2022-08-08 19:43:41 +02:00
throw helpers.makeRuntimeErrorMsg(ctx, "You must either be in BitNode-8 or have Source-File 8 Level 3.");
2021-10-14 09:22:02 +02:00
}
}
2022-07-20 01:08:22 +02:00
const orders: StockOrder = {};
2021-10-14 09:22:02 +02:00
const stockMarketOrders = StockMarket.Orders;
2022-01-16 01:45:03 +01:00
for (const symbol of Object.keys(stockMarketOrders)) {
2021-10-14 09:22:02 +02:00
const orderBook = stockMarketOrders[symbol];
if (orderBook.constructor === Array && orderBook.length > 0) {
orders[symbol] = [];
for (let i = 0; i < orderBook.length; ++i) {
orders[symbol].push({
shares: orderBook[i].shares,
price: orderBook[i].price,
type: orderBook[i].type,
position: orderBook[i].pos,
});
}
}
}
return orders;
},
getVolatility: (ctx) => (_symbol) => {
const symbol = helpers.string(ctx, "symbol", _symbol);
if (!player.has4SDataTixApi) {
throw helpers.makeRuntimeErrorMsg(ctx, "You don't have 4S Market Data TIX API Access!");
}
const stock = getStockFromSymbol(ctx, symbol);
2021-10-14 09:22:02 +02:00
return stock.mv / 100; // Convert from percentage to decimal
},
getForecast: (ctx) => (_symbol) => {
const symbol = helpers.string(ctx, "symbol", _symbol);
if (!player.has4SDataTixApi) {
throw helpers.makeRuntimeErrorMsg(ctx, "You don't have 4S Market Data TIX API Access!");
}
const stock = getStockFromSymbol(ctx, symbol);
let forecast = 50;
stock.b ? (forecast += stock.otlkMag) : (forecast -= stock.otlkMag);
return forecast / 100; // Convert from percentage to decimal
},
purchase4SMarketData: (ctx) => () => {
2021-10-14 09:22:02 +02:00
if (player.has4SData) {
2022-08-08 21:51:50 +02:00
helpers.log(ctx, () => "Already purchased 4S Market Data.");
2021-10-14 09:22:02 +02:00
return true;
}
2021-11-12 03:35:26 +01:00
if (player.money < getStockMarket4SDataCost()) {
2022-08-08 21:51:50 +02:00
helpers.log(ctx, () => "Not enough money to purchase 4S Market Data.");
2021-10-14 09:22:02 +02:00
return false;
}
player.has4SData = true;
2021-10-27 20:18:33 +02:00
player.loseMoney(getStockMarket4SDataCost(), "stock");
2022-08-08 21:51:50 +02:00
helpers.log(ctx, () => "Purchased 4S Market Data");
2021-10-14 09:22:02 +02:00
return true;
},
purchase4SMarketDataTixApi: (ctx) => () => {
2022-05-08 01:08:07 +02:00
checkTixApiAccess(ctx);
2021-10-14 09:22:02 +02:00
if (player.has4SDataTixApi) {
2022-08-08 21:51:50 +02:00
helpers.log(ctx, () => "Already purchased 4S Market Data TIX API");
2021-10-14 09:22:02 +02:00
return true;
}
2021-11-12 03:35:26 +01:00
if (player.money < getStockMarket4STixApiCost()) {
2022-08-08 21:51:50 +02:00
helpers.log(ctx, () => "Not enough money to purchase 4S Market Data TIX API");
2021-10-14 09:22:02 +02:00
return false;
}
player.has4SDataTixApi = true;
2021-10-27 20:18:33 +02:00
player.loseMoney(getStockMarket4STixApiCost(), "stock");
2022-08-08 21:51:50 +02:00
helpers.log(ctx, () => "Purchased 4S Market Data TIX API");
2021-10-14 09:22:02 +02:00
return true;
},
purchaseWseAccount: (ctx) => () => {
2022-03-19 04:58:18 +01:00
if (player.hasWseAccount) {
2022-08-08 21:51:50 +02:00
helpers.log(ctx, () => "Already purchased WSE Account");
2022-03-19 04:58:18 +01:00
return true;
}
if (player.money < getStockMarketWseCost()) {
2022-08-08 21:51:50 +02:00
helpers.log(ctx, () => "Not enough money to purchase WSE Account Access");
2022-03-19 04:58:18 +01:00
return false;
}
player.hasWseAccount = true;
initStockMarket();
2022-03-19 04:58:18 +01:00
player.loseMoney(getStockMarketWseCost(), "stock");
2022-08-08 21:51:50 +02:00
helpers.log(ctx, () => "Purchased WSE Account Access");
2022-03-19 04:58:18 +01:00
return true;
},
purchaseTixApi: (ctx) => () => {
2022-03-19 04:58:18 +01:00
if (player.hasTixApiAccess) {
2022-08-08 21:51:50 +02:00
helpers.log(ctx, () => "Already purchased TIX API");
2022-03-19 04:58:18 +01:00
return true;
}
if (player.money < getStockMarketTixApiCost()) {
2022-08-08 21:51:50 +02:00
helpers.log(ctx, () => "Not enough money to purchase TIX API Access");
2022-03-19 04:58:18 +01:00
return false;
}
player.hasTixApiAccess = true;
player.loseMoney(getStockMarketTixApiCost(), "stock");
2022-08-08 21:51:50 +02:00
helpers.log(ctx, () => "Purchased TIX API");
2022-03-19 04:58:18 +01:00
return true;
},
2021-10-14 09:22:02 +02:00
};
}