In [1]:
import tushare as ts
import pandas as pd
import numpy as np

from xpinyin import Pinyin

In [2]:
df=ts.get_stock_basics()
df.head(5)
att=df.columns.values.tolist()
#clommun_show = ['name', 'pe', 'outstanding', 'totals', 'totalAssets', 'liquidAssets', 'fixedAssets',
#'esp', 'bvps', 'pb',  'perundp', 'rev', 'profit', 'gpr', 'npr', 'holders']

pin=Pinyin()
df['UP'] = None
for index, row in df.iterrows():
    name_str = df.name[index]
    #print(name_str)
    up_letter = pin.get_initials(name_str,u'')
    #print(up_letter)
    df.at[index,['UP']]=up_letter
#df[df['UP']=='HTGD']
df['code']=df.index
#print(df.UP)

code,代码 name,名称 industry,所属行业 area,地区 pe,市盈率 outstanding,流通股本(亿) totals,总股本(亿) totalAssets,总资产(万) liquidAssets,流动资产 fixedAssets,固定资产 reserved,公积金 reservedPerShare,每股公积金 esp,每股收益 bvps,每股净资 pb,市净率 timeToMarket,上市日期 undp,未分利润 perundp, 每股未分配 rev,收入同比(%) profit,利润同比(%) gpr,毛利率(%) npr,净利润率(%) holders,股东人数 ['name', 'pe', 'outstanding', 'totals', 'totalAssets', 'liquidAssets', 'fixedAssets', 'esp', 'bvps', 'pb', 'perundp', 'rev', 'profit', 'gpr', 'npr', 'holders']


In [3]:
col_show = ['name', 'open', 'pre_close', 'price', 'high', 'low', 'volume', 'amount',  'time', 'code']
initial_letter = ['HTGD','OFKJ','CDKJ','ZJXC','GXKJ','FHTX','DZJG']
code =[]
for letter in initial_letter:
    code.append(df[df['UP']==letter].code[0])
    #print(code)
if code != '': #not empty != ''
    df_price = ts.get_realtime_quotes(code)
    #print(df_price)
    #df_price.columns.values.tolist()
df_price[col_show]


Out[3]:
name open pre_close price high low volume amount time code
0 亨通光电 21.500 22.000 21.960 23.110 21.200 98727052 2198642229.000 15:00:00 600487
1 欧菲科技 14.000 14.740 14.110 14.940 13.800 170908883 2454933765.380 15:00:03 002456
2 长电科技 14.300 14.700 15.900 16.170 14.020 165795445 2632525261.000 15:00:00 600584
3 中际旭创 56.990 59.370 58.400 64.390 56.000 9156938 549740196.910 15:00:03 300308
4 光迅科技 31.000 31.620 32.580 34.500 30.680 47640638 1578763475.520 15:00:03 002281
5 烽火通信 31.700 32.370 32.290 33.460 31.250 50910497 1661789943.000 15:00:00 600498
6 大族激光 40.600 42.440 42.550 45.100 39.800 56482328 2428853657.020 15:00:03 002008

TO-DO

Add the map from initial to code

build up a dataframe with fundamental and indicotors

For Leadings, need cache more data for the begining data


In [4]:
from matplotlib.mlab import csv2rec

In [5]:
df=ts.get_k_data("002456",start='2018-01-05',end='2018-01-09')
df.to_csv("temp.csv")
r=csv2rec("temp.csv")
#r.date


/home/yunfeiz/anaconda3/lib/python3.7/site-packages/ipykernel_launcher.py:3: MatplotlibDeprecationWarning: The csv2rec function was deprecated in Matplotlib 2.2 and will be removed in 3.1.
  This is separate from the ipykernel package so we can avoid doing imports until

In [6]:
import time, datetime

#str = df[df.code == '600487'][clommun_show].name.values
#print(str)
today=datetime.date.today()
yesterday = today - datetime.timedelta(1)
#print(today, yesterday)
i = datetime.datetime.now()
print ("当前的日期和时间是 %s" % i)
print ("ISO格式的日期和时间是 %s" % i.isoformat() )
print ("当前的年份是 %s" %i.year)
print ("当前的月份是 %s" %i.month)
print ("当前的日期是  %s" %i.day)
print ("dd/mm/yyyy 格式是  %s/%s/%s" % (i.day, i.month, i.year) )
print ("当前小时是 %s" %i.hour)
print ("当前分钟是 %s" %i.minute)
print ("当前秒是  %s" %i.second)


当前的日期和时间是 2019-03-10 11:13:55.914648
ISO格式的日期和时间是 2019-03-10T11:13:55.914648
当前的年份是 2019
当前的月份是 3
当前的日期是  10
dd/mm/yyyy 格式是  10/3/2019
当前小时是 11
当前分钟是 13
当前秒是  55

In [7]:
import time
 
localtime = time.localtime(time.time())
print("本地时间为 :", localtime)

# 格式化成2016-03-20 11:45:39形式
print(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()))
 
# 格式化成Sat Mar 28 22:24:24 2016形式
print(time.strftime("%a %b %d %H:%M:%S %Y", time.localtime()))


本地时间为 : time.struct_time(tm_year=2019, tm_mon=3, tm_mday=10, tm_hour=11, tm_min=13, tm_sec=55, tm_wday=6, tm_yday=69, tm_isdst=0)
2019-03-10 11:13:55
Sun Mar 10 11:13:55 2019

In [8]:
#!/usr/bin/python
# -*- coding: UTF-8 -*-
 
import calendar 
cal = calendar.month(2019, 3)
#print (cal)