Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Coinbase Pro Limit Orders
import cbpro
import json
#Insert your API key, secret, and passphrase from Coinbase Pro
my_key = ""
my_secret = ""
my_passphrase = ""
auth_client = cbpro.AuthenticatedClient(my_key, my_secret, my_passphrase)
#Replace symbol with whatever currencypair you want to trade - list of currency pairs available on Notion
symbol = "BTC-USD"
#Replace buy size with amount you want to buy/sell and execution price with the price you want to set the limit order for
execution_price = "30000.00"
buy_size = 0.0001
#If you want to set a buy for a USD buy size of $5 for 90% (factor) of the spot price. You'll need to change the rounding factor for certain coins.
#Uncomment these lines for this functionality
#factor = 0.9
#rounding_factor = 2
#execution_price = str(round(float(auth_client.get_product_ticker(symbol)['price'])*factor,rounding_factor))
#print(f"Execution price: {execution_price}")
#usd_buy_size = 5
#buy_size = round(usd_buy_size/float(execution_price),8)
#print(f"Buy size: {buy_size}")
#Order = "sell" for a sell order and "buy for a buy order
order = "buy"
def _makeOrder(auth_client, buy_size):
exececute = auth_client.place_limit_order(symbol, order, execution_price, buy_size)
print(exececute)
return buy_size
def lambda_handler(event, context):
amount = _makeOrder(auth_client, buy_size)
return {
'statusCode': 200,
'body': json.dumps(f"{order.capitalize()} order executed: {amount} {symbol.split('-')[1]} of {symbol.split('-')[0]}!")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment