bool get_next_bid() { try { if( _current_bid && _current_bid->get_quantity().amount > 0 ) return _current_bid.valid(); ++_orders_filled; _current_bid.reset(); if( _bid_itr.valid() ) { auto bid = market_order( bid_order, _bid_itr.key(), _bid_itr.value() ); if( bid.get_price().quote_asset_id == _quote_id && bid.get_price().base_asset_id == _base_id ) { if( bid.get_price() < _market_stat.center_price && get_next_short() ) { return _current_bid.valid(); } _current_bid = bid; --_bid_itr; return _current_bid.valid(); } } get_next_short(); return _current_bid.valid(); } FC_CAPTURE_AND_RETHROW() }
bool get_next_bid() { try { if( _current_bid && _current_bid->get_quantity().amount > 0 ) return _current_bid.valid(); _current_bid.reset(); if( _bid_itr.valid() ) { auto bid = market_order( bid_order, _bid_itr.key(), _bid_itr.value() ); if( bid.get_price().quote_asset_id == _quote_id && bid.get_price().base_asset_id == _base_id ) { _current_bid = bid; } } if( _short_itr.valid() ) { auto bid = market_order( short_order, _short_itr.key(), _short_itr.value() ); wlog( "SHORT ITER VALID: ${o}", ("o",bid) ); if( bid.get_price().quote_asset_id == _quote_id && bid.get_price().base_asset_id == _base_id ) { if( !_current_bid || _current_bid->get_price() < bid.get_price() ) { --_short_itr; _current_bid = bid; ilog( "returning ${v}", ("v",_current_bid.valid() ) ); return _current_bid.valid(); } } } else { wlog( " No Shorts **** " ); } if( _bid_itr.valid() ) --_bid_itr; return _current_bid.valid(); } FC_CAPTURE_AND_RETHROW() }
bool get_next_short() { if( _short_itr.valid() ) { auto bid = market_order( short_order, _short_itr.key(), _short_itr.value() ); if( bid.get_price().quote_asset_id == _quote_id && bid.get_price().base_asset_id == _base_id ) { ++_short_itr; _current_bid = bid; return _current_bid.valid(); } } return false; }
void cancel_all_shorts() { for( auto short_itr = _db_impl._short_db.begin(); short_itr.valid(); ++short_itr ) { const market_index_key market_idx = short_itr.key(); const order_record order_rec = short_itr.value(); _current_bid = market_order( short_order, market_idx, order_rec ); // Initialize the market transaction market_transaction mtrx; mtrx.bid_owner = _current_bid->get_owner(); mtrx.bid_type = short_order; cancel_current_short( mtrx, market_idx.order_price.quote_asset_id ); push_market_transaction( mtrx ); } }
void market_engine_v7::cancel_high_apr_shorts() { static const fc::uint128 max_apr = fc::uint128( BTS_BLOCKCHAIN_MAX_SHORT_APR_PCT ) * FC_REAL128_PRECISION / 100; for( auto short_itr = _db_impl._short_db.begin(); short_itr.valid(); ++short_itr ) { const market_index_key market_idx = short_itr.key(); if( market_idx.order_price.ratio <= max_apr ) continue; const order_record order_rec = short_itr.value(); _current_bid = market_order( short_order, market_idx, order_rec, order_rec.balance, market_idx.order_price ); // Initialize the market transaction market_transaction mtrx; mtrx.bid_index.owner = _current_bid->get_owner(); mtrx.bid_type = short_order; cancel_current_short( mtrx, market_idx.order_price.quote_asset_id ); push_market_transaction( mtrx ); } _pending_state->apply_changes(); }
bool get_next_ask() { try { if( _current_ask && _current_ask->state.balance > 0 ) { wlog( "current ask" ); return _current_ask.valid(); } _current_ask.reset(); /** * Margin calls take priority over all other ask orders */ while( _current_bid && _collateral_itr.valid() ) { auto cover_ask = market_order( cover_order, _collateral_itr.key(), order_record(_collateral_itr.value().payoff_balance), _collateral_itr.value().collateral_balance ); if( cover_ask.get_price().quote_asset_id == _quote_id && cover_ask.get_price().base_asset_id == _base_id ) { if( _current_bid->get_price() < cover_ask.get_highest_cover_price() ) { // cover position has been blown out, current bid is not able to // cover the position, so it will sit until the price recovers // enough to fill it. // // The idea here is that the longs have agreed to a maximum // protection equal to the collateral. If they would like to // sell their USD for XTS this is the best price the short is // obligated to offer. FC_CAPTURE_AND_THROW( insufficient_collateral, (_current_bid)(cover_ask)(cover_ask.get_highest_cover_price())); --_collateral_itr; continue; } // max bid must be greater than call price if( _current_bid->get_price() < cover_ask.get_price() ) { // if( _current_ask->get_price() > cover_ask.get_price() ) { _current_ask = cover_ask; _current_payoff_balance = _collateral_itr.value().payoff_balance; --_collateral_itr; return _current_ask.valid(); } } } break; } if( _ask_itr.valid() ) { auto ask = market_order( ask_order, _ask_itr.key(), _ask_itr.value() ); wlog( "ASK ITER VALID: ${o}", ("o",ask) ); if( ask.get_price().quote_asset_id == _quote_id && ask.get_price().base_asset_id == _base_id ) { _current_ask = ask; } ++_ask_itr; return true; } return _current_ask.valid(); } FC_CAPTURE_AND_RETHROW() }