Пример #1
0
def sell(api_url, auth, dataframe, cost_basis, BTC_BALANCE, FEE_PERCENT):
    print("Attempting To Sell BTC")
    curr_bid = float(dataframe['Bid Price'].iloc[-1])
    max_order_size = min(float(BTC_BALANCE), 10000)
    effective_order_size = formatting.truncate(
        max_order_size / (1 + FEE_PERCENT), 8)
    order_details = {
        'type': 'limit',
        'side': 'sell',
        'product_id': 'BTC-USD',
        'price': curr_bid,  # order limit is current ask price for fast fill 
        'size':
        effective_order_size  # max trade size accounting for fee % and maximum size precision
    }
    print("Attempting to sell %f BTC at $%f per BTC" %
          (effective_order_size, curr_bid))
    order = requests.post(api_url + 'orders', json=order_details, auth=auth)
    order_id = order.json().get('id')
    if order.status_code != 200:
        print("Sell order was rejected: %s\n" % order.json().get('message'))
        write_files.recordError('trade_errors.csv', 'SELL', order.status_code,
                                order.json().get('message'))
        return True, cost_basis, FEE_PERCENT
    time.sleep(1)  # allow time for order to fill
    order = requests.get(api_url + 'orders/' + order_id, auth=auth)
    if order.status_code == 200 and order.json().get('status') == 'done':
        executed_value = float(order.json().get('executed_value'))
        fill_size = float(order.json().get('size'))
        fill_price = executed_value / fill_size
        fill_fee = float(order.json().get('fill_fees'))
        final_cost_basis = ((cost_basis * fill_size) + fill_fee) / fill_size
        profit = (fill_price - final_cost_basis) * fill_size
        print("SELL SUCCEEDED")
        print("Sold %f BTC at $%f per BTC for a fee of $%f\n" %
              (fill_size, fill_price, fill_fee))
        write_files.recordActivity('trade_activity.csv', 'SELL', fill_price,
                                   fill_size, executed_value, fill_fee,
                                   final_cost_basis, profit)
        FEE_PERCENT = account.updateFeePercent('trade_activity.csv')
        return False, cost_basis, FEE_PERCENT
    else:
        delete = requests.delete(api_url + 'orders', auth=auth)
        print("Sell order was canceled\n")
        write_files.recordError('trade_errors.csv', 'BUY', order.status_code,
                                'CANCELED')
        return True, cost_basis, FEE_PERCENT
Пример #2
0
def bot():
    print("==== BEGINNING TRADING ====")
    print("===========================")
    auth = authentication.CoinbaseAuth(API_KEY, API_SECRET, API_PASS)

    BTC_data = deque(maxlen=200)

    FEE_PERCENT = account.updateFeePercent('trade_activity.csv')
    print("Current Trading Fee Percent: ", FEE_PERCENT)

    cost_basis = account.initializeCostBasis('trade_activity.csv')
    CASH_ACCOUNT, CASH_BALANCE, BTC_ACCOUNT, BTC_BALANCE = account.initializeAccountInfo(
        api_url, auth)
    print("Current USD Balance: ", CASH_BALANCE)
    print("Current BTC Balance: ", BTC_BALANCE, "\n")

    curr_data = requests.get(api_url + 'products/BTC-USD/book',
                             auth=auth).json()
    curr_bid = float(curr_data.get('bids')[0][0])

    long_flag = True if BTC_BALANCE * curr_bid > CASH_BALANCE else False

    while (True):
        dataframe = market.getMarketData(api_url, auth, BTC_data)

        curr_bid = float(dataframe['Bid Price'].iloc[-1])
        new_cost_basis = ((cost_basis * BTC_BALANCE) +
                          (curr_bid * BTC_BALANCE * FEE_PERCENT)) / BTC_BALANCE

        if long_flag == False and dataframe['MACD'].iloc[-1] > dataframe[
                'Signal'].iloc[-1]:
            long_flag, cost_basis, FEE_PERCENT = trade.buy(
                api_url, auth, dataframe, FEE_PERCENT, CASH_BALANCE)
            account.printUpdatedInfo(api_url, auth, FEE_PERCENT)
        elif long_flag == True and dataframe['MACD'].iloc[-1] < dataframe[
                'Signal'].iloc[-1] and curr_bid > new_cost_basis:
            long_flag, cost_basis, FEE_PERCENT = trade.sell(
                api_url, auth, dataframe, cost_basis, BTC_BALANCE, FEE_PERCENT)
            account.printUpdatedInfo(api_url, auth, FEE_PERCENT)

        time.sleep(1)
        print(BTC_data)
        print(dataframe)
Пример #3
0
def test_updateFeePercent2():
    assert account.updateFeePercent('./bot/mocks/trade_activity_mock2.csv') == 0.002