# Coinbase Pro DCA Python Code - CBP定投代码 **Published by:** [Kenny (Kang Zeng)](https://paragraph.com/@kenny-kang-zeng/) **Published on:** 2021-11-12 **URL:** https://paragraph.com/@kenny-kang-zeng/coinbase-pro-dca-python-code-cbp ## Content Install pythonInstall cbpro library by: pip install cbproIn Coinbase Pro, create your trading API, replace the key, b64secret and passphrase in the codeIn Coinbase Pro, connect your bank account for USD fund depositUncomment this line: #print(auth_client.get_accounts()) , temporarily delete all code after this line, run the code to retrieve the account string for your bank account, replace the ‘bbbbbbb’ string in the code with the account stringRun the code with command: python coinbase-dca.py 100 DOTNow your Coinbase Pro account should automatically bought 100 USD of DOT token with market price. Have fun!import cbpro import sys import time key = 'kkkkkkkkkkkkkkkkkkkkkkkkkk' b64secret = 'ssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssss' passphrase = 'pppppppppp' amount = sys.argv[1] coin = sys.argv[2] usd_amount = int(amount) print('Buy ' + coin + ' with $' + amount + ' ...') auth_client = cbpro.AuthenticatedClient(key, b64secret, passphrase) public_client = cbpro.PublicClient() coin_ticker = coin + '-USD' coin_price=round(float(public_client.get_product_ticker(product_id=coin_ticker)['price']),2) print('Current ' + coin + ' Price is: $' + str(coin_price)) ## Display all accounts ##print(auth_client.get_accounts()) ## Default is BTC coin_string = '43b6105b-731f-4659-9a67-4afb9d0befd6' if coin == 'UNI': coin_string='b308ab2f-8108-418e-b7ab-ed200bc9b7bb' if coin == 'LINK': coin_string='00845604-e10d-4158-8c9d-2536b98ac7ea' if coin == 'ANKR': coin_string='79635096-5ab7-4d03-ac0a-9cfc8ce9fad6' if coin == 'MATIC': coin_string='78066072-9e61-4e90-b77f-b8660bf87ac3' if coin == 'STORJ': coin_string=' b0cadd56-4b53-46d5-8b1a-af148d5ee67f' if coin == 'BAL': coin_string='62155e5a-a664-42bf-8bf8-dc5b10de5d49' if coin == 'MANA': coin_string='44e8585c-0708-42d3-9126-b1061bac1919' if coin == 'ALGO': coin_string='1e3131ff-c290-49fb-a349-3a64ee3f6003' if coin == 'DOGE': coin_string='931a38f0-beb1-4aca-b48a-9ef1f1342033' if coin == 'ADA': coin_string='aeeae914-85b3-46cd-a0af-b2cff05b27d4' if coin == 'ADA': coin_string='aeeae914-85b3-46cd-a0af-b2cff05b27d4' if coin == 'ICP': coin_string='35d0aa3f-c702-432b-a27b-0897c93099ae' if coin == 'FIL': coin_string='1588ca41-3e14-40d0-98f5-b2a282f83757' if coin == 'AAVE': coin_string='3370b1db-9807-4487-a3fa-d3fb8f5d4081' if coin == 'ALGO': coin_string='1e3131ff-c290-49fb-a349-3a64ee3f6003' if coin == 'SNX': coin_string='5ce13adb-bd33-4688-a4c7-ee067d94d3b8' if coin == 'FET': coin_string='e0a6572b-1c5d-4008-b294-ed018df8a43c' if coin == 'GRT': coin_string='6d020f78-5505-4f5d-b218-cc0a5633201b' if coin == 'CRV': coin_string='4a87fa2a-95a7-481c-9564-f3603fbe19da' if coin == 'ATOM': coin_string='91bbb9a4-c030-404c-aacb-a4a4c8f4d890' if coin == 'AXS': coin_string='b6d00425-20e2-426a-96b1-beab13e81bcd' if coin == 'DOT': coin_string='4a47096f-97f7-4069-b44e-ce6613d521f2' if coin == 'AAVE': coin_string='3370b1db-9807-4487-a3fa-d3fb8f5d4081' if coin == 'REN': coin_string='83a27416-c523-4f55-9b1e-337b65a836de' ## Show coin balance print(auth_client.get_account(coin_string)) ## Show USD balance print(auth_client.get_account('eb3f5c47-6b9f-432d-9a43-67bc3c79ee8d')) ## Print coin and USD balance coin_account = round(float(auth_client.get_account(coin_string)['available']),5) usd_account = round(float(auth_client.get_account('eb3f5c47-6b9f-432d-9a43-67bc3c79ee8d')['available']),2) print (coin + ' Balance is: ' + str(coin_account)) print ('USD Balance is: $' + str(usd_account)) #### Deposit amount USD from bank account print ('Deposit ' + amount + ' USD from Bank account to CoinBase Pro account') ######### Depositing USD ######### if usd_amount > 1: print(auth_client.deposit(amount, 'USD', 'bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb')) ## Bank Account time.sleep(5) usd_new_account = round(float(auth_client.get_account('eb3f5c47-6b9f-432d-9a43-67bc3c79ee8d')['available']),2) usd_available= round(usd_new_account - usd_account,2) ## print coin and USD balance print ('New USD Balance is: $' + str(usd_new_account)) print ('Total USD Added: $' + str(usd_available)) ## debug ##usd_available = 100 if (usd_available >= usd_amount) : #CoinBase Pro Buy coin with USD print ('CoinBase Pro Account: Buy $' + amount + ' USD of ' + coin + ' ...' ) ########## Buying coin ########## print(auth_client.place_market_order(product_id=coin_ticker, side='buy', funds=amount)) time.sleep(10) coin_new_account = round(float(auth_client.get_account(coin_string)['available']),5) print ('New ' + coin + ' Balance is: ' + str(coin_new_account)) coin_available= round(coin_new_account - coin_account,5) print ('Total ' + coin + ' Added: ' + str(coin_available)) ## Publication Information - [Kenny (Kang Zeng)](https://paragraph.com/@kenny-kang-zeng/): Publication homepage - [All Posts](https://paragraph.com/@kenny-kang-zeng/): More posts from this publication - [RSS Feed](https://api.paragraph.com/blogs/rss/@kenny-kang-zeng): Subscribe to updates