Cookie Clicker

Cookie Clicker

CookiStocker 2 | Automatic Stock Trading Mod
z  [developer] 29 Jul, 2023 @ 1:28am
The Trading Algorithm
In this thread, I will discuss the trading algorithm used by CookiStocker. I will start with the algorithm as it is currently implemented for CookiStocker 2.0, and I will gradually refine this post as the algorithm is refined for version 3.0. The algorithm as presented here will be divided into two parts: those parts that have been extensively tested and are not expected to change, and those parts which currently form the CookiStocker 2.0 algorithm but are expected to be substantially revised for the next version. Here they are:

(Note: This description of the trading algorithm is now obsolete and has been replaced by the description in Post #2.)

Those parts of the algorithm that have been extensively tested and are not expected to change:
  1. Always buy stocks when the price drops below 2.
  2. Never sell stocks when the price is below 11. These first two rules override all others.
  3. Always buy stocks when the mode changes from 4 [rapidly falling] to 3 [rapidly rising].
  4. Always sell stocks when the mode changes from 3 to 4.
  5. Buy stocks if the trend changes so that the stock price rises four ticks in a row, unless the mode is 1 [slowly rising] or 3 [rapidly rising], in which case the threshold is three ticks in a row.
  6. Sell stocks it the trend changes so that the stock price falls four ticks in a row, unless the mode is 2 [slowly falling] or 4 [rapidly falling], in which case the threshold is three ticks in a row.. In both this case and the previous one, if the stock price does not change from one tick to another, such a tick counts toward the total buy or sell threshold.
  7. The counts used in the buy and sell rules mentioned directly above are always reset during mode changes that take the mode either in or out of a buy or sell mode.

    These parts of the algorithm will be changed, either completely or in part.

  8. Stocks are always bought when the mode changes to 1 [slowly rising] or 3 [rapidly rising] from any of the other four modes.
  9. Stocks are always sold when the mode changes to 2 [slowly falling] or 4 [rapidly falling] from any of the other four modes.
  10. None of the other mode transitions trigger a buy or sell.
This algorithm is what I am currently using in my tests, which are consistently generating an average of over $15 million per day, compared to less that $2 million for the currently released version of CookiStocker. This algorithm can be used manually to generate similar returns. However, using it manually requires almost constant attention to the market.
Last edited by z; 16 Oct @ 10:15pm
< >
Showing 1-2 of 2 comments
z  [developer] 29 Jul, 2023 @ 1:29am 
(This description of the trading algorithm is now obsolete and has been replaced by the description in Post #2.)

I thought that I would give an interim update on the trading algorithm, specifically on the part that deals with mode transitions. Here is what I have so far. The format is simply my own personal format; I will be using an easier-to-read format when I publish the full algorithm when it's done. It contains rules for all 30 mode transitions (6! / 4!); I have found 29 of them in use so far. Can anyone guess which one I haven't found?

Here's a guide to reading the algorithm:
  • The numbers in the first column show the starting mode and the ending mode; they are followed by the actual rules for the transition.
  • "Stay" means neither buy nor sell; i.e., do nothing.
  • "Hold" is true if any of the stock is owned.
    [*}"Not hold" is treated as a single word and is true if none of the stock is owned.
  • The "buy" test is always evaluated before the (options) "sell" test.
  • If the stock fulfills neither the buy nor sell rules of the test, nothing is done.
  • If the word "mode" is associated with an inequality, it is testing to see if the mode changed in the ticks directly preceding the current one. So "mode >= 3" is testing to see if the mode in the three ticks preceding the one containing the mode transition were all the same as the one at the start of the mode transition. A tick containing a mode transition is considered to have the mode at the end of the transition.
  • "Previous mode" refers to the mode that was in effect before the mode that is in effect at the start of the transition.
  • Buy and sell orders are always given for the maximum amount of stock.
  • Rising and falling refer to the price action of the stock in the specified number of ticks immediately preceding the transition tick. For example, "rising >= 3" means that starting with the fourth tick prior to the mode transition, the stock price either rose or stayed the same in each of the following three ticks. Similarly, "rising < 4" means that starting with the fifth tick before the transition, there is at least one pair of ticks where the stock price falls rather than rises.
  • If a rising or falling test is followed by the word "limited" in parentheses, it means that the full range of the test is used only if the mode remains unchanged for the ticks used in the test. If a mode change occurs close enough to the current mode change that the requisite number of ticks cannot be included in the test, the result of the test is deemed to be false.
  • "Resting" refers to the stock's resting value.
  • The bank ceiling is fully explained in the Ultimate Stock Market Guide under its own section. Briefly, it's equal to $100 when your banks are at level 1 (which is where the stock market first becomes visible) and increases by $3 for every level that your banks increase.
  • The word "ceiling" used when not preceded by the word "bank" refers to CookiStocker's custom ceiling, which is the maximum of the bank ceiling and the stock ceiling, times a fixed constant (currently 0.9). The stock ceiling is equal to the stock's resting price plus 50. It is no longer directly used by the stock market minigame, but I find it a useful number for the trading strategies.
  • The precedence of operations is standard. When a different precedence is required, parentheses are used.
And here's the algorithm itself. Note that this is just a subset of the full trading algorithm; it replaces steps 8 through 10 in the post above. Steps 1 through 7 remain, and they take precedence if there are any conflicts.

0 -> 1 Stay.
0 -> 2 Stay if hold and mode >=3 and rising >=3 and previous mode != 1 and previous mode != 3; otherwise sell.
0 -> 3 Always buy.
0 -> 4 Always sell.
0 -> 5 Buy if falling >= 4.
1 -> 0 Buy if not hold and price < bank ceiling and sell price > bank ceiling.
1 -> 2 Buy if not hold; stay if mode >= 4 and (rising >= 3 or price < resting + 20); otherwise sell.
1 -> 3 Always buy.
1 -> 4 Stay if hold < 6, otherwise sell.
1 -> 5 Sell if mode < 6 or rise >= 3 or price > resting + 10.
2 -> 0 Buy if mode >= 5 and falling >= 4 or mode < 5 and previous mode == 4; sell if hold >= 4 and rising < 4.
2 -> 1 Buy or hold if mode >= 4 and (hold or not hold >= 4) and (price < resting – 20 or falling < 4) or if mode < 4 and (previous mode == 5 or falling >= 5); otherwise sell if hold.
2 -> 3 Always buy.
2 -> 4 Always sell.
2 -> 5 Buy if (not hold >= 4 and rising < 4 or price < resting + 10) and price < ceiling; sell if hold >= 4 and price > resting + 10.
3 -> 0 Stay if rising >=4; otherwise sell.
3 -> 1 Sell if hold.
3 -> 2 Sell if mode < 30.
3 -> 4 Always sell.
3 -> 5 Stay if mode < 20 and (rising < 4 or price < resting + 20), otherwise sell.
4 -> 0 Buy if price < rising - 10.
4 -> 1 Buy.
4 -> 2 Sell if hold.
4 -> 3 Always buy.
4 -> 5 Buy if not hold >= 8 and (falling < 4 or price < resting) and (previous mode != 2 or mode < 14) and previous mode != 3.
5 -> 0 Stay.
5 -> 1 Buy if not hold and mode < 22 and (rising >= 4 (limited) or price < resting + 20 or previous mode == 4); sell if hold and mode >= 4 and rising < 4 and price > resting - 20.
5 -> 2 Buy or stay if (not hold >= 3 or mode >= 3) and (falling >= 3 (limited) or rising >= 3 (limited) or price < resting – 10 or (previous mode == 4 or previous mode == 2) and price < ceiling); sell if (rising >= 4 (limited) or mode < 12 and previous mode == 3 or price > resting + 10.
5 -> 3 Buy if not hold >= 3; stay if hold.
5 -> 4 Always sell.

Note that with the first two posts in this thread, you have a complete algorithm that you can trade on, and it's not even in any automated system yet.

If people would like to see occasional updates of this algorithm, please let me know.
Last edited by z; 16 Oct @ 10:13pm
z  [developer] 16 Oct @ 6:36pm 
(Note: This post is still under development.)

Here is an updated guide to the current trading algorithm used in CookiStocker 3.0:

CookiStocker Trading Algorithm (Plain English Version)

This describes how CookiStocker decides when to buy and sell stocks in the Cookie Clicker Stock Market minigame.

Definitions
  • Current Price – the stock’s price right now.
  • Mode – the market’s current trend:
    • 0 = Stable
    • 1 = Slowly Rising
    • 2 = Slowly Falling
    • 3 = Rapidly Rising
    • 4 = Rapidly Falling
    • 5 = Chaotic / Neutral
  • Resting Value – the stock’s natural “fair” price, computed by the minigame.
  • Bank Ceiling – maximum stock price allowed by your current Bank level.
  • Stock Ceiling – the stock’s internal static cap.
  • Effective Ceiling – the higher of Bank Ceiling and Stock Ceiling.
  • Distance to Floor = Current Price − Resting Value.
  • Distance to Ceiling = Effective Ceiling − Current Price.
  • Max Shares – maximum shares allowed for this stock.
  • Shares Owned – how many shares you currently hold.
  • Room to Buy = Max Shares − Shares Owned.
  • CPS Highest – your run’s peak raw cookies-per-second (used for valuation).
  • Cost to Fill = Room to Buy × CPS Highest × Current Price.
  • Bank Fraction Cap – fraction of your cookie bank allowed for a single purchase (user adjustable).
  • Spend Cap = Current Cookies × Bank Fraction Cap.
  • Minimum Brokers – minimum number of brokers required before trading.
  • Always-Buy Cushion = $2 (near the Resting Value).
  • Small / Large Delta = $3 / $4 (headroom thresholds).
  • Near-Ceiling Cushion = $11 (profit-taking zone).

Order of Operations
  • 1. Skip all actions if you’re ascending or reincarnating (the mod is inactive during countdown).
  • 2. For each stock, compute the values listed above.
  • 3. Buy Decision – evaluate possible entries (first).
  • 4. Sell Decision – evaluate possible exits (after buy check).
  • 5. If neither condition is met, hold position (“stay”).

Buy Decision
  • A. Value buy near the floor
    If Current Price ≤ Resting Value + $2 → BUY (if budget and brokers allow).

  • B. Trend-aligned buys
    If Mode is 1 (Slowly Rising) or 3 (Rapidly Rising):
    • If Current Price < Resting Value → BUY (undervalued and rising).
    • Else if Distance to Ceiling ≥ $4 → BUY (still plenty of headroom).
    • Else if Distance to Ceiling ≥ $3 → Consider BUY (mild headroom).

  • C. Neutral / Chaotic buys (below Resting Value)
    If Mode is 0 (Stable) or 5 (Chaotic) and Current Price < Resting Value:
    • If short-term drift is upward → BUY.
    • If drift is downward → wait for a cheaper entry near Resting Value + $2.

  • D. Never chase near the top
    If Distance to Ceiling < $3 → DO NOT BUY.

  • E. Budget rules
    Any BUY must pass all gates: Brokers ≥ Minimum, Room to Buy > 0, Cost to Fill ≤ Spend Cap.

Sell Decision
  • F. Immediate exit on falling modes
    If Mode is 2 (Slowly Falling) or 4 (Rapidly Falling) → SELL IMMEDIATELY.

  • G. Ceiling management
    • If Current Price ≥ Effective Ceiling → SELL IMMEDIATELY.
    • Else if Distance to Ceiling ≤ $11 → Prefer selling (take profits near top).
      Exception: If Resting Value > Bank Ceiling and Mode ∈ {3, 5, 0, 1}, you may hold briefly, then sell when the price returns near Resting Value or Mode changes to 2 / 4.

  • H. Above Resting Value scenarios
    • If Mode is 0 (Stable) or 5 (Chaotic) and trend is down → SELL.
    • If Mode is 1 or 3 and Distance to Ceiling is large → HOLD or trim; as you enter the $11 zone, SELL.

  • I. Otherwise
    No buy or sell condition met → STAY (Hold Position).

Additional Notes
  • CookiStocker does nothing during ascension / reincarnation.
  • Auto-buy of additional brokers is limited to a very small % of bank and never during ascension.
  • All spending obeys the Bank Fraction Cap (user-set bank % per purchase).
  • Buys and sells are evaluated each market tick; reports print immediately after a tick, and summaries appear 30 seconds later (0 delay if “Force Loop Updates” is on).

Example

A stock has Resting Value $60, Effective Ceiling $180, Current Price $58, Mode = 1 (Slowly Rising).

  • Because Current Price is below Resting by $2 and Mode is rising → BUY.
  • Later, price reaches $172 (only $8 below Ceiling) and Mode flips to 2 (Slowly Falling) → SELL IMMEDIATELY.

All actions respect budget caps and ascension safety rules.
Last edited by z; 17 Oct @ 3:33pm
< >
Showing 1-2 of 2 comments
Per page: 1530 50