Advertisement
leofev

BitBurner 2.0.1 Stock Script /w Shorts

Aug 18th, 2022 (edited)
547
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
JavaScript 2.69 KB | Source Code | 0 0
  1. // u/RocketChap 's stock script.
  2. // Game version 2.0.1
  3. // Built upon u/ferrus_aub stock script by battering it the other way around with a turtle shell
  4. /** u/param {NS} ns **/
  5. export async function main(ns) {
  6. var maxSharePer = 1.00
  7. var stockBuyPer = 0.60
  8. var stockShortBuyPer = 0.35 //Testing this still! Too high, and buying pushes its forecast over sell threshold
  9. var stockVolPer = 0.05
  10. var moneyKeep = 1e9 //Set this to your liking! Keeps a reserve of cash on hand.
  11. var minSharePer = 50 //Too high leaves opportunities untouched, too low wastes money on transaction costs!
  12. while (true) {
  13. ns.disableLog('disableLog');
  14. ns.disableLog('sleep');
  15. ns.disableLog('getServerMoneyAvailable');
  16. var stocks = ns.stock.getSymbols()
  17. for (const stock of stocks) {
  18. var position = ns.stock.getPosition(stock);
  19. if (position[0]) {
  20. //ns.print('Position: ' + stock + ', ')
  21.                 sellPositions(stock);
  22. }
  23.             buyPositions(stock);
  24. if (position[2]) {
  25. //ns.print('Position: ' + stock + ', ')
  26.                 sellShortPositions(stock);
  27. }
  28.             buyShortPositions(stock);
  29. }
  30. ns.print('Cycle Complete');
  31. await ns.sleep(6000);
  32. }
  33. function buyPositions(stock) {
  34. var maxShares = (ns.stock.getMaxShares(stock) * maxSharePer) - position[0];
  35. var askPrice = ns.stock.getAskPrice(stock);
  36. var forecast = ns.stock.getForecast(stock);
  37. var volPer = ns.stock.getVolatility(stock);
  38. var playerMoney = ns.getServerMoneyAvailable('home');
  39.  
  40. if (forecast >= stockBuyPer && volPer <= stockVolPer) {
  41. if (playerMoney - moneyKeep > ns.stock.getPurchaseCost(stock,minSharePer, "Long")) {
  42. var shares = Math.min((playerMoney - moneyKeep - 100000) / askPrice, maxShares);
  43. ns.stock.buyStock(stock, shares);
  44. //ns.print('Bought: '+ stock + '')
  45. }
  46. }
  47. }
  48. function sellPositions(stock) {
  49. var forecast = ns.stock.getForecast(stock);
  50. if (forecast < 0.5) {
  51. ns.stock.sellStock(stock, position[0]);
  52. //ns.print('Sold: '+ stock + '')
  53. }
  54. }
  55. function buyShortPositions(stock) {
  56. var maxShares = (ns.stock.getMaxShares(stock) * maxSharePer) - position[2];
  57. var askPrice = ns.stock.getAskPrice(stock);
  58. var forecast = ns.stock.getForecast(stock);
  59. var volPer = ns.stock.getVolatility(stock);
  60. var playerMoney = ns.getServerMoneyAvailable('home');
  61.  
  62. if (forecast <= stockShortBuyPer && volPer <= stockVolPer) {
  63. if (playerMoney - moneyKeep > ns.stock.getPurchaseCost(stock,minSharePer, "Short")) {
  64. var shares = Math.min((playerMoney - moneyKeep - 100000) / askPrice, maxShares);
  65. ns.stock.buyShort(stock, shares);
  66. //ns.print('Shorted: '+ stock + '')
  67. }
  68. }
  69. }
  70. function sellShortPositions(stock) {
  71. var forecast = ns.stock.getForecast(stock);
  72. if (forecast > 0.5) {
  73. ns.stock.sellShort(stock, position[2]);
  74. //ns.print('Sold short: '+ stock + '')
  75. }
  76. }
  77. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement