Decentralized Programmer: Simple KuCoin Trading Bot

in #trading6 years ago (edited)

The code below illustrates how easy is to create a trading bot that buys and sells currency within preset boundaries. In this example it's set to trade CV (Car Vertical) on KuCoin exchange. It sells when it gets to 30 satoshi, and buys when it drops to 29 satoshi. Source code can be found on github: https://gist.github.com/izotx/9a1b0ccbd181cc62b9a9f1d70530b2e3

Good application for this bot are currencies that 'flatlined' and are trading in tight boundaries, for example check the chart below within last few days:

The bot is too primitive to adjust when the price drops, or raises outside preset boundaries, so be careful!

*Use it on your own risk!

Installation

  1. Install Python on your machine: https://www.python.org/getit/
  2. Install necessary libraries: kucoin, json,math,datetime.
    Easiest way to do it is with package manager pip
  3. Replace the api key and secret with your own.
  4. Run the program: python [nameofthefile]
#importing libraries
from kucoin.client import Client
import json
import math
import time
import datetime


#API KEYS to the exchange
api_key = "xxxxxx"
api_secret = "xxxx"

#instantiate client
apiclient = Client(api_key, api_secret)

#set buyin and selling price
pricebuy = 0.00000029
pricesell = 0.00000030

#tickers to trade
ticker1 = 'CV'
ticker2 = 'BTC'

#Each exchange can have different fees
fee_perc = 0.1

#Saving logs
def saveLog(logmessage):
    with open('/Users/jc_admin/Documents/GitHub/Bittrex/log', 'a') as file:
        file.write(datetime.datetime.now().strftime("%Y-%m-%d %H:%M") + " ")
        file.write(logmessage +"\n")
        file.close()

    return

#main function
def mainf():

    saveLog("Running\n")
    balance_json1 = apiclient.get_coin_balance(ticker1)
    balance_json2 = apiclient.get_coin_balance(ticker2)

    coin_available1 = truncate(balance_json1["balance"],2)
    coin_available2 = balance_json2["balance"]

    print(coin_available1)
    print(coin_available2)

    #checks if we have minimum amount of coins to sell (100 is arbitrary, change it to your number)
    if  coin_available1 > 100:
        #selling
        message = "Setting sell order for: "  + str(ticker1)
        print(message)
        saveLog(message)

        #executing sell order
        apiclient.create_sell_order("CV-BTC", pricesell, coin_available1)

    #Checks if we have enough to buy
    if  coin_available2 > 0:
        #buing
        message = "Setting buy order for:  " + str(ticker1)
        print(message)
        saveLog(message)
        buy = calcBuy(coin_available2,pricebuy)
        if buy > 100:
            #executing buy order
            apiclient.create_buy_order("CV-BTC",pricebuy, buy)

    return

#calcuates how
def calcBuy(available,price):
    gross = available/price
    fee = fee_perc/100.0
    net = gross * (1 - fee)
    print(net)
    rounded = math.floor(net)

    print(rounded)
    return rounded

#for continuos execution
def update():
    while True:
        mainf()
        time.sleep(30 * 60)

#APIs might have different number formats/precision
def truncate(f, n):
    return math.floor(f * 10 ** n) / 10 ** n

#one time execution
mainf()



Coin Marketplace

STEEM 0.21
TRX 0.20
JST 0.034
BTC 99163.21
ETH 3283.22
USDT 1.00
SBD 3.05