Skip to content

Instantly share code, notes, and snippets.

@pdaian
Last active January 21, 2018 03:31
Show Gist options
  • Save pdaian/d634aed4fc040299513c2ac7294d7db4 to your computer and use it in GitHub Desktop.
Save pdaian/d634aed4fc040299513c2ac7294d7db4 to your computer and use it in GitHub Desktop.
# THIS IS PROVIDED WITHOUT ANY WARRANTY EXPLICIT OR IMPLICIT
# THE AUTHORS ARE NOT RESPONSIBLE FOR ANY SECURITY ISSUES OR FINANCIAL LOSSES
# Installing requirements: pip install python-bittrex
# Then, change the next two lines with your Bittrex API info (with limit order & view info access)
BITTREX_PUBLIC = "INSERT BITTREX PUBLIC HERE"
BITTREX_SECRET = "INSERT BITTREX SECRET HERE"
# Finally, see the bottom of this file
# (do not edit below this point)
import bittrex, time, os
from bittrex.bittrex import Bittrex, API_V2_0
def run_async(func):
""" Async decorator """
from threading import Thread
from functools import wraps
@wraps(func)
def async_func(*args, **kwargs):
func_hl = Thread(target = func, args = args, kwargs = kwargs)
func_hl.start()
return func_hl
return async_func
@run_async
def do_dca(market, ordersize, interval, action, orderbook_id):
my_bittrex = Bittrex(BITTREX_PUBLIC, BITTREX_SECRET, api_version=API_V2_0)
trade_method = my_bittrex.trade_buy
if action == "SELL":
trade_method = my_bittrex.trade_sell
while True:
try:
orderbook = my_bittrex.get_orderbook(market, depth_type=orderbook_id)['result'][orderbook_id]
rates = [x['Rate'] for x in orderbook]
# best rate on book is min rate when buying, max when selling
top_rate = min(rates)
if action == "SELL":
top_rate = max(rates)
my_rate = top_rate * 1.05 # add some cushion to get fill; will tend to underestimate order size; adjust for fill probability / size closeness tradeoff
amount = ordersize / my_rate
print action, market, "@", my_rate, "order size", my_rate * amount, "expected order size", ordersize
print trade_method(market=market, order_type=bittrex.bittrex.ORDERTYPE_LIMIT, quantity=amount, time_in_effect=bittrex.bittrex.TIMEINEFFECT_IMMEDIATE_OR_CANCEL, condition_type=bittrex.bittrex.CONDITIONTYPE_NONE, target=0.0, rate=my_rate)
except Exception as e:
print "Buy Failed", e
print "Waiting", interval, "seconds"
time.sleep(interval)
def dca_buy(market, rate, interval):
""" Buys into market at specified order size (of first currency in market pair) using market orders with time spacing interval """
do_dca(market, rate, interval, "BUY", bittrex.SELL_ORDERBOOK)
def dca_sell(market, rate, interval):
""" Sells into market at specified order size using market orders with time spacing interval """
do_dca(market, rate, interval, "SELL", bittrex.BUY_ORDERBOOK)
if __name__ == "__main__":
# Start your dca threads up here
dca_buy("BTC-ETH", 1.0 / (2.0 * 24 * 7 * 1.0), 1800) # buy ETH with BTC at a rate of 1BTC/wk, with market orders every 30 mins
time.sleep(5) # stop outputs from colliding
dca_buy("BTC-ZEC", 0.5 / (2.0 * 24 * 7 * 1.0), 1800) # buy ZEC with BTC at a rate of .5BTC/wk, with market orders every 30 mins
while True: # allow kills
try:
time.sleep(1)
except KeyboardInterrupt:
print "exiting"
os._exit(1)
# WARNING: we recommend constant monitoring of this script and associated Bittrex accounts; also DO NOT store money on exchanges
# THIS IS NOT AN ENDORSEMENT OF ANY EXCHANGE
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment