Skip to content

Commit

Permalink
Merge pull request #77 from f3rno/feature-order-callbacks
Browse files Browse the repository at this point in the history
Feature: Order Callbacks & Cancel Order
  • Loading branch information
prdn authored Nov 23, 2018
2 parents f0604e9 + c69ebfe commit 838534e
Show file tree
Hide file tree
Showing 3 changed files with 269 additions and 27 deletions.
28 changes: 27 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ client = Bitfinex::RESTv2.new({
Then use it to submit queries, i.e. `client.balances`

## Usage of WSv2

To use version 2 of the WS API, construct a new client with your credentials, bind listeners to react to stream events, and open the connection:

```ruby
Expand Down Expand Up @@ -81,6 +80,33 @@ end
client.open!
```

### Order Manipulation
Three methods are provided for dealing with orders: `submit_order`, `update_order` and `cancel_order`. All methods support callback blocks, which are triggered upon receiving the relevant confirmation notifications. Example:

```ruby
o = Bitfinex::Models::Order.new({
:type => 'EXCHANGE LIMIT',
:price => 3.0152235,
:amount => 2.0235235263262,
:symbol => 'tEOSUSD'
})

client.submit_order(o) do |order_packet|
p "recv order confirmation packet with ID #{order_packet.id}"

client.update_order({
:id => order_packet.id,
:price => '3.0'
}) do |update_packet|
p "updated order #{update_packet.id} with price #{update_packet.price}"

client.cancel_order(order_packet) do |canceled_order|
p "canceled order with ID #{canceled_order[0]}"
end
end
end
```

### Available Events
#### Lifecycle Events
* `:open`
Expand Down
20 changes: 17 additions & 3 deletions examples/ws/v2/orders.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
client = Bitfinex::WSv2.new({
:url => ENV['WS_URL'],
:api_key => ENV['API_KEY'],
:api_secret => ENV['API_SECRET']
:api_secret => ENV['API_SECRET'],
:transform => true
})

client.on(:open) do
Expand All @@ -20,11 +21,24 @@
:symbol => 'tEOSUSD'
})

client.submit_order(o)
client.submit_order(o) do |order_packet|
p "recv order confirmation packet with ID #{order_packet.id}"

client.update_order({
:id => order_packet.id,
:price => '3.0'
}) do |update_packet|
p "updated order #{update_packet.id} with price #{update_packet.price}"

client.cancel_order(order_packet) do |canceled_order|
p "canceled order with ID #{canceled_order[0]}"
end
end
end
end

client.on(:notification) do |n|
p 'received notification: %s' % [n]
p 'received notification: %s' % [n.serialize.join('|')]
end

client.on(:order_new) do |msg|
Expand Down
Loading

0 comments on commit 838534e

Please sign in to comment.