Current location - Trademark Inquiry Complete Network - Tian Tian Fund - Is there any way for hybrid equity funds to check the ups and downs in real time? Such as index funds.
Is there any way for hybrid equity funds to check the ups and downs in real time? Such as index funds.
Below I mainly take the stock code as an example, and it is also possible to change it to the fund code. Pay attention in real time!

Ref = = => Tusharet

System: Ubuntu 15.04 Python:2.7 Environment package: Anaconda, for entertainment only. ...

I'm learning Python recently, so I want to learn how to trade stocks. So after learning some basic knowledge, I plan to do some demonstration exercises related to stocks. The first problem to be solved is of course data collection.

I searched the internet and read some code fragments of my friends. I know that the format of API request data is like this:',' ifeng': 'ifeng.com'}

URL_ERROR_MSG =' Failed to get, please check the network status or the API port URL does not match!'

Get _ History _ Data.py

#-*-encoding: utf-8 -*-

"""

Created on Thursday, July 23rd, 2007 at 09: 15:40 20 15.

@ Author: jet

"""

Import constants as contact types

Import pandas as pd

Import json

Import urlopen from urllib2, request

def get_hist_data(code = None,start = None,end = None,ktype = 'D '):

"""

Function:

Obtain historical transaction data of individual stocks

-

Input:

-

Code: String

Stock code, for example: 60 1989.

Start: string

Start date format: when YYYY-MM-DD is blank, the earliest date data provided by API is taken.

End: string

End date format: when YYYY-MM-DD is blank, the data of the latest trading day will be taken.

Ktype:string (default =D, functions are automatically unified to uppercase)

Data type D= daily K line, W= weekly K line, M= monthly K line, 5=5 minutes, 15= 15 minutes.

30=30 minutes, 60=60 minutes?

Output:

-

data frames

Date date

opening prices

Highest price

Closing price

rock-bottom price

Chg ups and downs?

P_chg ups and downs

Five-day average price of Ma5

Ma 10 10 daily average price.

Ma20 20-day average price

5-day average of VMA5

Vma 10 daily average

Vma20 20 Daily Average

Turnover rate (this index does not have this item)

"""?

code = code _ to _ API code(code . upper())

ktype = ktype.upper()

url =“”?

url = get_url(ktype,code)?

Print (url)

js = json.loads(ping_API(url))

cols = []

if len(js[' record '][0])= = 14:

cols = ct。 Index _ Day _ Price _ Column

Otherwise:

cols = ct。 Daily price column

df = pd。 DataFrame(js['record'],columns=cols)

If ktype in ct. K_TYPE_KEY:

df = df . apply map(lambda x:x . replace(u ',',u ' '))

For column [1:] in column:

df[col]=df[col]。 Astype (floating point type)

If not none at first:

df = df[df . date & gt; = Start]

If end is not None:

df = df[df . date & lt; = End]

df = df.set_index('date ')

Also df?

Def code_to_APIcode (code):

"""

Function:

Verify that the entered stock code is correct, and if it is correct, return the stock code used by API.

"""

Print (code)

If code in ct. INDEX_KEY:

Return to ct. INDEX _ LIST[ code]

Otherwise:

If len (code)! = 6:

IOError ('code input error!' )

Otherwise:

If the code [: 1] is in ['5',' 6'] else 'sz%s'%code,' sh%s'%code is returned.

def get_url(ktype,code):

"""

Function:

Verify that the K-line type entered is correct, and return the url if it is correct.

"""?

If ktype in ct. K_TYPE_KEY:

url = ct。 Daily price website%. PAGE_TYPE['http'],ct。 PAGE_DOMAIN['ifeng'],

ct。 K_TYPE[ktype], code)

Return url

Elif ktype in contact type. K_TYPE_MIN_KEY:

url = ct。 Lowest price URL%. PAGE_TYPE['http'],ct。 PAGE_DOMAIN['ifeng'],

Code, ktype)

Return url

Otherwise:

IOError('ktype input error!' )

Define ping_API(url):

"""

Function:

Send data request to API, and return data if the link is normal.

"""

text = ' '

Try:

Req = request (url)

text = urlopen(req,timeout= 10)。 read()?

if len(text)& lt; 15:

IOError is raised ('No data!' )?

The exception is e:

Print (e)

Otherwise:

Return text

# Test Portal

print(get _ hist _ data(' 60 1989 ',' 20 15-07- 1 1 ',' 20 15-07-22 '))