How can I find the optimal buy and sell points for a stock if I have a transaction cost?
It seems that optimal selling indices are those i such that
price[i-1] < price[i] and price[i+1] <= price[i] and for some j > i, price[i] - price[j] > 2. I don't know about names for an algorithm like that, but list comprehensions and the function any should be enough.
If I have a list of prices, say [4,2,5,6,9,3,1,2,5] and I have a transaction cost of $2 and I am able to buy and short sell then the optimal strategy is buy at 2 switch positions at 9 and and switch again at 1. So the optimal buy indices are [1,6] and the optimal sell indices are [4]. How can this be solved programmatically? Specifically I'm looking to be pointed in the right direction (i.e. This is a perfect case for A* ... or whatever) rather than a solution.