给定数组中的一系列股票价格。选择何时买入和何时卖出以获得最大利润。 从一个网站我得到了这样的东西-ef maxprofit(prices):
buydate, selldate = 0, 0
maxprof = 0 minprice = prices[0] mindate = 0
for d, p in enumerate(prices[1:]): if p < minprice: minprice = p mindate = d + 1 continue
prof = p - minprice
if prof > maxprof: maxprof = prof buydate, selldate = mindate, d + 1
return (buydate, selldate), maxprof
但我认为..我们也应该考虑负价格..不是吗?欢迎任何其他解决方案或意见
最佳答案
由于您有历史记录并且交易似乎没有惩罚,只需对价格求导并在 d/dx 从负变为正时买入,在从正变为负时卖出。
这样您就可以在每次获利前买入,在每次亏损前卖出。
关于algorithm - 给定数组中的一系列股票价格。选择何时买入和何时卖出以获得最大利润,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6666776/