-
-
Notifications
You must be signed in to change notification settings - Fork 370
Expand file tree
/
Copy pathExchangeTicker.cs
More file actions
171 lines (146 loc) · 5.14 KB
/
ExchangeTicker.cs
File metadata and controls
171 lines (146 loc) · 5.14 KB
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
/*
MIT LICENSE
Copyright 2017 Digital Ruby, LLC - http://www.digitalruby.com
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
namespace ExchangeSharp
{
/// <summary>
/// Details of the current price of an exchange asset
/// </summary>
public sealed class ExchangeTicker
{
/// <summary>
/// An exchange specific id if known, otherwise null
/// </summary>
public string Id { get; set; }
/// <summary>
/// The name of the exchange the tick was sent from.
/// </summary>
public string Exchange { get; set; }
/// <summary>
/// The currency pair symbol that this ticker is in reference to
/// </summary>
public string MarketSymbol { get; set; }
/// <summary>
/// The bid is the price to sell at
/// </summary>
public decimal Bid { get; set; }
/// <summary>
/// The ask is the price to buy at
/// </summary>
public decimal Ask { get; set; }
/// <summary>
/// The last trade purchase price
/// </summary>
public decimal Last { get; set; }
/// <summary>
/// This property contains the content of the complete api response. This may contain additional information
/// </summary>
public JToken ApiResponse { get; set; }
/// <summary>
/// Volume info
/// </summary>
public ExchangeVolume Volume { get; set; }
/// <summary>
/// Get a string for this ticker
/// </summary>
/// <returns>String</returns>
public override string ToString()
{
return string.Format("Bid: {0}, Ask: {1}, Last: {2}, Vol: {3}", Bid, Ask, Last, Volume);
}
/// <summary>
/// Write to writer
/// </summary>
/// <param name="writer">Writer</param>
public void ToBinary(BinaryWriter writer)
{
writer.Write((double)Bid);
writer.Write((double)Ask);
writer.Write((double)Last);
Volume.ToBinary(writer);
}
/// <summary>
/// Read from reader
/// </summary>
/// <param name="reader">Reader</param>
public void FromBinary(BinaryReader reader)
{
Bid = (decimal)reader.ReadDouble();
Ask = (decimal)reader.ReadDouble();
Last = (decimal)reader.ReadDouble();
Volume = (Volume ?? new ExchangeVolume());
Volume.FromBinary(reader);
}
}
/// <summary>
/// Info about exchange volume
/// </summary>
public sealed class ExchangeVolume
{
/// <summary>
/// Last volume update timestamp
/// </summary>
public DateTime Timestamp { get; set; }
/// <summary>
/// Quote / Price currency - will equal base currency if exchange doesn't break it out by price unit and quantity unit
/// In BTC-USD, this would be USD
/// </summary>
public string QuoteCurrency { get; set; }
/// <summary>
/// Amount in units of the QuoteCurrency - will equal BaseCurrencyVolume if exchange doesn't break it out by price unit and quantity unit
/// In BTC-USD, this would be USD volume
/// </summary>
public decimal QuoteCurrencyVolume { get; set; }
/// <summary>
/// Base currency
/// In BTC-USD, this would be BTC
/// </summary>
public string BaseCurrency { get; set; }
/// <summary>
/// Base currency amount (this many units total)
/// In BTC-USD this would be BTC volume
/// </summary>
public decimal BaseCurrencyVolume { get; set; }
/// <inheritdoc />
public override string ToString()
{
return $"{BaseCurrencyVolume:0.0000}/{QuoteCurrencyVolume:0.0000}";
}
/// <summary>
/// Write to a binary writer
/// </summary>
/// <param name="writer">Binary writer</param>
public void ToBinary(BinaryWriter writer)
{
writer.Write(Timestamp.ToUniversalTime().Ticks);
writer.Write(QuoteCurrency);
writer.Write((double)QuoteCurrencyVolume);
writer.Write(BaseCurrency);
writer.Write((double)BaseCurrencyVolume);
}
/// <summary>
/// Read from a binary reader
/// </summary>
/// <param name="reader">Binary reader</param>
public void FromBinary(BinaryReader reader)
{
Timestamp = new DateTime(reader.ReadInt64(), DateTimeKind.Utc);
QuoteCurrency = reader.ReadString();
QuoteCurrencyVolume = (decimal)reader.ReadDouble();
BaseCurrency = reader.ReadString();
BaseCurrencyVolume = (decimal)reader.ReadDouble();
}
}
}