无法使用numpy loadtxt

时间:2017-01-12 04:36:55

标签: python numpy

import matplotlib
import matplotlib.pyplot as plt
import matplotlib.ticker as mticker
import matplotlib.dates as mdates
import numpy as np
import time

date, bid, ask = np.loadtxt('A:\Python\GBPUSD1d.txt', unpack = True, delimiter = ',', converters = {0:mdates.strpdate2num('%Y%m%d%H%M%S')})


def percentChange(startPoint, currentPoint):
    return ((currentPoint - startPoint)/startPoint) * 100

def patternFinder():
    avgLine = ((bid + ask)/2.0)
    x = len(avgLine) - 30
    y = 11

    while y < x:
        p1 = percentChange(avgLine[y-10],avgLine[y-9])
        p2 = percentChange(avgLine[y-10],avgLine[y-8])
        p3 = percentChange(avgLine[y-10],avgLine[y-7])
        p4 = percentChange(avgLine[y-10],avgLine[y-6])
        p5 = percentChange(avgLine[y-10],avgLine[y-5])
        p6 = percentChange(avgLine[y-10],avgLine[y-4])
        p7 = percentChange(avgLine[y-10],avgLine[y-3])
        p8 = percentChange(avgLine[y-10],avgLine[y-2])
        p9 = percentChange(avgLine[y-10],avgLine[y-1])
        p10 = percentChange(avgLine[y-10],avgLine[y])

        outcomeRange = avgLine[y+20:y+30]
        currentPoint = avgLine[y]
        print reduce(lambda x, y: x+y, outcomeRange)/len(outcomeRange)
        print currentPoint
        print '_______'
        print p1, p2, p3, p4, p5, p6, p7, p8, p9, p10
        y += 1
        time.sleep(5555)

def graphRawFX():
    fig = plt.figure(figsize=(10,7))
    ax1 = plt.subplot2grid((40,40), (0,0), rowspan=40, colspan=40)

    ax1.plot(date,bid)
    ax1.plot(date, ask)

    ax1.xaxis.set_major_formatter(mdates.DateFormatter('%Y-%m-%d %H:%M:%S'))
    for label in ax1.xaxis.get_ticklabels():
        label.set_rotation(45)

    plt.gca().get_yaxis().get_major_formatter().set_useOffset(False)

    plt.grid(True)
    plt.show()

patternFinder()

我正在尝试将GBPUSD1d.txt中的数据加载到变量date,bid和ask中。当行在函数graphRawFX()下时,我能够完成此操作,但是当我将它移到函数外部时,变量将是全局的,我收到此错误消息。

Traceback (most recent call last):
  File "A:\Python\Forextest.py", line 8, in <module>
    date, bid, ask = np.loadtxt('A:\Python\GBPUSD1d.txt', unpack = True, delimiter = ',', converters = {0:mdates.strpdate2num('%Y%m%d%H%M%S')})
  File "C:\Python27\lib\site-packages\numpy\lib\npyio.py", line 803, in loadtxt
     fh = iter(open(fname, 'U'))
IOError: [Errno 2] No such file or directory: 'A:\\Python\\GBPUSD1d.txt'

[以0.2秒完成]

文本文件与正在运行的程序位于同一文件夹中。谢谢你的帮助。

0 个答案:

没有答案