COINGATOR: a statistical rusty searcher ๐—ถ๐—ป ๐—ฟ๐˜‚๐˜€๐˜

Overview

๐ŸŠ ๐Ÿค– COINGATOR: a statistical rusty searcher ๐Ÿ‘พ โœจ


(need to commit the rest of this work: i will add everything when i am back from vacation)


tl; dr

๐ŸŠ this program implements a searcher running statistical strategies on several exchanges. It's called coingator because it's a cute animal that rhymes with cointegratoooor.

๐Ÿ“š for more details about this project, check my mirror post: bot #3: coingator, a rusty cointegratooor searcher.

๐Ÿšจ disclaimer: this project is a boilerplate to get you started; you might or might not profit from it: in the mev world, nobody is going to handle you the alpha. i am not responsible for anything you do with my free code.



strategies


One of the most well-known strategies among different algorithmic trading methods is the statistical arbitrage strategy: a profitable situation stemming from pricing inefficiencies among financial markets. Statistical arbitrage is a mere strategy to obtain profit by applying past statistics.


cointegration

  • cointegration is the test correlation between two or more non-stationary time series for a specified period (identifying long-run parameters and determining when stationary time series do not depart from equilibrium).

Formally, if (X,Y,Z) are each integrated of order d, and there exist coefficients a, b, c such that aX + bY + cZ is integrated of the order less than d, then X, Y, and Z are cointegrated.


  • in this tool, we implement this strategy with the following steps:
    1. retrieve a list of tradeable symbols
    2. generate price history
    3. identify cointegrated pairs
    4. identify trends
    5. backtest


monitoring

with bybit

  • using this code, we use websockets and bybit's API to monitor the following:
    * spot pair orderbook, depth, k-lines, and private execution reports
    * inverse public orderbooks, trades, insurances, perpetuals, futures, liquidations
    * inverse private positions, executions, stop orders
  • to create a trading bot, you can select: pair, price range, number of grids, and total investment.


local set up

Make sure you have rust installed and add info to a .env file:

cp .env.example .env
vim .env

Then install COINGATOR with:

make build

Run with:

make run


running with bybit


Screen Shot 2023-01-02 at 12 42 02 PM


bybit REST endpoints:
- Testnet: https://api-testnet.bybit.com 
- Mainnet: https://api.bybit.com, https://api.bytick.com 


subscribing to topics on a derivative


Crypto derivatives are financial contracts that derive their values from underlying assets. Crypto futures contracts are proxy tools to speculate on the future prices of cryptocurrencies or to be used to hedge against their price changes.


Select 1:

๐ŸŠ welcome to coingator ๐Ÿช™. type your option:

โžก 1: sub to public topics for a derivative (e.g., ETHUSDT)

This will open a websocket with bybit and subscribe to the following derivative's topics:

- depths
- trades
- book tickers
- realtimes

Example output:

๐ŸŠ subscribing to websockets for: "ETHUSDT"
โœ… depth: ResponseV2 { topic: "depth", params: ResCommonParamsV2 { binary: "false", symbol: "ETHUSDT", symbol_name: "ETHUSDT" }, data: Depth { t: 1672627313579, s: "ETHUSDT", v: "1672627313579_1", b: [OrderBookItem("1196.53", "6.81229"), OrderBookItem("1196.52", "10"), OrderBookItem("1196.51", "0.08357"), OrderBookItem("1196.46", "0.01876"), OrderBookItem("1196.44", "0.16778"), OrderBookItem("1196.4", "10.00414"), OrderBookItem("1196.39", "3.62"), OrderBookItem("1196.33", "11.98536"), OrderBookItem("1196.32", "4.37768"), OrderBookItem("1196.31", "0.25238"), OrderBookItem("1196.3", "2.44169"), OrderBookItem("1196.28", "6.48"), OrderBookItem("1196.27", "3.46"), OrderBookItem("1196.24", "0.001"), OrderBookItem("1196.22", "1.52901"), OrderBookItem("1196.21", "3.14679"), OrderBookItem("1196.19", "1.54244"), OrderBookItem("1196.18", "1.24823"), OrderBookItem("1196.16", "6.75"), OrderBookItem("1196.15", "4.35209"), OrderBookItem("1196.14", "0.30445"), OrderBookItem("1196.13", "0.41108"), OrderBookItem("1196.06", "16.713"), OrderBookItem("1196.04", "5"), OrderBookItem("1196.01", "9.5338"), OrderBookItem("1196", "4.009"), OrderBookItem("1195.99", "1.7134"), OrderBookItem("1195.97", "1.2251"), OrderBookItem("1195.95", "26.34158"), OrderBookItem("1195.91", "5"), OrderBookItem("1195.9", "0.08359"), OrderBookItem("1195.85", "0.89955"), OrderBookItem("1195.83", "0.16989"), OrderBookItem("1195.82", "1.25766"), OrderBookItem("1195.8", "2.515"), OrderBookItem("1195.76", "3.57"), OrderBookItem("1195.75", "5"), OrderBookItem("1195.72", "0.1672"), OrderBookItem("1195.7", "3.78046"), OrderBookItem("1195.69", "1.41835")], a: [OrderBookItem("1196.69", "2.02591"), OrderBookItem("1196.72", "0.001"), OrderBookItem("1196.8", "0.0192"), OrderBookItem("1196.84", "0.25238"), OrderBookItem("1196.9", "3"), OrderBookItem("1196.92", "1.94"), OrderBookItem("1196.93", "0.16315"), OrderBookItem("1196.94", "0.89955"), OrderBookItem("1196.95", "2.02888"), OrderBookItem("1196.96", "4.00607"), OrderBookItem("1196.98", "3.687"), OrderBookItem("1197", "0.01624"), OrderBookItem("1197.02", "3.05537"), OrderBookItem("1197.05", "0.84081"), OrderBookItem("1197.08", "1.51308"), OrderBookItem("1197.09", "3.174"), OrderBookItem("1197.1", "0.03835"), OrderBookItem("1197.13", "1.5768"), OrderBookItem("1197.15", "24.16"), OrderBookItem("1197.18", "0.83532"), OrderBookItem("1197.2", "2.18155"), OrderBookItem("1197.22", "1.52477"), OrderBookItem("1197.23", "1.71323"), OrderBookItem("1197.27", "9.5338"), OrderBookItem("1197.29", "2.73673"), OrderBookItem("1197.32", "0.0503"), OrderBookItem("1197.35", "25.07989"), OrderBookItem("1197.42", "16.713"), OrderBookItem("1197.43", "0.03916"), OrderBookItem("1197.44", "0.83499"), OrderBookItem("1197.5", "46.17"), OrderBookItem("1197.52", "0.12796"), OrderBookItem("1197.53", "1.15349"), OrderBookItem("1197.55", "25.75042"), OrderBookItem("1197.6", "0.09146"), OrderBookItem("1197.65", "0.26999"), OrderBookItem("1197.66", "0.89955"), OrderBookItem("1197.67", "12.01463"), OrderBookItem("1197.71", "0.8725"), OrderBookItem("1197.72", "0.1")] } }
โœ… trade: ResponseV2 { topic: "trade", params: ResCommonParamsV2 { binary: "false", symbol: "ETHUSDT", symbol_name: "ETHUSDT" }, data: Trade { v: "2280000000030465445", t: 1672627309524, p: "1196.69", q: "0.00083", m: true } }
โœ… book ticker: ResponseV2 { topic: "bookTicker", params: ResCommonParamsV2 { binary: "false", symbol: "ETHUSDT", symbol_name: "ETHUSDT" }, data: BookTicker { symbol: "ETHUSDT", bid_price: "1196.53", bid_qty: "6.81229", ask_price: "1196.69", ask_qty: "2.02591", time: 1672627312138 } }
โœ… realtimes: ResponseV2 { topic: "realtimes", params: ResCommonParamsV2 { binary: "false", symbol: "ETHUSDT", symbol_name: "ETHUSDT" }, data: Realtimes { t: 1672627309524, s: "ETHUSDT", c: "1196.69", h: "1204.66", l: "1190.96", o: "1195.91", v: "20134.37723", qv: "24113384.0565187", m: "0.0007" } }
โœ… pong: Pong { pong: 1672627314329 }
(...)


subscribing to topics for a pair of crypto assets

Select 2:

๐ŸŠ welcome to coingator ๐Ÿช™. type your option:

โžก 2: sub to public topics for a pair of derivatives

This will open a websocket with bybit and subscribe the pair to the following topics:

- orderbook l2 25, 200
- trades
- instruments info
- k-lines
- liquidations

Example output:

๐ŸŠ subscribing to websockets for: ["ETHUSDT", "BTCUSDT"]
โœ… instrument info snapshot: Response { topic: "instrument_info.100ms.ETHUSDT", res_type: "snapshot", data: InstrumentInfoSnapshot { id: 2, symbol: "ETHUSDT", last_price_e4: "11966500", last_price: "1196.65", bid1_price_e4: "11966000", bid1_price: "1196.60", ask1_price_e4: "11966500", ask1_price: "1196.65", last_tick_direction: "ZeroPlusTick", prev_price_24h_e4: "11939000", prev_price_24h: "1193.90", price_24h_pcnt_e6: "2303", high_price_24h_e4: "12050500", high_price_24h: "1205.05", low_price_24h_e4: "11913500", low_price_24h: "1191.35", prev_price_1h_e4: "11962500", prev_price_1h: "1196.25", price_1h_pcnt_e6: "334", mark_price_e4: "11966100", mark_price: "1196.61", index_price_e4: "11965400", index_price: "1196.54", open_interest_e8: "58516141000000", total_turnover_e8: "4093197159274450000", turnover_24h_e8: "35260448999150020", total_volume_e8: "38356353516000000", volume_24h_e8: "29427055999999", funding_rate_e6: "100", predicted_funding_rate_e6: "100", cross_seq: "24972973797", created_at: "2022-03-31T03:56:16.000Z", updated_at: "2023-01-02T03:14:29.000Z", next_funding_time: "2023-01-02T08:00:00Z", count_down_hour: "5", funding_rate_interval: "8", settle_time_e9: "0", delisting_status: "0" }, cross_seq: "24972976935", timestamp_e6: "1672629272903187" }
(...)


subscribing to inverse perpetuals info


A perpetual contract has no expiration date. At Bybit, funding occurs every amount of hours, and they use the interest rate and the premium index to calculate the funding through Time-Weighted-Average-Price (TWAP) over the series of minutes rates.


To subscribe to inverse perpetuals and futures info, select 3:

๐ŸŠ welcome to coingator ๐Ÿช™. type your option:

โžก 3: sub to public inverse perpetual info topics

This will open a websocket with bybit and subscribe to the following topics:

- orderbook l2 25, 200
- trades
- insurances
- instrument info
- k-lines
- liquidations


subscribing to the spot local order book


In the spot market, one can buy and sell cryptocurrencies for immediate delivery. They are directly transferred between market participants (buyers and sellers), which have direct ownership of the assets and are entitled to economic benefits, such as voting or staking participation.


Select 4:

๐ŸŠ welcome to coingator ๐Ÿช™. type your option:

โžก 4: sub to spot local order book topics

This will open a websocket with bybit and subscribe to spot info on the following topics:

- trades
- diff depths

Example output:

โœจ๐ŸŠ ETHUSDT order book

๐Ÿ’ฐ price              ๐Ÿ› quantity            
1217.31              1.71858             
1217.29              0.9398              
1217.28              3.17709             
1217.25              1.71                
1217.24              3.27                
1217.23              5                   
1217.19              0.25474             
1217.15              0.08218             
1217.14              5.18416             
1217.09              5.0018              

๐Ÿ”ป 1216.27

1216.98              0.1489              
1216.94              0.08218             
1216.92              0.3                 
1216.85              6.01827             
1216.8               0.09138             
1216.71              0.3                 
1216.7               2.06                
1216.67              0.08347             
1216.66              5.03956             
1216.63              3.31   


subscribing to private inverse execution reports

Get your API creds from bybit. You can also use their testnet.

Select 5:

๐ŸŠ welcome to coingator ๐Ÿช™. type your option:

โžก 5: sub to private inverse execution topics

This will open a websocket with bybit and subscribe to a private account for the following topics:

- ticket info sequences
- outbound account info sequences


subscribing to private positions

Get your API creds from bybit. You could also use their testnet.

Select 6:

๐ŸŠ welcome to coingator ๐Ÿช™. type your option:

โžก 6: sub to private positions topics

This will open a websocket with bybit and subscribe to private positions on the following topics:

- positions
- executions
- orders
- stop orders
- wallets


resources


  • Cointegration-Based Pairs Trading Strategy in the Cryptocurrency Market (arxiv:2109.10662)
    • "By considering the main limitations in the market microstructure, our strategy exceeds the naive buy-and-hold approach in the Bitmex exchange. Another significant finding is that we implement a numerous collection of cryptocurrency coins to formulate the modelโ€™s spread, which improves the risk-adjusted profitability of the pairs trading strategy. Besides, the strategyโ€™s maximum drawdown level is reasonably low, which makes it useful to be deployed. The results also indicate that a class of coins has better potential arbitrage opportunities than others."

  • Constructing Cointegrated Cryptocurrency Portfolios
    • As the cryptocurrency market continues to grow with new coins and new exchanges, itโ€™s very important for individual investors, crypto-fund managers, as well as regulators to understand the price dependency among all cryptocurrencies, along with their derivatives.
You might also like...
RDFM - The Rusty DotFiles Manager

d8888b. d88888b. 8888888b 8888b d8888 88 `8D 88 `8D 88' 88'YbdP`88 88oobY' 88 88

 rusty-donut - ASCII raymarching inside a terminal
rusty-donut - ASCII raymarching inside a terminal

ASCII raymarching inside a terminal

A rusty, dual-wielding Quake and Half-Life texture WAD parser.

Ogre โ€ƒ A rusty, dual-wielding Quake and Half-Life texture WAD parser ogre is a rust representation and nom parser for Quake and Half-Life WAD files. I

A Rusty CUDA wrapper

cuda-oxide cuda-oxide is a safe wrapper for CUDA. With cuda-oxide you can execute and coordinate CUDA kernels. Safety Philosophy cuda-oxide does not o

Rusty NuGet client

ruget It's a NuGet client built in Rust. It's not really meant to replace existing nuget clients. It's more of a playground for experimenting with rel

Rusty NuGet client

turron It's a NuGet client built in Rust. It's not really meant to replace existing nuget clients. It's more of a playground for experimenting with re

Rusty Armor Builds - Monster Hunter Rise Armor Set Creation Tool
Rusty Armor Builds - Monster Hunter Rise Armor Set Creation Tool

RAB Rusty Armor Builds - Monster Hunter Rise Armor Set Creation Tool Armor files used by RAB

Sombervale, a Rusty Jam 2021 game
Sombervale, a Rusty Jam 2021 game

Sombervale Made in 7 days for Rusty Jam 2021. About It is dark and spooky in Sombervale park. There are shadow creatures all around you. Run away with

Rusty Ray Tracer

rtrtr - Rusty Ray Tracer Cameron Christensen - Fall 2021 -- I started this both to brush up on ray tracing and to learn Rust. For the former I've been

A rusty, spiky, heat-seeking quake map parser.

Shalrath โ€ƒ A rusty, spiky, heat-seeking quake map parser shalrath is a rust representation, [nom] parser and string serializer for Quake map files. It

โŒš A command-line tool (and library) for the rusty Swatch Internet Time.

โŒš A command-line tool (and library) for the rusty Swatch Internet Time. Comes with XBar/Swiftbar support.

๐Ÿฆ€ My Rusty Playground

rust playground A language empowering everyone to build reliable and efficient software. Resources The Book Rustlings Rust by Example Take your first

๐ŸŽ„ My Rusty Solutions to AOC 2021

๐ŸŽ„ Advent-Of-Code-2021 My Rusty Solutions to Advent Of Code 2021 Day 01: Sonar Sweep [A, B] Day 02: Dive! [A, B] Day 03: Binary Diagnostic [A, B] Day

Rusty Hog is a secret scanner built in Rust for performance, and based on TruffleHog which is written in Python.
Rusty Hog is a secret scanner built in Rust for performance, and based on TruffleHog which is written in Python.

Rusty Hog is a secret scanner built in Rust for performance, and based on TruffleHog which is written in Python. Rusty Hog provides the following bina

A Rust API for Vega-Lite V4 to build chart with a rusty API.
A Rust API for Vega-Lite V4 to build chart with a rusty API.

Vega-Lite V4 for Rust A Rust API for Vega-Lite V4 to build chart with a rusty API. Similar to the Altair project in python, this crate build upon Vega

A highly modular Bitcoin Lightning library written in Rust. Its Rust-Lightning, not Rusty's Lightning!

Rust-Lightning is a Bitcoin Lightning library written in Rust. The main crate, lightning, does not handle networking, persistence, or any other I/O. Thus, it is runtime-agnostic, but users must implement basic networking logic, chain interactions, and disk storage. More information is available in the About section.

rusty-riscy is a performance testing and system resource monitoring tool written in Rust to benchmark RISC-V processors.

rusty-riscy rusty-riscy is a performance testing and system resource monitoring tool written in Rust to benchmark RISC-V processors. Objectives To cre

API tool,but egui style and rusty
API tool,but egui style and rusty

WEAVER About Weaver is a simple,easy-to-use and cross-platform API tool.Inspired by hoppscotch . It uses the Rust egui GUI library. Features Get,Post

๐Ÿฆ€ Rusty DLL Injector with GUI
๐Ÿฆ€ Rusty DLL Injector with GUI

DLL Crab Rusty DLL Injector with GUI Screenshot Why? Because I can't find a GUI DLL Injector that written in Rust. And i wrote it myself! Methods Crea

Owner
go outside labs
๐—ฏ๐—น๐—ผ๐—ฐ๐—ธ๐—ฐ๐—ต๐—ฎ๐—ถ๐—ป๐˜€ ๐—ถ๐—ป๐—ณ๐—ฟ๐—ฎ๐˜€๐˜๐—ฟ๐˜‚๐—ฐ๐˜๐˜‚๐—ฟ๐—ฒ, ๐—บ๐—ฒ๐˜ƒ, ๐˜€๐—ฒ๐—ฐ๐˜‚๐—ฟ๐—ถ๐˜๐˜†, ๐—ฎ๐—น๐—ด๐—ผ๐—ฟ๐—ถ๐˜๐—ต๐—บ๐—ถ๐—ฐ ๐˜๐—ฟ๐—ฎ๐—ฑ๐—ถ๐—ป๐—ด
go outside labs
A highly modular Bitcoin Lightning library written in Rust. Its Rust-Lightning, not Rusty's Lightning!

Rust-Lightning is a Bitcoin Lightning library written in Rust. The main crate, lightning, does not handle networking, persistence, or any other I/O. Thus, it is runtime-agnostic, but users must implement basic networking logic, chain interactions, and disk storage. More information is available in the About section.

Lightning Dev Kit 850 Jan 3, 2023
Rusty Reflective DLL Injection - A small reflective loader in Rust 4KB in size

Reflective Loader in Rust (4KB in size) A small reflective loader PoC in Rust. I remade this from my old project (https://github.com/memN0ps/arsenal-r

null 97 Apr 8, 2023
A rusty interface to llama.cpp for rust

llama-cpp-rs Higher level API for the llama-cpp-sys library here: https://github.com/shadowmint/llama-cpp-sys/ A full end-to-end example can be found

Doug 3 Apr 16, 2023
A game made for the Rusty Jam https://itch.io/jam/rusty-jam

Murder-User Dungeon Introduction Tony is a young man. Finally having its own apartment is a good thing! He will learn how to live by himself and how t

null 62 Dec 6, 2022
Statistical computation library for Rust

statrs Current Version: v0.13.0 Should work for both nightly and stable Rust. NOTE: While I will try to maintain backwards compatibility as much as po

Michael Ma 384 Dec 27, 2022
Statistical routines for ndarray

ndarray-stats This crate provides statistical methods for ndarray's ArrayBase type. Currently available routines include: order statistics (minimum, m

null 150 Dec 26, 2022
Statistical computation library for Rust

statrs Current Version: v0.15.0 Should work for both nightly and stable Rust. NOTE: While I will try to maintain backwards compatibility as much as po

null 385 Jan 4, 2023
m2cgen (Model 2 Code Generator) - is a lightweight library which provides an easy way to transpile trained statistical models into a native code

Transform ML models into a native code (Java, C, Python, Go, JavaScript, Visual Basic, C#, R, PowerShell, PHP, Dart, Haskell, Ruby, F#, Rust) with zero dependencies

Bayes' Witnesses 2.3k Dec 31, 2022
Rusty Object Notation

Rusty Object Notation RON is a simple readable data serialization format that looks similar to Rust syntax. It's designed to support all of Serde's da

ron-rs 2.3k Jan 1, 2023
A rusty dynamically typed scripting language

dyon A rusty dynamically typed scripting language Tutorial Dyon-Interactive Dyon Snippets /r/dyon Dyon script files end with .dyon. To run Dyon script

PistonDevelopers 1.5k Dec 27, 2022