From 0b4968d148adaa386dc73bacc795131b6abfded0 Mon Sep 17 00:00:00 2001 From: danielyxie Date: Sun, 2 Jun 2019 20:28:02 -0700 Subject: [PATCH] Re-added the getStockPurchaseCost() and getStockSaleGain() functions so we don't break user scripts --- doc/source/netscript/netscriptixapi.rst | 2 + .../netscript/tixapi/getStockPurchaseCost.rst | 14 ++++++ .../netscript/tixapi/getStockSaleGain.rst | 14 ++++++ src/Constants.ts | 4 +- src/NetscriptFunctions.js | 46 +++++++++++++++++++ src/StockMarket/StockMarket.jsx | 3 ++ 6 files changed, 81 insertions(+), 2 deletions(-) create mode 100644 doc/source/netscript/tixapi/getStockPurchaseCost.rst create mode 100644 doc/source/netscript/tixapi/getStockSaleGain.rst diff --git a/doc/source/netscript/netscriptixapi.rst b/doc/source/netscript/netscriptixapi.rst index a836ade0a..76636efc5 100644 --- a/doc/source/netscript/netscriptixapi.rst +++ b/doc/source/netscript/netscriptixapi.rst @@ -22,6 +22,8 @@ access even after you 'reset' by installing Augmentations getStockBidPrice() getStockPosition() getStockMaxShares() + getStockPurchaseCost() + getStockSaleGain() buyStock() sellStock() shortStock() diff --git a/doc/source/netscript/tixapi/getStockPurchaseCost.rst b/doc/source/netscript/tixapi/getStockPurchaseCost.rst new file mode 100644 index 000000000..a686d1bca --- /dev/null +++ b/doc/source/netscript/tixapi/getStockPurchaseCost.rst @@ -0,0 +1,14 @@ +getStockPurchaseCost() Netscript Function +========================================= + + .. js:function:: getStockPurchaseCost(sym, shares, posType) + + :param string sym: Stock symbol + :param number shares: Number of shares to purchase + :param string posType: Specifies whether the order is a "Long" or "Short" position. + The values "L" or "S" can also be used. + :RAM cost: 2 GB + + Calculates and returns how much it would cost to buy a given number of + shares of a stock. This takes into account :ref:`spread ` + and commission fees. diff --git a/doc/source/netscript/tixapi/getStockSaleGain.rst b/doc/source/netscript/tixapi/getStockSaleGain.rst new file mode 100644 index 000000000..2dcbf8b78 --- /dev/null +++ b/doc/source/netscript/tixapi/getStockSaleGain.rst @@ -0,0 +1,14 @@ +getStockSaleGain() Netscript Function +===================================== + + .. js:function:: getStockSaleGain(sym, shares, posType) + + :param string sym: Stock symbol + :param number shares: Number of shares to sell + :param string posType: Specifies whether the order is a "Long" or "Short" position. + The values "L" or "S" can also be used. + :RAM cost: 2 GB + + Calculates and returns how much you would gain from selling a given number of + shares of a stock. This takes into account :ref:`spread ` + and commission fees. diff --git a/src/Constants.ts b/src/Constants.ts index 41a0985f0..49e72d2be 100644 --- a/src/Constants.ts +++ b/src/Constants.ts @@ -224,10 +224,10 @@ export let CONSTANTS: IMap = { v0.47.0 * Stock Market changes: ** Transactions no longer influence stock prices (but they still influence forecast) - ** Removed getStockPurchaseCost() and getStockSaleGain() Netscript functions - ** * Scripts now start/stop instantly + * Improved performance when starting up many copies of a new script (by Ornedan) + * Dialog boxes can now be closed with the ESC key (by jaguilar) v0.47.0 * Stock Market changes: diff --git a/src/NetscriptFunctions.js b/src/NetscriptFunctions.js index aa4c877dc..8ad911b79 100644 --- a/src/NetscriptFunctions.js +++ b/src/NetscriptFunctions.js @@ -98,6 +98,10 @@ import { cancelOrder, displayStockMarketContent, } from "./StockMarket/StockMarket"; +import { + getBuyTransactionCost, + getSellTransactionGain, +} from "./StockMarket/StockMarketHelpers"; import { OrderTypes } from "./StockMarket/data/OrderTypes"; import { PositionTypes } from "./StockMarket/data/PositionTypes"; import { StockSymbols } from "./StockMarket/data/StockSymbols"; @@ -1492,6 +1496,48 @@ function NetscriptFunctions(workerScript) { return stock.maxShares; }, + getStockPurchaseCost: function(symbol, shares, posType) { + updateDynamicRam("getStockPurchaseCost", getRamCost("getStockPurchaseCost")); + checkTixApiAccess("getStockPurchaseCost"); + const stock = getStockFromSymbol(symbol, "getStockPurchaseCost"); + 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; + } + + const res = getBuyTransactionCost(stock, shares, pos); + if (res == null) { return Infinity; } + + return res; + }, + getStockSaleGain: function(symbol, shares, posType) { + updateDynamicRam("getStockSaleGain", getRamCost("getStockSaleGain")); + checkTixApiAccess("getStockSaleGain"); + const stock = getStockFromSymbol(symbol, "getStockSaleGain"); + 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: function(symbol, shares) { updateDynamicRam("buyStock", getRamCost("buyStock")); checkTixApiAccess("buyStock"); diff --git a/src/StockMarket/StockMarket.jsx b/src/StockMarket/StockMarket.jsx index 42fc315a6..3dd1365a5 100644 --- a/src/StockMarket/StockMarket.jsx +++ b/src/StockMarket/StockMarket.jsx @@ -203,6 +203,9 @@ export function stockMarketCycle() { } else if (roll < 0.8) { stock.otlkMagForecast -= 0.5; stock.otlkMagForecast = stock.otlkMagForecast * (1 / 1.02); + } else if (roll < 0.9) { + stock.b = !stock.b; + stock.flipForecastForecast(); } } }