Skip to content

Commit 838534e

Browse files
authored
Merge pull request #77 from f3rno/feature-order-callbacks
Feature: Order Callbacks & Cancel Order
2 parents f0604e9 + c69ebfe commit 838534e

File tree

3 files changed

+269
-27
lines changed

3 files changed

+269
-27
lines changed

README.md

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ client = Bitfinex::RESTv2.new({
3939
Then use it to submit queries, i.e. `client.balances`
4040

4141
## Usage of WSv2
42-
4342
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:
4443

4544
```ruby
@@ -81,6 +80,33 @@ end
8180
client.open!
8281
```
8382

83+
### Order Manipulation
84+
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:
85+
86+
```ruby
87+
o = Bitfinex::Models::Order.new({
88+
:type => 'EXCHANGE LIMIT',
89+
:price => 3.0152235,
90+
:amount => 2.0235235263262,
91+
:symbol => 'tEOSUSD'
92+
})
93+
94+
client.submit_order(o) do |order_packet|
95+
p "recv order confirmation packet with ID #{order_packet.id}"
96+
97+
client.update_order({
98+
:id => order_packet.id,
99+
:price => '3.0'
100+
}) do |update_packet|
101+
p "updated order #{update_packet.id} with price #{update_packet.price}"
102+
103+
client.cancel_order(order_packet) do |canceled_order|
104+
p "canceled order with ID #{canceled_order[0]}"
105+
end
106+
end
107+
end
108+
```
109+
84110
### Available Events
85111
#### Lifecycle Events
86112
* `:open`

examples/ws/v2/orders.rb

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
client = Bitfinex::WSv2.new({
44
:url => ENV['WS_URL'],
55
:api_key => ENV['API_KEY'],
6-
:api_secret => ENV['API_SECRET']
6+
:api_secret => ENV['API_SECRET'],
7+
:transform => true
78
})
89

910
client.on(:open) do
@@ -20,11 +21,24 @@
2021
:symbol => 'tEOSUSD'
2122
})
2223

23-
client.submit_order(o)
24+
client.submit_order(o) do |order_packet|
25+
p "recv order confirmation packet with ID #{order_packet.id}"
26+
27+
client.update_order({
28+
:id => order_packet.id,
29+
:price => '3.0'
30+
}) do |update_packet|
31+
p "updated order #{update_packet.id} with price #{update_packet.price}"
32+
33+
client.cancel_order(order_packet) do |canceled_order|
34+
p "canceled order with ID #{canceled_order[0]}"
35+
end
36+
end
37+
end
2438
end
2539

2640
client.on(:notification) do |n|
27-
p 'received notification: %s' % [n]
41+
p 'received notification: %s' % [n.serialize.join('|')]
2842
end
2943

3044
client.on(:order_new) do |msg|

0 commit comments

Comments
 (0)