一、获取实时价格

def tdxgetprice(self,scode):                               # 取实时价格
price =0.0
pmarkcode =0
sip =''
sport =0
time_now =datetime.now().minute

if (scode[0]=='0' and scode[1]=='0') or scode[0]=='3':
pmarkcode=0
else:
pmarkcode=1

api = TdxHq_API()
if time_now%2==0 and time_now%4==0:
sip='218.*.51.*'
sport =7709
elif time_now%2==0 and time_now%4!=0:

sip='*.235.*.14'
sport=7711
else:
sip='*.73.48.*'
sport=7709
with api.connect(sip,sport):
data = api.get_security_quotes([pmarkcode,scode])
cdata = api.get_company_info_category(pmarkcode,scode)
d1= collections.OrderedDict()
d1 = data[0]
#print(d1)
for key ,value in d1.items():
if key=='price':
price= value
api.disconnect()
return price
二、返回当日数据 价格 成交量 开盘, 最低、最高价格。
def getnewdata(self, scode):  # 

s1 = []
Date1 = str(datetime.now().date())
s1.insert(0, Date1)
napi = TdxHq_API()
sip = self.getserverip()[0]
sport = self.getserverip()[1]
markcode = int(self.getmarkcode(scode))
with napi.connect(sip, sport):
ndata = napi.get_security_quotes([markcode, scode])
d1 = ndata[0]

for key, value in d1.items():
if key == 'open':
s1.insert(1, value)
if key == 'price':
s1.insert(2, value)
if key == 'high':
s1.insert(3, value)
if key == 'low':
s1.insert(4, value)
if key == 'amount':
s1.insert(5, value)
napi.disconnect()

return s1
posted on 2024-05-02 17:30  Lionever  阅读(12)  评论(0编辑  收藏  举报