|
1 | 1 | #pragma once
|
2 | 2 |
|
| 3 | +#include "sort.h" |
| 4 | + |
| 5 | +#include <limits.h> |
| 6 | + |
3 | 7 | #ifdef __cplusplus
|
4 | 8 | extern "C" {
|
5 | 9 | #endif
|
@@ -294,31 +298,59 @@ static void upd_aggregate( pc_price_t *ptr, uint64_t slot )
|
294 | 298 | ptr->valid_slot_ = ptr->agg_.pub_slot_;// valid slot-time of agg. price
|
295 | 299 | ptr->agg_.pub_slot_ = slot; // publish slot-time of agg. price
|
296 | 300 |
|
| 301 | + uint32_t numv = 0; |
| 302 | + uint32_t vidx[ PC_COMP_SIZE ]; |
297 | 303 | // 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 ) { |
301 | 305 | pc_price_comp_t *iptr = &ptr->comp_[i];
|
302 | 306 | // copy contributing price to aggregate snapshot
|
303 | 307 | iptr->agg_ = iptr->latest_;
|
304 | 308 | // add quote to sorted permutation array if it is valid
|
305 | 309 | int64_t slot_diff = ( int64_t )slot - ( int64_t )( iptr->agg_.pub_slot_ );
|
306 | 310 | if ( iptr->agg_.status_ == PC_STATUS_TRADING &&
|
307 | 311 | iptr->agg_.conf_ != 0UL &&
|
308 |
| - iptr->agg_.price_ != 0L && |
| 312 | + iptr->agg_.price_ > 0L && |
309 | 313 | 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; |
314 | 347 | }
|
315 |
| - aidx[j] = i; |
316 | 348 | }
|
317 | 349 | }
|
318 | 350 |
|
319 | 351 | // zero quoters
|
320 | 352 | ptr->num_qt_ = numa;
|
321 |
| - if ( numa == 0 ) { |
| 353 | + if ( numa == 0 || numa * 2 <= numv ) { |
322 | 354 | ptr->agg_.status_ = PC_STATUS_UNKNOWN;
|
323 | 355 | upd_twap( ptr, agg_diff, qs );
|
324 | 356 | return;
|
|
0 commit comments