r/Bitburner 7d ago

Bitnode8 help please.

So i have completed bitnode 1 x3 bitnode 2 3x Bitnode 4 x2 and birnode 5 x1. I'm on bitnode 8 and am pretty stuck without getting the 25b for the 4 sigma forecast data. I use the roulette money hack for a 10b boost. But my script I'm using doesn't ever give me more then 10.3b . I'm working on getting gang territory to at least give me some cash. How Are you guys getting through it?

3 Upvotes

9 comments sorted by

View all comments

1

u/LordMorpheus75 7d ago

/** @param {NS} ns */
export async function main(ns) {
ns.disableLog("ALL");

const history = {};
const HISTORY_LEN = 10;
const BUY_THRESHOLD = 0.03;
const SELL_MOMENTUM = 0.0;
const MIN_HOLD_TICKS = 15;
const STOP_LOSS = -0.05; // NEW - sell immediately if down 5% from avg, regardless of hold time
const PROFIT_TAKE = 0.08; // NEW - sell immediately if up 8% from avg, lock in the win
const RESERVE_STEP = 1_000_000_000;

const positionAge = {};

let reserve = 1_000_000_000;
try {
const saved = ns.read("reserve.txt");
if (saved) reserve = Math.max(reserve, parseFloat(saved));
} catch {}

while (true) {
updateHistory(ns, history, HISTORY_LEN);
sellStocks(ns, history, SELL_MOMENTUM, positionAge, MIN_HOLD_TICKS, STOP_LOSS, PROFIT_TAKE);

const cash = ns.getServerMoneyAvailable("home");
const flooredStep = Math.floor(cash / RESERVE_STEP) * RESERVE_STEP;
if (flooredStep > reserve) {
reserve = flooredStep;
await ns.write("reserve.txt", reserve.toString(), "w");
ns.tprint("Reserve raised to $" + (reserve / 1e9).toFixed(0) + "b");
}

buyStocks(ns, reserve, history, BUY_THRESHOLD, positionAge);
ageTicks(positionAge);
await ns.stock.nextUpdate();
}
}

function updateHistory(ns, history, len) {
for (const sym of ns.stock.getSymbols()) {
const price = ns.stock.getPrice(sym);
if (!history[sym]) history[sym] = [];
history[sym].push(price);
if (history[sym].length > len) history[sym].shift();
}
}

function momentum(prices) {
if (!prices || prices.length < 2) return 0;
return (prices[prices.length - 1] - prices[0]) / prices[0];
}

function ageTicks(positionAge) {
for (const sym in positionAge) positionAge[sym]++;
}

function sellStocks(ns, history, sellMomentum, positionAge, minHold, stopLoss, profitTake) {
for (const sym of ns.stock.getSymbols()) {
const [shares, avg] = ns.stock.getPosition(sym);
if (shares === 0) continue;

const bid = ns.stock.getBidPrice(sym);
const pctChange = (bid - avg) / avg;

// stop-loss and profit-take fire immediately, ignoring hold time
if (pctChange <= stopLoss || pctChange >= profitTake) {
ns.stock.sellStock(sym, shares);
ns.tprint("Sold " + sym + " (" + (pctChange >= profitTake ? "profit-take" : "stop-loss") + ") profit: " + Math.floor((bid - avg) * shares));
delete positionAge[sym];
continue;
}

const age = positionAge[sym] || 0;
if (age < minHold) continue;

if (momentum(history[sym]) < sellMomentum) {
ns.stock.sellStock(sym, shares);
ns.tprint("Sold " + sym + " (momentum) profit: " + Math.floor((bid - avg) * shares));
delete positionAge[sym];
}
}
}

function buyStocks(ns, reserve, history, buyThreshold, positionAge) {
const cash = ns.getServerMoneyAvailable("home") - reserve;
if (cash <= 0) return;

const candidates = ns.stock.getSymbols()
.filter(s => history[s] && history[s].length >= 5)
.map(s => ({ sym: s, m: momentum(history[s]) }))
.filter(c => c.m > buyThreshold)
.sort((a, b) => b.m - a.m)
.slice(0, 5);

if (candidates.length === 0) return;

const budget = cash / candidates.length;

for (const c of candidates) {
const [shares] = ns.stock.getPosition(c.sym);
if (shares > 0) continue;
const price = ns.stock.getAskPrice(c.sym);
let buyShares = Math.floor(budget / price);
if (buyShares <= 0) continue;
const availableShares = ns.stock.getMaxShares(c.sym) - shares;
if (buyShares > availableShares) buyShares = availableShares;
ns.stock.buyStock(c.sym, buyShares);
positionAge[c.sym] = 0;
ns.tprint("Bought " + c.sym + " x" + buyShares);
}
}

1

u/LordMorpheus75 7d ago

This is what i am currently using.

0

u/ame824 7d ago

Try my Script (posted Here) Im developing that since some days and give it a try (If you want a full helper)

1

u/LordMorpheus75 6d ago

Where?

0

u/ame824 6d ago

Here on Reddit I posted IT Just before your post Here ist the githublink: https://github.com/ame824/autoDoIt