Skip to content

Commit

Permalink
Feat: Deserialize message frame payload into object and include class…
Browse files Browse the repository at this point in the history
…es for public responses.
  • Loading branch information
RobJohnston committed Oct 10, 2018
1 parent 48ca0d2 commit dc0bc5b
Show file tree
Hide file tree
Showing 13 changed files with 535 additions and 59 deletions.
158 changes: 99 additions & 59 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@ A future version will use this as a base to more easily consume API methods and
```csharp
using AlphaPoint.Api;
using AlphaPoint.Api.Models;
using AlphaPoint.Api.Models.Responses;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;

Expand All @@ -28,7 +30,7 @@ namespace ConsoleApp1
task.Wait();

// Keep the console window open.
Console.WriteLine("Press <enter> key to exit.");
Console.WriteLine("\nPress <enter> key to exit.");
ConsoleKeyInfo keyInfo = Console.ReadKey();
while (keyInfo.Key != ConsoleKey.Enter)
keyInfo = Console.ReadKey();
Expand All @@ -54,7 +56,26 @@ namespace ConsoleApp1
Console.WriteLine(" m > " + e.MessageFrameResponse.MessageType);
Console.WriteLine(" i > " + e.MessageFrameResponse.SequenceNumber);
Console.WriteLine(" n > " + e.MessageFrameResponse.FunctionName);
Console.WriteLine(" o > " + e.MessageFrameResponse.Payload);
//Console.WriteLine(" o > " + e.MessageFrameResponse.Payload);
Console.WriteLine();

// Demo how to work with the result.
if (e.MessageFrameResponse.FunctionName == "GetL2Snapshot")
{
var l2 = e.MessageFrameResponse.PayloadAs<List<Level2Snapshot>>();

Console.WriteLine($"{"Date/Time",22} |{"Type",8} |{"Price",10} |{ "Quantity",11} |{"Side",5}");
Console.WriteLine(new string('-', 65));

foreach (var item in l2)
{
Console.WriteLine($"{PosixTimeStampToDateTime(item.ActionDateTime),22} |" +
$"{item.ActionType,8} |" +
$"{item.Price.ToString("N2"),10} |" +
$"{item.Quantity.ToString("N8"),11} |" +
$"{item.Side,5}");
}
}

ExitEvent.Set();
};
Expand All @@ -81,6 +102,13 @@ namespace ConsoleApp1
ExitEvent.WaitOne();
}
}

public static DateTime PosixTimeStampToDateTime(double timeStamp)
{
DateTime dt = new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc);
dt = dt.AddMilliseconds(timeStamp).ToLocalTime();
return dt;
}
}
}
```
Expand All @@ -92,64 +120,76 @@ MessageFrame received:
m > Reply
i > 2
n > GetL2Snapshot
o > [[33653795,1,1539134020119,0,8550,1,8449.81,1,7,0],
[33653795,1,1539134020119,0,8550,1,8449.43,1,1.21310602,0],
[33653795,1,1539134020119,0,8550,1,8448.67,1,0.07465268,0],
[33653795,1,1539134020119,0,8550,1,8448.29,1,0.14189868,0],
[33653795,1,1539134020119,0,8550,1,8448.16,1,2.95022608,0],
[33653795,1,1539134020119,0,8550,1,8447.27,1,0.00253175,0],
[33653795,1,1539134020119,0,8550,1,8447.14,1,0.93315848,0],
[33653795,1,1539134020119,0,8550,1,8446.76,1,0.01014973,0],
[33653795,1,1539134020119,0,8550,1,8446.38,1,2.23211508,0],
[33653795,1,1539134020119,0,8550,1,8446.25,1,0.00922703,0],
[33653795,1,1539134020119,0,8550,1,8446,1,1.39973772,0],
[33653795,1,1539134020119,0,8550,1,8444.35,1,4.54448179,0],
[33653795,1,1539134020119,0,8550,1,8444.22,1,0.1403765,0],
[33653795,1,1539134020119,0,8550,1,8443.71,1,0.13997377,0],
[33653795,1,1539134020119,0,8550,1,8443.46,1,1.17992127,0],
[33653795,1,1539134020119,0,8550,1,8443.2,1,7,0],
[33653795,1,1539134020119,0,8550,1,8443.07,1,0.56167038,0],
[33653795,1,1539134020119,0,8550,1,8442.82,1,1.27881168,0],
[33653795,1,1539134020119,0,8550,1,8442.69,1,3.82594977,0],
[33653795,1,1539134020119,0,8550,1,8442.18,1,0.21066443,0],
[33653795,1,1539134020119,0,8550,1,8441.93,1,2.3328962,0],
[33653795,1,1539134020119,0,8550,1,8441.8,1,0.84265773,0],
[33653795,1,1539134020119,0,8550,1,8441.68,1,0.37326339,0],
[33653795,1,1539134020119,0,8550,1,8441.55,1,2.26066101,0],
[33653795,1,1539134020119,0,8550,1,8441.42,1,0.1866317,0],
[33653795,1,1539134020119,0,8550,1,8349.0077,1,1.67684598,0],
[33653795,2,1539134020119,0,8550,2,8200,1,0.21392684,0],
[33653795,0,1539134020119,0,8550,1,0.01,1,31,0],
[33653795,1,1539134020119,0,8550,1,8550,1,1.62613,1],
[33653795,1,1539134020119,0,8550,1,8600,1,2,1],
[33653795,1,1539134020119,0,8550,1,8700,1,2,1],
[33653795,1,1539134020119,0,8550,1,8751.73,1,7,1],
[33653795,1,1539134020119,0,8550,1,8751.86,1,0.38479167,1],
[33653795,1,1539134020119,0,8550,1,8752.25,1,0.1,1],
[33653795,1,1539134020119,0,8550,1,8752.38,1,0.12,1],
[33653795,1,1539134020119,0,8550,1,8752.78,1,0.41444587,1],[
33653795,1,1539134020119,0,8550,1,8753.96,1,0.011,1],
[33653795,1,1539134020119,0,8550,1,8754.1,1,1.20780263,1],
[33653795,1,1539134020119,0,8550,1,8754.23,1,0.01,1],
[33653795,1,1539134020119,0,8550,1,8756.2,1,0.17919283,1],
[33653795,1,1539134020119,0,8550,1,8756.33,1,0.4,1],
[33653795,1,1539134020119,0,8550,1,8756.47,1,0.27796986,1],
[33653795,1,1539134020119,0,8550,1,8756.6,1,0.63324654,1],
[33653795,1,1539134020119,0,8550,1,8756.73,1,0.18190475,1],
[33653795,1,1539134020119,0,8550,1,8757.26,1,0.02199081,1],
[33653795,1,1539134020119,0,8550,1,8757.39,1,1.98,1],
[33653795,1,1539134020119,0,8550,1,8757.52,1,0.28644275,1],
[33653795,1,1539134020119,0,8550,1,8757.65,1,0.12800312,1],
[33653795,1,1539134020119,0,8550,1,8757.78,1,0.3,1],
[33653795,1,1539134020119,0,8550,1,8757.92,1,0.026,1],
[33653795,1,1539134020119,0,8550,1,8758.05,1,0.22575335,1],
[33653795,1,1539134020119,0,8550,1,8758.18,1,7,1],
[33653795,1,1539134020119,0,8550,1,8758.44,1,0.2,1],
[33653795,1,1539134020119,0,8550,1,8758.57,1,0.13885664,1],
[33653795,1,1539134020119,0,8550,1,8758.71,1,0.65440634,1],
[33653795,1,1539134020119,0,8550,1,8758.84,1,0.1772403,1],
[33653795,1,1539134020119,0,8550,1,8800,1,2,1]]
Date/Time | Type | Price | Quantity | Side
-----------------------------------------------------------------
2018-10-10 6:52:31 PM | New | 8,530.00 | 0.70344880 | Buy
2018-10-10 6:52:31 PM | New | 8,500.00 | 0.01300000 | Buy
2018-10-10 6:52:31 PM | New | 8,460.01 | 0.63737062 | Buy
2018-10-10 6:52:31 PM | New | 8,459.89 | 0.57906748 | Buy
2018-10-10 6:52:31 PM | New | 8,459.37 | 0.02523204 | Buy
2018-10-10 6:52:31 PM | New | 8,459.12 | 0.13396543 | Buy
2018-10-10 6:52:31 PM | New | 8,458.86 | 0.39436493 | Buy
2018-10-10 6:52:31 PM | New | 8,458.48 | 0.00572819 | Buy
2018-10-10 6:52:31 PM | New | 8,457.97 | 0.21305503 | Buy
2018-10-10 6:52:31 PM | New | 8,457.84 | 0.07534153 | Buy
2018-10-10 6:52:31 PM | New | 8,457.33 | 0.18856173 | Buy
2018-10-10 6:52:31 PM | New | 8,457.07 | 0.00572370 | Buy
2018-10-10 6:52:31 PM | New | 8,456.82 | 0.49919611 | Buy
2018-10-10 6:52:31 PM | New | 8,456.69 | 0.03960801 | Buy
2018-10-10 6:52:31 PM | New | 8,456.30 | 0.38466649 | Buy
2018-10-10 6:52:31 PM | New | 8,456.18 | 0.07527020 | Buy
2018-10-10 6:52:31 PM | New | 8,455.66 | 1.60153236 | Buy
2018-10-10 6:52:31 PM | New | 8,455.28 | 0.75478451 | Buy
2018-10-10 6:52:31 PM | New | 8,455.15 | 1.49758832 | Buy
2018-10-10 6:52:31 PM | New | 8,455.03 | 7.00000000 | Buy
2018-10-10 6:52:31 PM | New | 8,453.87 | 0.16392300 | Buy
2018-10-10 6:52:31 PM | New | 8,453.36 | 0.99839222 | Buy
2018-10-10 6:52:31 PM | New | 8,453.23 | 0.49919611 | Buy
2018-10-10 6:52:31 PM | New | 8,453.11 | 1.95684874 | Buy
2018-10-10 6:52:31 PM | New | 8,452.98 | 2.25353822 | Buy
2018-10-10 6:52:31 PM | New | 8,452.08 | 0.49919611 | Buy
2018-10-10 6:52:31 PM | New | 8,451.96 | 0.03801526 | Buy
2018-10-10 6:52:31 PM | New | 8,246.70 | 0.41800000 | Buy
2018-10-10 6:52:31 PM | New | 8,246.45 | 0.25000000 | Buy
2018-10-10 6:52:31 PM | New | 8,238.48 | 0.51100000 | Buy
2018-10-10 6:52:31 PM | New | 8,238.23 | 0.32000000 | Buy
2018-10-10 6:52:31 PM | New | 8,237.60 | 0.30000000 | Buy
2018-10-10 6:52:31 PM | New | 8,200.00 | 0.21392684 | Buy
2018-10-10 6:52:31 PM | New | 0.01 |31.00000000 | Buy
2018-10-10 6:52:31 PM | New | 8,671.92 | 0.10826416 | Sell
2018-10-10 6:52:31 PM | New | 8,672.05 | 1.42836521 | Sell
2018-10-10 6:52:31 PM | New | 8,672.19 | 2.00000000 | Sell
2018-10-10 6:52:31 PM | New | 8,672.32 | 0.72176110 | Sell
2018-10-10 6:52:31 PM | New | 8,672.59 | 2.00000000 | Sell
2018-10-10 6:52:31 PM | New | 8,762.29 | 7.00000000 | Sell
2018-10-10 6:52:31 PM | New | 8,762.42 | 2.00000000 | Sell
2018-10-10 6:52:31 PM | New | 8,762.55 | 0.06077132 | Sell
2018-10-10 6:52:31 PM | New | 8,762.69 | 0.76951331 | Sell
2018-10-10 6:52:31 PM | New | 8,762.82 | 0.00999000 | Sell
2018-10-10 6:52:31 PM | New | 8,763.75 | 0.02879279 | Sell
2018-10-10 6:52:31 PM | New | 8,764.14 | 0.03079200 | Sell
2018-10-10 6:52:31 PM | New | 8,764.28 | 3.00000000 | Sell
2018-10-10 6:52:31 PM | New | 8,765.34 | 1.00000000 | Sell
2018-10-10 6:52:31 PM | New | 8,765.60 | 3.20000000 | Sell
2018-10-10 6:52:31 PM | New | 8,765.73 | 2.42500103 | Sell
2018-10-10 6:52:31 PM | New | 8,765.87 | 0.06077132 | Sell
2018-10-10 6:52:31 PM | New | 8,766.00 | 1.13603811 | Sell
2018-10-10 6:52:31 PM | New | 8,766.53 | 0.48787545 | Sell
2018-10-10 6:52:31 PM | New | 8,766.66 | 0.14720710 | Sell
2018-10-10 6:52:31 PM | New | 8,766.79 | 0.19520208 | Sell
2018-10-10 6:52:31 PM | New | 8,767.46 | 0.07479691 | Sell
2018-10-10 6:52:31 PM | New | 8,767.59 | 0.54720711 | Sell
2018-10-10 6:52:31 PM | New | 8,767.72 | 0.73224120 | Sell
2018-10-10 6:52:31 PM | New | 8,768.38 | 0.68658606 | Sell
2018-10-10 6:52:31 PM | New | 8,768.52 | 0.41307854 | Sell
2018-10-10 6:52:31 PM | New | 8,768.65 | 0.19520208 | Sell
2018-10-10 6:52:31 PM | New | 8,769.44 | 0.19651065 | Sell
2018-10-10 6:52:31 PM | New | 8,769.71 | 0.41720609 | Sell
2018-10-10 6:52:31 PM | New | 8,769.84 | 7.00000000 | Sell
Press <enter> key to exit.
```

## My related projects
Expand Down
23 changes: 23 additions & 0 deletions src/AlphaPoint.Api/Models/Enums/ActionType.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
namespace AlphaPoint.Api.Models
{
/// <summary>
/// Represents the type of L2 information price data.
/// </summary>
public enum ActionType
{
/// <summary>
/// New data.
/// </summary>
New = 0,

/// <summary>
/// Updated data.
/// </summary>
Update = 1,

/// <summary>
/// Deleted data.
/// </summary>
Delete = 2,
}
}
35 changes: 35 additions & 0 deletions src/AlphaPoint.Api/Models/Enums/ErrorCode.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
namespace AlphaPoint.Api.Models
{
/// <summary>
/// Return value indicating successful or unsuccessful receipt of a call.
/// </summary>
public enum ErrorCode
{
Success = 0,

/// <summary>
/// Not authorized (error code 20)
/// </summary>
NotAuthorized = 20,

/// <summary>
/// Invalid request (error code 100)
/// </summary>
InvalidRequest = 100,

/// <summary>
/// Operation failed (error code 101)
/// </summary>
OperationFailed = 101,

/// <summary>
/// Server error (error code 102)
/// </summary>
ServerError = 102,

/// <summary>
/// Resource not found (error code 104).
/// </summary>
ResourceNotFound = 104,
}
}
18 changes: 18 additions & 0 deletions src/AlphaPoint.Api/Models/Enums/InstrumentType.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
namespace AlphaPoint.Api.Models
{
/// <summary>
/// Represents the type of the instrument.
/// </summary>
public enum InstrumentType
{
/// <summary>
/// Unknown. An error condition.
/// </summary>
Unknown,

/// <summary>
/// An exchange of one product for another.
/// </summary>
Standard,
}
}
19 changes: 19 additions & 0 deletions src/AlphaPoint.Api/Models/Enums/ProductType.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
namespace AlphaPoint.Api.Models
{
/// <summary>
/// The nature of the product.
/// </summary>
public enum ProductType
{
/// <summary>
/// Unknown. An error condition.
/// </summary>
Unknown = 0,

NationalCurrency = 1,

CryptoCurrency = 2,

Contract = 3
}
}
11 changes: 11 additions & 0 deletions src/AlphaPoint.Api/Models/Enums/SessionStatus.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
namespace AlphaPoint.Api.Models
{
public enum SessionStatus
{
Unknown = 0,
Running = 1,
Paused = 2,
Stopped = 3,
Starting = 4,
}
}
28 changes: 28 additions & 0 deletions src/AlphaPoint.Api/Models/Enums/Side.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
namespace AlphaPoint.Api.Models
{
/// <summary>
/// Represents one side of a trade. Every trade has two sides.
/// </summary>
public enum Side
{
/// <summary>
/// The buy side of a trade..
/// </summary>
Buy = 0,

/// <summary>
/// The sell side of a trade.
/// </summary>
Sell = 1,

/// <summary>
/// Short (reserved for future use).
/// </summary>
Short = 2,

/// <summary>
/// Unknown (error condition).
/// </summary>
Unknown = 3
}
}
10 changes: 10 additions & 0 deletions src/AlphaPoint.Api/Models/MessageFrame.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,5 +36,15 @@ public class MessageFrame
/// </summary>
[JsonProperty("o")]
public string Payload { get; set; }

/// <summary>
/// Deserialized the <c>Payload</c> to an object of arbitrary type <typeparamref name="T"/>.
/// </summary>
/// <typeparam name="T">The type of the payload.</typeparam>
/// <returns>The <c>Payload</c> deserialized to the specified type.</returns>
public T PayloadAs<T>()
{
return JsonConvert.DeserializeObject<T>(Payload);
}
}
}
40 changes: 40 additions & 0 deletions src/AlphaPoint.Api/Models/Responses/GenericResponse.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
using Newtonsoft.Json;

namespace AlphaPoint.Api.Models.Responses
{
/// <summary>
/// A generic response to an API call that verifies that the call was received.
/// </summary>
public class GenericResponse
{
/// <summary>
/// If the call has been successfully received by the Order Management System, result is true; otherwise, it is false.
/// </summary>
[JsonProperty("result")]
bool Result { get; set; }

/// <summary>
/// A successful receipt of the call returns null; the errormsg parameter for an unsuccessful call returns one of the following messages:
/// Not Authorized (errorcode 20)
/// Invalid Request (errorcode 100)
/// Operation Failed (errorcode 101)
/// Server Error (errorcode 102)
/// Resource Not Found (errorcode 104)
/// </summary>
[JsonProperty("errormsg")]
string ErrorMsg { get; set; }

/// <summary>
/// A successful receipt of the call returns 0.
/// An unsuccessful receipt of the call returns one of the errorcodes shown in the errormsg list.
/// </summary>
[JsonProperty("errorcode")]
ErrorCode ErrorCode { get; set; }

/// <summary>
/// Message text that the system may send. The content of this parameter is usually null.
/// </summary>
[JsonProperty("detail")]
string Detail { get; set; }
}
}
Loading

0 comments on commit dc0bc5b

Please sign in to comment.