给定的一系列股票价格在数组中。匹克什么时候买,什么时候卖,以获取最大利润 [英] given a series of stock prices in an array. Pick when to buy and when to sell to gain max profit

查看:271
本文介绍了给定的一系列股票价格在数组中。匹克什么时候买,什么时候卖,以获取最大利润的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给定的一系列股票价格在数组中。而选择什么时候买什么时候卖,以获取最大的利润。 从一个网站,我得到了这样的事情,EF MAXPROFIT(价格):

given a series of stock prices in an array. Pick when to buy and when to sell to gain max profit. from one website i got something like this-ef maxprofit(prices):

buydate,selldate = 0,0

buydate, selldate = 0, 0

maxprof = 0 minprice =价格[0]为MinDate = 0

maxprof = 0 minprice = prices[0] mindate = 0

为D,P在枚举(价格[1:]):如果P< minprice:minprice = P   为MinDate = D + 1继续

for d, p in enumerate(prices[1:]): if p < minprice: minprice = p mindate = d + 1 continue

教授= P - minprice

prof = p - minprice

如果教授> maxprof:maxprof =教授buydate,selldate =为MinDate,D + 1

if prof > maxprof: maxprof = prof buydate, selldate = mindate, d + 1

收益率(buydate,selldate),maxprof

return (buydate, selldate), maxprof

但我think..we应该考虑的负面价格too..isn't?任何其他解决方案或意见是最欢迎的。

but i think..we should consider negative prices too..isn't? any other solution or comments are most welcome

推荐答案

在理想情况下,你想买股票时,他们的最低卖出时,他们最高的。因此,找出其中的波峰和波谷都在你的数据,而这些都是你的卖出及买入点,分别为。

Ideally you want to buy stocks when they're at their lowest and sell when they're highest. So figure out where the peaks and the troughs are in your data and those are your sell and buy points, respectively.

这是简单,只要经过数据集,看着两个相邻点的时间,并跟踪当前的潮流(市场是否会向上或向下)。如果这一趋势是不断上升,价格下跌,那么你就应该卖的前一天就下跌。在另一方面,如果该趋势持续下降和价格上涨,你应该买的前一天就上涨了。

This is as simple as going through the data set, looking at two adjacent points at a time, and keeping track of the current "trend" (whether or not the market is going up or down). If the trend was going up and the price fell, then you should sell on the day before it fell. On the other hand, if the trend was going down and the price rose, you should buy on the day before it rose.

这篇关于给定的一系列股票价格在数组中。匹克什么时候买,什么时候卖,以获取最大利润的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆