Skip to content

Commit

Permalink
add python ccxt version
Browse files Browse the repository at this point in the history
  • Loading branch information
hackingthemarkets committed Mar 27, 2022
1 parent b389ef8 commit 00ae60a
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 0 deletions.
13 changes: 13 additions & 0 deletions config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
API_KEY = "yourapikey"
SECRET_KEY = "yoursecretkey"

SYMBOL = "ETH/USD"
POSITION_SIZE = 0.001

# gridbot settings
NUM_BUY_GRID_LINES = 5
NUM_SELL_GRID_LINES = 5
GRID_SIZE = 2

CHECK_ORDERS_FREQUENCY = 1
CLOSED_ORDER_STATUS = 'closed'
75 changes: 75 additions & 0 deletions gridbot.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
import ccxt, config, time, sys

exchange = ccxt.ftxus({
'apiKey': config.API_KEY,
'secret': config.SECRET_KEY
})

ticker = exchange.fetch_ticker(config.SYMBOL)

buy_orders = []
sell_orders = []

# initial_buy_order = exchange.create_market_buy_order(config.SYMBOL, config.POSITION_SIZE * config.NUM_SELL_GRID_LINES)

for i in range(config.NUM_BUY_GRID_LINES):
price = ticker['bid'] - (config.GRID_SIZE * (i+1))
print("submitting market limit buy order at {}".format(price))
order = exchange.create_limit_buy_order(config.SYMBOL, config.POSITION_SIZE, price)
buy_orders.append(order['info'])

for i in range(config.NUM_SELL_GRID_LINES):
price = ticker['bid'] + (config.GRID_SIZE * (i+1))
print("submitting market limit sell order at {}".format(price))
order = exchange.create_limit_sell_order(config.SYMBOL, config.POSITION_SIZE, price)
sell_orders.append(order['info'])

while True:
closed_order_ids = []

for buy_order in buy_orders:
print("checking buy order {}".format(buy_order['id']))
try:
order = exchange.fetch_order(buy_order['id'])
except Exception as e:
print("request failed, retrying")
continue

order_info = order['info']

if order_info['status'] == config.CLOSED_ORDER_STATUS:
closed_order_ids.append(order_info['id'])
print("buy order executed at {}".format(order_info['price']))
new_sell_price = float(order_info['price']) + config.GRID_SIZE
print("creating new limit sell order at {}".format(new_sell_price))
new_sell_order = exchange.create_limit_sell_order(config.SYMBOL, config.POSITION_SIZE, new_sell_price)
sell_orders.append(new_sell_order)

time.sleep(config.CHECK_ORDERS_FREQUENCY)

for sell_order in sell_orders:
print("checking sell order {}".format(sell_order['id']))
try:
order = exchange.fetch_order(sell_order['id'])
except Exception as e:
print("request failed, retrying")
continue

order_info = order['info']

if order_info['status'] == config.CLOSED_ORDER_STATUS:
closed_order_ids.append(order_info['id'])
print("sell order executed at {}".format(order_info['price']))
new_buy_price = float(order_info['price']) - config.GRID_SIZE
print("creating new limit buy order at {}".format(new_buy_price))
new_buy_order = exchange.create_limit_buy_order(config.SYMBOL, config.POSITION_SIZE, new_buy_price)
buy_orders.append(new_buy_order)

time.sleep(config.CHECK_ORDERS_FREQUENCY)

for order_id in closed_order_ids:
buy_orders = [buy_order for buy_order in buy_orders if buy_order['id'] != order_id]
sell_orders = [sell_order for sell_order in sell_orders if sell_order['id'] != order_id]

if len(sell_orders) == 0:
sys.exit("stopping bot, nothing left to sell")

0 comments on commit 00ae60a

Please sign in to comment.