how can I keep running into the same issue?
No markets to trade.
MEDIA balance: 0.0005830584695009
ETC balance: 0.0000000009560910
ACH balance: 0.0469744170654206
FET balance: 0.0459576695878804
SOL balance: 0.0009579198030976
USD balance: 132.7270413141750500
Sleeping for 10790 seconds until the next trading time 01:00:00.
Available products: [{'product_id': 'SOL-USD', 'price': '20.23', 'price_percentage_change_24h': '-0.83333333333333', 'volume_24h': '366269.186', 'volume_percentage_change_24h': '-47.84592958198064', 'base_increment': '0.001', 'quote_increment': '0.01', 'quote_min_size': '1', 'quote_max_size': '25000000', 'base_min_size': '0.08', 'base_max_size': '780000', 'base_name': 'Solana', 'quote_name': 'US Dollar', 'watched': False, 'is_disabled': False, 'new': False, 'status': 'online', 'cancel_only': False, 'limit_only': False, 'post_only': False, 'trading_disabled': False, 'auction_mode': False, 'product_type': 'SPOT', 'quote_currency_id': 'USD', 'base_currency_id': 'SOL', 'fcm_trading_session_details': None, 'mid_market_price': '', 'alias': '', 'alias_to': [], 'base_display_symbol': 'SOL', 'quote_display_symbol': 'USD', 'view_only': False, 'price_increment': '0.01'}, {'product_id': 'FET-USD', 'price': '0.2571', 'price_percentage_change_24h': '-3.09084055785903', 'volume_24h': '14940275.1', 'volume_percentage_change_24h': '41.38840115205009', 'base_increment': '0.1', 'quote_increment': '0.0001', 'quote_min_size': '1', 'quote_max_size': '10000000', 'base_min_size': '2', 'base_max_size': '760000', 'base_name': 'Fetch.ai', 'quote_name': 'US Dollar', 'watched': False, 'is_disabled': False, 'new': False, 'status': 'online', 'cancel_only': False, 'limit_only': False, 'post_only': False, 'trading_disabled': False, 'auction_mode': False, 'product_type': 'SPOT', 'quote_currency_id': 'USD', 'base_currency_id': 'FET', 'fcm_trading_session_details': None, 'mid_market_price': '', 'alias': '', 'alias_to': [], 'base_display_symbol': 'FET', 'quote_display_symbol': 'USD', 'view_only': False, 'price_increment': '0.0001'}, {'product_id': 'ACH-USD', 'price': '0.026161', 'price_percentage_change_24h': '-0.95032560957141', 'volume_24h': '48232352.5', 'volume_percentage_change_24h': '-37.48951926433623', 'base_increment': '0.1', 'quote_increment': '0.000001', 'quote_min_size': '1', 'quote_max_size': '10000000', 'base_min_size': '110', 'base_max_size': '20000000', 'base_name': 'Alchemy Pay', 'quote_name': 'US Dollar', 'watched': False, 'is_disabled': False, 'new': False, 'status': 'online', 'cancel_only': False, 'limit_only': False, 'post_only': False, 'trading_disabled': False, 'auction_mode': False, 'product_type': 'SPOT', 'quote_currency_id': 'USD', 'base_currency_id': 'ACH', 'fcm_trading_session_details': None, 'mid_market_price': '', 'alias': '', 'alias_to': [], 'base_display_symbol': 'ACH', 'quote_display_symbol': 'USD', 'view_only': False, 'price_increment': '0.000001'}, {'product_id': 'ETC-USD', 'price': '18.35', 'price_percentage_change_24h': '0.21845985800109', 'volume_24h': '17581.93832104', 'volume_percentage_change_24h': '-59.81754439088938', 'base_increment': '0.00000001', 'quote_increment': '0.01', 'quote_min_size': '1', 'quote_max_size': '10000000', 'base_min_size': '0.018', 'base_max_size': '44000', 'base_name': 'Ethereum Classic', 'quote_name': 'US Dollar', 'watched': False, 'is_disabled': False, 'new': False, 'status': 'online', 'cancel_only': False, 'limit_only': False, 'post_only': False, 'trading_disabled': False, 'auction_mode': False, 'product_type': 'SPOT', 'quote_currency_id': 'USD', 'base_currency_id': 'ETC', 'fcm_trading_session_details': None, 'mid_market_price': '', 'alias': '', 'alias_to': [], 'base_display_symbol': 'ETC', 'quote_display_symbol': 'USD', 'view_only': False, 'price_increment': '0.01'}, {'product_id': 'MEDIA-USD', 'price': '9.19', 'price_percentage_change_24h': '2.45261984392419', 'volume_24h': '2498.688', 'volume_percentage_change_24h': '-23.06618458231176', 'base_increment': '0.001', 'quote_increment': '0.01', 'quote_min_size': '1', 'quote_max_size': '10000000', 'base_min_size': '0.0385694847882526', 'base_max_size': '385694.8478825260614222', 'base_name': 'Media Network', 'quote_name': 'US Dollar', 'watched': False, 'is_disabled': False, 'new': False, 'status': 'online', 'cancel_only': False, 'limit_only': False, 'post_only': False, 'trading_disabled': False, 'auction_mode': False, 'product_type': 'SPOT', 'quote_currency_id': 'USD', 'base_currency_id': 'MEDIA', 'fcm_trading_session_details': None, 'mid_market_price': '', 'alias': '', 'alias_to': [], 'base_display_symbol': 'MEDIA', 'quote_display_symbol': 'USD', 'view_only': False, 'price_increment': '0.01'}]
Filtered markets: []
No markets to trade.
MEDIA balance: 0.0005830584695009
ETC balance: 0.0000000009560910
ACH balance: 0.0469744170654206
FET balance: 0.0459576695878804
SOL balance: 0.0009579198030976
USD balance: 132.7270413141750500
Sleeping for 14390 seconds until the next trading time 05:00:00.
#crypto_trading_bot.py
import http.client
import json
import time
import datetime
import logging
import pytz
from auth import Auth
from trading_indicators import trading_algo, get_available_products
# Configure logging
logging.basicConfig(filename='tradebot.log', level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
markets = ['MEDIA-USD', 'ETC-USD', 'ACH-USD', 'FET-USD', 'SOL-USD']
crypto_currencies = [market.split("-")[0] for market in markets]
budget = 300
def send_request(method, path, payload, params='', max_retries=3, retry_delay=2):
retry_count = 0
while retry_count < max_retries:
try:
auth = Auth()
headers = auth(method, path, payload) # Pass the parameters separately
conn = http.client.HTTPSConnection('api.coinbase.com')
conn.request(method, path + params, payload, headers)
response = conn.getresponse()
if response.status == 200:
return json.loads(response.read().decode('utf-8'))
else:
raise Exception(f"Failed request. Status: {response.status}, Headers: {response.getheaders()}, Body: {response.read()}")
except Exception as e:
print(e)
retry_count += 1
time.sleep(retry_delay)
raise Exception(f"Failed to send request after {max_retries} attempts.")
def list_accounts(currency=None, uuid=None, balance=None):
method = 'GET'
path = '/api/v3/brokerage/accounts'
payload = ''
data = send_request(method, path, payload, params='?limit=250')
if currency is not None and "accounts" in data:
for i in data["accounts"]:
if i["currency"] == currency:
uuid = i["uuid"]
balance = i["available_balance"]["value"]
if currency != "USD":
print(f"{currency} balance: {balance}")
return uuid, balance
elif "accounts" not in data:
print("Error: Data not found in the response")
return None, None
else:
return data
def trade_bot():
global markets
trading_times = ["01:00:00", "05:00:00", "08:00:00", "10:00:00", "13:00:00", "15:00:00", "17:00:00", "20:00:00", "22:00:00"]
while True:
now = datetime.datetime.now(pytz.timezone('Asia/Shanghai'))
current_time = now.time()
trading_times_dt = [datetime.datetime.strptime(t, "%H:%M:%S").time() for t in trading_times]
closest_time, sleep_seconds = min(((t, (datetime.datetime.combine(datetime.date.min, t) - datetime.datetime.combine(datetime.date.min, current_time)).seconds % 86400) for t in trading_times_dt), key=lambda x: x[1])
# Print account balances before sleeping
for currency in crypto_currencies:
_, balance = list_accounts(currency=currency)
# Retrieve USD balance
_, usd_balance = list_accounts(currency="USD")
print(f"USD balance: {usd_balance}")
# Calculate trade_amount based on the current USD balance and the number of markets
usd_balance = float(usd_balance)
print(f"Sleeping for {sleep_seconds} seconds until the next trading time {closest_time.strftime('%H:%M:%S')}.")
time.sleep(sleep_seconds)
available_products = get_available_products(markets) # Pass 'markets' parameter here
print("Available products:", available_products)
# Filter the markets based on available products
filtered_markets = list(filter(lambda x: x in available_products, markets))
print("Filtered markets:", filtered_markets)
if filtered_markets: # This checks if the list is not empty
trade_amount = round(usd_balance / len(filtered_markets), 2)
else:
print("No markets to trade.")
trade_amount = 0
for market in filtered_markets:
currency = market.split("-")[0]
trade_decision = trading_algo(market)
print(f"Trade decision for {market}: {trade_decision}")
_, balance = list_accounts(currency=currency)
if trade_decision['buy']:
print(f"Buying {market}")
try:
# For buy orders
buy_order = send_request(
'POST',
'/api/v3/brokerage/orders',
json.dumps({
"product_id": market,
"side": 'buy',
"order_configuration": {
"market_market_ioc": {
"quote_size": str(trade_amount)
}
}
}))
except Exception as e:
logging.error(f"Error placing buy order for {market}: {e}")
elif trade_decision['sell'] and balance is not None:
print(f"Selling {market}")
try:
# For sell orders
sell_order = send_request(
'POST',
'/api/v3/brokerage/orders',
json.dumps({
"product_id": market,
"side": 'sell',
"order_configuration": {
"market_market_ioc": {
"base_size": balance # Use the available balance for the sell order
}
}
}))
except Exception as e:
logging.error(f"Error placing sell order for {market}: {e}")
#time.sleep(60)
if __name__ == "__main__":
trade_bot()
#trading_indicator.py
import pandas as pd
import numpy as np
import talib
from auth import Auth
import http.client
import json
import time
def send_request(method, path, payload, params='', max_retries=3, retry_delay=2):
auth = Auth()
headers = auth(method, path, payload) # Pass the parameters separately
conn = http.client.HTTPSConnection('api.coinbase.com')
retry_count = 0
while retry_count < max_retries:
try:
conn.request(method, path + params, payload, headers)
response = conn.getresponse()
if response.status == 429: # Rate limit exceeded
time.sleep(int(response.getheader("Retry-After", retry_delay)))
retry_count += 1
continue
elif response.status != 200: # Other non-successful status codes
print(f"Failed request. Status: {response.status}, Headers: {response.getheaders()}, Body: {response.read()}")
retry_count += 1
time.sleep(retry_delay)
continue
data = json.loads(response.read())
return data
except Exception as e:
print(f"Request failed with error: {e}")
retry_count += 1
time.sleep(retry_delay)
raise Exception(f"Failed to send request after {max_retries} attempts.")
def get_available_products(markets):
method = 'GET'
path = '/api/v3/brokerage/products'
payload = ''
data = send_request(method, path, payload)
if 'errors' in data:
print("Error:", data['errors'][0]['message'])
return []
filtered_products = [product for product in data['products'] if product['product_id'] in markets]
return filtered_products
def get_historical_data(product_id, granularity=300):
method = 'GET'
path = f'/products/{product_id}/candles'
payload = ''
data = send_request(method, path, payload, params=f'?granularity={granularity}')
print("Raw data:", data)
df = pd.DataFrame(data, columns=['time', 'low', 'high', 'open', 'close', 'volume'])
print("DataFrame:", df)
return df
def compute_sma(data, window):
sma = talib.SMA(data.close, timeperiod = window)
return sma
def compute_ema(data, window):
ema = talib.EMA(data.close, timeperiod = window)
return ema
def compute_macd(data, short_window, long_window):
macd, signal, hist = talib.MACD(data.close, fastperiod=short_window, slowperiod=long_window, signalperiod=9)
return macd, signal, hist
def compute_rsi(data, window):
rsi = talib.RSI(data.close, timeperiod = window)
return rsi
def compute_stochastic(data, window):
slowk, slowd = talib.STOCH(data.high, data.low, data.close, fastk_period=14, slowk_period=window, slowk_matype=0, slowd_period=3, slowd_matype=0)
return slowk, slowd
def compute_bollinger(data, window):
upper, middle, lower = talib.BBANDS(data.close, timeperiod = window, nbdevup = 2, nbdevdn = 2, matype = 0)
return upper, middle, lower
def trading_algo(data):
rsi_threshold = 45
sma_short_period = 5
sma_long_period = 20
sma_tolerance = 0.01
rsi = compute_rsi(data, period=14)
sma_short = compute_sma(data, window=sma_short_period)
sma_long = compute_sma(data, window=sma_long_period)
if rsi[-1] < rsi_threshold and \
(1 - sma_tolerance) * sma_short[-1] < data['close'].iloc[-1] < (1 + sma_tolerance) * sma_short[-1] and \
(1 - sma_tolerance) * sma_long[-1] < data['close'].iloc[-1] < (1 + sma_tolerance) * sma_long[-1]:
return True # Here, instead of placing a buy order, I'm returning True. Adapt this line to suit your actual trading environment.
else:
return False
if __name__ == "__main__":
markets = ['MEDIA-USD', 'ETC-USD', 'ACH-USD', 'FET-USD', 'SOL-USD']
available_products = get_available_products(markets)
for product in available_products:
product_id = product['product_id']
print(f"Running trading algorithm for {product_id}")
data = get_historical_data(product_id) # Add this line
buy_signal = trading_algo(data) # Adjust this line
print(f"Buy signal: {buy_signal}\n") # Adjust this line