def percent_sell_strat(self): """ If a coin drops more than 10% of its highest price then sell """ held_markets = [market for market in self.held_coins] for coin_market in held_markets: current_high = self.history_coins[coin_market] coin_price = float(self.bittrex_coins[coin_market]['Last']) """ if cur_24h_change > highest_24h_change: self.held_coins[coin_market]['highest_24h_change'] = cur_24h_change highest_24h_change = cur_24h_change utils.json_to_file(self.held_coins, "held_coins.json") self.held_coins[coin_market]['sell_threshold'] = self.updated_threshold(coin_market, self.held_coins) utils.json_to_file(self.held_coins, "held_coins.json") """ if coin_price < current_high * 0.97: coin_to_sell = utils.get_second_market_coin(coin_market) balance = self.api.get_balance(coin_to_sell) if balance['success']: amount = float(balance['result']['Available']) if amount > 0: utils.sell(self.api, amount, coin_market, self.bittrex_coins) else: utils.print_and_write_to_logfile( "Could not retrieve balance: " + balance['message'])
def percent_sell_strat(self): ignored = utils.file_to_json("ignored_coins.json") held_markets = [market for market in self.held_coins] for coin_market in held_markets: current_high = self.history_coins[coin_market] coin_price = float(self.bittrex_coins[coin_market]['Last']) percent_change_24h = utils.get_percent_change_24h( self.bittrex_coins[coin_market]) if coin_price < current_high * 0.80 \ or percent_change_24h >= 110: coin_to_sell = utils.get_second_market_coin(coin_market) balance = self.api.get_balance(coin_to_sell) if balance['success']: amount = float(balance['result']['Available']) if amount > 0: utils.sell(self.api, amount, coin_market, self.bittrex_coins) ignored[coin_market] = 1 utils.json_to_file(ignored, "ignored_coins.json") else: utils.print_and_write_to_logfile( "Could not retrieve balance: " + balance['message'])
def buy(self, market, total_bitcoin): coins_pending_buy = [ market for market in self.pending_orders['Buying'] ] coins_pending_sell = [ market for market in self.pending_orders['Selling'] ] if market not in self.held_coins and market not in coins_pending_buy and market not in \ coins_pending_sell: slots_open = self.total_slots - len(self.held_coins) - len( self.pending_orders['Buying']) - len( self.pending_orders['Selling']) bitcoin_to_use = float(total_bitcoin / (slots_open + .25)) coin_price = float(self.bittrex_coins[market]['Last']) amount = bitcoin_to_use / coin_price if amount > 0: percent_change_24h = utils.get_percent_change_24h( self.bittrex_coins[market]) result = utils.buy(self.api, market, amount, coin_price, percent_change_24h, 0, 0) if result['success']: utils.print_and_write_to_logfile('Buy order of' + str(amount) + 'of' + market + 'Unsuccessful') else: utils.print_and_write_to_logfile('Buy order of' + str(amount) + 'of' + market + 'Successful') return result
def update_pending_orders(orders): """ Checks bitttrex's open orders and if a coin that's in pending_orders is no longer in bittrex's pending orders it has gone through, so we add it to our held coins. :param orders: :return: """ pending_orders = utils.file_to_json("pending_orders.json") # Move processed buy orders from pending_orders into held_coins processing_orders = [order['OrderUuid'] for order in orders] buy_uuids_markets = [(pending_orders['Buying'][market]['uuid'], market) for market in pending_orders['Buying']] for buy_uuids_market in buy_uuids_markets: buy_uuid = buy_uuids_market[0] if buy_uuid not in processing_orders: buy_market = buy_uuids_market[1] pending_buy_order = pending_orders['Buying'][buy_market] amount = str(pending_buy_order['amount']) utils.print_and_write_to_logfile("Buy order: " + amount + " of " + buy_market + " Processed Successfully " + "UUID: " + buy_uuid + " " + utils.get_date_time()) move_to_from_held(buy_market, 'Buying') # Add price to highest_price_history highest_price_list = utils.file_to_json( "coin_highest_price_history.json") highest_price_list[buy_market] = pending_orders['Buying'][ buy_market]['price_bought'] utils.json_to_file(highest_price_list, 'coin_highest_price_history.json') # Move processed sold orders from pending_orders into held_coins sell_uuids_markets = [(pending_orders['Selling'][market]['uuid'], market) for market in pending_orders['Selling']] for sell_uuids_market in sell_uuids_markets: if sell_uuids_market[0] not in processing_orders: pending_sell_order = pending_orders['Selling'][ sell_uuids_market[1]] amount = str(pending_sell_order['amount']) utils.print_and_write_to_logfile("Sell order: " + amount + " of " + " " + sell_uuids_market[1] + " Processed Successfully " + "UUID: " + sell_uuids_market[0] + " " + utils.get_date_time()) move_to_from_held(sell_uuids_market[1], 'Selling')
def clean_orders(orders): """ Finds any order that has been attempting to buy or sell for longer than the variable time_until_cancel_processing_order_minutes and attempt to cancel them on bittrex, and also deletes them from pending_orders.json :param orders: :return void: """ pending_orders = utils.file_to_json("pending_orders.json") for order in orders: time_opened = order['Opened'] time_passed = utils.get_time_passed_minutes(time_opened) uuid = order['OrderUuid'] market = "" buying_or_selling = 'Buying' if order[ 'OrderType'] == 'LIMIT_BUY' else 'Selling' for pending_market in pending_orders[buying_or_selling]: if pending_orders[buying_or_selling][pending_market][ 'uuid'] == uuid: market = pending_market if time_passed > time_until_cancel_processing_order_minutes: uuid = order['OrderUuid'] cancel_order = api.cancel(uuid) if cancel_order['success']: if market in pending_orders[buying_or_selling]: del pending_orders[buying_or_selling][market] utils.json_to_file(pending_orders, "pending_orders.json") utils.print_and_write_to_logfile("Cancel Order of " + str(order["Quantity"]) + " " + str(order['Exchange']) + " Successful " + utils.get_date_time()) else: utils.print_and_write_to_logfile("Cancel Order of " + str(order["Quantity"]) + order['Exchange'] + " Unsuccessful: " + cancel_order['message'] + " " + utils.get_date_time())
def low_high_buy_strat(self, total_bitcoin): top_reddit_coins = utils.file_to_json('reddit_top_coins.json') markets_to_ignore = ['BTC-ETH', 'BTC-BTC'] for rank in range(len(top_reddit_coins)): market = top_reddit_coins[str(rank)]['market'] if market not in markets_to_ignore: slots_open = self.total_slots - len(self.held_coins) - len( self.pending_orders['Buying']) - len( self.pending_orders['Selling']) bitcoin_to_use = float(total_bitcoin / (slots_open + .25)) coins_pending_buy = [ market for market in self.pending_orders['Buying'] ] coins_pending_sell = [ market for market in self.pending_orders['Selling'] ] if market not in self.held_coins and market not in coins_pending_buy and market not in \ coins_pending_sell: percent_change_24h = utils.get_percent_change_24h( self.bittrex_coins[market]) coin_low_bars = utils.file_to_json('coin_low_bars.json') low_bar = coin_low_bars[market] if percent_change_24h <= self.desired_low_point and percent_change_24h <= low_bar: coin_low_bars[market] = low_bar - 10 utils.json_to_file(coin_low_bars, 'coin_low_bars.json') elif low_bar != self.desired_low_point and percent_change_24h > low_bar + 10: self.update_bittrex_coins() coin_price = float(self.bittrex_coins[market]['Last']) amount = bitcoin_to_use / coin_price if amount > 0: result = utils.buy(self.api, market, amount, coin_price, percent_change_24h, self.desired_gain, 0) if not result['success']: utils.print_and_write_to_logfile( "Failed to make buy order " + market) else: utils.print_and_write_to_logfile( "Attempting buy order of " + str(amount) + " of " + market)
def hodl_sell_strat(self): for market in self.held_coins: cur_price = float(self.bittrex_coins[market]['Last']) bought_price = self.held_coins[market]['price_bought'] change = utils.percent_change(bought_price, cur_price) desired_gain = self.held_coins[market]['desired_gain'] if change >= desired_gain: coin_to_sell = utils.get_second_market_coin(market) balance = self.api.get_balance(coin_to_sell) if balance['success']: amount = float(balance['result']['Available']) utils.sell(self.api, amount, market, self.bittrex_coins) else: utils.print_and_write_to_logfile( "Could not retrieve balance: " + balance['message'])
def hodl_buy_strat(self, total_bitcoin): slots_open = self.total_slots - len(self.held_coins) - len( self.pending_orders['Buying']) - len( self.pending_orders['Selling']) bitcoin_to_use = float(total_bitcoin / (slots_open + .25)) failed_pairs = [] for market_gain_pair in self.markets_desired_gain: market = market_gain_pair[0] desired_gain = market_gain_pair[1] coins_pending_buy = [ market for market in self.pending_orders['Buying'] ] coins_pending_sell = [ market for market in self.pending_orders['Selling'] ] if market not in self.held_coins and market not in coins_pending_buy and market not in \ coins_pending_sell: coin_price = float(self.bittrex_coins[market]['Last']) amount = bitcoin_to_use / coin_price if amount > 0: percent_change_24h = utils.get_percent_change_24h( self.bittrex_coins[market]) result = utils.buy(self.api, market, amount, coin_price, percent_change_24h, desired_gain, 0) if result['success']: utils.print_and_write_to_logfile('Buy order of' + str(amount) + 'of' + market + 'unsuccessful') else: failed_pairs.append(market_gain_pair) utils.print_and_write_to_logfile('Buy order of' + str(amount) + 'of' + market + 'successful') self.markets_desired_gain = failed_pairs
def reddit_buy_strat(self, total_bitcoin): coin_rank = 0 coins_to_dismiss = utils.file_to_json( 'coins_to_dismiss.json')["coins_to_dismiss"] slots_open = self.total_slots - len(self.held_coins) - len( self.pending_orders['Buying']) - len( self.pending_orders['Selling']) while coin_rank < 5: cur_coin = self.rank_by_upvotes()[coin_rank][0] if cur_coin not in coins_to_dismiss: market = "BTC-" + cur_coin coins_pending_buy = [ market for market in self.pending_orders['Buying'] ] coins_pending_sell = [ market for market in self.pending_orders['Selling'] ] if market not in self.held_coins and market not in coins_pending_buy and market not in \ coins_pending_sell: bitcoin_to_use = float(total_bitcoin / (slots_open + .25)) coin_price = float(self.bittrex_coins[market]['Last']) amount = bitcoin_to_use / coin_price if amount > 0: percent_change_24h = utils.get_percent_change_24h( self.bittrex_coins[market]) buy_request = utils.buy(self.api, market, amount, coin_price, percent_change_24h, 0) if buy_request['success']: utils.print_and_write_to_logfile("Buy order of " + str(amount) + " " + market + " requested") else: utils.print_and_write_to_logfile( buy_request['message']) if cur_coin not in coins_to_dismiss: coin_rank += 1
def keltner_sell_strat(self): if len(self.keltner_coins['BTC-ETH'] ['upper_band_data']) > self.keltner_period: for coin in self.keltner_coins: market = self.bittrex_coins[coin]['MarketName'] lower_band_data = self.keltner_coins[market]['lower_band_data'] if len(lower_band_data) == self.keltner_period: market = self.bittrex_coins[coin]['MarketName'] coins_pending_buy = [ market for market in self.pending_orders['Buying'] ] coins_pending_sell = [ market for market in self.pending_orders['Selling'] ] highest_price_history = utils.file_to_json( 'coin_highest_price_history.json') if market not in coins_pending_buy and market not in coins_pending_sell and market in self.held_coins: cur_price = price_data_seconds[-1] price_data_seconds = self.keltner_coins[market][ 'price_data_seconds'] deviation = self.get_deviation_of_last_x( 5, price_data_seconds) highest_coin_price = highest_price_history[market] if self.downward_cross( market, 'upper_band_data' ) or cur_price <= highest_coin_price - 2 * deviation: coin_to_sell = utils.get_second_market_coin(market) balance = self.api.get_balance(coin_to_sell) if balance['success']: amount = float(balance['result']['Available']) utils.sell(self.api, amount, market, self.bittrex_coins) else: utils.print_and_write_to_logfile( "Could not retrieve balance: " + balance['message'])
def low_high_sell_strat(self): for market in self.held_coins: cur_price = float(self.bittrex_coins[market]['Last']) bought_price = self.held_coins[market]['price_bought'] change = utils.percent_change(bought_price, cur_price) desired_gain = self.held_coins[market]['desired_gain'] high_bar = self.held_coins[market]['high_bar'] if change >= desired_gain and change >= high_bar: self.held_coins[market]['high_bar'] = high_bar + 10 utils.json_to_file(self.held_coins, 'held_coins.json') elif high_bar != desired_gain and change < high_bar - 10: coin_to_sell = utils.get_second_market_coin(market) balance = self.api.get_balance(coin_to_sell) if balance['success']: amount = float(balance['result']['Available']) self.update_bittrex_coins() utils.sell(self.api, amount, market, self.bittrex_coins) else: utils.print_and_write_to_logfile("Could not retrieve balance: " + balance['message'])
time.sleep(10) api = utils.get_api() time_until_cancel_processing_order_minutes = 5 satoshi_50k = 0.0005 ks = initialize_keltner_strat() ps = initialize_percent_strat() hs = initialize_hodl_strat() rs = initialize_reddit_strat() hl = initialize_buy_low_sell_high_strat() rands = initialize_random_strat() utils.print_and_write_to_logfile("\n** Beginning run at " + utils.get_date_time() + " **\n") # Main Driver while True: try: total_bitcoin = utils.get_total_bitcoin(api) # run_keltner_strat() # run_percent_strat() # run_hodl_strat() # run_buy_low_sell_high_strat() # run_random_strat() # orders_query = api.get_open_orders("")
def percent_buy_strat(self, total_bitcoin): symbol_1h_change_pairs = utils.get_coinmarketcap_1hr_change( self.coinmarketcap_coins) slots_open = self.total_slots - len(self.held_coins) - len( self.pending_orders['Buying']) - len( self.pending_orders['Selling']) if slots_open <= 0: utils.print_and_write_to_logfile("0 slots open") return bitcoin_to_use = float(total_bitcoin / slots_open * 0.990) if bitcoin_to_use < self.satoshi_50k: utils.print_and_write_to_logfile( "Order less than 50k satoshi (~$2). Attempted to use: $" + str(utils.bitcoin_to_USD(bitcoin_to_use)) + ", BTC: " + str(bitcoin_to_use)) return for hist_coin in self.history_coins: coin_price = float(self.bittrex_coins[hist_coin]['Last']) # update highest price recorded while held if hist_coin in self.held_coins: highest_recorded_price = float(self.history_coins[hist_coin]) if coin_price > highest_recorded_price: self.history_coins[hist_coin] = coin_price utils.json_to_file(self.history_coins, "coin_highest_price_history.json") ignored = utils.file_to_json("ignored_coins.json") # checking all bittrex coins to find the one for coin in self.bittrex_coins: if coin in ignored: continue percent_change_24h = utils.get_percent_change_24h( self.bittrex_coins[coin]) # if coin 24 increase between x and y if self.buy_min_percent <= percent_change_24h <= self.buy_max_percent: rank = utils.get_ranks(self.coinmarketcap_coins) coin_rank = rank[utils.get_second_market_coin(coin)] coin_volume = self.bittrex_coins[coin]['Volume'] # volume must be > 200 so we can sell when want if float( coin_rank ) > 40 and coin not in self.history_coins and coin_volume > 200: market = self.bittrex_coins[coin]['MarketName'] if market.startswith('ETH'): break if market.startswith('BTC'): coin_to_buy = utils.get_second_market_coin(market) coin_1h_change = float( symbol_1h_change_pairs[coin_to_buy]) coins_pending_buy = [ market for market in self.pending_orders['Buying'] ] coins_pending_sell = [ market for market in self.pending_orders['Selling'] ] if market not in self.held_coins and market not in coins_pending_buy and market not in \ coins_pending_sell and coin_1h_change > self.buy_desired_1h_change: coin_price = float( self.bittrex_coins[coin]['Last']) amount = bitcoin_to_use / coin_price if amount > 0: utils.buy(self.api, market, amount, coin_price, percent_change_24h, 0, coin_1h_change)
def percent_buy_strat(self, total_bitcoin): """ Searches all coins on bittrex and buys up to the variable "total_slots" different coins. Splits the amount of bitcoin use on each evenly. :param total_bitcoin: :return: """ symbol_1h_change_pairs = utils.get_coinmarketcap_1hr_change( self.coinmarketcap_coins) slots_open = self.total_slots - len(self.held_coins) - len( self.pending_orders['Buying']) - len( self.pending_orders['Selling']) if slots_open <= 0: utils.print_and_write_to_logfile("0 slots open") return bitcoin_to_use = float(total_bitcoin / slots_open * 0.990) if bitcoin_to_use < self.satoshi_50k: utils.print_and_write_to_logfile( "Order less than 50k satoshi (~$2). Attempted to use: $" + str(utils.bitcoin_to_USD(bitcoin_to_use)) + ", BTC: " + str(bitcoin_to_use)) return for hist_coin in self.history_coins: bitcoin_to_use = float(total_bitcoin / slots_open * 0.990) coin_price = float(self.bittrex_coins[hist_coin]['Last']) # update highest price recorded while held if hist_coin in self.held_coins: highest_recorded_price = float(self.history_coins[hist_coin]) if coin_price > highest_recorded_price: self.history_coins[hist_coin] = coin_price utils.json_to_file(self.history_coins, "coin_highest_price_history.json") # checking if the price of the sold coin is now greater then previously recorded high elif float(self.history_coins[hist_coin]) * 1.1 < coin_price: coins_pending_buy = [ market for market in self.pending_orders['Buying'] ] coins_pending_sell = [ market for market in self.pending_orders['Selling'] ] if hist_coin not in coins_pending_buy and hist_coin not in coins_pending_sell: amount = bitcoin_to_use / coin_price if amount > 0: coin_to_buy = utils.get_second_market_coin(hist_coin) coin_1h_change = float( symbol_1h_change_pairs[coin_to_buy]) percent_change_24h = utils.get_percent_change_24h( self.bittrex_coins[hist_coin]) utils.buy(self.api, hist_coin, amount, coin_price, percent_change_24h, 0, coin_1h_change) slots_open = self.total_slots - len( self.held_coins) - len( self.pending_orders['Buying']) - len( self.pending_orders['Selling']) bitcoin_to_use = float(total_bitcoin / slots_open * 0.990) # checking all bittrex coins to find the one for coin in self.bittrex_coins: percent_change_24h = utils.get_percent_change_24h( self.bittrex_coins[coin]) # if coin 24 increase between x and y if self.buy_min_percent <= percent_change_24h <= self.buy_max_percent: rank = utils.get_ranks(self.coinmarketcap_coins) coin_rank = rank[utils.get_second_market_coin(coin)] coin_volume = self.bittrex_coins[coin]['Volume'] # volume must be > 200 so we can sell when want if float( coin_rank ) > 50 and coin not in self.history_coins and coin_volume > 200: market = self.bittrex_coins[coin]['MarketName'] if market.startswith('ETH'): break if market.startswith('BTC'): coin_to_buy = utils.get_second_market_coin(market) coin_1h_change = float( symbol_1h_change_pairs[coin_to_buy]) coins_pending_buy = [ market for market in self.pending_orders['Buying'] ] coins_pending_sell = [ market for market in self.pending_orders['Selling'] ] if market not in self.held_coins and market not in coins_pending_buy and market not in \ coins_pending_sell and coin_1h_change > self.buy_desired_1h_change: coin_price = float( self.bittrex_coins[coin]['Last']) amount = bitcoin_to_use / coin_price if amount > 0: buy_request = utils.buy( self.api, market, amount, coin_price, percent_change_24h, 0, coin_1h_change) if buy_request['success']: utils.print_and_write_to_logfile( "Buy order of " + str(amount) + " " + market + " requested") self.refresh_held_pending() else: utils.print_and_write_to_logfile( buy_request['message'])