using Newtonsoft.Json.Linq;
using System.Collections.Generic;
using System.Net.Http.Headers;
static string _baseURL = "https://sandbox.livevol.com/api/v1/delayed/";
public static void Main()
string idServerURL = "https://sandbox.livevol.com/id/connect/token";
string authenticationHeader = Convert.ToBase64String(Encoding.UTF8.GetBytes("sandbox_api_examples_client:api_example_secret"));
var accessToken = GetBearerToken(idServerURL, authenticationHeader);
if (!string.IsNullOrEmpty(accessToken))
GetData("time-and-sales/trades-and-quotes?mode=NBBO_CHANGES&limit=50&order_by_time=ASC&min_time=09:30:00.000&max_time=10:00:00.000&date=2019-01-18&symbol=SPY", accessToken);
static string GetBearerToken(string idServer, string authorization)
var httpClient = new HttpClient{BaseAddress = new Uri(idServer)};
Console.Write("Accessing URL: " + idServer + " ...");
httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", authorization);
HttpResponseMessage response = httpClient.PostAsync(string.Empty, (HttpContent)new FormUrlEncodedContent(new Dictionary<string, string>()
{{"grant_type", "client_credentials"}}), CancellationToken.None).Result;
if (response.StatusCode == HttpStatusCode.OK)
var data = response.Content.ReadAsStringAsync().Result;
var content = JObject.Parse(data);
Console.WriteLine("successfully received access token...");
return content.GetValue("access_token").ToString();
Console.WriteLine(string.Format("Response status code does not indicate success: {0} ({1})", (int)response.StatusCode, response.ReasonPhrase));
Console.WriteLine("Did you remember to update the client_id:client_secret?");
static void GetData(string url, string accessToken)
var httpClient = new HttpClient { BaseAddress = new Uri(_baseURL) };
Console.WriteLine("Accessing URL: " + _baseURL + url);
httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", accessToken);
var response = httpClient.GetAsync(url).Result;
if (response.StatusCode == HttpStatusCode.OK)
var data = JArray.Parse(response.Content.ReadAsStringAsync().Result);
DisplayNetFiddleTable(data);
Console.WriteLine(string.Format("Response status code does not indicate success: {0} ({1})", (int)response.StatusCode, response.ReasonPhrase));
static void DisplayNetFiddleTable(JArray data)
DataTable table = new DataTable();
table.Columns.Add("Type", typeof(string));
table.Columns.Add("Time", typeof(string));
table.Columns.Add("Price", typeof(double));
table.Columns.Add("Size", typeof(int));
table.Columns.Add("Bid", typeof(double));
table.Columns.Add("Bid Size", typeof(int));
table.Columns.Add("Ask", typeof(double));
table.Columns.Add("Ask Size", typeof(int));
table.Columns.Add("NBBO Bid", typeof(double));
table.Columns.Add("NBBO Ask", typeof(double));
foreach (var dataValue in data)
var price = dataValue.Value<double>("price");
var type = price > 0 ? "Trade" : "Quote";
table.Rows.Add(type, dataValue.Value<string>("time"), price, dataValue.Value<int>("size"), dataValue.Value<double>("bid"), dataValue.Value<int>("bid_size"), dataValue.Value<double>("ask"), dataValue.Value<int>("ask_size"),
dataValue.Value<double>("nbbo_bid"), dataValue.Value<double>("nbbo_ask") );
FiddleHelper.WriteTable(table);