Skip to content

Instantly share code, notes, and snippets.

@hammertoe
Created June 17, 2021 14:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hammertoe/626360a0ebd88e4f7b41aefb24d758e9 to your computer and use it in GitHub Desktop.
Save hammertoe/626360a0ebd88e4f7b41aefb24d758e9 to your computer and use it in GitHub Desktop.
A very simple trading bot
import ccxt
import numpy as np
key = "mykey"
secret = "topsecret"
symbol = 'USDX/USDT'
exch = ccxt.ascendex({
'apiKey': key,
'secret': secret,
'timeout': 30000,
'enableRateLimit': True,
})
# Grab the last 24 1-hour candles
candles = exch.fetch_ohlcv(symbol, '1h', limit=24)
highs = [ candle[2] for candle in candles ]
lows = [ candle[3] for candle in candles ]
# Get the top 10% and bottom 10%
high = np.quantile(highs, 0.90)
low = np.quantile(lows, 0.10)
# Calculate a spread between high and low
spread = ((high / low) - 1) * 100
print("High", high)
print("Low", low)
print("Spread", spread)
# If the spread is greater than 5%
if spread > 5:
# Fetch and cancel all current open orders
open_orders = exch.fetch_open_orders(symbol)
print("Number of open orders: ", len(open_orders))
for order in open_orders:
order_id = order['id']
print("Cancelling order id: ", order_id)
exch.cancel_order(order_id, symbol)
# get balances
balances = exch.fetch_balance()
free_balances = balances['free']
usdt_free = free_balances.get('USDT', 0)
usdx_free = free_balances.get('USDX', 0)
print("USDT free:", usdt_free)
print("USDX free:", usdx_free)
# if we have some USDX then create sell order
if usdx_free > 50:
amount = int(usdx_free * 0.998)
print(f"Creating Sell order for {amount} @ {high}")
exch.createLimitSellOrder(symbol, amount, high)
# if we have any USDT then create buy order
if usdt_free > 50:
amount = int((usdt_free * 0.998) / low)
print(f"Creating Buy order for {amount} @ {low}")
exch.createLimitBuyOrder(symbol, amount, low)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment