Rename amount to quantity

More idiomatic naming for the order quantities.
This commit is contained in:
Tim Kächele 2023-10-29 00:03:25 +02:00
parent 2b9c82c4f6
commit 5862368498
6 changed files with 34 additions and 34 deletions

View File

@ -7,15 +7,15 @@ module Rex
return trades if highest_buy_order.nil? || lowest_sell_order.nil?
while highest_buy_order.price >= lowest_sell_order.price
max_amount = [highest_buy_order.remaining_amount, lowest_sell_order.remaining_amount].min
highest_buy_order.remaining_amount -= max_amount
lowest_sell_order.remaining_amount -= max_amount
max_quantity = [highest_buy_order.remaining_quantity, lowest_sell_order.remaining_quantity].min
highest_buy_order.remaining_quantity -= max_quantity
lowest_sell_order.remaining_quantity -= max_quantity
trades << Trade.new(
id: order_book.next_trade_id,
buy_order: highest_buy_order,
sell_order: lowest_sell_order,
amount: max_amount,
quantity: max_quantity,
price: lowest_sell_order.price
)

View File

@ -5,10 +5,10 @@ module Rex
:is_buy,
:price,
:user_id,
:amount,
:quantity,
:previous_order,
:next_order,
:remaining_amount
:remaining_quantity
)
def initialize(attrs = {})
@ -17,15 +17,15 @@ module Rex
@user_id = attrs[:user_id]
@price = attrs[:price]
@is_buy = attrs[:is_buy]
@amount = attrs[:amount]
@remaining_amount = attrs[:amount]
@quantity = attrs[:quantity]
@remaining_quantity = attrs[:quantity]
@next_order = nil
@previous_order = nil
end
def filled?
remaining_amount == 0
remaining_quantity == 0
end
end
end

View File

@ -4,7 +4,7 @@ module Rex
:id,
:buy_order,
:sell_order,
:amount,
:quantity,
:price
)
@ -12,7 +12,7 @@ module Rex
@id = attributes[:id]
@buy_order = attributes[:buy_order]
@sell_order = attributes[:sell_order]
@amount = attributes[:amount]
@quantity = attributes[:quantity]
@price = attributes[:price]
end
end

View File

@ -6,7 +6,7 @@ FactoryBot.define do
sequence(:user_id) { |i| i }
sequence(:is_buy) { |i| (i % 2).zero? }
sequence(:price) { 100 }
sequence(:amount) { 20 }
remaining_amount { amount }
sequence(:quantity) { 20 }
remaining_quantity { quantity }
end
end

View File

@ -5,9 +5,9 @@ RSpec.describe Rex::Matcher do
describe "#match" do
let(:order_book) { Rex::OrderBook.new }
let(:buy_order) { build(:order, price: 100, is_buy: true, amount: 100, remaining_amount: 100) }
let(:cheaper_sell_order) { build(:order, price: 99, is_buy: false, amount: 50, remaining_amount: 50) }
let(:pricier_sell_order) { build(:order, price: 100, is_buy: false, amount: 70, remaining_amount: 70) }
let(:buy_order) { build(:order, price: 100, is_buy: true, quantity: 100, remaining_quantity: 100) }
let(:cheaper_sell_order) { build(:order, price: 99, is_buy: false, quantity: 50, remaining_quantity: 50) }
let(:pricier_sell_order) { build(:order, price: 100, is_buy: false, quantity: 70, remaining_quantity: 70) }
context "when order book has unmatched orders" do
before do
@ -25,13 +25,13 @@ RSpec.describe Rex::Matcher do
expect(trades[0].buy_order).to eq(buy_order)
expect(trades[0].sell_order).to eq(cheaper_sell_order)
expect(trades[0].price).to eq(99)
expect(trades[0].amount).to eq(50)
expect(trades[0].quantity).to eq(50)
expect(trades[1].id).to eq(2)
expect(trades[1].buy_order).to eq(buy_order)
expect(trades[1].sell_order).to eq(pricier_sell_order)
expect(trades[1].price).to eq(100)
expect(trades[1].amount).to eq(50)
expect(trades[1].quantity).to eq(50)
end
it "removes filled orders from the order book" do
@ -39,7 +39,7 @@ RSpec.describe Rex::Matcher do
expect(order_book.highest_buy_order).to eq(nil)
expect(order_book.lowest_sell_order).to eq(pricier_sell_order)
expect(order_book.lowest_sell_order.remaining_amount).to eq(20)
expect(order_book.lowest_sell_order.remaining_quantity).to eq(20)
end
end
@ -52,14 +52,14 @@ RSpec.describe Rex::Matcher do
# https://stackoverflow.com/a/18524231/3200224
# Testing a common example with a proven to be correct solution
context "stack overflow scenario" do
let(:order_1) { build(:order, price: 2030, is_buy: false, amount: 100) }
let(:order_2) { build(:order, price: 2025, is_buy: false, amount: 100) }
let(:order_3) { build(:order, price: 2030, is_buy: false, amount: 200) }
let(:order_4) { build(:order, price: 2015, is_buy: true, amount: 100) }
let(:order_5) { build(:order, price: 2020, is_buy: true, amount: 200) }
let(:order_6) { build(:order, price: 2015, is_buy: true, amount: 200) }
let(:order_1) { build(:order, price: 2030, is_buy: false, quantity: 100) }
let(:order_2) { build(:order, price: 2025, is_buy: false, quantity: 100) }
let(:order_3) { build(:order, price: 2030, is_buy: false, quantity: 200) }
let(:order_4) { build(:order, price: 2015, is_buy: true, quantity: 100) }
let(:order_5) { build(:order, price: 2020, is_buy: true, quantity: 200) }
let(:order_6) { build(:order, price: 2015, is_buy: true, quantity: 200) }
let(:crossing_order) { build(:order, is_buy: true, amount: 250, price: 2035) }
let(:crossing_order) { build(:order, is_buy: true, quantity: 250, price: 2035) }
before do
order_book.add_order(order_1)
@ -79,19 +79,19 @@ RSpec.describe Rex::Matcher do
expect(trades[0].id).to eq(1)
expect(trades[0].price).to eq(2025)
expect(trades[0].amount).to eq(100)
expect(trades[0].quantity).to eq(100)
expect(trades[0].buy_order).to eq(crossing_order)
expect(trades[0].sell_order).to eq(order_2)
expect(trades[1].id).to eq(2)
expect(trades[1].price).to eq(2030)
expect(trades[1].amount).to eq(100)
expect(trades[1].quantity).to eq(100)
expect(trades[1].buy_order).to eq(crossing_order)
expect(trades[1].sell_order).to eq(order_1)
expect(trades[2].id).to eq(3)
expect(trades[2].price).to eq(2030)
expect(trades[2].amount).to eq(50)
expect(trades[2].quantity).to eq(50)
expect(trades[2].buy_order).to eq(crossing_order)
expect(trades[2].sell_order).to eq(order_3)
end

View File

@ -4,20 +4,20 @@ RSpec.describe Rex::Order do
describe "#filled?" do
let(:order) do
instance = build(:order)
instance.remaining_amount = remaining_amount
instance.remaining_quantity = remaining_quantity
instance
end
subject { order.filled? }
context "when remaining amount is not zero" do
let(:remaining_amount) { 50 }
context "when remaining quantity is not zero" do
let(:remaining_quantity) { 50 }
it { is_expected.to be(false) }
end
context "when remaining amount is zero" do
let(:remaining_amount) { 0 }
context "when remaining quantity is zero" do
let(:remaining_quantity) { 0 }
it { is_expected.to be(true) }
end