def check_price_for_sell(bot_config, buy_price, current_price): current_price = float(current_price) buy_price = float(buy_price) if current_price > buy_price: price_diff = current_price - buy_price price_diff_percent = price_diff / buy_price * 100 message = "SELL_CHECK|current_price: {}, price_dif_percentage: {}" custom_logger.write_log( message.format(current_price, price_diff_percent)) if price_diff_percent > float( bot_config['sell_price_diff_percentage']): return current_price else: return None else: return None
def check_price_for_buy(buy_price, bot_config, db_price_data_obj): price_range_days = int(bot_config['average_price_range_days']) current_price = float(buy_price) starting_timestamp = int(time.time()) - (price_range_days * 86400) minimum_data_points = int(bot_config['minimum_data_points_per_minute'] ) * 60 * 24 * price_range_days avg_price_data = db_price_data_obj.get_average_price_in_range( starting_timestamp) if avg_price_data['count'] < minimum_data_points: custom_logger.write_log('Not enough price data, count: ' + str(avg_price_data['count'])) return None avg_price = float(avg_price_data['avg_price']) if current_price < avg_price: message = "BUY_CHECK|avg_price: {}, current_price: {}" custom_logger.write_log(message.format(avg_price, current_price)) return {'current_price': current_price} else: return None
def sell_coin(symbol, quantity, price): url = api_base_url + '/api/v3/order' api_key = config_parser.get('exchange_settings', 'api_key') secret_key = config_parser.get('exchange_settings', 'secret_key') secret_key = bytes(secret_key, 'utf-8') payload = { 'symbol': symbol, 'side': 'SELL', 'type': 'LIMIT', 'quantity': quantity, 'price': price, 'timeInForce': 'GTC', 'timestamp': int(time.time() * 1000) } total_params = urlencode(payload) total_params = bytes(total_params, 'utf-8') signature = hmac.new(secret_key, total_params, hashlib.sha256).hexdigest() payload['signature'] = signature headers = {'X-MBX-APIKEY': api_key} custom_logger.write_log(json.dumps(payload)) resp = requests.post(url, data=payload, headers=headers) data = resp.json() return data
from unicorn_binance_websocket_api.unicorn_binance_websocket_api_manager import BinanceWebSocketApiManager import database import json import custom_logger import time db_bot_config_obj = database.BotConfig() db_price_data_obj = database.PriceData() bot_config = db_bot_config_obj.get_bot_configs() coin_pair_symbol = bot_config['base_coin'] + bot_config['quote_coin'] binance_websocket_api_manager = BinanceWebSocketApiManager(exchange="binance.com") binance_websocket_api_manager.create_stream(['ticker'], [coin_pair_symbol]) while True: oldest_stream_data_from_stream_buffer = binance_websocket_api_manager.pop_stream_data_from_stream_buffer() if oldest_stream_data_from_stream_buffer: stream_data = json.loads(oldest_stream_data_from_stream_buffer) if stream_data.get('data') is not None: price = stream_data['data']['c'] low = stream_data['data']['l'] high = stream_data['data']['h'] db_price_data_obj.insert_price_data(price, low, high) else: custom_logger.write_log(oldest_stream_data_from_stream_buffer) else: time.sleep(0.2)
# SELL logic if action == 'SELL': last_buy_transaction = db_transaction_obj.get_last_buy_transaction( ) buy_price = float(last_buy_transaction['fill_price']) price_for_sell = db_price_data_obj.get_price_for_sell() if (int(time.time()) - price_for_sell['last_updated']) > 2: sell_price = price_for_sell['price'] current_price = calculator.check_price_for_sell( bot_config, buy_price, sell_price) quantity = round(float(last_buy_transaction['fill_quantity']), int(bot_config['coin_quantity_precision'])) if current_price is not None: # Log message = "Place sell order of {} coins at rate {}" custom_logger.write_log( message.format(quantity, current_price)) # Place sell order result = binance_apis.sell_coin(coin_pair_symbol, quantity, current_price) custom_logger.write_log(json.dumps(result)) # make entry in database db_transaction_obj.insert_sell_transaction( result, last_buy_transaction['id']) # Determine next operation action = 'BUY' if result[ 'status'] == 'FILLED' else 'CHECK_SELL_STATUS' else: custom_logger.write_log('Price data not up to date') time.sleep(SLEEP_TIME) # BUY logic