triangular_arbitrage_bot/src/triangle.h

119 lines
4.3 KiB
C

#ifndef FUSED_TRIANGLE_H
#define FUSED_TRIANGLE_H
#include <stdint.h>
#include <stdbool.h>
#include <stdatomic.h>
#include "book.h"
#include "hash.h"
#include "config.h"
#define MAX_TRIANGLES 16384
#define MAX_CURRENCIES 512
/* Describes one triangular arbitrage path of three trading pairs */
typedef struct {
uint16_t symbol_idx[3]; /* indices into the symbol table for each leg */
uint8_t use_bid[3]; /* 1=use bid price, 0=use ask price for this leg */
double fee_factor[3]; /* fee multiplier (1 - fee_rate) for each leg */
double base_increment[3]; /* base asset lot step for each leg */
double quote_increment[3]; /* quote asset lot step for each leg */
double funds_increment[3]; /* funds lot step for market buys for each leg */
double base_min_size[3]; /* min base order size for each leg */
double quote_min_size[3]; /* min quote order size for each leg */
uint16_t id; /* unique triangle ID */
char symbol_names[3][SYMBOL_NAME_LEN]; /* trading pair names for each leg */
char fee_currency[3][CURRENCY_NAME_LEN]; /* fee currency per leg */
char base[CURRENCY_NAME_LEN]; /* base currency of the triangle */
char mid[CURRENCY_NAME_LEN]; /* intermediate currency */
char quote[CURRENCY_NAME_LEN]; /* quote currency of the triangle */
} triangle_t;
/* Index entry into the per-currency triangle lookup table */
typedef struct {
uint32_t offset; /* start index in the flat triangle array */
uint32_t count; /* number of triangles for this currency */
} tri_index_entry_t;
/* Complete set of enumerated triangles with fast per-currency indexing */
typedef struct {
triangle_t *triangles; /* contiguous array of all triangles */
uint32_t triangle_count; /* total number of triangles */
tri_index_entry_t *tri_index; /* per-currency index into tri_flat */
uint32_t *tri_flat; /* flat array mapping currency->triangle indices */
} triangle_set_t;
/* Fee rate entry for a specific currency (from fee table) */
typedef struct {
char currency[CURRENCY_NAME_LEN]; /* currency name */
double taker_fee; /* taker fee rate for this currency */
double maker_fee; /* maker fee rate for this currency */
} fee_entry_t;
/* Signal types — moved here from queue.h */
/* Single price+size level in an order book snapshot */
typedef struct {
double price;
double size;
} book_level_t;
/* Snapshot of one leg's order book included in a signal */
typedef struct {
char symbol[SYMBOL_NAME_LEN];
int64_t ts_ms;
book_level_t bids[MAX_BOOK_LEVELS];
book_level_t asks[MAX_BOOK_LEVELS];
uint8_t bid_count;
uint8_t ask_count;
} signal_book_t;
/* One leg of a triangular arbitrage signal */
typedef struct {
char symbol[SYMBOL_NAME_LEN];
char input_currency[CURRENCY_NAME_LEN];
char output_currency[CURRENCY_NAME_LEN];
char fee_currency[CURRENCY_NAME_LEN];
double fee_rate;
double exchange_rate;
char side[5];
char order_param[32];
double quote_volume;
double base_increment;
double quote_increment;
double funds_increment;
double base_min_size;
} signal_leg_t;
/* Collection of up to 3 legs comprising a triangular signal */
typedef struct {
uint8_t leg_count;
signal_leg_t legs[3];
} signal_legs_t;
/* Entry describing one triangular arbitrage opportunity */
typedef struct {
char triangle_key[CURRENCY_NAME_LEN * 3 + 4];
char primary_quote[CURRENCY_NAME_LEN];
double predicted_bps;
char max_volume[32];
double starting_volume;
bool live;
int64_t ts_ms;
int64_t book_ts_ms;
int64_t t_sock_arrive_ms;
int64_t t_arrive_ms;
int64_t t_eval_ms;
uint8_t book_count;
signal_book_t books[3];
signal_legs_t legs;
} signal_entry_t;
/* Initialise triangle set: enumerate all triangles from the symbol table */
int triangle_set_init(triangle_set_t *set, const symbol_table_t *symbols,
const config_t *cfg);
/* Free all memory owned by a triangle set */
void triangle_set_free(triangle_set_t *set);
#endif