I built a crypto bot and gave it my entire wallet

Kaushal Agarwal
3 min readJun 1, 2021

--

Disclaimer: This article doesn’t emphasize any trading algorithm but how one could go about building a simple bot. By no means this is any financial advice.

Long story short

I coded a bot that uses the CoinDCX exchange to carry out trades for me which runs on a scheduler so that it continuously plays by my defined trading signals (all under an hour)

So let’s get right on it!

Set up an account in CoinDCX

Get your API & Secret key

Go to your CoinDCX profile section
Click Access API dashboard
Click Create API key button and follow the process of verifications

Now the fun part starts (abracadabra)

Create a file named bot.py

We’ll need the following imports

import hmacimport hashlibimport jsonimport timeimport requestsimport scheduleimport time

Setup your keys

key = “REPLACE_WITH_YOURS”secret = “REPLACE_WITH_YOURS”secret_bytes = bytes(secret, encoding=’utf-8')

Function to fetch your wallet balance

def get_balance():
timeStamp = int(round(time.time() * 1000))
body = {
"timestamp": timeStamp
}
json_body = json.dumps(body, separators=(',', ':'))
signature = hmac.new(secret_bytes, json_body.encode(),
hashlib.sha256).hexdigest()
url = "https://api.coindcx.com/exchange/v1/users/balances"
headers = {
'Content-Type': 'application/json',
'X-AUTH-APIKEY': key,
'X-AUTH-SIGNATURE': signature
}
response = requests.post(url, data=json_body, headers=headers)
data = response.json()
for i in data:
if i['currency'] == 'INR':
return i

Function to get the last updated price of a cryptocurrency

def get_live_price(ticker):
url = "https://api.coindcx.com/exchange/ticker"
response = requests.get(url)
data = response.json()
for i in data:
if i['market'] == ticker:
return i

ticker — ETHINR (eg. Bitcoin — BTCINR, Ethereum — ETHINR)

Function to execute your order

def create_order(action, ticker, price, quantity):
timeStamp = int(round(time.time() * 1000))
body = {
"side": action,
"order_type": "limit_order",
"market": ticker,
"price_per_unit": price,
"total_quantity": quantity,
"timestamp": timeStamp
}
json_body = json.dumps(body, separators=(',', ':'))
signature = hmac.new(secret_bytes, json_body.encode(),
hashlib.sha256).hexdigest()
url = "https://api.coindcx.com/exchange/v1/orders/create"
headers = {
'Content-Type': 'application/json',
'X-AUTH-APIKEY': key,
'X-AUTH-SIGNATURE': signature
}
response = requests.post(url, data=json_body, headers=headers)
data = response.json()

action — buy or sell

ticker — ETHINR

price — 200000 (price you’re quoting for one unit of the currency. it’s mandatory if it’s a limit_order but not if it is a market_order)

quantity — 0.0004

Function to place your logic

def program():
#Place your logic here
#Don't forget to use the above util fucntions

Lastly, run a scheduler

schedule.every(30).seconds.do(program)while 1:
schedule.run_pending()
time.sleep(1)

Great 🎉 Just hit

> python bot.py

We’re done (or we aren’t). Just a small tiny bit of work pending. Let’s not keep our machine running 24x7 but rather offload the task to some cloud provider.

My favorite is Heroku. It’s easy to set up and free 🤑

Setup an account here

We’ll need to create two files, requirements.txt and Procfile

Run the following command in CLI

> pip freeze > requirements.txt

Make sure your requirements.txt also contains gunicorn==19.9.0

Create an empty file named Procfile (without any extension)

Paste the following content

web: python bot.pyworker: python bot.py

Now host all contents on a GitHub repository (make sure it’s private as it contains secrets)

Let’s login to our Heroku dashboard

Create a new app

Move to the Deployment tab

Connect your Github account and your repository

Select your branch and hit Deploy Branch

Once the build is successful, then move to the Resources tab

Toggle both web and worker under Free Dynos

That’s all. We have a worker that’s continuously running on the cloud trading for us.

Thanks for sticking around till here. If you enjoyed the article, do share it. If you have any questions, feel free to reach out.

Until next time…

--

--

Kaushal Agarwal

Games, Tech, & XR. They fascinate me. Here to share insights from personal experiences & thoughts.