无法使用Numpy的loadtxt函数将文本文件数据导入全局变量

2024-06-28 15:30:30 发布

您现在位置:Python中文网/ 问答频道 /正文

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()

我正在尝试将gbpUSDAD.txt中的数据加载到变量dates、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秒内完成]

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


Tags: importtxtdatematplotlibasnppltask