Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #Current error, looping through Master Control and Tradelogic self.data gets assigned another value.
- #This program accesses Krakenex the bitcoin exchange, analyses and trades.
- #THE STORAGE OBJECT - { PAIR LIST ITERATION TRADE NUMBER TRADE QUALITY }
- #MasterControl -> DataStorage CurPair to get data.
- #Threading
- #threads = []
- #for i in range(5):
- # t = threading.Thread(target=worker)
- # threads.append(t)
- # t.start()
- #for i in threads():
- # print i
- import json
- import urllib
- import shelve
- import smtplib
- # private query nonce
- import time
- import pickle
- import hashlib
- import hmac
- import base64
- import connection
- class API(object):
- """Kraken.com cryptocurrency Exchange API.
- Public methods:
- load_key
- query_public
- query_private
- """
- keypath = "key.txt"
- def __init__(self, key = '', secret = ''):
- """Create an object with authentication information.
- Arguments:
- key -- key required to make queries to the API (default: '')
- secret -- private key used to sign API messages (default: '')
- """
- self.key = key
- self.secret = secret
- self.uri = 'https://api.kraken.com'
- self.apiversion = '0'
- self.method = "Balance"
- def load_key(self, path):
- """Load key and secret from file.
- Argument:
- path -- path to file (string, no default)
- """
- f = open(path, "r")
- self.key = f.readline().strip()
- self.secret = f.readline().strip()
- def _query(self, urlpath, req = {}, conn = None, headers = {}):
- """Low-level query handling.
- Arguments:
- urlpath -- API URL path sans host (string, no default)
- req -- additional API request parameters (default: {})
- conn -- kraken.Connection object (default: None)
- headers -- HTTPS headers (default: {})
- """
- url = self.uri + urlpath
- if conn is None:
- conn = connection.Connection()
- ret = conn._request(url, req, headers)
- conn.close()
- return json.loads(ret)
- def query_public(self, method, req = {}, conn = None):
- """API queries that do not require a valid key/secret pair.
- "https://api.kraken.com/0/private/Balance"
- Arguments:
- method -- API method name (string, no default)
- req -- additional API request parameters (default: {})
- conn -- connection object to reuse (default: None)
- """
- urlpath = '/' + self.apiversion + '/public/' + method
- return self._query(urlpath, req, conn)
- def query_private(self, method, req={}, conn = None):
- """API queries that require a valid key/secret pair.
- Arguments:
- method -- API method name (string, no default)
- req -- additional API request parameters (default: {})
- conn -- connection object to reuse (default: None)
- """
- urlpath = '/' + self.apiversion + '/private/' + method
- req['nonce'] = int(1000*time.time())
- postdata = urllib.urlencode(req)
- message = urlpath + hashlib.sha256(str(req['nonce']) +
- postdata).digest()
- signature = hmac.new(base64.b64decode(self.secret),
- message, hashlib.sha512)
- headers = {
- 'API-Key': self.key,
- 'API-Sign': base64.b64encode(signature.digest())
- }
- return self._query(urlpath, req, conn, headers)
- def close_connection(self,method = "",req={}, conn = "None"):
- conn.close()
- #List of keys. First pair then
- class shelfControl():
- database = None
- def __init__(self):
- self.database = shelve.open("Krakenex.db")
- def retreive(self, key):
- if self.database != None:
- return self.database[key]
- def store(self,key, item):
- self.database[key] = item
- def storeData(self, item):
- items = self.database["items"]
- temp = self.database[i]
- while i < items:
- self.database[i] = temp
- self.database[i] = self.database[i+1]
- temp = self.database[i+1]
- self.database[0] = item
- #Update a List and combine.
- #http://stackoverflow.com/questions/30055830/pythonic-way-to-merge-two-overlapping-lists-preserving-order
- def update(self, key, newList):
- oldList = self.database[key]
- print oldList[0]
- class trade():
- def __init__(self, pairIn = None, priceIn = None, volumeIn = None, buySellIn = None, timeIn = None, idIn = None):
- self.pair = pairIn
- self.price = priceIn
- self.volume = volumeIn
- self.buySell = buySellIn #None means NA -1 means sell, +1 means buy
- self.time = time
- self.id = idIn
- def __getstate__(self): return self.__dict__
- def __setstate__(self, d): self.__dict__.update(d)
- #This will create a trade from the Trades Public Query to KrakenEX
- def tradeFromTrades(pairIn, dictIn):
- if dictIn[3] == 's':
- dictIn[3] = -1
- elif dictIn[3] == 'b':
- dictIn[3] = 1
- newTrade = trade(pairIn, dictIn[0], dictIn[1], dictIn[3], dictIn[2],dictIn[2])
- return newTrade
- def tradeFromOrders(pairIn, dictIn, buySell):
- return trade(pairIn, dictIn[0], dictIn[1], buySell, dictIn[2], dictIn[2])
- class tradeList():
- description = None
- trades = []
- listType = 0
- def __getstate__(self): return self.__dict__
- def __setstate__(self, d): self.__dict__.update(d)
- def __init__(self,pair,listTypeIn):
- self.description = pair
- self.listType = listTypeIn
- self.trades = []
- def addTrade(self,trade):
- self.trades.append(trade)
- return self.trades
- #unless I can figure out how to pickle objects I'll have to go with the middle of the road solution. The Middle of the road solution dicts.
- #So to get a price it would be dict[pair][age][list][trade][price] which is slow and long. Trade is also somewhat arbitrary.
- class DataStorage(object):
- data = {}
- shelf = None
- iterations = None
- def __init__(self):
- self.shelf = shelfControl()
- self.shelf.store("Iterations",1)
- self.iterations = self.shelf.retreive("Iterations")
- #data = self.update(400)
- def save():
- self.shelf.store(self.data)
- #redo
- def load(iteration):
- self.data = self.shelf.retreive("data")
- self.data[keyPair] = self.shelf.retreive(0)
- #Update all data one place.
- #For number of iterations saved update all of them
- def update(self):
- #We need to decide how to do this. Simple way.
- print "Update"
- class getPair(object):
- api = API()
- pair = None
- data = {} #Might be wrong.
- shelf = None
- def __init__ (self,dataIn, pairIn = 'XXBTZCAD'):
- self.pair = pairIn
- self.data = dataIn
- self.assetinfo(self.pair, 60)
- #These are supposed to provide object permenence but don't seem to
- def __getstate__(self): return self.__dict__
- def __setstate__(self, d): self.__dict__.update(d)
- #Gets a Kraken Pair from the server and parses it into lists.
- def assetinfo(self,pair, count = 30):
- #Orderbook
- requestOrderbook = {"pair": pair, "count" : count}
- ordersPending = self.api.query_public( "Depth", requestOrderbook)
- print "In AssetInfo"
- count = 0
- itemList = []
- print self.data[0]
- self.data[0][pair] = {}
- self.data[0][pair]['bids'] = {}
- for item in ordersPending["result"][pair]["bids"]:
- print item
- itemList.append(item[0])
- itemList.append(item[1])
- itemList.append(item[2])
- # FIX THIS! PAIR LIST ITERATION TRADENUMBER VALUES
- self.data[0][pair]['bids'][count] = {}
- self.data[0][pair]['bids'][count] = itemList
- count += 1
- itemList = []
- self.data[0][pair]['asks'] = {}
- count = 0
- for item in ordersPending["result"][pair]["asks"]:
- print item
- itemList.append(item[0])
- itemList.append(item[1])
- itemList.append(item[2])
- # FIX THIS! PAIR LIST ITERATION TRADENUMBER VALUES
- self.data[0][pair]['asks'][count] = {}
- self.data[0][pair]['asks'][count] = itemList
- count += 1
- itemList = []
- #Past Trades
- requestPastTrades = {"pair": pair}
- trades = self.api.query_public( "Trades", requestPastTrades)
- itemList = []
- countBuy = 0
- countSell = 0
- self.data[0][pair]['sold'] = {}
- self.data[0][pair]['bought'] = {}
- for item in trades["result"][pair]:
- if item[3] == 's':
- item[3] = -1
- elif item[3] == 'b':
- item[3] = 1
- # FIX THIS! PAIR LIST ITERATION TRADENUMBER VALUES
- else:
- print "ErrorTrade"
- print item
- itemList.append(item[0])
- itemList.append(item[1])
- itemList.append(item[3])
- itemList.append(item[2])
- if item[3] == -1:
- self.data[0][pair]['sold'][countSell] = itemList
- countSell += 1
- elif item[3] == 1:
- self.data[0][pair]['bought'][countBuy] = itemList
- countBuy += 1
- else:
- print "Trade Error"
- itemList = []
- print "AssetInfo Finished"
- print len(self.data)
- #TODO Curpair, Trade Lists, TradeList, Trade
- class tradeLogic(object):
- shelf = None
- settings = {"patience" : .75, "patiencea" : .5, "patiencego" : 0, "spreadgo": .04}
- pairSettings = {}
- pairList = {}
- thisPair = None
- def __init__( self, dataIn,pairListIn = ['XXBTZCAD'] ):
- #Goes through list of pairs and creates CurPair items
- self.pairList = pairListIn
- self.data = dataIn
- for item in self.pairList:
- self.thisPair = item
- print item
- self.patience(self.settings,item)
- self.trendSpot(item)
- self.spread(self.settings, item)
- #Save relevant data
- def pairList(listIn):
- self.pairList = listIn
- def trendSpot(self, pair):
- print "TrendSpot"
- #Checks for rapid update in Price via % or Low volume of orders before major price swing.
- #for item in self.data[0][pair].sellOrders.trades:
- #print item.price
- #print item.volume
- #for item in self.data[0][pair].buyOrders.trades:
- # print item.price
- # print item.volume
- # print item
- #2nd guess. Got to get good at this!!!
- #for item in self.data[0][pair]['bids'][count]:
- # print item
- #Checks if there is a big spread and sends: spread, pair, and volume at the spread
- def spread(self,setting,pairs):
- print "Spread"
- spreadInPlace = self.data[0][self.thisPair]['bids'][0][0]/self.data[0][self.thisPair]['asks'][0][0]
- volumeSpread = self.data[0][self.thisPair]['bids'][0][1] * self.data[0][self.thisPair]['asks'][0][1]
- spreadGoing = setting['spreadgo']
- if spreadInPlace >= spreadGoing:
- toSend = "Spread detected" , spreadInPlace, pairs, volumeSpread
- message(toSend)
- #2 up 1 down, then two down one up. Two sells one buy or two buys one sell all the time.
- def theThreeTrades():
- print "Three Trades"
- def patience(self, setting, pairs):
- aggression = setting["patience"]
- amount = setting["patiencea"]
- go = setting["patiencego"]
- #Make a list of trades starting from the daily high and low and extending 75% counting from the amount traded.
- #The idea for patience is to trade based on how much we're trading. So if we're trading blocks of .5 units then we trade that far into the buy or sell orders.
- #So go through trades starting at sold0 - soldX subtrade the volume of each from amount until it hits 0 then make the trade.
- x = 0
- while x < amount:
- print "Amount ", amount, "x = " , x
- amount += 1
- print amount
- x +=2
- if go:
- #DO TRADE
- print "In Go Not Trading yet"
- #Send Email
- print "No Email Either"
- def easyMoney(self, volume = 35, aggression = 100):
- #Get 5 bids and asks. Find lowest/highest price which has least volume between it and current price.
- y = {}
- dataStack = {}
- aggression2 = aggression * 2
- y['tradebid'] = 0
- y['tradeask'] = 0
- count = 0
- x = 0
- z = 0
- while x < volume:
- print count
- x += float(self.data[0][self.thisPair]['bids'][count][1])#Find a bid or ask add volume.
- print self.data[0][self.thisPair]['bids'][0][1]
- y['bid'] = self.data[0][self.thisPair]['bids'][count][0]#Price 1 for bid.
- y['tradebid'] = (1 + y['tradebid'])
- y['diffbid'] = (y['bid'])# - firstbid)
- if y['diffbid'] >= aggression2:
- #fire trade Size Aggression.
- print "We'd be trading"
- count += 1
- print "This is x", x
- print "Done finding trade before volume 1"
- count = 0
- for z in 0, volume:
- z += float(self.data[0][self.thisPair]['asks'][count][1])#find opposite bid or ask.
- y['ask'] = float(self.data[0][self.thisPair]['asks'][count][0])#Price 2
- y['tradeask'] = (1 + y['tradeask'])
- y['diffask'] = (y['ask'])# + firstbidasks)
- # if y['diffask'] >= aggression2:
- # message("Trade Made")
- #make Trade (/s? Like multiple?)
- #Send Email
- print self.settings
- return dataStack
- #Master Control, the central Object
- class MasterControl(object):
- #Currencies = XBT, ETH, DAO,ETC,ETH,LTC,XDG,XLM,XRP,CAD,EUR,JPY,GBP,USD
- pairList = [ 'XETHZCAD', 'XETHZEUR','XETHZJPY']#,'XLTCZEUR','XXBTZCAD']#'XDAOZEUR', 'XETCZEUR','XLTCZCAD','XXBTZEUR','XETHZEUR.d', 'XXBTZJPY', 'XXBTZGBP', 'XDAOXXBT', 'XETHZUSD','XLTCZUSD', 'XETCXXBT','XDAOXETH', 'XETHZGBP', 'XXBTZGBP', 'XLTCZEUR', 'XETCXETH', 'XETHZGBP', 'XLTCXXBT', 'XXLTCZJPY','XXBTZUSD', 'XETCZEUR', 'XXRPXXBT', 'XDAOZGBP','XETCZUSD', 'XDAOZUSD', 'XETHXXBT' ]
- ds = None
- data = {}
- data[0] = {}
- aPair = None
- def __init__(self):
- myapi = API()
- self.ds = DataStorage()
- #GetStorage working. Don't worry about speed.
- for items in self.pairList:
- print "This is the curPair we're working on"
- print items
- #This updates the data.
- self.data = getPair(self.data, items)
- #Logic - Load old data. Update Data in datastorage. Get new data put into dataStorage. Do calculations on data.
- print "Should be data Here"
- tl = tradeLogic(self.data, self.pairList)
- tl.easyMoney(70, 130)#Volume and aggression
- myapi = API()
- myapi.load_key(myapi.keypath)
- #print "Private Balance"
- #balance = myapi.query_private("Balance")
- #print balance
- #print "Private OpenOrders"
- #openorders = myapi.query_private("OpenOrders")
- #print openorders
- #print "Private ClosedOrders"
- #closedorders = myapi.query_private("ClosedOrders")
- #print closedorders
- print "Private QueryOrders"
- #queryorders = myapi.query_private("QueryOrders", {'trades':"True"})
- #print queryorders
- print "Private TradeHistory"
- #tradeshistory = myapi.query_private("TradesHistory")
- #print tradeshistory
- print "Private QueryTrades"
- #querytrades = myapi.query_private("QueryTrades")
- #print querytrades
- print "Private OpenPositions"
- openpositions = myapi.query_private("OpenPositions")
- print openpositions
- print "Private Ledgers"
- #ledgers = myapi.query_private("Ledgers")
- #print ledgers
- print "Query Ledgers"
- #queryledgers = myapi.query_private("QueryLedgers")
- #print queryledgers
- #tradevolume = myapi.query_private("TradeVolume")
- #print "TradeVolume "
- #print tradevolume
- #addorderQuery = {"pair": "XXBTZCAD", "type":"buy", "ordertype": "limit", "price": "1.0", "volume" : "0.1"}
- #addorder = myapi.query_private("AddOrder", addorderQuery)
- #print "ORDER ADDED"
- #print addorder
- #assetpairs = myapi.query_public("AssetPairs")
- #existing.append(getPair("XXBTZCAD"))
- #existingTL = tradeLogic()
- #TODO check for price changes and update lists, send email.
- #LOAD, UPDATE, GET
- global mc
- mc = MasterControl()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement