[英]loading csv file dynamically to pandas
請參閱模式查找腳本,一切都按預期工作,但我堅持以下
正如您在底部看到的,我們能夠 plot 價格 x 軸但不能 plot 時間在 y 軸。
import pandas as pd import numpy as np from scipy.signal import argrelextrema import matplotlib.pyplot as plt from harmonic_functions import * import uuid data = pd.read_csv('file.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 = 3.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) plt.title(label) plt.plot(np.arange(start,i+15),price.values[start:i+15]) plt.scatter(current_idx,current_pat,c='r') filename = str(uuid.uuid1())[:8] print(current_pat) print(start) print(end) print(filename) plt.savefig(filename) plt.close(filename) plt.show()
樣本輸入文件
time,open,high,low,close,volume
07.05.2020 09:15:00.000,9157.4,9210.8,9157.4,9194,653100
07.05.2020 09:17:00.000,9195,9209.35,9191.4,9203.25,292800
07.05.2020 09:19:00.000,9205.95,9212.2,9187.65,9193.25,312150
07.05.2020 09:21:00.000,9195,9221.65,9188.8,9215.4,226725
07.05.2020 09:23:00.000,9215.5,9249.95,9213.75,9241.2,244350
07.05.2020 09:25:00.000,9237.4,9238.6,9205.6,9207,284325
07.05.2020 09:27:00.000,9205.05,9215,9192.75,9201.25,286500
07.05.2020 09:29:00.000,9199.9,9206.1,9181.15,9183.55,268500
07.05.2020 09:31:00.000,9183.8,9207.05,9182.05,9207.05,203100
07.05.2020 09:33:00.000,9206.05,9220.4,9201.85,9216.55,158775
07.05.2020 09:35:00.000,9217.05,9224.6,9210.35,9218.95,113775
07.05.2020 09:37:00.000,9219.4,9223.6,9206.65,9209.7,84525
07.05.2020 09:39:00.000,9212.05,9220.4,9202.1,9219.75,101775
07.05.2020 09:41:00.000,9219.75,9223.95,9212.6,9219.75,91050
07.05.2020 09:43:00.000,9219.8,9224.05,9205.6,9218.85,125025
07.05.2020 09:45:00.000,9220.45,9223.4,9210.05,9212.6,72375
07.05.2020 09:47:00.000,9213.2,9233.65,9212,9231.65,111375
07.05.2020 09:49:00.000,9228.75,9241.6,9220,9238.15,136725
07.05.2020 09:51:00.000,9237.9,9259.55,9236.6,9243.45,273600
07.05.2020 09:53:00.000,9245.5,9248.3,9233,9237.2,107550
07.05.2020 09:55:00.000,9238.8,9256.05,9236.25,9256.05,92775
07.05.2020 09:57:00.000,9255.65,9257,9243.05,9246.55,94200
07.05.2020 09:59:00.000,9245,9263.4,9241.8,9257.1,116625
07.05.2020 10:01:00.000,9257.25,9263.6,9250.9,9250.9,93150
07.05.2020 10:03:00.000,9250.9,9253.1,9235,9237.45,114825
07.05.2020 10:05:00.000,9236.3,9236.35,9222.35,9225.6,133575
07.05.2020 10:07:00.000,9224.45,9240,9224.45,9231.6,54150
07.05.2020 10:09:00.000,9231.15,9236.6,9225.15,9228.05,45900
07.05.2020 10:11:00.000,9227.35,9228.15,9218.2,9225.5,91800
07.05.2020 10:13:00.000,9225.35,9232,9224.85,9228.85,66300
07.05.2020 10:15:00.000,9225.85,9231.9,9224.05,9229.55,69075
07.05.2020 10:17:00.000,9231.4,9239,9229.55,9234.95,64350
07.05.2020 10:19:00.000,9234.95,9235.9,9222,9225.25,52200
07.05.2020 10:21:00.000,9225.25,9227.2,9216.3,9221.7,65250
07.05.2020 10:23:00.000,9222,9227,9217,9219,45675
07.05.2020 10:25:00.000,9217.3,9219.95,9206.65,9214.05,122325
07.05.2020 10:27:00.000,9213.8,9221.5,9210.1,9211,55875
07.05.2020 10:29:00.000,9210.15,9218,9207.6,9208.55,52725
07.05.2020 10:31:00.000,9208.55,9209.4,9200.7,9206.9,101625
07.05.2020 10:33:00.000,9205.35,9214.4,9202,9211.4,84900
07.05.2020 10:35:00.000,9211.7,9215.5,9205.15,9212.65,46425
07.05.2020 10:37:00.000,9213,9216.7,9203.2,9203.2,54300
07.05.2020 10:39:00.000,9204.7,9204.7,9188.6,9188.65,181500
07.05.2020 10:41:00.000,9188.7,9195.5,9183,9189.85,128925
07.05.2020 10:43:00.000,9189,9192.05,9173.75,9182.2,183300
07.05.2020 10:45:00.000,9180.85,9186.7,9180.15,9184.75,100275
07.05.2020 10:47:00.000,9186,9193.25,9182.25,9188.3,91050
07.05.2020 10:49:00.000,9187.95,9197.2,9187.95,9195.75,66600
07.05.2020 10:51:00.000,9198.3,9206.75,9197,9200.5,99675
07.05.2020 10:53:00.000,9200.45,9206,9194.9,9196.65,40500
07.05.2020 10:55:00.000,9196.65,9196.65,9186.6,9193,46650
07.05.2020 10:57:00.000,9193.95,9197.25,9190,9194,31275
07.05.2020 10:59:00.000,9194,9210,9189.95,9208.6,77550
07.05.2020 11:01:00.000,9212.25,9212.25,9201.75,9202,94200
07.05.2020 11:03:00.000,9201.6,9205.35,9192.2,9193.35,52350
07.05.2020 11:05:00.000,9193.55,9194.95,9181.1,9183,86475
07.05.2020 11:07:00.000,9183.25,9189.95,9180.8,9187.55,44475
07.05.2020 11:09:00.000,9187.95,9198.7,9185,9197,79950
07.05.2020 11:11:00.000,9197,9204.9,9192.55,9202.45,48150
07.05.2020 11:13:00.000,9201.75,9205.3,9196,9200.25,65850
07.05.2020 11:15:00.000,9201.75,9210.65,9201.45,9206.45,60300
07.05.2020 11:17:00.000,9207.95,9215,9205.95,9210,55425
07.05.2020 11:19:00.000,9210.85,9215,9202.3,9202.3,45825
07.05.2020 11:21:00.000,9202.55,9214,9202.55,9209,29775
07.05.2020 11:23:00.000,9209,9211.3,9202,9203.5,30600
07.05.2020 11:25:00.000,9203.5,9213.75,9203.5,9210,41175
07.05.2020 11:27:00.000,9210,9211.7,9195.6,9199.5,49575
07.05.2020 11:29:00.000,9200,9209.85,9197.5,9208.3,46950
07.05.2020 11:31:00.000,9208.3,9226.85,9208,9222.6,177900
07.05.2020 11:33:00.000,9224,9227.65,9220,9226,67800
07.05.2020 11:35:00.000,9225.9,9226,9216.75,9216.75,50550
07.05.2020 11:37:00.000,9217.15,9233.8,9216.75,9232,117750
07.05.2020 11:39:00.000,9232,9232.3,9220.75,9224.1,47400
07.05.2020 11:41:00.000,9224.1,9224.1,9217.2,9222,35550
07.05.2020 11:43:00.000,9221.2,9225,9218.8,9221.6,20250
07.05.2020 11:45:00.000,9222.8,9228,9218.25,9221.2,23400
07.05.2020 11:47:00.000,9221.25,9224,9210,9210.65,51825
07.05.2020 11:49:00.000,9210.65,9213.4,9194.35,9196.4,111675
07.05.2020 11:51:00.000,9196,9204.4,9195,9197,53175
07.05.2020 11:53:00.000,9196.2,9201.9,9192.1,9199.65,42525
07.05.2020 11:55:00.000,9199.8,9206.3,9197.5,9203,31650
07.05.2020 11:57:00.000,9203.1,9208.95,9200,9207,26475
07.05.2020 11:59:00.000,9207.3,9213,9206,9206.8,33750
07.05.2020 12:01:00.000,9207.8,9211.1,9196.25,9197.7,52875
07.05.2020 12:03:00.000,9197.8,9203,9193.7,9195.25,46725
07.05.2020 12:05:00.000,9195.2,9195.6,9186,9189,122475
07.05.2020 12:07:00.000,9188.95,9198.2,9188.05,9194.55,77775
07.05.2020 12:09:00.000,9194.6,9198,9187.6,9193.6,59700
07.05.2020 12:11:00.000,9194.45,9199.9,9193.6,9195.4,49125
07.05.2020 12:13:00.000,9195.3,9198.9,9190.4,9194.75,43950
07.05.2020 12:15:00.000,9194.75,9196.3,9181.55,9183.85,103800
07.05.2020 12:17:00.000,9182.25,9197.2,9181.9,9196.55,54900
07.05.2020 12:19:00.000,9197.15,9205.5,9195.6,9200.6,72750
07.05.2020 12:21:00.000,9199,9200.75,9192.7,9198.2,41400
07.05.2020 12:23:00.000,9198.2,9198.2,9184.15,9190.9,83400
07.05.2020 12:25:00.000,9192,9193.9,9178.95,9181.75,94275
07.05.2020 12:27:00.000,9181.75,9186.75,9173,9182,135525
07.05.2020 12:29:00.000,9181.95,9189.95,9180.45,9187.2,52575
07.05.2020 12:31:00.000,9187.6,9194.05,9184.1,9185.4,48525
07.05.2020 12:33:00.000,9185.55,9190,9182,9190,39675
07.05.2020 12:35:00.000,9190,9211.1,9188,9208.55,133725
07.05.2020 12:37:00.000,9209.2,9224.65,9208.55,9213.25,183750
07.05.2020 12:39:00.000,9215,9215.6,9203.95,9210,154275
07.05.2020 12:41:00.000,9210,9217.4,9205.5,9217.4,41175
07.05.2020 12:43:00.000,9217.25,9225,9214.2,9217.9,66075
07.05.2020 12:45:00.000,9217.6,9228,9216.05,9224,107325
07.05.2020 12:47:00.000,9224.45,9229,9220.1,9223.65,72000
07.05.2020 12:49:00.000,9224.05,9225.35,9215,9218.1,64500
07.05.2020 12:51:00.000,9218.1,9218.1,9209.7,9212.3,69825
07.05.2020 12:53:00.000,9213.05,9223,9211.75,9221.4,51075
07.05.2020 12:55:00.000,9218.85,9225,9215.5,9219.1,46350
07.05.2020 12:57:00.000,9220.95,9223,9212.25,9212.3,57450
07.05.2020 12:59:00.000,9212.35,9215.6,9207.6,9212.4,78975
07.05.2020 13:01:00.000,9211.1,9212,9201.5,9204.95,89625
07.05.2020 13:03:00.000,9206.85,9217,9205,9216.2,28575
07.05.2020 13:05:00.000,9216,9216,9209,9213.8,17850
07.05.2020 13:07:00.000,9213.75,9230,9213.2,9227.1,86550
07.05.2020 13:09:00.000,9228.25,9239.85,9222.2,9231.6,123675
07.05.2020 13:11:00.000,9231.75,9235,9222.75,9228.5,63225
07.05.2020 13:13:00.000,9229.5,9236.8,9228.4,9230,46200
07.05.2020 13:15:00.000,9228.45,9249,9228,9249,141525
07.05.2020 13:17:00.000,9246.95,9249.3,9239.5,9241,90375
07.05.2020 13:19:00.000,9240.95,9245,9236,9237.7,42000
07.05.2020 13:21:00.000,9236.25,9243,9232.45,9234.9,38325
07.05.2020 13:23:00.000,9237,9237.85,9225.1,9230.95,48075
07.05.2020 13:25:00.000,9230.75,9230.95,9216.35,9219.95,69525
07.05.2020 13:27:00.000,9218.85,9224.35,9216,9221.35,58275
07.05.2020 13:29:00.000,9222,9222,9216,9217.25,35400
07.05.2020 13:31:00.000,9217.6,9217.6,9203.6,9209.2,108825
07.05.2020 13:33:00.000,9209.9,9210.1,9202.25,9205,75150
07.05.2020 13:35:00.000,9205,9211,9197,9200.9,148125
07.05.2020 13:37:00.000,9198.8,9201.9,9191.1,9195.8,114975
07.05.2020 13:39:00.000,9196.8,9197.4,9182,9183.9,127350
07.05.2020 13:41:00.000,9183.9,9189,9173.5,9183.25,223800
07.05.2020 13:43:00.000,9182.75,9187.8,9180.8,9185.65,127350
07.05.2020 13:45:00.000,9186,9187.8,9173.5,9177.05,97425
07.05.2020 13:47:00.000,9178.35,9204,9177.35,9199.9,134025
07.05.2020 13:49:00.000,9199.95,9199.95,9186.8,9188.6,54000
07.05.2020 13:51:00.000,9188,9193.95,9184.5,9187.25,45600
07.05.2020 13:53:00.000,9187.95,9196.95,9187.05,9190,47550
07.05.2020 13:55:00.000,9190,9195,9185.45,9188.65,48600
07.05.2020 13:57:00.000,9190.5,9204,9188.65,9199.95,58125
07.05.2020 13:59:00.000,9199.55,9213.9,9196,9210,79575
07.05.2020 14:01:00.000,9209.85,9210,9198.65,9202.6,50100
07.05.2020 14:03:00.000,9203.4,9203.75,9195,9197.7,44775
07.05.2020 14:05:00.000,9197.7,9198,9191.65,9194.75,45675
07.05.2020 14:07:00.000,9194.75,9206.9,9193,9202.4,37050
07.05.2020 14:09:00.000,9202.4,9207,9197,9207,28275
07.05.2020 14:11:00.000,9205.5,9209.9,9204,9208.95,37575
07.05.2020 14:13:00.000,9208.45,9209,9192.7,9193.55,41850
07.05.2020 14:15:00.000,9193.85,9195,9183,9188.2,93750
07.05.2020 14:17:00.000,9188.55,9203,9188.25,9201.1,44175
07.05.2020 14:19:00.000,9201.1,9209,9199.05,9205.75,43725
07.05.2020 14:21:00.000,9205.8,9209,9202.1,9202.2,31875
07.05.2020 14:23:00.000,9200.05,9202.9,9196.8,9198.4,29775
07.05.2020 14:25:00.000,9200.7,9208,9197.05,9208,36600
07.05.2020 14:27:00.000,9206.95,9208.7,9201.55,9205.7,25800
07.05.2020 14:29:00.000,9205.7,9224,9202,9219.25,130575
07.05.2020 14:31:00.000,9220.05,9220.95,9212.8,9213,56925
07.05.2020 14:33:00.000,9213.3,9213.6,9208,9208.8,42450
07.05.2020 14:35:00.000,9208.95,9219.65,9208,9212.25,37725
07.05.2020 14:37:00.000,9213.05,9213.2,9201.05,9201.4,53925
07.05.2020 14:39:00.000,9201.35,9210.95,9201.35,9208.05,41850
07.05.2020 14:41:00.000,9213,9217.95,9208.6,9211.3,40875
07.05.2020 14:43:00.000,9210.7,9211.65,9202,9202,51225
07.05.2020 14:45:00.000,9201,9203.85,9186.35,9187.8,132000
07.05.2020 14:47:00.000,9188.95,9198,9186.7,9195.25,73650
07.05.2020 14:49:00.000,9196.1,9196.65,9188,9191,57075
07.05.2020 14:51:00.000,9190.45,9205,9189.2,9204.55,65400
07.05.2020 14:53:00.000,9204.85,9204.85,9196.8,9204.3,45675
07.05.2020 14:55:00.000,9204,9204,9198,9203.05,41325
07.05.2020 14:57:00.000,9203.95,9212.7,9202.65,9208,63975
07.05.2020 14:59:00.000,9208,9220,9207.35,9212.2,107775
07.05.2020 15:01:00.000,9211.45,9217.05,9198.05,9198.05,74700
07.05.2020 15:03:00.000,9200,9202.15,9191.05,9194.55,118350
07.05.2020 15:05:00.000,9194.5,9194.55,9185.65,9191.75,111600
07.05.2020 15:07:00.000,9191.55,9197,9188,9189.95,68475
07.05.2020 15:09:00.000,9191.75,9208,9189.95,9206.45,96900
07.05.2020 15:11:00.000,9207.7,9210.5,9201.45,9209.55,129150
07.05.2020 15:13:00.000,9208,9209.5,9205,9207.3,57825
07.05.2020 15:15:00.000,9208.35,9209,9202.05,9207,77550
07.05.2020 15:17:00.000,9206.25,9207.2,9200,9200.7,86025
07.05.2020 15:19:00.000,9200.5,9201,9191.6,9194,143700
07.05.2020 15:21:00.000,9194.35,9209,9192.4,9209,128550
07.05.2020 15:23:00.000,9209.25,9214.4,9208.1,9209.95,111075
07.05.2020 15:25:00.000,9209.95,9216.85,9208.55,9216,114750
07.05.2020 15:27:00.000,9216,9218.8,9215,9217.55,115875
07.05.2020 15:29:00.000,9217.05,9217.7,9211,9217.45,71625
在這一行:
plt.plot(np.arange(start,i+15),price.values[start:i+15])
您實際上是在 x 軸上繪制一系列數字 [start, end) 並在 y 軸上繪制前 15 個價格。 PyPlot.plt()具有調用簽名plot([x], y, [fmt], *, data=None, **kwargs)
這意味着前兩個 arguments 可以分別繪制在 x 和 y 軸上的可迭代.
我想你想做的是:
plt.plot(data.time.values[start,i+15], price.values[start:i+15])
即將您想要的時間值傳遞給 x 軸參數。
如果您確實想要 x 軸上的價格和 y 軸上的時間(這通常是非常規的),您可以切換 arguments 的順序。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.