-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathZTrader.py
79 lines (63 loc) · 2.85 KB
/
ZTrader.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import requests,json
import urllib.parse
import ZBaseFunc
class TDAAPIs:
def __init__(self, OAuthUserID, RefreshToken,TdaAccountID):
self.OAuthUserID = OAuthUserID
self.RefreshToken = RefreshToken
self.TdaAccountID = TdaAccountID
ZBaseFunc.SetDLAPIPara('OAuthUserID', OAuthUserID)
ZBaseFunc.SetDLAPIPara('RefreshToken', RefreshToken)
ZBaseFunc.SetDLAPIPara('TdaAccountID', TdaAccountID)
def GetNewAccessToken(self,OAuthUserID=None, RefreshToken = None,TradeDict=dict()):
if OAuthUserID !=None:
self.OAuthUserID = OAuthUserID
if RefreshToken !=None:
self.RefreshToken = RefreshToken
headers = {
'Content-Type':'application/x-www-form-urlencoded'
}
self.RefreshToken = urllib.parse.quote(self.RefreshToken)
grant_type = "grant_type=refresh_token"
refresh_token = "refresh_token="+self.RefreshToken
access_type = "access_type=offline"
code = "code="
client_id = "client_id="+self.OAuthUserID
redirect_uri = "redirect_uri=https%3A%2F%2Flocalhost%3A8080"
Text = grant_type+"&"+refresh_token+"&"+access_type+"&"+code+"&"+client_id+"&"+redirect_uri
try:
response = requests.post("https://api.tdameritrade.com/v1/oauth2/token", headers=headers, data=Text)
Result = json.loads(response.content.decode())
self.TdaAPIAccessToken = access_token = Result['access_token']
self.RefreshToken = refresh_token =Result['refresh_token']
Status = "Get Token Successed"
TradeDict["TdaAPIRefreshToken"] = refresh_token
ZBaseFunc.SaveConfigFile(FileName="DefaultTradePara.ZFtd", DumpDict=TradeDict)
ZBaseFunc.SetDLAPIPara('AccessToken', Result['access_token'])
except:
Status = "Get Token Failed"
access_token = refresh_token = None
return (Status ,refresh_token ,access_token)
def GetAccountInfo(self,TdaAccountID=None, TdaAPIAccessToken = None):
if TdaAccountID !=None:
self.TdaAccountID = TdaAccountID
if TdaAPIAccessToken !=None:
self.TdaAPIAccessToken = TdaAPIAccessToken
AccountInfo = dict()
headers = {
'Authorization':'Bearer '+self.TdaAPIAccessToken
}
url = "https://api.tdameritrade.com/v1/accounts/"+self.TdaAccountID +'?fields=positions'
try:
response = requests.get(url=url, headers=headers)
Result = json.loads(response.content.decode())
AccountInfo["NetLiq"] = Result['securitiesAccount']['currentBalances']['liquidationValue']
Status = "Account Successed"
except:
pass
Status = "Account Failed"
return Status,AccountInfo
import pandas as pd
import numpy as np
import talib
import ZfinanceCfg