I have tried everything. When I coinbase assistance, they referred me to here.
I have selected all for when I generated my API key. I checked to make sure it was entered correctly. I even generated API keys several times. So, I know it is not that.
I can strangely access the accounts, but I cannot do a buy or sell order. Please someone help me. I have lost the last of my hair on my head, trying to figure this out.
ERROR MESSAGE - as you can see, I can access information.
Product details fetched
{âidâ: âACS-USDâ, âbase_currencyâ: âACSâ, âquote_currencyâ: âUSDâ, âquote_incrementâ: â0.0000001â, âbase_incrementâ: â1â, âdisplay_nameâ: âACS/USDâ, âmin_market_fundsâ: â1â, âmargin_enabledâ: False, âpost_onlyâ: False, âlimit_onlyâ: False, âcancel_onlyâ: False, âstatusâ: âonlineâ, âstatus_messageâ: ââ, âtrading_disabledâ: False, âfx_stablecoinâ: False, âmax_slippage_percentageâ: â0.03000000â, âauction_modeâ: False, âhigh_bid_limit_percentageâ: ââ}
{âaskâ: â0.0070707â, âbidâ: â0.0070625â, âvolumeâ: â75812538â, âtrade_idâ: 744681, âpriceâ: â0.0070659â, âsizeâ: â116761â, âtimeâ: â2023-04-06T07:41:05.255731Zâ}
Minimum order size for ACS-USD: 1.0
Minimum base size for the buy order: 141.52478806662987
Headers: {âAcceptâ: âapplication/jsonâ, âCB-ACCESS-KEYâ: ââ, âCB-ACCESS-SIGNâ: ââ, âCB-ACCESS-TIMESTAMPâ: ââ, âCB-ACCESS-PASSPHRASEâ: ââ}
Response status code: 401
Response text: {âmessageâ:âInvalid API Keyâ}
Buy order response: {âmessageâ: âInvalid API Keyâ}
Headers: {âAcceptâ: âapplication/jsonâ, âCB-ACCESS-KEYâ: ââ, âCB-ACCESS-SIGNâ: ââ, âCB-ACCESS-TIMESTAMPâ: ââ, âCB-ACCESS-PASSPHRASEâ: ââ}
Response status code: 401
Response text: {âmessageâ:âInvalid API Keyâ}
Sell order response: {âmessageâ: âInvalid API Keyâ}
PYTHON SCRIPT
import base64
import time
import hmac
import hashlib
import requests
import json
API_KEY = ââ
API_SECRET = ââ
API_PASSPHRASE = ââ
BASE_URL = âhttps://api.pro.coinbase.comâ
Add the create_signed_headers function here
def create_signed_headers(method, request_path, body):
timestamp = str(time.time())
signature = generate_signature(timestamp, request_path, method, body, API_SECRET)
headers = {
'Accept': 'application/json',
'CB-ACCESS-KEY': API_KEY,
'CB-ACCESS-SIGN': signature,
'CB-ACCESS-TIMESTAMP': timestamp,
'CB-ACCESS-PASSPHRASE': API_PASSPHRASE
}
return headers
def generate_signature(timestamp, request_path, method, body, secret_key):
message = str(timestamp) + method.upper() + request_path + body
hmac_key = base64.b64decode(secret_key)
signature = hmac.new(hmac_key, message.encode(âutf-8â), hashlib.sha256)
return base64.b64encode(signature.digest()).decode(âutf-8â)
def get_product_details(product_id):
url = fâhttps://api.pro.coinbase.com/products/{product_id}â
response = requests.get(url)
if response.status_code == 200:
return response.json()
else:
raise Exception(f"Failed to fetch product details: {response.text}")
def get_product_ticker(product_id):
url = fâhttps://api.pro.coinbase.com/products/{product_id}/tickerâ
response = requests.get(url)
if response.status_code == 200:
return response.json()
else:
raise Exception(f"Failed to fetch product ticker: {response.text}")
Test the get_product_details function
product_id = âACS-USDâ
product_details = get_product_details(product_id)
print(âProduct details fetchedâ)
print(product_details)
Test the get_product_ticker function
product_ticker = get_product_ticker(product_id)
print(product_ticker)
market_price = float(product_ticker[âpriceâ])
Get the minimum order size
min_order_size = float(product_details.get(âbase_min_sizeâ, 1))
print(f"Minimum order size for {product_id}: {min_order_size}")
Calculate the minimum base size for the buy order
min_base_size = min_order_size / market_price
print(f"Minimum base size for the buy order: {min_base_size}")
def create_order(side, product_id, base_size, limit_price):
method = âPOSTâ
request_path = â/api/v3/brokerage/ordersâ
body = json.dumps({
âsideâ: side,
âproduct_idâ: product_id,
âorder_configurationâ: {
âlimit_limit_gtcâ: {
âbase_sizeâ: base_size,
âlimit_priceâ: limit_price,
âpost_onlyâ: True
}
},
âclient_order_idâ: ââ
}) if side in [âBUYâ, âSELLâ] else None
headers = create_signed_headers(method, request_path, body)
print("Headers:", headers)
response = requests.post(BASE_URL + request_path, headers=headers, data=body)
print("Response status code:", response.status_code)
print("Response text:", response.text)
return response
def buy_order(product_id, base_size, limit_price):
return create_order(âBUYâ, product_id, base_size, limit_price)
def sell_order(product_id, base_size, limit_price):
return create_order(âSELLâ, product_id, base_size, limit_price)
sell_base_size = max(min_base_size * 1.1, 500) # Adjust the multiplier (1.1) as needed
Example usage
product_id = âACS-USDâ
base_size = â500â
limit_price = â0.0078â
buy_response = buy_order(product_id, base_size, limit_price)
print(f"Buy order response: {buy_response.json()}") # Add this line
sell_response = sell_order(product_id, base_size, limit_price)
print(f"Sell order response: {sell_response.json()}") # Add this line