FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
Bitmex.NET/Bitmex.NET.Example/MainWindowViewModel.cs at master · semashkinvg/Bitmex.NET · GitHub
semashkinvg
/
Bitmex.NET
Public
Notifications
You must be signed in to change notification settings
Fork
24
Star
50
Code
Issues
6
Pull requests
9
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
Bitmex.NET
/
Bitmex.NET.Example
/
MainWindowViewModel.cs
Copy path
More file actions
More file actions
Latest commit
History
History
History
270 lines (241 loc) · 10.6 KB
Breadcrumbs
Bitmex.NET
/
Bitmex.NET.Example
/
MainWindowViewModel.cs
Copy path
File metadata and controls
270 lines (241 loc) · 10.6 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
using
AutoMapper
;
using
Bitmex
.
NET
.
Dtos
;
using
Bitmex
.
NET
.
Dtos
.
Socket
;
using
Bitmex
.
NET
.
Example
.
Annotations
;
using
Bitmex
.
NET
.
Example
.
Models
;
using
Bitmex
.
NET
.
Models
;
using
Prism
.
Commands
;
using
System
;
using
System
.
Collections
.
Generic
;
using
System
.
Collections
.
ObjectModel
;
using
System
.
ComponentModel
;
using
System
.
Linq
;
using
System
.
Runtime
.
CompilerServices
;
using
System
.
Threading
.
Tasks
;
using
System
.
Windows
;
using
System
.
Windows
.
Data
;
using
System
.
Windows
.
Input
;
namespace
Bitmex
.
NET
.
Example
{
public
class
MainWindowViewModel
:
INotifyPropertyChanged
{
private
readonly
IBitmexAuthorization
_bitmexAuthorization
;
private
readonly
IBitmexApiSocketService
_bitmexApiSocketService
;
private
readonly
object
_syncObj
=
new
object
(
)
;
private
readonly
object
_syncObjOrders
=
new
object
(
)
;
private
readonly
object
_syncObjOrderBook10
=
new
object
(
)
;
private
readonly
object
_syncObjOrderBookL2
=
new
object
(
)
;
private
int
_size
;
private
string
_key
;
private
string
_secret
;
private
bool
_isConnected
;
public
ObservableCollection
<
InstrumentModel
>
Instruments
{
get
;
}
public
ObservableCollection
<
OrderUpdateModel
>
OrderUpdates
{
get
;
}
public
ObservableCollection
<
OrderBookModel
>
OrderBookL2
{
get
;
}
public
List
<
OrderBookModel
>
OrderBook10
{
get
;
private
set
;
}
public
string
Secret
{
get
{
return
_secret
;
}
set
{
_secret
=
value
;
OnPropertyChanged
(
)
;
_bitmexAuthorization
.
Secret
=
_secret
;
StartLoadSymbolsCmd
.
RaiseCanExecuteChanged
(
)
;
}
}
public
string
Key
{
get
{
return
_key
;
}
set
{
_key
=
value
;
OnPropertyChanged
(
)
;
_bitmexAuthorization
.
Key
=
_key
;
StartLoadSymbolsCmd
.
RaiseCanExecuteChanged
(
)
;
}
}
public
int
Size
{
get
{
return
_size
;
}
set
{
_size
=
value
;
OnPropertyChanged
(
)
;
}
}
public
bool
IsConnected
{
get
{
return
_isConnected
;
}
set
{
_isConnected
=
value
;
OnPropertyChanged
(
)
;
OnPropertyChanged
(
nameof
(
IsNotConnected
)
)
;
}
}
public
bool
IsNotConnected
=>
!
IsConnected
;
public
ICommand
BuyCmd
{
get
;
}
public
ICommand
SellCmd
{
get
;
}
public
DelegateCommand
StartLoadSymbolsCmd
{
get
;
}
public
event
PropertyChangedEventHandler
PropertyChanged
;
public
MainWindowViewModel
(
)
{
_bitmexAuthorization
=
new
BitmexAuthorization
{
BitmexEnvironment
=
BitmexEnvironment
.
Test
}
;
_bitmexApiSocketService
=
BitmexApiSocketService
.
CreateDefaultApi
(
_bitmexAuthorization
)
;
BuyCmd
=
new
DelegateCommand
(
Buy
)
;
SellCmd
=
new
DelegateCommand
(
Sell
)
;
StartLoadSymbolsCmd
=
new
DelegateCommand
(
StartLoad
,
CanStart
)
;
Size
=
1
;
Instruments
=
new
ObservableCollection
<
InstrumentModel
>
(
)
;
OrderUpdates
=
new
ObservableCollection
<
OrderUpdateModel
>
(
)
;
OrderBookL2
=
new
ObservableCollection
<
OrderBookModel
>
(
)
;
BindingOperations
.
EnableCollectionSynchronization
(
Instruments
,
_syncObj
)
;
BindingOperations
.
EnableCollectionSynchronization
(
OrderUpdates
,
_syncObjOrders
)
;
BindingOperations
.
EnableCollectionSynchronization
(
OrderBookL2
,
_syncObjOrderBookL2
)
;
}
private
bool
CanStart
(
)
{
return
!
string
.
IsNullOrWhiteSpace
(
Key
)
&&
!
string
.
IsNullOrWhiteSpace
(
Secret
)
;
}
private
void
StartLoad
(
)
{
try
{
IsConnected
=
_bitmexApiSocketService
.
Connect
(
)
;
}
catch
(
Exception
e
)
{
MessageBox
.
Show
(
e
.
Message
)
;
}
if
(
IsConnected
)
{
_bitmexApiSocketService
.
Subscribe
(
BitmetSocketSubscriptions
.
CreateInstrumentSubsription
(
message
=>
{
foreach
(
var
instrumentDto
in
message
.
Data
)
{
lock
(
_syncObj
)
{
var
existing
=
Instruments
.
FirstOrDefault
(
a
=>
a
.
Symbol
==
instrumentDto
.
Symbol
)
;
if
(
existing
!=
null
&&
message
.
Action
==
BitmexActions
.
Update
)
{
Mapper
.
Map
<
InstrumentDto
,
InstrumentModel
>
(
instrumentDto
,
existing
)
;
}
else
if
(
message
.
Action
!=
BitmexActions
.
Partial
&&
message
.
Action
!=
BitmexActions
.
Delete
)
{
Instruments
.
Add
(
Mapper
.
Map
<
InstrumentDto
,
InstrumentModel
>
(
instrumentDto
)
)
;
}
}
}
}
)
)
;
_bitmexApiSocketService
.
Subscribe
(
BitmetSocketSubscriptions
.
CreateOrderSubsription
(
message
=>
{
foreach
(
var
order
in
message
.
Data
)
{
lock
(
_syncObjOrders
)
{
var
existing
=
OrderUpdates
.
FirstOrDefault
(
a
=>
a
.
OrderId
==
order
.
OrderId
)
;
if
(
existing
!=
null
&&
message
.
Action
==
BitmexActions
.
Update
)
{
Mapper
.
Map
<
OrderDto
,
OrderUpdateModel
>
(
order
,
existing
)
;
}
else
if
(
message
.
Action
!=
BitmexActions
.
Partial
&&
message
.
Action
!=
BitmexActions
.
Delete
)
{
OrderUpdates
.
Add
(
Mapper
.
Map
<
OrderDto
,
OrderUpdateModel
>
(
order
)
)
;
}
}
OnPropertyChanged
(
nameof
(
OrderUpdates
)
)
;
}
}
)
)
;
_bitmexApiSocketService
.
Subscribe
(
BitmetSocketSubscriptions
.
CreateOrderBook10Subsription
(
message
=>
{
foreach
(
var
dto
in
message
.
Data
)
{
if
(
dto
.
Symbol
!=
"XBTUSD"
)
{
continue
;
}
lock
(
_syncObjOrderBook10
)
{
OrderBook10
=
dto
.
Asks
.
Select
(
a
=>
new
OrderBookModel
{
Direction
=
"Sell"
,
Price
=
a
[
0
]
,
Size
=
a
[
1
]
}
)
.
Union
(
dto
.
Asks
.
Select
(
a
=>
new
OrderBookModel
{
Direction
=
"Buy"
,
Price
=
a
[
0
]
,
Size
=
a
[
1
]
}
)
)
.
ToList
(
)
;
}
OnPropertyChanged
(
nameof
(
OrderBook10
)
)
;
}
}
)
)
;
_bitmexApiSocketService
.
Subscribe
(
BitmetSocketSubscriptions
.
CreateOrderBookL2Subsription
(
message
=>
{
foreach
(
var
dto
in
message
.
Data
)
{
if
(
dto
.
Symbol
!=
"XBTUSD"
)
{
continue
;
}
lock
(
_syncObjOrderBookL2
)
{
if
(
message
.
Action
==
BitmexActions
.
Insert
||
message
.
Action
==
BitmexActions
.
Partial
)
{
OrderBookL2
.
Add
(
Mapper
.
Map
<
OrderBookDto
,
OrderBookModel
>
(
dto
)
)
;
}
if
(
message
.
Action
==
BitmexActions
.
Delete
)
{
var
existing
=
OrderBookL2
.
FirstOrDefault
(
a
=>
a
.
Id
==
dto
.
Id
)
;
if
(
existing
!=
null
)
{
OrderBookL2
.
Remove
(
existing
)
;
}
}
if
(
message
.
Action
==
BitmexActions
.
Update
)
{
var
existing
=
OrderBookL2
.
FirstOrDefault
(
a
=>
a
.
Id
==
dto
.
Id
)
;
if
(
existing
==
null
)
{
OrderBookL2
.
Add
(
Mapper
.
Map
<
OrderBookDto
,
OrderBookModel
>
(
dto
)
)
;
}
else
{
Mapper
.
Map
<
OrderBookDto
,
OrderBookModel
>
(
dto
,
existing
)
;
}
}
}
OnPropertyChanged
(
nameof
(
OrderBook10
)
)
;
}
}
)
)
;
}
}
[
NotifyPropertyChangedInvocator
]
protected
virtual
void
OnPropertyChanged
(
[
CallerMemberName
]
string
propertyName
=
null
)
{
PropertyChanged
?
.
Invoke
(
this
,
new
PropertyChangedEventArgs
(
propertyName
)
)
;
}
private
async
void
Sell
(
)
{
var
posOrderParams
=
OrderPOSTRequestParams
.
CreateSimpleMarket
(
"XBTUSD"
,
Size
,
OrderSide
.
Sell
)
;
var
bitmexApiService
=
BitmexApiService
.
CreateDefaultApi
(
_bitmexAuthorization
)
;
await
bitmexApiService
.
Execute
(
BitmexApiUrls
.
Order
.
PostOrder
,
posOrderParams
)
.
ContinueWith
(
ProcessPostOrderResult
)
;
}
private
async
void
Buy
(
)
{
var
posOrderParams
=
OrderPOSTRequestParams
.
CreateSimpleMarket
(
"XBTUSD"
,
Size
,
OrderSide
.
Buy
)
;
var
bitmexApiService
=
BitmexApiService
.
CreateDefaultApi
(
_bitmexAuthorization
)
;
await
bitmexApiService
.
Execute
(
BitmexApiUrls
.
Order
.
PostOrder
,
posOrderParams
)
.
ContinueWith
(
ProcessPostOrderResult
)
;
}
private
void
ProcessPostOrderResult
(
Task
<
BitmexApiResult
<
OrderDto
>
>
task
)
{
if
(
task
.
Exception
!=
null
)
{
MessageBox
.
Show
(
(
task
.
Exception
.
InnerException
??
task
.
Exception
)
.
Message
)
;
}
else
{
MessageBox
.
Show
(
$
"order has been placed with Id
{
task
.
Result
.
Result
.
OrderId
}
"
)
;
}
}
}
}
Back
|
FazBrowse Home
|
New Git URL