Created
June 9, 2011 05:32
-
-
Save christianjohnson/1016121 to your computer and use it in GitHub Desktop.
TK API Python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
###### | |
To use this class: | |
Replace self.appkey, userkey, secret, account_number | |
tradeking = TK.TK() | |
profit = tradeking.get_profit() | |
##### | |
class TK(threading.Thread): | |
def __init__(self, stock_list=[], quotes_cache={}): | |
threading.Thread.__init__(self) | |
self.appkey = 'xxxx' | |
self.userkey = 'xxxx' | |
self.secret = 'xxxx' | |
self.account_number = 'xxx' | |
self.quotes_cache = quotes_cache | |
self.counter = 0 | |
self.stock_list = stock_list | |
self.lock = threading.RLock() | |
def run(self): | |
while True: | |
self.get_quotes() | |
time.sleep(1) | |
def regenerate(self, body): | |
timestamp = str(int(time.time())) | |
encoded = base64.b64encode(body + timestamp) | |
signature = hmac.new(self.secret, encoded).hexdigest() | |
return timestamp, encoded, signature | |
def get_quotes(self): | |
body = "<request><account>%s</account><quote><symbols>" % (self.account_number) | |
for stock in self.stock_list: | |
body += "<symbol>%s</symbol>" % (stock) | |
body += "</symbols><delayed>false</delayed></quote></request>" | |
#print "Getting Quotes..." | |
timestamp, encoded, signature = self.regenerate(body) | |
request = urllib2.Request('https://tkapi.tradeking.com/beta/trade/quotes', body) | |
request.add_header('TKI_USERKEY', self.userkey) | |
request.add_header('TKI_APPKEY', self.appkey) | |
request.add_header('TKI_TIMESTAMP', timestamp) | |
request.add_header('TKI_SIGNATURE', signature) | |
request.add_header('Content-Type', 'application/xml') | |
request.add_header('Accept', 'application/json') | |
results = json.loads(urllib2.urlopen(request).read()) | |
#pprint.pprint(results['response']) | |
try: | |
if results != None: | |
#pprint.pprint(results['response']) | |
quotes = results['response']['quotes']['instrumentquote'] | |
for quote in quotes: | |
sym = quote['instrument']['sym'] | |
self.lock.acquire() | |
try: | |
self.quotes_cache[sym] = quote['quote'] | |
finally: | |
self.lock.release() | |
else: | |
print "Error with quotes..." | |
except KeyError: | |
if results['response']['type'] == 'Error': | |
print "Error: " + results['response']['name'] | |
def trade_preview(self, ticker, num_shares): | |
fixml = '''<FIXML xmlns="http://www.fixprotocol.org/FIXML-5-0-SP2"> | |
<Order TmInForce="0" Typ="1" Side="1" Acct="%s"> | |
<Instrmt SecTyp="CS" Sym="%s"/> | |
<OrdQty Qty="%d"/> | |
</Order> | |
</FIXML>''' % (self.account_number, ticker, num_shares) | |
body = '''<request> | |
<account>%s</account> | |
<trade> | |
<fixml> | |
<![CDATA[ | |
%s | |
]]> | |
</fixml> | |
</trade> | |
</request>''' % (self.account_number, fixml) | |
timestamp, encoded, signature = self.regenerate(body) | |
request = urllib2.Request('https://tkapi.tradeking.com/beta/trade/preview', body) | |
request.add_header('TKI_USERKEY', self.userkey) | |
request.add_header('TKI_APPKEY', self.appkey) | |
request.add_header('TKI_TIMESTAMP', timestamp) | |
request.add_header('TKI_SIGNATURE', signature) | |
request.add_header('Content-Type', 'application/xml') | |
request.add_header('Accept', 'application/json') | |
results = json.loads(urllib2.urlopen(request).read()) | |
try: | |
if results['response']['type'] == 'Error': | |
print "Error: " + results['response']['name'] | |
return None | |
except KeyError: | |
return results['response'] | |
def get_profit(self): | |
body = "<request><account>%s</account></request>" % (self.account_number) | |
timestamp, encoded, signature = self.regenerate(body) | |
request = urllib2.Request('https://tkapi.tradeking.com/beta/account/holdings', body) | |
request.add_header('TKI_USERKEY', self.userkey) | |
request.add_header('TKI_APPKEY', self.appkey) | |
request.add_header('TKI_TIMESTAMP', timestamp) | |
request.add_header('TKI_SIGNATURE', signature) | |
request.add_header('Content-Type', 'application/xml') | |
request.add_header('Accept', 'application/json') | |
results = json.loads(urllib2.urlopen(request).read()) | |
holdings = results['response']['accountholdings']['holding'] | |
profit = 0.0 | |
for holding in holdings: | |
profit += float(holding['gainloss']) | |
return "Profit of $%.2f" % (profit) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment