but I don't know exactly how / just stop thinking about the whole thing
Provided that you don't need to know how the algorithm ultimately works, but only need to do the job, you might look into Genetic Algorithms. It is a highly iterative process that starts with a random genome per candidate, and, using an analogue to natural selection, slowly converges from those initial inputs toward an approximately optimal solution. The catch is that you must provide a fitness function by which to judge the genes of each successive generation. In your case this would be an evaluation of how much money was spent by each candidate (gene set). You do this for say 100 sets of virtual "genes", look at the best ones as judged by your fitness function(s), and perform gene crossover and mutation to arrive at the next generation of 100. These will gradually improve. "Genes" are either a random sequence of bits or some variables randomised each run.
Important: You must make sure that your genes represent some behaviour of your code, not some state. This is crucial, or you won't be able to converge on a solution. The change in a gene needs to be directly proportional to some measure of failure / success. See top answer herehere.
Rinse, repeat till you are happy with result. This process can be fully automated in code.
Genetic Programming may also be useful; though I doubt it will be necessary in your case - as I imagine you only need your parameters to evolve - not your code as such.
These are large topics so I will not go further here. Refer there for a simple starter tutorial.