如何绘制收盘价?

时间:2017-08-08 12:54:49

标签: python

Python3.6中的

如何创建" last"的时间序列?这个字符串似乎是按日期排列的顺序:ticker,date,open,high,low,last,volume,openinterest

import urllib
import re
import numpy as np
import requests
import pandas as pd

ticker = 'NGU17'

url='https://www.barchart.com/proxies/timeseries/queryeod.ashx?symbol=' +ticker + '&data=daily&maxrecords=960&volume=contract&backadjust=false&daystoexpiration=1&contractroll=expiration'

data = urllib.request.urlopen(url).read()

data_clean = str(data.split()[len(data.split())-100:]).replace("b", "").replace("'","")

print(data_clean) #ticker, date, open, high, low, last, volume, openinterest

1 个答案:

答案 0 :(得分:-1)

此代码需要一些通用的“清理”,请看这里:

import requests
import pandas as pd
import matplotlib.pyplot as plt

ticker = 'NGU17'

url='https://www.barchart.com/proxies/timeseries/queryeod.ashx?symbol={}\
&data=daily&maxrecords=960&volume=contract&backadjust=false&\
daystoexpiration=1&contractroll=expiration'.format(ticker)


df = pd.read_csv(url, header = None)
df.columns = ['ticker', 'date', 'open', 'high', 'low', 'last', 'volume', 'openinterest']

series = df.set_index("date")["last"] # <-- timeseries with last

series.plot()
plt.show()

enter image description here

与此类似,但大熊猫也可以从网址读取csv。 Pandas read_csv from url

更新以下是另一个创建传递给格式字符串的字典的示例

import requests
import pandas as pd
import matplotlib.pyplot as plt

params = {"symbol":'NGU17',
          "data":"daily",
          "maxrecords":960,
          "volume": "contract",
          "backadjust": False,
          "daystoexpiration": 1,
          "contractroll":"expiration"
         }

url='https://www.barchart.com/proxies/timeseries/queryeod.ashx?\
symbol={0[symbol]}&data={0[data]}&maxrecords={0[maxrecords]}&volume={0[volume]}&backadjust={0[backadjust]}&\
daystoexpiration={0[daystoexpiration]}&contractroll={0[contractroll]}'.format(params)

df = pd.read_csv(url, header = None)
df.columns = ['ticker', 'date', 'open', 'high', 'low', 'last', 'volume', 'openinterest']

serie = df.set_index("date")["last"]

serie.plot()
plt.show()