Python 熊猫数据帧持续更新

Python 熊猫数据帧持续更新,python,python-3.x,pandas,numpy,matplotlib,Python,Python 3.x,Pandas,Numpy,Matplotlib,请参阅基于熊猫的模式扫描仪,这里我使用csv作为数据源,并将其加载到数据中 由于数据是从csv文件加载的,因此我必须每5分钟重新加载/重新运行脚本以读取更新的csv文件,因此每5分钟重复一次绘图 是否有任何方法可以使用df.update来避免重新加载脚本并防止一次又一次地重新加载绘图 import pandas as pd import numpy as np from scipy.signal import argrelextrema import matplotlib.pyplot as p

请参阅基于熊猫的模式扫描仪,这里我使用csv作为数据源,并将其加载到数据中

由于数据是从csv文件加载的,因此我必须每5分钟重新加载/重新运行脚本以读取更新的csv文件,因此每5分钟重复一次绘图

是否有任何方法可以使用df.update来避免重新加载脚本并防止一次又一次地重新加载绘图

import pandas as pd
import numpy as np
from scipy.signal import argrelextrema
import matplotlib.pyplot as plt
from harmonic_functions import *
import uuid
from csv import DictReader


data = pd.read_csv('temp.csv')



data.time = pd.to_datetime(data.time,format='%d.%m.%Y %H:%M:%S.%f')
data.index = data['time']
# data = data.drop_duplicates(keep=False)

price = data.close.copy()

err_allowed = 10.0/100

pnl = []
trade_dates=[]
correct_pats=0
pats=0

# plt.ion()

for i in range (100,len(price)):

    current_idx,current_pat,start,end = peak_detect(price.values[:i],order=7)

    XA = current_pat[1] - current_pat[0]
    AB = current_pat[2] - current_pat[1]
    BC = current_pat[3] - current_pat[2]
    CD = current_pat[4] - current_pat[3]

    moves = [XA,AB,BC,CD]

    gart = is_gartley(moves,err_allowed)
    butt = is_butterfly(moves,err_allowed)
    bat = is_bat(moves,err_allowed)
    crab = is_crab(moves,err_allowed)
    shark = is_shark(moves,err_allowed)
    trio = is_trio(moves,err_allowed)
    cyph = is_cyph(moves,err_allowed)
    three_dives = is_3dives(moves, err_allowed)
    fivezero = is_50(moves, err_allowed)
    altbat = is_altbat(moves, err_allowed)
    deepcrab = is_deepcrab(moves, err_allowed)
    dragon = is_dragon(moves, err_allowed)
    snorm = is_snorm(moves, err_allowed)

    harmonics = np.array([gart,butt,bat,crab,shark,trio,cyph,three_dives,fivezero,altbat,deepcrab,dragon,snorm])
    labels = ['Garterly','Butterfly','Bat','Crab','Shark','Trio','Cypher','3Dives','5Zero','AltBat','DeepCrab','Dragon','Snorm']


    if np.any(harmonics == 1) or np.any(harmonics == -1):
        for j in range (0,len(harmonics)):
            if harmonics[j] == 1 or harmonics[j]==-1:
                pats+=1
                sense = 'Bearish ' if harmonics[j]==-1 else 'Bullish '
                label = sense + labels[j] + ' found' 
                print(label)
                print(price.values[start])

                plt.title(label)
                plt.plot(np.arange(start,i+5),price.values[start:i+5])
                plt.scatter(current_idx,current_pat,c='r')
                filename = str(uuid.uuid1())[:8]
                print(current_pat)
                print(current_idx)
#                with open('temp.csv', mode='r') as csv_file:
#                    file = DictReader(csv_file, delimiter=',')
#                    close = str(current_pat[4])
#                    print(current_pat)
#                    rows = [row for row in file if row['close'] in close]
#                    closetime = rows[-1]['ID']
#                   print(closetime)
                write1 = str(current_idx)
                write2 = str(current_pat)
                write = write1 + ',' + write2
                print(write)
                with open("datadb", "r+") as file:
                    for line in file:
                        if write in line:
                            break
                    else:  # not found, we are at the eof
                        file.write(f"{write}\n")  # append missing data
                        print(filename)
                        plt.savefig(filename)
                        plt.close(filename)
#                plt.show()
                plt.clf()