【问题标题】:Matplotlib and Numpy MathMatplotlib 和 Numpy 数学
【发布时间】:2011-03-01 14:29:03
【问题描述】:

我正在尝试使用 Matplotlib 和 Numpy 获得一些牵引力,但这并不容易。

我正在做一个小项目来开始处理 Matplotlib 和 Numpy,但我卡住了......

代码如下:

# Modules
import datetime
import numpy as np
import matplotlib.finance as finance
import matplotlib.mlab as mlab
import matplotlib.pyplot as plot

# Define quote
startdate = datetime.date(2010,10,1)
today = enddate = datetime.date.today()
ticker = 'uso'

# Catch CSV
fh = finance.fetch_historical_yahoo(ticker, startdate, enddate)

# From CSV to REACARRAY
r = mlab.csv2rec(fh); fh.close()
# Order by Desc
r.sort()


### Methods Begin
def moving_average(x, n, type='simple'):
    """
    compute an n period moving average.

    type is 'simple' | 'exponential'

    """
    x = np.asarray(x)
    if type=='simple':
        weights = np.ones(n)
    else:
        weights = np.exp(np.linspace(-1., 0., n))

    weights /= weights.sum()


    a =  np.convolve(x, weights, mode='full')[:len(x)]
    a[:n] = a[n]
    return a
### Methods End


prices = r.adj_close
dates = r.date
ma20 = moving_average(prices, 20, type='simple')
ma50 = moving_average(prices, 50, type='simple')

# Get when ma20 crosses ma50
equal = np.round(ma20,1)==np.round(ma50,1)
dates_cross  = (dates[equal])
prices_cross = (prices[equal])

# Get when ma20 > ma50
ma20_greater_than_ma50 = np.round(ma20,1) > np.round(ma50,1)
dates_ma20_greater_than_ma50  = (dates[ma20_greater_than_ma50])
prices_ma20_greater_than_ma50 = (prices[ma20_greater_than_ma50])

print dates_ma20_greater_than_ma50
print prices_ma20_greater_than_ma50

现在我需要做这样的事情:

store the price of the "price_cross"
see if one day after the "ma20_greater_than_ma50" statment is true, if true store the price as "price of the one day after"
now do "next price_cross" - "price of the one day after"  (price2 - price1) for all occurences

我怎样才能做这个数学和更重要的。如何使用 Matplotlib 和 Numpy 获得牵引力。我应该买什么书?

给我一​​些线索。

最好的问候,

【问题讨论】:

    标签: python numpy matplotlib stockquotes


    【解决方案1】:

    我同意 Josh,但想添加 matplotlib 库:

    http://matplotlib.sourceforge.net/gallery.html

    我的大部分情节都是从直接复制接近我想要的东西开始的,然后根据我的需要对其进行修改。 matplotlib 库有很多这样的例子。

    【讨论】:

      【解决方案2】:

      我会说您不一定需要出去购买任何书籍。更好(且更便宜)的解决方案是查看在线教程,例如:

      http://www.scipy.org/Tentative_NumPy_Tutorial

      http://matplotlib.sourceforge.net/examples/index.html

      并尝试将文档中的内容拼凑起来并搜索相关关键字。从您提供的代码(假设您编写它)中,您对 numpy.您需要更具体地处理遇到的问题才能获得更具体/详细的帮助。

      【讨论】:

        【解决方案3】:

        这是一个开始的列表,您可能会在浏览后找到对您最重要的部分:

        1. Python教程http://docs.python.org/tutorial/
        2. 来自http://docs.scipy.org/doc/ 的 Numpy 用户指南
        3. Matplotlib 用户指南http://matplotlib.sourceforge.net/users/index.html
        4. Numpy/Scipy 其他文档来源http://www.scipy.org/Additional_Documentation

        您可能想要订阅 numpy 和/或 matplotlib 的邮件列表。

        【讨论】:

          【解决方案4】:

          matplotlib 和 numpy 有很多有用的函数,你应该在实现之前先 google。

          例如参见 matplotlib movavg 函数。

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2011-01-06
            • 2014-07-17
            • 1970-01-01
            • 1970-01-01
            • 2019-08-09
            • 1970-01-01
            • 1970-01-01
            • 2015-12-27
            相关资源
            最近更新 更多