I do not see the need for (much) logic, only math. Total power generated multiplied by selling price of power for the total revenues. Divide that by the resources consumed in producing that power, and you have the "break even" point (no profit or loss) for the resource price. If you subtract the current market price from that, you will get net profit (a positive result) or loss (a negative result). You already have the the capability (I think?) there in the button for engineers to only buy when profitable... The only logic needed may be how *much* to buy. But you could work that in to the game: Start with buying exactly one second worth of full production. Then you can add upgrades and research to buy more at a time. It will not "stock up" at the base rate since production is using it right away, but as you upgrade/research increasing the buy amount, the overall effect will be that it will buy extra when the price is profitable--Effectively "stocking up". And giving players incentives and reasons to pursue those upgrades/research paths.
This way of stocking would not help much, it would just replace continuous purchases with repeatable bulk ones, and that won't give a discount since the bulk buys happen on timer and not when the price is at the lowest. Alternately, if it buys even when there are supplies, people would have difficulties with autobuyers wasting money on oversupplying.
That's why I said that an effective auto-buy algorithm is complicated: you need to determine that the current price is low enough in a sense that the chances of it becoming even lower are slim, and you need to somehow pick an amount to buy that is obvious to players and won't screw them over even if they had other plans for the money they stocked up.
My main thought is not about stocking up so much, anyhow. It's about using and managing resources--not always optimally, but at least not at a consistently wasteful rate. I think you are seeing this as trying to achieve the impossible: Trying to predict the market, and waiting for the right time to act. I am not suggesting that; I am only suggesting that when buying clearly is at a loss, it doesn't buy. If it costs X to supply a generator, but the market price is currently X+100, it should not buy, as it is losing that extra 100 for nothing. That's usually what wastes my money, not oversupply. I calculated the break even point for my current game, and as long as the market price is at or below that point, I go ahead and buy, even if the market price is not at the lowest. I am not trying to "get rich" off rock bottom buying, I'm trying to avoid not spending more resourecs than necessary: Avoiding that X+100 scenario.
There's no need (and as you said, it's not just complicated, but actually impossible) to create a predictive algorithm to buy at the lowest point in the market. I.E., I am not concerned with trying to determine if there's a chance the market will be even lower the next "tick"; It might be. It might not be. That is a random probability. On the other hand, if at this point in time, the market price is that "X+100", there is no probability at all that you will buy at a loss--it is a 100% certainty that I wish to avoid.
In math terms, let's say at time T, the market is at X+5. Buying would be at a loss. So skip buying. At T+1, the market drops to X-10. If you buy a small amount, say S+1 (S for supply)--enough to supply this tick and the next, then no matter if the next tick the price goes up or down, you'll have the supply to cover it. Then at T+2, the price drops even lower to X-50. So again, buy S+1. Notice I am still only buying an extra +1? That is because I'm not trying to predict; I'm only trying to buy when it's not at a loss, which *might* end up stockpiling (in this case, it would now have +2 stockpiled; +1 from the previous, and +1 from this). But let's say the *player* decides to manually buy an extra +10, that would increase the stockpile to +12. But that is under the power of the player, not an algorithm.
Another way of putting it, is that if you are going to have any kind of auto-feature to make decisions fr the player, you can at least make it not make clearly bad decisions. You are correct that trying to make the best decision is complicated or not possible, but you can determine if a decision is certainly a bad one--and avoid it.