-2

in Python3.6 How do I create a time series of "last" out of this string that seems to be arranged by date in the order of: 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
MSeifert
  • 145,886
  • 38
  • 333
  • 352
Herman L
  • 165
  • 1
  • 7

1 Answers1

-1

This code needs some general "cleaning", look here:

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

Similar to this but pandas can read csv from urls too. Pandas read_csv from url


Update Here is another example where you create a dictionary that you pass to the format string

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()
Anton vBR
  • 18,287
  • 5
  • 40
  • 46