Skip to content

Instantly share code, notes, and snippets.

@asvanberg
Last active February 8, 2023 12:36
Show Gist options
  • Save asvanberg/6366fc68d4efc68efae2ae3f1709a516 to your computer and use it in GitHub Desktop.
Save asvanberg/6366fc68d4efc68efae2ae3f1709a516 to your computer and use it in GitHub Desktop.
Automatic stock market trader for Cookie Clicker
return (function () {
// Price must be this percentage of resting value to buy during slow climb
const slowClimbBuyThreshhold = 0.50;
// Sell when stable this close to market ceiling
const stableSellThreshhold = 0.96;
// Price must be this percentage of resting value to buy during fast climb
const fastClimbBuyThreshhold = 100.0;
const numberFormat = new Intl.NumberFormat([], { 'style': 'currency', 'currency': 'USD' });
let Broker = {};
Broker.stockMarket = Game.Objects['Bank'].minigame;
Broker.Notify = function (msg) {
console.log(`[Broker] ${msg}`);
Game.Notify('Broker', msg, null, 8);
}
Broker.run = function () {
for (const stock of Object.values(Game.Objects['Bank'].minigame.goods)) {
const restingValue = Broker.stockMarket.getRestingVal(stock.id);
const brokers = Broker.stockMarket.brokers;
const fee = 0.20 * Math.pow(0.95, brokers);
const purchasePrice = stock.val * (1 + fee);
const marketCeiling = 100 + (Broker.stockMarket.parent.level - 1) * 3;
const restingPull = (restingValue - stock.val) * 0.01;
const boughtFor = stock.prev * (1 + fee);
if (stock.val === 1) {
if (Broker.stockMarket.buyGood(stock.id, 10000)) {
Broker.Notify(`Buying ${stock.symbol} for ${numberFormat.format(stock.val)}`)
}
}
else {
switch (stock.mode) {
case 0: // Stable
case 5: // Chaotic
const shouldSell = stock.val > marketCeiling * stableSellThreshhold || stock.val > restingValue || stock.val > boughtFor || stock.d < 0;
if (shouldSell) {
const profit = (stock.val - boughtFor) * stock.stock;
if (Broker.stockMarket.sellGood(stock.id, 10000)) {
Broker.Notify(`Selling ${stock.symbol} for ${numberFormat.format(stock.val)}, profit ${numberFormat.format(profit)} (stable stock reached ceiling)`);
}
}
break;
case 1: // Slow climb
const trendingUpward = stock.d + restingPull > 0;
if (purchasePrice < restingValue * slowClimbBuyThreshhold && trendingUpward) {
if (Broker.stockMarket.buyGood(stock.id, 10000)) {
Broker.Notify(`Buying ${stock.symbol} for ${numberFormat.format(stock.val)} (slow climb with positive trend)`);
}
}
if (!trendingUpward) {
const profit = (stock.val - boughtFor) * stock.stock;
if (Broker.stockMarket.sellGood(stock.id, 10000)) {
Broker.Notify(`Selling ${stock.symbol} for ${numberFormat.format(stock.val)}, profit ${numberFormat.format(profit)} (slow climb too high over resting)`);
}
}
break;
case 2: // Slow fall
{
const profit = (stock.val - (stock.prev * (1 + fee))) * stock.stock;
const trendingDownward = stock.d + restingPull < + 0;
if (profit > 0 || trendingDownward) {
if (Broker.stockMarket.sellGood(stock.id, 10000)) {
Broker.Notify(`Selling ${stock.symbol} for ${numberFormat.format(stock.val)}, profit ${numberFormat.format(profit)}`);
}
}
break;
}
case 3: // Fast climb
if (purchasePrice < restingValue * fastClimbBuyThreshhold && stock.d >= -1) {
if (Broker.stockMarket.buyGood(stock.id, 10000)) {
Broker.Notify(`Buying ${stock.symbol} for ${numberFormat.format(stock.val)} (entered fast climb)`);
}
}
break;
case 4: // Fast fall
{
const profit = (stock.val - (stock.prev * (1 + fee))) * stock.stock
if (Broker.stockMarket.sellGood(stock.id, 10000)) {
Broker.Notify(`Selling ${stock.symbol} for ${numberFormat.format(stock.val)}, profit ${numberFormat.format(profit)} (entered fast fall)`);
}
break;
}
}
}
}
}
var interval;
Broker.init = function () {
interval = setInterval(Broker.run, 60000);
Broker.run();
}
Broker.stop = function () {
clearInterval(interval);
}
return Broker;
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment