r/Bitburner • • Dec 10 '21

Announcement Steam release

386 Upvotes

The game has launched on Steam. Please give it a review. :)


r/Bitburner • • Jan 15 '22

I am an idiot

Post image
271 Upvotes

r/Bitburner • • Mar 25 '22

It's my first day

Post image
265 Upvotes

r/Bitburner • • Jan 23 '22

when n00dles uses a whopping 2 threads on a target

Post image
215 Upvotes

r/Bitburner • • Jan 07 '22

let memeObj;

Post image
188 Upvotes

r/Bitburner • • Jan 18 '22

Me trying to write a server name list scanner

Post image
176 Upvotes

r/Bitburner • • Jan 10 '22

The struggle is real and eternal.

Post image
171 Upvotes

r/Bitburner • • Dec 22 '21

The BEST hacking approach I've seen so far

157 Upvotes

I wrote this in a comment a day or two ago. Figured I make it a main post

-------------------------------------------------------------------------------------------------------------------------------------

Weak, Hack, and Grow only affect the server *once they execute*.Time for execution is calculated at *launch of the process*

You can run multiple Weak, Hack, and Grow commands on the same server *in parallel*.

For example, if you've fully weaken and grown your servers first (which you must do for this to work) you could get something like this

WeakenTime = 1 minuteGrowTime = 45 secondHackTime = 15 seconds

You can execute a command to weaken the server, grow the server (with a sleep that is the difference of WeakenTime and GrowTime) and hack (with a sleep that is the difference between WeakenTime and HackTime).

Then you adjust the sleeps by 100-200ms, making sure Hack fires first ( which needs to only take 50% of the funds), then Grow (which needs to grow at least 100% to replace funds hacked) then Weaken which will reset the security level back to the minimum.

Then repeat that script every 1 second, and you'll get 50% of the server's maximum money per second.

-------------------------------------------------------------------------------------------------------------------------------------

Below are the results of my implementationHack Level 1860BN1.2, I've only completed 1 nodeTime played since last Augmentation: 10 hours 6 minutes 3 secondsTime played since last Bitnode destroyed:1 days 15 hours 20 minutes 9 secondsTotal Time played: 6 days 10 hours 49 minutes 33 seconds

Hacking Chance multiplier: 177.55%Hacking Speed multiplier: 186.78%Hacking Money multiplier: 356.01%Hacking Growth multiplier: 195.44%

Using my methodology nets some INSANE money/s. And it really, really, really shines on the largest hosts. That number isn't even accurate yet and is still growing.

I believe I've identified a logic miss in how I'm handling the spacing. It works beautifully the first round through but after that I believe it can step on its own toes and get out of sync (not that it appears to be hurting much for it). I still want to try and perfect it. Also, I'm not a coder and my script is un-commented, doesn't use functions, and is probably wildly inefficient :)

EDIT: It appears the application has a setting that dictates how long to wait for each script. Default is 25ms. I lowered mine to 5ms and quintupled my output. Not sure if every machine can handle it though

EDIT2: I'm working on an update script that will fix some of my mistakes (noted in the comments below, mainly the grow percentage and my weaken threads) as well as try and account for the events getting out of sync. In troubleshooting last night I can see that when some of my events launch after their sleep time the server is not at its minimal security level which is throwing everything out of whack. Seems to happen more for servers with more than 1 second between cycles. Still trying to figure out what's causing it though, as its not consistent. I can see it happen after 24 weaken instances(PIDs), but then kill all and run the same script again and it won't happen for 174 instances... :(

Also working on making a script that automatically picks the best server to hack based on server RAM size, as we need to take into account the amount of concurrent hack attempts we can do against as server as well as its max money. This includes growth amount as that means we need less threads, so more hack attempts in a cycle.

FINAL EDIT: Due to the holidays I haven't have much time to work on it. And, since I've moved onto BN4.1 and have access to the singularity API my interests have now changed :). At this point it works well enough to get moar money, quickly, than most people need. Some ideas I had if someone else wants to mess with it.

  • Have a single instance attack more than 1 server at a time. So , round-robin between servers, doing a single weaken against the server once a run in complete. That would stop the script from stepping on the toes of other scripts.

  • Someone mentioned timestamps in the comments. You could calculate the execution time of each process, write the timestamp to an array. Then you could have each process check its execution time against that array and if it sees a matching timestamp, kill that runs scripts.

Also, I have my 'Best Server' calculator script. It could easily be added to the OP.ns script to be called on launch to auto-find the best server to attack. Currently just requires the ram of the server, then it will log out the list of servers, best to worst, based on if the server has been rooted, max money available, and how many threads (hence cycles) the OPs script can run against it. IE. If one host has 1 million max money, and another has 2 million max money, it would seem like the best server to hack would be the 2 million. However, its possible you can run more than twice as many attacks against the 1 million server than the 2 million server based on security level, so the 1 million server would give you more money.

Best Server to attack using my script... script https://pastebin.com/CZb41hYm


r/Bitburner • • Dec 24 '21

me irl

Post image
139 Upvotes

r/Bitburner • • Apr 01 '22

Giant game overhaul

119 Upvotes

1.7.0 2022-04-01 - Giant overhaul

** Platform **

  • Available on the Atari 2600 and flipphones.

** Hacking **

  • Now gated by a real life subscription to Nord-west VPN

  • Is now considered rude, thus lowers karma.

** PVP **

  • Added PVP, you can now hack other players.

** New location **

  • Added new location "Outer Space" to the travel agency. Traveling to Outer Space results in the player instantly dying, resetting your save file.

** UI **

  • Upgraded to support VR

  • Removed all whitespace from the UI to make room for added features

  • Defaults to light theme

  • UI moved up 3px to accomodate average player height

** Bladeburner **

  • Bladeburner now scales off of your INT stat instead of physical stats. Stat requirements remain unchanged.

** API **

  • No longer supports javascript, now supports python, befunge, and whitespace.

  • Added ‘willScriptHalt’ (collecting my nobel prize soon)

** Stock Market **

  • The stock market now has a net worth of $25, allowing players to buy large portions of the market earlier in the game

** Hashnet **

  • Hashes can no longer be spent

** Keystrokes **

  • Combat stats now tied to keyboard stroke (press hard = more strength, type quickly = more agi)

** Bug Fix **

  • Fixed a long-standing bug where installing augs wiped scripts on all servers except home. Now wipes home as well.

  • Fixed bug where buying aug didn't cost any reputation.

** Misc **

  • Updated ToS (Terms of Service): For legal reasons, we must now inform your employer if you are playing Bitburner during business hours.

  • Faction rep/favour now decreases if you purchase one of their augs from another faction.

  • Connections to remote servers only last 5 seconds, so you have to hack faster

  • Added "Hunger" stat, must eat noodles every 8 hours to avoid hospitalization.

  • Copy-pasting has been disabled to prevent players from cheating

  • Gain 1 entropy virus every time a script errors.

  • Can only enter a BN if the current day of the month matches the BN number. (More BNs in progress to make it possible to play in the second half of the month)

  • Players now start in BN8

  • Bathroom breaks will now be timed

  • Mouse support deprecated

  • Scripts can be minted and sold on the Steam marketplace.

  • In-game location can now be purchased by sponsors.

  • Sponsored by Roids legendary shadows

  • Added in-app purchases (1$ in game for 1$ IRL)

  • Removed tutorial and documentation, just read the source I’m tired of updating docs.

  • Sleeves now go rogue when their shock reaches 0

  • Added Gigabyteburner3000 inside Megabyteburner2000.

  • BN12 now gives levels of QLink instead of Neuroflux.

  • Gang and Corporation now drain money from the player.

  • Joe from joesguns found a rocket launcher and will now hunt the player.

  • Boost noodle bar


r/Bitburner • • Jan 22 '22

Finally finished my control script logs formatting!

Post image
117 Upvotes

r/Bitburner • • Feb 08 '22

I just got to say, this is one of the best games I've ever played.

114 Upvotes

This game just feels so free form. It has a surprising amount of depth and can get as complex as you make it. Im between 150%-250% on my augmentations, 2pb of home ram with 6 cores, and feel like I'm just scratching the surface. I've got my hacking scripts to be pretty efficient but I've not even touched the stock market yet. There seem to be tons of things I still haven't unlocked either. Right now I'm mostly being bottle necked by faction rep.

There is just way more that I expected from this. At first I thought it was going to be a dinky little button clicker in a "terminal" where you watch a couple numbers go up. Its way more than that though! Also, even though programming is my day job this has defiantly helped exercise my skills. Once I finish it I might even be motivated to expand on it and I haven't done mods for games since Minecraft beta. Thanks a ton for making this game!


r/Bitburner • • Feb 03 '22

Cobbled together some ascii charts just for kicks

Post image
113 Upvotes

r/Bitburner • • Jul 16 '22

Well played developers, well played...

Post image
112 Upvotes

r/Bitburner • • Dec 09 '22

Doom in Bitburner

Post image
109 Upvotes

r/Bitburner • • Dec 21 '21

Discord > Reddit

109 Upvotes

You'll get help faster on discord

https://discord.gg/TFc3hKD

I can't be everywhere at once.


r/Bitburner • • Jan 13 '25

this proud moment took a little under 100 days as a very new programmer

Post image
106 Upvotes

r/Bitburner • • Sep 15 '22

I keep forgetting to play the game

Post image
93 Upvotes

r/Bitburner • • Dec 16 '21

NetscriptJS Script Scan Script updated for Bitburner v1.1.0 Spoiler

91 Upvotes

I've updated the excellent Scan Script by u/havoc_mayhem to work in the current version.

Note these new scripts are quite RAM heavy and require ~33GB of RAM free to use.

The previous version: https://www.reddit.com/r/Bitburner/comments/9nrz3v/scan_script_v2/

Features:

  • Lists every single server, irrespective of depth.

  • The servers you need to hack manually are highlighted in color.

  • Click on any server name to instantly connect to that server. There is no need to manually type in anything.

  • A small square before the server name shows if you have root access.

  • Hover over a server name, to pull up all relevant details about it. Example.

  • There's a purple '©' symbol next to servers with Coding Contracts on them, if you want to go over and solve the contract manually.

  • Hover over the '©' symbol to see what kind of contract it is. Example.

scan.js (32.75GB) This can be reduced to 27.75GB if you comment out the function that gets the Contract Name

I would recommend setting up the alias: alias scan="home; run scan.js"

let facServers = {
    "CSEC" : "yellow",
    "avmnite-02h" : "yellow",
    "I.I.I.I" : "yellow",
    "run4theh111z" : "yellow",
    "The-Cave" : "orange",
    "w0r1d_d43m0n" : "red"
};

let svObj = (name = 'home', depth = 0) => ({ name: name, depth: depth });
export function getServers(ns) {
    let result = [];
    let visited = { 'home': 0 };
    let queue = Object.keys(visited);
    let name;
    while ((name = queue.pop())) {
        let depth = visited[name];
        result.push(svObj(name, depth));
        let scanRes = ns.scan(name);
        for (let i = scanRes.length; i >= 0; i--) {
            if (visited[scanRes[i]] === undefined) {
                queue.push(scanRes[i]);
                visited[scanRes[i]] = depth + 1;
            }
        }
    }
    return result;
}

export async function main(ns) {
    let output = "Network:";

    getServers(ns).forEach(server => {
        let name = server.name;
        let hackColor = ns.hasRootAccess(name) ? "lime" : "red";
        let nameColor = facServers[name] ? facServers[name] : "white";
        
        let hoverText = ["Req Level: ", ns.getServerRequiredHackingLevel(name),
            "
Req Ports: ", ns.getServerNumPortsRequired(name),
            "
Memory: ", ns.getServerRam(name)[0], "GB",
            "
Security: ", ns.getServerSecurityLevel(name),
            "/", ns.getServerMinSecurityLevel(name),
            "
Money: ", Math.round(ns.getServerMoneyAvailable(name)).toLocaleString(), " (", 
            Math.round(100 * ns.getServerMoneyAvailable(name)/ns.getServerMaxMoney(name)), "%)"
            ].join("");
        
        let ctText = "";
        ns.ls(name, ".cct").forEach(ctName => {
            ctText += ["<a title='", ctName,
                //Comment out the next line to reduce footprint by 5 GB
                "&#10;", ns.codingcontract.getContractType(ctName, name),
                "'>©</a>"].join(""); 
        });
               
        output += ["<br>", "---".repeat(server.depth),
            `<font color=${hackColor}>■ </font>`,
            `<a class='scan-analyze-link' title='${hoverText}''

            onClick="(function()
            {
                const terminalInput = document.getElementById('terminal-input');
                terminalInput.value='home; run connect.js ${name}';
                const handler = Object.keys(terminalInput)[1];
                terminalInput[handler].onChange({target:terminalInput});
                terminalInput[handler].onKeyDown({keyCode:13,preventDefault:()=>null});
            })();"
        
            style='color:${nameColor}'>${name}</a> `,
            `<font color='fuchisa'>${ctText}</font>`,
            ].join("");
    });

    const list = document.getElementById("terminal");
    list.insertAdjacentHTML('beforeend',output);
}

Connect.js enables you to directly connect to a server when you use the scan command by simply clicking on a server.

It can also be used separately to connect you to any server without worrying about how to navigate to it.

Usage: run connect.js SERVER

E.g. 'run connect.js run4theh111z' - Directly connects to the run4theh111z server.

I would recommend setting up the alias: alias connect="home; run connect.js"

connect.js (26.8GB)

export async function main(ns) {
    let target = ns.args[0];
    let paths = { "home": "" };
    let queue = Object.keys(paths);
    let name;
    let output;
    let pathToTarget = [];
    while ((name = queue.shift())) {
        let path = paths[name];
        let scanRes = ns.scan(name);
        for (let newSv of scanRes) {
            if (paths[newSv] === undefined) {
                queue.push(newSv);
                paths[newSv] = `${path},${newSv}`;
                if (newSv == target)
                    pathToTarget = paths[newSv].substr(1).split(",");
                    
            }
        }
    }
    output = "home; ";

    pathToTarget.forEach(server=> output += " connect " + server + ";");

    const terminalInput = document.getElementById("terminal-input");
    terminalInput.value=output;
    const handler = Object.keys(terminalInput)[1];
    terminalInput[handler].onChange({target:terminalInput});
    terminalInput[handler].onKeyDown({keyCode:13,preventDefault:()=>null});
}

export function autocomplete(data, args) {
    return [...data.servers];
}

Edit: Thanks u/h41nr1ch for picking up on the exception when clicking on home, I added the fix.
Thanks u/LangyMD for the suggestion of adding autocomplete. Note that autocomplete doesn't currently work with alias's.


r/Bitburner • • Mar 16 '22

Music Player

85 Upvotes

r/Bitburner • • Apr 22 '22

Pulling off a big hack be like

82 Upvotes

r/Bitburner • • Feb 22 '22

Script to export the network as flow chart for mermaid

Post image
81 Upvotes

r/Bitburner • • Jan 26 '22

Phoenix OS (source included)

Thumbnail
gallery
81 Upvotes

r/Bitburner • • Dec 31 '21

NetscriptJS Script BitNode 8: StockMarket algo trader script without 4S Market Data Access

81 Upvotes

Sorry for the long post, people who are only interested in the solution can scroll down and copy the final script, but this time I think the journey was more important than the destination.

I'm a Data Scientist, working in finance, so I felt this challenge was very fitting trying to model and exploit the stock market simulation without any fancy stuff such as manipulating the market with hack/grow cycles.

Understanding the game

I've dug through the game source code, most related files can be found here: SourceCode

Few things to take note:

  • At any given "tick" each stock changes:

    • The game first determines if it increases or decreases, which is based on some internal, unknown probability, called Outlook Magnitude.

    • Next it determines the volume of change which is again stochastic, based on the maximum volatility of the stock.

  • This unknown probability also changes over time, because of two factors:

    • Bull or Bear: changes the general direction of change (e.g.: 60% chance to increase would become 40% meaning it is more likely to decrease). These are steep changes in probability, but they are seldom, happening at about every 50-100 ticks.

    • Outlook Magnitude Forecast: defines the value Outlook Magnitude will tend to over time. These changes are gradually happening every tick, but these are also stochastic. Even the forecast is changing stochastically.

Based on the first impressions my general thought was that we can have an estimate of the underlying Outlook Magnitude based on past observations, as the changes are either seldom or slow enough so we can assume it to be constant given a limited a time-frame.

I've also created multiple testing environments to test my models. I've captured "live" data to be able to replay them in these test environments. I've also created a generic algo trader that only needed a good predictive model so I could change approaches separately.

Neural Networks

I've started with capturing the changes for a long period (200 ticks) for all the stocks. I've made about 5 such observations so I would have enough data to test my model performances without waiting 6 seconds for every tick to happen.

I knew that I could bring in some fancy stochastic model for these observations but I just went with the easier approach what I do in work anyways.

I tried some MLP Neural Network models first, the Tanh layer also seemed ideal to output change percentages between [-1.0, 1.0]. I wanted the model to use past data to try to predict probable future changes. These models are also easy to implement even without using any external library after you get the trained weights.

To make the models more robust, I've transformed the incoming past prices to relative changes, e.g.: [5200, 5250, 5170] would become [+0.0096, -0.0152], as 5250 = 5200 * (1+0.0096), and 5170 = 5250 * (1-0.0152). These vector of changes would become the inputs for the network.

The desired output was the subsequent change in the time-series data. Note that the next change is again stochastic e.g.: even if all the input data is increasing there is a chance that the next change would be negative, so I've used the mean of the upcoming few data-points to have more robust training data. Again, I could not sample too much here, because of the changing nature of the underlying probability.

These details would become hyperparameters during my training along with the model parameters. I've used 2 or 3 depth of Linear layers with or without bias and Tanh activations. I've varied the pre-prediction sampling length between 3 and 20, and the post-prediction sampling length between 1 and 5.

Even with these transformations the models had really hard time learning. It is probably the noisy environment with the occasional steep changes out of thin air. Even if the validation loss was considerably small, it always generated loss on my test-benchmarks on the replayed market-data and performed even worse in the game.

Intuitive approaches

I had plenty of time while the models were training, so I've started with some intuitive models. I did not wanna do the math yet, I was a bit lazy :)

I knew I was more interested in the ratio of positive vs. negative price changes but as time goes on, the past data becomes less reliable. I've created a model that counts the positive/negative occurrences with some weighting. Something like this Python code:

def sign(change):
    return 1 if change > 0 else (-1 if change < 0 else 0)
    
sum(sign(change) * (weight)**idx for idx, change in enumerate(reversed(changes))) 

If weight is 1, this just becomes a simple change counting. When I've executed a hyperparameter search to find a good weight, I somehow got poor results for all weights. This was probably due to some bug in my evaluation code. Anyways, I went past this approach, however now I think this should have been the best.

Another approach that is worth mentioning is the idea of when to sell your shares. Let's say we see that for our long positions the prices would go down. Because of the stochastic nature the stocks can still go up during this recession. It might also turn out that it was just bad luck that we've observed decreases and we might want to introduce a short wait to see if it starts increasing again.

In literature it is called the Secretary Problem. What is the best strategy to find the best candidate, if you know that you would wait for at most N candidates but you can never go back? First, you need to check the first N * 36.8% candidates and just let them go. After doing that, stop at the one that is better than everyone else you have seen in the first portion, or wait until the last candidate if you are unlucky. It is a known result that this strategy would most likely get you the best candidate, about 36.8% chance.

Unfortunately introducing such waiting mechanism proved to be futile, as these would assume that candidates (good or bad) are uniformly distributed, which is definitely not the case as you are already seeing a decreasing tendency. Based on experiments, you would not want to wait any time in general if your predictive model is good, except for a few misbehaving ones.

Stochastic approach

As neither combination of my approaches seemed to even pass my local evaluation environment I knew it was time to use the heavy-hitters and create my own stochastic model. I really wanted to avoid it as it almost always introduces some nasty integrals and I really hate integrals.

I kept my assumptions that there is an underlying P_inc probability, if the stock would increase or (1-P_inc) chance to decrease, and this probability can be assumed to be constant if you only use a few observations. The more observations you would make the better you can estimate P_inc, but the more it would vary over time. This would add additional noise and thus make your estimate less reliable. A hyper-parameter search can fix this problem later to find the best number of samples to take.

When you have a single probability to go either one way or another and want to estimate it from observations you always want to use Beta distribution. It has two parameters, a which is 1 + no. of first event and b which is 1 + the no. of the other event observed. The definite integral ∫ Beta(x, a, b) dx from 0 to x_0 would produce the chance that given (a-1) and (b-1) observations, what is the probability that P_inc < x_0. Key properties:

∫ Beta(x, a, b) dx = 1

∫ x * Beta(x, a, b) dx = a/(a+b)

In the source code we can find, how the prices are actually calculated:

if (Math.random() < P_inc) {
   price = price * (1+volatility);
} else {
   price = price / (1+volatility);
}

As division gets messy in integrals, let's do a slight trick. As volatility << 1, we can use the geometric series sum to use nicer estimates:

1 / ( 1 - (-r)) = 1 + (-r) + (-r)2 + (-r)3 + ...

The higher order terms becomes negligible if the volatility is close to 0, which is applicable, meaning in our case:

price / (1+vol) ≈ price * (1-vol)

We are interested in the expected value of a change after each tick, which is:

price_1 = P_inc * price_0 * (1+vol) + (1-P_inc) * price_0 * (1-vol)

= price_0 * (P_inc + P_inc * vol + 1 - P_inc - vol + P_inc * vol)

= price_0 * (1 - vol + 2 * vol * P_inc)

We don't know what P_inc is, but we can account for every possible value using this integral:

price_1 = ∫ price_0 * (1 - vol + 2 * vol * P_inc) Beta(P_inc, a, b) dP_inc

Rearranging the integral we can move the unrelated terms outside:

price_1 = price_0 * ∫ (1 - vol + vol * 2 * P_inc) Beta(P_inc, a, b) dP_inc

= price_0 * [ (1 - vol) ∫ Beta(P_inc, a, b) dP_inc + 2 vol ∫ P_inc Beta(P_inc, a, b) dP_inc

= price_0 * [ (1 - vol) * 1 + 2 * vol * a / (a+b) ]

Which gives the solution, that:

price_1 = price_0 + price_0 * vol * (2a/(a+b) - 1)

We are interested in the relative change:

(price_1 - price_0)/price_0 = vol * (2a/(a+b) - 1)

Which is in Python:

def expected_change(changes):
   a = sum(change > 0 for change in changes) + 1
   b = sum(change < 0 for change in changes) + 1
   vol = sum(abs(change) for change in changes) / len(changes)
   return vol * (2*a/(a+b) - 1)

Fortunately, the model turned out the be neat and the performance was super. I just had to sell my shares when the expected change was below a certain threshold and buy the one which I expected to change the most. Same goes for shorting but for the other direction. I've did a hyperparameter search on my validation data, to find out that I should use about 12 past changes and the threshold is around 0.002 and it produced about 60% profit when I've executed on my evaluation datasets.

Unfortunately, it turned out to be a disaster when I ran the algo-trader in game.

In-depth game analysis

After plotting and analysing several diagrams what happened in game, I decided to take a deeper dive to understand the in-game stock-market mechanics. It turned out that my initial assumption that stock prices are changing slowly is not true at all. There is a part in the game code, which lowers the Outlook Magnitude, based on the number of shares you've just bought, which essentially draws P_inc probability towards 50%. This change might even surpass 50% in which case the general direction would also change, changing the stock from Bull to Bear or Bear to Bull.

What happened is that when I replayed my static time-series data during my evaluations, it could not simulate the same effect. My algo-trader found the stock that changed the most, and bought all shares it could. In game, this triggered the "pull to 50%" effect, which not just changed the prices to the opposite direction as expected but my algo-trader immediately sensed it as it bet on the wrong horse, time to sell the shares even if it means loss. And it meant loss most of the time.

This let me down, but fortunately, I've also found additionally information to find out that a simpler and more robust algorithm can succeed. The P_inc is essentially computed in the following way: P_inc = BullOrBear ? 0.5 + OutlookMagnitude/100 : 0.5 - OutlookMagnitude/100. There is also another part, which ensures that Outlook Magnitude is at least 5%, which means that we can expect that P_inc is either less than 0.45 or greater than 0.55 depending on whether the stock is bear or bull.

Second Stochastic model

I've rejected the need to determine the volume of expected change. From my in-depth game analysis I've concluded that I should not bother finding out what is the expected change of the given stock, it is sufficient if I can conclude that a stock is either Bull or Bear, because:

  • The game makes sure that it stays in that state for a generally long period, with a minimum expected change that I should be able to pull some profit from it.

  • The game also makes sure that I would be able to distinguish Bear or Bull state given the 10% difference of P_inc chance (<0.45 or >0.55).

  • Finally compound interest makes sure that I'll make an awful lot of money if I can pull some percentage of profit consistently.

You would also not buy shares indefinitely to avoid changing Bull and Bear states. I've achieved this buy using high priced stocks (which means higher market capitalization) to be sure that my stock-trading does not have that much effect on prices and hard-limiting the number of shares bought and the frequency of trading.

My second stochastic model used Beta distribution again but for another use this time. In Data Science, it is also a widely used tool to test assumptions with some confidence level. I wanted to know with 98% confidence, that if I observe X positive changes and Y negative changes that stock is not in Bear state (<0.45). I initially used 95% confidence level, but changed it later as 5% chance to miss-label the current state turned out to be a bit too much. One can also argue to test against the more strict "stock is in a Bull state" statement (>0.55). There is also a reasonable amount of uncertainty, when we cannot tell for needed confidence level what state we are in. One can also argue if you want to sell the stocks the moment you are not certain that your are in the required state anymore, or you want to wait until you are certain that you are in the wrong state. I've used the later approach, as I would limit my trading frequency as much as possible as discussed above.

Normally you would need statistic libraries to compute this, so I precomputed in Python the number of positive observations needed for the different number of total observations to determine with certainty that stock is not in Bear state:

from scipy.stats import beta

if __name__ == '__main__':
    max_total = 30
    increasing = []
    for total in range(1, max_total+1):
        limit = None
        for i in range(0, total+1):
            if 1-beta.cdf(0.45, i+1, total-i+1) > 0.98:
                limit = i
                break
        increasing.append(limit)
    print(increasing)

Beta distribution is symmetric in a sense that event-types are interchangeable, which means that I can use the same values to determine both Bull and Bear state by swapping them. I would also want to determine changes of state as soon as possible, so I've modified the test that if there is sub-sequence at the tail of price changes that can be used to determine if it is either Bull or Bear, use that early result.

Finally, let me share my code. It has produced 35 billions overnight, which is enough to finally buy the 4S Market Data Access, so I don't have to rely on chance anymore :)

const commission = 100000;
const samplingLength = 30;

function predictState(samples) {
  const limits = [null, null, null, 4, 5, 6, 6, 7, 8, 8, 9, 10, 10, 11, 11, 12, 12, 13, 14, 14, 15, 15, 16, 16, 17, 17, 18, 19, 19, 20];
  let inc = 0;
  for (let i = 0; i < samples.length; ++i) {
    const total = i + 1;
    const idx = samples.length - total;
    if (samples[idx] > 1.) {
      ++inc;
    }
    const limit = limits[i];
    if (limit === null) {
      continue;
    }
    if (inc >= limit) {
      return 1;
    }
    if ((total-inc) >= limit) {
      return -1;
    }
  }
  return 0;
}

function format(money) {
    const prefixes = ["", "k", "m", "b", "t", "q"];
    for (let i = 0; i < prefixes.length; i++) {
        if (Math.abs(money) < 1000) {
            return `${Math.floor(money * 10) / 10}${prefixes[i]}`;
        } else {
            money /= 1000;
        }
    }
    return `${Math.floor(money * 10) / 10}${prefixes[prefixes.length - 1]}`;
}

function posNegDiff(samples) {
  const pos = samples.reduce((acc, curr) => acc + (curr > 1. ? 1 : 0), 0);
  return Math.abs(samples.length - 2*pos);
}

function posNegRatio(samples) {
  const pos = samples.reduce((acc, curr) => acc + (curr > 1. ? 1 : 0), 0);
  return Math.round(100*(2*pos / samples.length - 1));
}

export async function main(ns) {
    ns.disableLog("ALL");
    let symLastPrice = {};
    let symChanges = {};
    for (const sym of ns.stock.getSymbols()) {
      symLastPrice[sym] = ns.stock.getPrice(sym);
      symChanges[sym] = []
    }

    while (true) {
        await ns.sleep(2000);

        if (symLastPrice['FSIG'] === ns.stock.getPrice('FSIG')) {
          continue;
        }

        for (const sym of ns.stock.getSymbols()) {
          const current = ns.stock.getPrice(sym);
          symChanges[sym].push(current/symLastPrice[sym]);
          symLastPrice[sym] = current;
          if (symChanges[sym].length > samplingLength) {
            symChanges[sym] = symChanges[sym].slice(symChanges[sym].length - samplingLength);
          }
        }

        const prioritizedSymbols = [...ns.stock.getSymbols()];
        prioritizedSymbols.sort((a, b) => posNegDiff(symChanges[b]) - posNegDiff(symChanges[a]));
        
        for (const sym of prioritizedSymbols) {
          const positions = ns.stock.getPosition(sym);
          const longShares = positions[0];
          const longPrice = positions[1];
          const shortShares = positions[2];
          const shortPrice = positions[3];
          const state = predictState(symChanges[sym]);
          const ratio = posNegRatio(symChanges[sym]);
          const bidPrice = ns.stock.getBidPrice(sym);
          const askPrice = ns.stock.getAskPrice(sym);
          if (longShares <= 0 && shortShares <= 0 && ns.stock.getPrice(sym) < 30000) {
            continue;
          }

          if (longShares > 0) {
            const cost = longShares * longPrice;
            const profit = longShares * (bidPrice - longPrice) - 2 * commission;
            if (state < 0) {
              const sellPrice = ns.stock.sell(sym, longShares);
              if (sellPrice > 0) {
                  ns.print(`SOLD (long) ${sym}. Profit: ${format(profit)}`);
              }
            } else {
                ns.print(`${sym} (${ratio}): ${format(profit+cost)} / ${format(profit)} (${Math.round(profit/cost*10000)/100}%)`);
            }
          } else if (shortShares > 0) {
            const cost = shortShares * shortPrice;
            const profit = shortShares * (shortPrice - askPrice) - 2 * commission;
            if (state > 0) {
              const sellPrice = ns.stock.sellShort(sym, shortShares);
              if (sellPrice > 0) {
                  ns.print(`SOLD (short) ${sym}. Profit: ${format(profit)}`);
              }
            } else {
                ns.print(`${sym} (${ratio}): ${format(profit+cost)} / ${format(profit)} (${Math.round(profit/cost*10000)/100}%)`);
            }
          } else {
            const money = ns.getServerMoneyAvailable("home");
            if (state > 0) {
              const sharesToBuy = Math.min(10000, ns.stock.getMaxShares(sym), Math.floor((money - commission) / askPrice));
              if (ns.stock.buy(sym, sharesToBuy) > 0) {
                  ns.print(`BOUGHT (long) ${sym}.`);
              }
            } else if (state < 0) {
              const sharesToBuy = Math.min(10000, ns.stock.getMaxShares(sym), Math.floor((money - commission) / bidPrice));
              if (ns.stock.short(sym, sharesToBuy) > 0) {
                  ns.print(`BOUGHT (short) ${sym}.`);
              }
            }
          }
        }
    }
}