A few days ago reading online, I saw how a friend had made a BOT for Bittrex.
Although I did not use the Bittrex API, as I read above, it worked quite well, then I noticed some logical inconsistencies.
That POST helped me to realize that I did not have to have a finished product, I only had to first make a functional prototype.
Then make a more specific BOT or as they say by adding more functionality and robustness.
From here I took out the structure of my colleague @tstieff.
NOTE: I did not do the structure exactly the same, I want to share some things that we should keep in mind:
The original code is connect to a series of pages with the keys, it is like scraping the web or web scrapping.
Let's say that if it has a bit of API form since the data it returns is in a dictionary or JSON, what I did was to change the code and leave it almost functional, but I'll tell you why almost.
Another important thing to buy and sell in Bittrex is to know the least that can be buy besides to the balance or balance that you have to buy.
If we have no balance we can not buy anything and fail, that was another strange thing that I saw in the code I see why the author said that the algorithm was stupid, said buy or sell 5 always.
The new code that improves or makes me look like this: @sethroot/ADA-CRYPBOT
Add within the code the Bittrex library that we are going to use, so you can copy and paste in a py and then execute it and it will work.
Make sure to place your API Tokens, this part of the code I am talking about:
class Bittrex(object):
def __init__(self, api_key, api_secret):
self.api_key = str(api_key) if api_key is not None else ''
self.api_secret = str(api_secret) if api_secret is not None else ''
def api_query(self, method, options={}):
nonce = str(int(time.time() * 1000))
method_set = 'public'
if method in MARKET_SET:
method_set = 'market'
elif method in ACCOUNT_SET:
method_set = 'account'
request_url = (BASE_URL % method_set) + method + '?'
if method_set != 'public':
request_url += 'apikey=' + self.api_key + "&nonce=" + nonce + '&'
request_url += urllib.urlencode(options)
return requests.get(
request_url,
headers={"apisign": hmac.new(self.api_secret.encode(), request_url.encode(), hashlib.sha512).hexdigest()}
).json()
def get_market_summaries(self):
return self.api_query('getmarketsummaries')
def buy_limit(self, market, quantity, rate):
return self.api_query('buylimit', {'market': market, 'quantity': quantity, 'rate': rate})
def sell_limit(self, market, quantity, rate):
return self.api_query('selllimit', {'market': market, 'quantity': quantity, 'rate': rate})
For Bittrex we had a File before, now add it to the same File to solve compatibility or execution problems.
In that list of functions we have get_market_summaries, sell_limit, buy_limit with that right now it's enough for us.
Then create the main function to run the Script there, then the tick () function to execute the tickets:
def tick():
print('RUNNING')
market_summaries = bittrex.get_market_summaries()
for summary in market_summaries['result']:
print summary
market = summary['MarketName']
day_close = summary['PrevDay']
last = summary['Last']
if day_close > 0:
percent_chg = ((last / day_close) - 1) * 100
else:
print ('day_close zero for ' + market)
print(market + ' changed ' + str(percent_chg))
print "Run other Crypto"
It is important to note that this function does not buy or sell anything because we still need 2 things.
The function to cancel a buy or sale and the function to extract the balance of what we have, how can we sell something we do not have? ?
The part of buying and selling as the price has changed would be like that, and in the other entry, we will expect it:
#if 40 < percent_chg < 60:
# Fomo strikes! Let's buy some
#print('Purchasing 5 units of ' + market + ' for ' + str(format_float(last)))
#res = buy_limit(market, 5, last)
#print(res)
#if percent_chg < -20:
# Ship is sinking, get out!
#print('Selling 5 units of ' + market + ' for ' + str(format_float(last)))
#res = sell_limit(market, 5, last)
#print(res)
Then we have these 2 functions that are very important:
These are the functions that connect to the API and go to buy and sell.
The Bot has not been automated, we must execute it manually, we could create a while and execute it infinitely, but we can only add:
def buy_limit(market, quantity, rate):
buy_limit = bittrex.buy_limit( market, quantity, rate)
return buy_limit
def sell_limit(market, quantity, rate):
sell_limit = bittrex.sell_limit( market, quantity, rate)
return sell_limit
Balance of what the BOT is going to sell Balance of what we have available in BTC to buy Function to check that we have more than the minimum purchase amount Function to buy or sell something like what we already have if it changes + 50% sell. Function to make the floor, for example if in the last hour it raised 70% and in the following hour under 20% then that it sells, this will need of the database that already we have in TXT.
We are closer! and let's go with everything!
I tell you that I have another Bittrex paralaleo project that I already talked about and it gave rise to a big comparator alarm project that I am going to automate for the STEEMIT community.
I hope you like it!
Happy Day and God Bless Venezuela