#!/usr/bin/env python # ------------------------------------------------------------------------------ # # Disclaimer: # # DTN grants you an nonexclusive copyright license to use this code and you can # use it to prepare similar functions tailored to your own needs. # # The code is provided for illustrative purposes only and has not been tested under # all conditions. For that reason, DTN provides this code to you "AS IS" without # any warranties of any kind. # # ------------------------------------------------------------------------------ # # Code to invoke PXWeb operations. # # Imports from datetime import date, datetime import urllib.request import urllib.parse import xml.etree.ElementTree as eTree # ------------------------------------------------------------------------------ # PXWeb URL URL = 'https://pxweb.dtn.com/PXWebSvc/PXServiceWeb.svc' # Request string formats GETMARKETSCANS = 'GetMarketScans?UserID=UserID&Password=Password&Report={}&Market={}&Type={}&Limit={}' GETOPTIONGREEKS = 'GetOptionGreeks?UserID=UserID&Password=Password&Type={}&Basis={}&Symbol={}' GETQUOTESNAP = 'GetQuoteSnap?UserID=UserID&Password=Password&Type={}&Symbol={}&Market={}&Vendor={}' GETDAILYHISTORY = 'GetDailyHistory?UserID=UserID&Password=Password&Symbol={}&Interval={}&StartDate={}&EndDate={}&Limit={}&Market={}&Vendor={}' GETMINUTEHISTORY = 'GetMinuteHistory?UserID=UserID&Password=Password&Symbol={}&Interval={}&StartTime={}&EndTime={}&Limit={}&Market={}&Vendor={}' GETTICKHISTORY = 'GetTickHistory?UserID=UserID&Password=Password&Symbol={}&Filter={}&StartTime={}&EndTime={}&Limit={}&Market={}&Vendor={}' GETFORWARDCURVE = 'GetForwardCurve?UserID=UserID&Password=Password&Root={}&CurveDate={}&Limit={}&Market={}&Vendor={}' GETNEWSINDEX = 'GetNewsIndex?UserID=UserID&Password=Password&Filter={}&Limit={}&TZOffset={}' GETNEWSSTORY = 'GetNewsStory?UserID=UserID&Password=Password&Key={}' FINDSYMBOL = 'FindSymbol?UserID=UserID&Password=Password&Category={}&SearchType={}&SearchString={}&Limit={}' GETACCOUNTSTATUS = 'GetAccountStatus?UserID=UserID&Password=Password&Group={}' # ============================================================================== # Client class for PXServiceWeb web service class PXServiceWebClient: # ------------------------------------------------------------------------------ # Constructor def __init__( self, userId, passwd, url=URL ): self.userId = f'UserID={userId}' self.passwd = f'Password={passwd}' self.url = url # ------------------------------------------------------------------------------ # Send a request to PXWeb and return and XML tree object def __send( self, request ): # Prepare final URL url = self.url + '/' + urllib.parse.quote( request, '?&=:' ) url = url.replace( 'UserID=UserID', self.userId ) url = url.replace( 'Password=Password', self.passwd ) # Send request and get response as an XML string web = urllib.request.urlopen( url ) response = web.read() # Check response header assert response.startswith( b' node returned for hNode in xml: # Get actual symbol symbol = hNode.attrib[ 'TickerSymbol' ] data[ symbol ] = hList = [] symbols.append( symbol ) # For each in node for dNode in hNode: # Get date and days from today dDateStr = dNode.attrib[ 'Date' ] dDate = datetime.strptime( dDateStr , '%Y-%m-%d' ).date() days = ( today - dDate ).days # Append days and price as coordinates price = float( dNode.attrib[ 'Close' ] ) hList.append( ( days, price, ) ) # Save border values priceMax = max( price, priceMax ) priceMin = min( price, priceMin ) daysMax = max( days, daysMax ) daysMin = min( days, daysMin ) dateMax = max( dDateStr, dateMax ) dateMin = min( dDateStr, dateMin ) # Save actual object properties self.data = data self.symbols = symbols self.priceMax = priceMax self.priceMin = priceMin self.daysMax = daysMax self.daysMin = daysMin self.dateMax = dateMax self.dateMin = dateMin self.today = today