001量化專案總結 --01獲取實時價格

Lionever發表於2024-05-02

一、獲取實時價格

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

相關文章