Skip to content

Instantly share code, notes, and snippets.

@chiliec
Last active April 29, 2026 14:00
Show Gist options
  • Select an option

  • Save chiliec/eb4745d9a0d2ffbf5295b33982fb5a71 to your computer and use it in GitHub Desktop.

Select an option

Save chiliec/eb4745d9a0d2ffbf5295b33982fb5a71 to your computer and use it in GitHub Desktop.
Dead simple XRocket trading bot
import asyncio
import requests
from xrocket import TradeAPI
API_KEY = "XROCKET_API_KEY"
PAIR = "WOOF-TONCOIN"
MIN_VALUE = 1e-7 # 0.0000001
DEVIATION = 1e-1 # 0.1 or 10%
POOL_ADDR = "EQBLj-WT9pKhDCBx9EQHLZyPbq2flKspXDl6BPc9GzfIU4Jh"
async def defi_price():
r = requests.get(
"https://api.ston.fi/v1/pools/" + POOL_ADDR,
)
pool = r.json()["pool"]
price = float(pool["reserve1"]) / float(pool["reserve0"])
return price
async def cancel_orders(api):
orders = await api.order_list_by_pair(pair=PAIR)
if orders and orders["success"] and len(orders["results"]) > 0:
for order in orders["results"]:
await api.order_delete(order_id=order["orderId"])
print("Canceled order", order["orderId"])
async def check_balance(api):
balance = await api.balance()
if balance and balance["success"]:
toncoin = next(
(b for b in balance["data"]["balances"] if b["code"] == "TONCOIN"), None
)
woof = next(
(b for b in balance["data"]["balances"] if b["code"] == "WOOF"), None
)
return toncoin, woof
return None, None
def check_deviates(base, compare):
deviation = abs(float(compare) - base) / base
return deviation > DEVIATION
async def place_orders(api):
pair_data = await api.pair_by_name(PAIR)
if pair_data and pair_data["success"]:
buy_price = pair_data["data"]["buyPrice"]
sell_price = pair_data["data"]["sellPrice"]
if sell_price - buy_price > MIN_VALUE * 2:
buy_price = buy_price + MIN_VALUE
sell_price = sell_price -MIN_VALUE
else:
print("Empty spread")
price_on_defi = await defi_price()
toncoin, woof = await check_balance(api)
toncoin_value = toncoin["amount"] / price_on_defi
woof_value = woof["amount"]
toncoin_ratio = toncoin_value / (toncoin_value + woof_value)
toncoin_order_amount = min(toncoin["amount"] * 0.5, toncoin["amount"] * toncoin_ratio)
woof_ratio = woof_value / (toncoin_value + woof_value)
woof_order_amount = min(woof["amount"] * 0.5, woof["amount"] * woof_ratio)
if toncoin_order_amount > 0.02:
if (check_deviates(buy_price, price_on_defi)):
print("Deviation to BUY price on DEFI")
else:
result = await api.order_execute(
pair=PAIR,
order_type="BUY",
execute_type="LIMIT",
rate=round(float(buy_price), 7),
amount=round(float(toncoin_order_amount), 9),
currency="TONCOIN",
)
if "error" in result or result['success'] == False:
print(result)
else:
print(
"Placed",
"{:.7f}".format(buy_price),
"BUY with",
result["data"]["secondaryAmount"],
f"({0.5 * 100 if toncoin_ratio > 0.5 else (toncoin_ratio * 100):.2f}%) TON in order #",
result["data"]["orderId"],
)
else:
print("No TON for BUY")
if woof_order_amount > 200:
if check_deviates(sell_price, price_on_defi):
print("Deviation to SELL price on DEFI")
else:
result = await api.order_execute(
pair=PAIR,
order_type="SELL",
execute_type="LIMIT",
rate=round(float(sell_price), 7),
amount=round(float(woof_order_amount), 9),
currency="WOOF",
)
if "error" in result or result['success'] == False:
print(result)
else:
print(
"Placed",
"{:.7f}".format(sell_price),
"SELL with",
result["data"]["mainAmount"],
f"({(0.5 * 100) if woof_ratio > 0.5 else (woof_ratio * 100):.2f}%) $WOOF in order #",
result["data"]["orderId"]
)
else:
print("No $WOOF to SELL")
async def main():
api = TradeAPI(api_key=API_KEY)
while True:
try:
await cancel_orders(api)
await place_orders(api)
except Exception as e:
print("Error in main loop:", e)
await asyncio.sleep(60)
asyncio.run(main())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment