Skip to content

mpwsh/exchange-observer

Repository files navigation

Exchange Observer | Trading sim

A trading simulator for OKX exchange. Reads OKX websocket data and tries to make smart trades based on your Strategy configuration settings.

Disclaimer

The trading simulation logic implemented is super rough and kinda dumb (im still experimenting with it tho). The scheduler will try to follow the line going up, by querying all tokens and buying whatever one is doing good numbers. I'm just sharing this because i think its a good template to start with a stream based: producer -> consumer -> app architecture. (not event driven, at least for now)

That being said, im not responsible if you lose money by using it to trade with money.

Each setting in config.toml is explained but feel free open an issue to ask questions if something is not clear.

Setup

Required infrastructure

  • Redpanda
  • Scylla
  • Pushover (optional)

Components

  • Producer (Okx websocket messages -> Redpanda)
  • Consumer (Redpanda messages -> Scylla)
  • Scheduler (ScyllaDB Queries -> Token evaluation -> UI)

Deploy Redpanda and Scylla

docker-compose up -d

Create required keyspace and tables in Scylla

Wait until the node is up and run the migrations.

alias nodetool="docker exec -it scylla nodetool"
nodetool status
### Should show 'UN' (up-normal)
Status=Up/Down
|/ State=Normal/Leaving/Joining/Moving
--  Address     Load       Tokens       Owns    Host ID                               Rack
UN  172.26.0.2  540 KB     256          ?       c35c31db-0c92-4064-b2ba-2da43fa6e1a0  Rack1

Run the migrations

alias cqlsh="docker exec -it scylla cqlsh"
cqlsh -f /tmp/migration.cql

Endpoints

ScyllaDB: 127.0.0.1:9042
Redpanda: 127.0.0.1:9092
Redpanda console: http://localhost:8080/topics

Configure data retention for each topic

The producer will create the topics based on the config.toml settings To create them manually use:

alias rpk="docker exec redpanda rpk"
rpk topic create candle1m tickers trades --partitions 10 --replicas 1 -c cleanup.policy=compact

# Topic configuration is not available on the rskafka crate.
# To set 12hs retention use:
rpk topic alter-config candle1m tickers trades --set retention.ms=43200000 --brokers localhost

Spin up all the producer and consumer

cargo run --bin producer
cargo run --bin consumer

Configure scheduler account and strategy settings

vim config.toml

Start

cargo run --bin scheduler

Scheduler terminal UI

This is how the scheduler UI looks with ui.enable = true exchange-observer ui

Scheduler GUI

If using the scheduler with websocket server enabled you can connect to it using a very rough expermiental wasm UI made with egui and ewebsock.

The console is just a listener, so can't send stuff back to the scheduler for now.

Run the app with:

cargo run --bin console

Or build web and run with:

cd console && ./build_web.sh && ./start_server.sh

Or run compiled version in the repo with:

basic-http-server --addr 0.0.0.0:8082 .

exchange-observer gui

Push Notifications

The scheduler can send status updates every 30 minutes informing you about your current balance, bot uptime and account change % if. It will also send a notification when cash-outs and stop loss trigger.

Debug

Connect to Scylla using cqlsh

docker exec -it scylla cqlsh
Use HELP for help.
cqlsh> use okx;
cqlsh:okx> describe tables;
candle1m  tickers  trades
cqlsh:okx> SELECT * FROM trades WHERE instid='XCH-USDT' ORDER BY ts DESC LIMIT 10;

 instid   | ts                              | tradeid  | px    | side | sz
----------+---------------------------------+----------+-------+------+----------
 XCH-USDT | 2022-10-01 21:34:00.425000+0000 | 19102621 |  33.4 |  buy | 0.040482
 XCH-USDT | 2022-10-01 21:33:59.339000+0000 | 19102620 | 33.41 |  buy | 0.132378
 XCH-USDT | 2022-10-01 21:33:49.342000+0000 | 19102619 | 33.41 |  buy | 0.103742
 XCH-USDT | 2022-10-01 21:33:48.423000+0000 | 19102618 | 33.41 |  buy | 0.088245
 XCH-USDT | 2022-10-01 21:33:40.424000+0000 | 19102617 |  33.4 |  buy | 0.103606
 XCH-USDT | 2022-10-01 21:33:36.340000+0000 | 19102616 | 33.42 |  buy | 0.086683
 XCH-USDT | 2022-10-01 21:33:31.423000+0000 | 19102615 | 33.41 |  buy | 0.066893
 XCH-USDT | 2022-10-01 21:33:26.423000+0000 | 19102614 | 33.41 | sell | 0.102778
 XCH-USDT | 2022-10-01 21:33:23.340000+0000 | 19102613 | 33.41 |  buy | 0.043765
 XCH-USDT | 2022-10-01 21:33:15.339000+0000 | 19102612 | 33.42 |  buy | 0.071039
(10 rows)

Delete topics from redpanda

rpk topic delete candle1m trades tickers

Destroy

docker-compose down
#remove scylla DB data folder
rm ./scylla/data

About

OKX Exchange Observer & Trading simulator

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published