def test_4_returns_latest_buy_signal(self): buydf = DataFrame([{'buy': 1, 'date': arrow.utcnow()}]) with patch('analyze.analyze_ticker', return_value=buydf): self.assertEqual(get_buy_signal('BTC-ETH'), True) buydf = DataFrame([{'buy': 0, 'date': arrow.utcnow()}]) with patch('analyze.analyze_ticker', return_value=buydf): self.assertEqual(get_buy_signal('BTC-ETH'), False)
def create_trade(stake_amount: float, _exchange: exchange.Exchange) -> Optional[Trade]: """ Checks the implemented trading indicator(s) for a randomly picked pair, if one pair triggers the buy_signal a new trade record gets created :param stake_amount: amount of btc to spend :param _exchange: exchange to use """ logger.info('Creating new trade with stake_amount: %f ...', stake_amount) whitelist = _CONF[_exchange.name.lower()]['pair_whitelist'] # Check if btc_amount is fulfilled if exchange.get_balance(_CONF['stake_currency']) < stake_amount: raise ValueError( 'stake amount is not fulfilled (currency={}'.format(_CONF['stake_currency']) ) # Remove currently opened and latest pairs from whitelist trades = Trade.query.filter(Trade.is_open.is_(True)).all() latest_trade = Trade.query.filter(Trade.is_open.is_(False)).order_by(Trade.id.desc()).first() if latest_trade: trades.append(latest_trade) for trade in trades: if trade.pair in whitelist: whitelist.remove(trade.pair) logger.debug('Ignoring %s in pair whitelist', trade.pair) if not whitelist: raise ValueError('No pair in whitelist') # Pick pair based on StochRSI buy signals for _pair in whitelist: if get_buy_signal(_pair): pair = _pair break else: return None open_rate = exchange.get_ticker(pair)['ask'] amount = stake_amount / open_rate order_id = exchange.buy(pair, open_rate, amount) # Create trade entity and return message = '*{}:* Buying [{}]({}) at rate `{:f}`'.format( _exchange.name, pair.replace('_', '/'), exchange.get_pair_detail_url(pair), open_rate ) logger.info(message) telegram.send_msg(message) return Trade(pair=pair, btc_amount=stake_amount, open_rate=open_rate, open_date=datetime.utcnow(), amount=amount, exchange=_exchange, open_order_id=order_id, is_open=True)
def create_trade(stake_amount: float, exchange): """ Creates a new trade record with a random pair :param stake_amount: amount of btc to spend :param exchange: exchange to use """ logger.info('Creating new trade with stake_amount: %f ...', stake_amount) whitelist = CONFIG[exchange.name.lower()]['pair_whitelist'] # Check if btc_amount is fulfilled if api_wrapper.get_balance(CONFIG['stake_currency']) < stake_amount: raise ValueError('stake amount is not fulfilled (currency={}'.format( CONFIG['stake_currency'])) # Remove currently opened and latest pairs from whitelist trades = Trade.query.filter(Trade.is_open.is_(True)).all() latest_trade = Trade.query.filter(Trade.is_open.is_(False)).order_by( Trade.id.desc()).first() if latest_trade: trades.append(latest_trade) for trade in trades: if trade.pair in whitelist: whitelist.remove(trade.pair) logger.debug('Ignoring %s in pair whitelist', trade.pair) if not whitelist: raise ValueError('No pair in whitelist') # Pick pair based on StochRSI buy signals for p in whitelist: if get_buy_signal(p): pair = p break else: raise ValueError('No buy signal from pairs: {}'.format( ', '.join(whitelist))) open_rate = api_wrapper.get_ticker(pair)['ask'] amount = stake_amount / open_rate exchange = exchange order_id = api_wrapper.buy(pair, open_rate, amount) # Create trade entity and return message = '*{}:* Buying [{}]({}) at rate `{:f}`'.format( exchange.name, pair.replace('_', '/'), api_wrapper.get_pair_detail_url(pair), open_rate) logger.info(message) TelegramHandler.send_msg(message) return Trade(pair=pair, btc_amount=stake_amount, open_rate=open_rate, amount=amount, exchange=exchange, open_order_id=order_id)