Skip to content

Commit dd22651

Browse files
committed
filter out quoters far from median
1 parent 14a7742 commit dd22651

File tree

13 files changed

+116
-19
lines changed

13 files changed

+116
-19
lines changed

CMakeLists.txt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ set( CMAKE_INCLUDE_CURRENT_DIR ON )
1515
include_directories( program/src/ )
1616

1717
# gcc compiler/linker flags
18-
add_compile_options( -ggdb -Wall -Wextra -Werror -std=c++11 -m64 )
18+
add_compile_options( -ggdb -Wall -Wextra -Werror -m64 )
19+
set( CMAKE_CXX_FLAGS -std=c++11 )
1920
set( CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -pthread")
2021

2122
#
@@ -37,6 +38,7 @@ set( PC_SRC
3738
pc/request.cpp;
3839
pc/rpc_client.cpp;
3940
pc/user.cpp;
41+
program/src/oracle/sort.c
4042
)
4143

4244
set( PC_HDR

program/src/oracle/sort.c

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
#include "sort.h"
2+
3+
static inline void swap_int64( int64_t *a, int64_t *b )
4+
{
5+
int64_t const temp = *a;
6+
*a = *b;
7+
*b = temp;
8+
}
9+
10+
static inline int partition_int64( int64_t* v, int i, int j )
11+
{
12+
int const p = i;
13+
int64_t const pv = v[ p ];
14+
15+
while ( i < j ) {
16+
while ( v[ i ] <= pv && i <= j ) {
17+
++i;
18+
}
19+
while ( v[ j ] > pv ) {
20+
--j;
21+
}
22+
if ( i < j ) {
23+
swap_int64( &v[ i ], &v[ j ] );
24+
}
25+
}
26+
swap_int64( &v[ p ], &v[ j ] );
27+
return j;
28+
}
29+
30+
static void qsort_help_int64( int64_t* const v, int i, int j )
31+
{
32+
if ( i >= j ) {
33+
return;
34+
}
35+
int p = partition_int64( v, i, j );
36+
qsort_help_int64( v, i, p - 1 );
37+
qsort_help_int64( v, p + 1, j );
38+
}
39+
40+
void qsort_int64( int64_t* const v, unsigned const size )
41+
{
42+
qsort_help_int64( v, 0, ( int )size - 1 );
43+
}
44+

program/src/oracle/sort.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#pragma once
2+
3+
#ifdef __x86_64__
4+
#include <stdint.h>
5+
#else
6+
#include <solana_sdk.h>
7+
#endif
8+
9+
#ifdef __cplusplus
10+
extern "C" {
11+
#endif
12+
13+
void qsort_int64( int64_t* v, unsigned size );
14+
15+
#ifdef __cplusplus
16+
}
17+
#endif
18+

program/src/oracle/test_oracle.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
char heap_start[8192];
22
#define PC_HEAP_START (heap_start)
33
#include "oracle.c"
4+
#include "sort.c"
45
#include <criterion/criterion.h>
56

67
Test(oracle, init_mapping) {

program/src/oracle/upd_aggregate.h

Lines changed: 42 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
#pragma once
22

3+
#include "sort.h"
4+
5+
#include <limits.h>
6+
37
#ifdef __cplusplus
48
extern "C" {
59
#endif
@@ -294,31 +298,59 @@ static void upd_aggregate( pc_price_t *ptr, uint64_t slot )
294298
ptr->valid_slot_ = ptr->agg_.pub_slot_;// valid slot-time of agg. price
295299
ptr->agg_.pub_slot_ = slot; // publish slot-time of agg. price
296300

301+
uint32_t numv = 0;
302+
uint32_t vidx[ PC_COMP_SIZE ];
297303
// identify valid quotes and order them by price
298-
uint32_t numa = 0 ;
299-
uint32_t aidx[PC_COMP_SIZE];
300-
for( uint32_t i=0; i != ptr->num_; ++i ) {
304+
for ( uint32_t i = 0; i != ptr->num_; ++i ) {
301305
pc_price_comp_t *iptr = &ptr->comp_[i];
302306
// copy contributing price to aggregate snapshot
303307
iptr->agg_ = iptr->latest_;
304308
// add quote to sorted permutation array if it is valid
305309
int64_t slot_diff = ( int64_t )slot - ( int64_t )( iptr->agg_.pub_slot_ );
306310
if ( iptr->agg_.status_ == PC_STATUS_TRADING &&
307311
iptr->agg_.conf_ != 0UL &&
308-
iptr->agg_.price_ != 0L &&
312+
iptr->agg_.price_ > 0L &&
309313
slot_diff >= 0 && slot_diff <= PC_MAX_SEND_LATENCY ) {
310-
int64_t ipx = iptr->agg_.price_;
311-
uint32_t j = numa++;
312-
for( ; j > 0 && ptr->comp_[aidx[j-1]].agg_.price_ > ipx; --j ) {
313-
aidx[j] = aidx[j-1];
314+
vidx[numv++] = i;
315+
}
316+
}
317+
318+
uint32_t nprcs = 0;
319+
int64_t prcs[ PC_COMP_SIZE * 2 ];
320+
for ( uint32_t i = 0; i < numv; ++i ) {
321+
pc_price_comp_t const *iptr = &ptr->comp_[ vidx[ i ] ];
322+
int64_t const price = iptr->agg_.price_;
323+
int64_t const conf = ( int64_t )( iptr->agg_.conf_ );
324+
prcs[ nprcs++ ] = price - conf;
325+
prcs[ nprcs++ ] = price + conf;
326+
}
327+
qsort_int64( prcs, nprcs );
328+
329+
uint32_t numa = 0;
330+
uint32_t aidx[PC_COMP_SIZE];
331+
332+
if ( nprcs ) {
333+
int64_t const mprc = ( prcs[ numv - 1 ] + prcs[ numv ] ) / 2;
334+
int64_t const lb = mprc / 5;
335+
int64_t const ub = ( mprc > LONG_MAX / 5 ) ? LONG_MAX : mprc * 5;
336+
337+
for ( uint32_t i = 0; i < numv; ++i ) {
338+
uint32_t const idx = vidx[ i ];
339+
pc_price_comp_t const *iptr = &ptr->comp_[ idx ];
340+
int64_t const prc = iptr->agg_.price_;
341+
if ( prc >= lb && prc <= ub ) {
342+
uint32_t j = numa++;
343+
for( ; j > 0 && ptr->comp_[ aidx[ j - 1 ] ].agg_.price_ > prc; --j ) {
344+
aidx[ j ] = aidx[ j - 1 ];
345+
}
346+
aidx[ j ] = idx;
314347
}
315-
aidx[j] = i;
316348
}
317349
}
318350

319351
// zero quoters
320352
ptr->num_qt_ = numa;
321-
if ( numa == 0 ) {
353+
if ( numa == 0 || numa * 2 <= numv ) {
322354
ptr->agg_.status_ = PC_STATUS_UNKNOWN;
323355
upd_twap( ptr, agg_diff, qs );
324356
return;

pyth/tests/qset/11.result

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
{"exponent":-3,"price":500501440000,"conf":499530000,"status":"trading"}
1+
{"exponent":-3,"price":500502460000,"conf":498770000,"status":"trading"}

pyth/tests/qset/2.result

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
{"exponent":-3,"price":-10000,"conf":1000,"status":"trading"}
1+
{"exponent":-3,"price":0,"conf":0,"status":"unknown"}

pyth/tests/qset/31.result

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
{"exponent":-8,"price":4329782800000,"conf":2616650000,"status":"trading"}
1+
{"exponent":-8,"price":4329847900000,"conf":3031550000,"status":"trading"}

pyth/tests/qset/33.result

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
{"exponent":-8,"price":4329782800000,"conf":2616650000,"status":"trading"}
1+
{"exponent":-8,"price":4329847900000,"conf":3031550000,"status":"trading"}

pyth/tests/qset/35.result

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
{"exponent":-8,"price":40000000000,"conf":1698517500000,"status":"trading"}
1+
{"exponent":-8,"price":4000200000000,"conf":1000000000,"status":"trading"}

pyth/tests/qset/36.result

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
{"exponent":-8,"price":1001000060,"conf":1000000,"status":"trading"}
1+
{"exponent":-8,"price":0,"conf":0,"status":"unknown"}

pyth/tests/qset/37.result

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
{"exponent":-8,"price":400000000000000,"conf":169854770000000,"status":"trading"}
1+
{"exponent":-8,"price":4000200000000,"conf":1000000000,"status":"trading"}

pyth/tests/qset/39.result

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
{"exponent":-8,"price":115225655,"conf":94337828,"status":"trading"}
1+
{"exponent":-8,"price":0,"conf":0,"status":"unknown"}

0 commit comments

Comments
 (0)