Exemple #1
0
    def buy(self, ticker):
        if self.available_cash == 0 or self.available_cash < config[
                'buy_amount_per_trade'] or self.is_trading_locked:
            return False

        # Retrieve the actual ask price from Robinhood
        if not config['simulate_api_calls']:
            try:
                quote = rh.get_crypto_quote(ticker)
                price = float(quote['ask_price'])
            except:
                print(
                    'Could not retrieve ask price from Robinhood. Using Kraken\'s.'
                )
                price = self.data.iloc[-1][ticker]
        else:
            price = self.data.iloc[-1][ticker]

        # Values need to be specified to no more precision than listed in min_price_increments.
        # Truncate to 7 decimal places to avoid floating point problems way out at the precision limit
        price_precision = round(
            floor(price / self.min_price_increments[ticker]) *
            self.min_price_increments[ticker], 7)

        # How much to buy depends on the configuration
        quantity = (self.available_cash if
                    (config['buy_amount_per_trade'] == 0) else
                    config['buy_amount_per_trade']) / price_precision
        quantity = round(
            floor(quantity / self.min_share_increments[ticker]) *
            self.min_share_increments[ticker], 7)

        if config['trades_enabled'] and not config['simulate_api_calls']:
            try:
                buy_info = rh.order_buy_crypto_limit(str(ticker), quantity,
                                                     price_precision)

                # Add this new asset to our orders
                self.orders[buy_info['id']] = asset(ticker, quantity,
                                                    price_precision,
                                                    buy_info['id'], 'PB')

                print('## Submitted order to buy ' + str(quantity) + ' ' +
                      str(ticker) + ' at $' + str(price_precision))

                if (price != self.data.iloc[-1][ticker]):
                    print('## Price Difference: Kraken $' +
                          str(self.data.iloc[-1][ticker]) + ', Robinhood $ ' +
                          str(price))
            except:
                print('An exception occurred while trying to buy.')
                return False
        else:
            print('## Would have bought ' + str(ticker) + ' ' + str(quantity) +
                  ' at $' + str(price_precision) + ', if trades were enabled')
            return False

        return True
Exemple #2
0
    def buy(self, ticker):
        if (self.available_cash == 0
                or self.available_cash < config['buy_amount_per_trade']
                or self.is_trading_locked):
            return False

        # Values need to be specified to no more precision than listed in min_price_increments.
        # Truncate to 7 decimal places to avoid floating point problems way out at the precision limit
        price = round(
            floor(self.data.iloc[-1][ticker] /
                  self.min_price_increments[ticker]) *
            self.min_price_increments[ticker], 7)

        # How much to buy depends on the configuration
        quantity = (self.available_cash if
                    (config['buy_amount_per_trade']
                     == 0) else config['buy_amount_per_trade']) / price
        quantity = round(
            floor(quantity / self.min_share_increments[ticker]) *
            self.min_share_increments[ticker], 7)

        if (config['trades_enabled'] and not config['simulate_api_calls']):
            try:
                buy_info = rh.order_buy_crypto_limit(str(ticker), quantity,
                                                     price)

                # Add this new asset to our orders
                self.orders[buy_info['id']] = asset(ticker, quantity, price,
                                                    buy_info['id'])

                # Update the available cash for trading
                self.available_cash = round(
                    self.available_cash - quantity * price, 3)

                print('## Bought ' + str(ticker) + ' ' + str(quantity) +
                      ' at $' + str(price))
            except:
                print('An exception occurred while trying to buy.')
                return False
        else:
            print('## Would have bought ' + str(ticker) + ' ' + str(quantity) +
                  ' at $' + str(price) + ', if trades were enabled')
            return False

        return True
orders = {}
data = pd.DataFrame()

# Load assets and data
if path.exists( 'pickle/orders.pickle' ):
    with open( 'pickle/orders.pickle', 'rb' ) as f:
        orders = pickle.load( f )

if path.exists( 'pickle/dataframe.pickle' ):
    data = pd.read_pickle( 'pickle/dataframe.pickle' )

if len( sys.argv ) > 1:
    if sys.argv[ 1 ] == 'buy':
        try:
            orders[ str( len( orders ) ) ] = asset( sys.argv[ 2 ], sys.argv[ 3 ], sys.argv[ 4 ], str( len( orders ) ) )
        except:
            print( 'Syntax: manage-asset.py buy ticker quantity price' )
            exit()

    elif sys.argv[ 1 ] == 'sell':
        try:
            orders[ sys.argv[ 2 ] ].status = 'S'
        except:
            print( 'Error: asset not found' )
            exit()

        try:
            if float( sys.argv[ 3 ] ) > 0:
                orders[ sys.argv[ 2 ] ].profit = round( (  orders[ sys.argv[ 2 ] ].quantity * float( sys.argv[ 3 ] ) ) - (  orders[ sys.argv[ 2 ] ].quantity *  orders[ sys.argv[ 2 ] ].price ), 3 )
            else: