Skip to content

Instantly share code, notes, and snippets.

@jeffdeville
Created July 10, 2013 13:54
Show Gist options
  • Select an option

  • Save jeffdeville/5966470 to your computer and use it in GitHub Desktop.

Select an option

Save jeffdeville/5966470 to your computer and use it in GitHub Desktop.
Loading realtime advise data on all stocks in an exchange via .net
using System;
using System.Linq;
using System.Threading;
using NLog;
using RealTick.Api.ClientAdapter;
using RealTick.Api.Domain;
using RealTick.Api.Domain.Livequote;
namespace Chaikin.RealTick
{
public class IntradayPriceQuery : BaseRealtickQuery {
public IntradayPriceQuery(ClientAdapterToolkitApp app=null){
_app = app;
}
readonly AutoResetEvent _lostConnection = new AutoResetEvent(false);
public event EventHandler<IntradayPriceEventArgs> PriceReceived;
public static readonly string[] Exchanges = new[] { "NAS", "NYS", "ARC" };
static Logger Log = LogManager.GetCurrentClassLogger();
public void FindPrices(string[] exchanges, DateTime untilWhen){
var timeSpan = DateTime.UtcNow - untilWhen.ToUniversalTime();
FindPrices(exchanges, timeSpan);
}
public void FindPrices(string[] exchanges, TimeSpan waitDuration){
foreach (var exchange in exchanges){
using (var quoteTable = new IntradayTable(App))
{
var tql = quoteTable.TqlToGetAllSymbolsAndData(exchange);
Console.WriteLine(tql);
quoteTable.OnData += lqt_OnData;
quoteTable.OnDead += (o, args) => _lostConnection.Set();
quoteTable.OnAck += (o, ackEventArgs) => Console.WriteLine("... GOT {0} for TQL ITEM {1}", (ackEventArgs.Status & 0x8000) == 0 ? "NACK" : "ACK", ackEventArgs.Item); ;
quoteTable.WantData(tql, true, true);
quoteTable.Start();
}
}
if (_lostConnection.WaitOne(waitDuration)){
// We lost the connection. bad.
Log.Error("Lost the connection to realtick.");
Console.WriteLine("Lost the connection to realtick.");
}
else{
Console.WriteLine("The timespan elapsed. It is (UTC): {0}", DateTime.UtcNow.ToLongTimeString());
Log.Info("The timespan elapsed. It is (UTC): {0}", DateTime.UtcNow.ToLongTimeString());
}
}
void lqt_OnData(object sender, DataEventArgs<LivequoteRecord> e){
Console.WriteLine("Data Received");
System.Diagnostics.Debugger.Launch();
var updateTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
foreach (var livequoteRecord in e.Where(lqr => lqr.Trdprc1 != null)){
Console.WriteLine(livequoteRecord.DispName);
var priceEventArgs = new IntradayPriceEventArgs(livequoteRecord.DispName, updateTime,
livequoteRecord.Trdprc1.Value.DecimalValue,
livequoteRecord.Trdprc1.Value.DecimalValue - livequoteRecord.HstClose.Value.DecimalValue);
PriceReceived.Raise(this, priceEventArgs);
}
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using Chaikin.RealTick;
using NSpec;
namespace Chaikin.Specs.RealTick
{
public class IntradayPriceQuerySpec : nspec
{
IntradayPriceQuery priceQuery;
IList<IntradayPriceEventArgs> _allEvents;
void unit_specs(){
context["when querying for stock prices"] = () =>
{
before = () =>
{
_allEvents = new List<IntradayPriceEventArgs>();
priceQuery = new IntradayPriceQuery();
priceQuery.PriceReceived += (o, priceReceivedEvent) => _allEvents.Add(priceReceivedEvent);
};
act = () => priceQuery.FindPrices(new[]{"NYS"}, new TimeSpan(0,0,30));
it["should obtain some prices"] = () =>
{
_allEvents.Count().should_be_greater_than(0);
Console.WriteLine(_allEvents.First());
};
};
}
}
}
using System;
using System.Linq;
using System.Text;
using RealTick.Api.Application;
using RealTick.Api.Data;
using RealTick.Api.Domain;
using RealTick.Api.Domain.Livequote;
namespace Chaikin.RealTick
{
public class IntradayTable : TalTable
{
public event EventHandler<DataEventArgs<LivequoteRecord>> OnData;
public IntradayTable(ToolkitApp app, string machine = "@recombiner")
: base(app, "TA_TICKER", "LIVEQUOTE", machine) {}
protected override void RaiseOnData(string item, IDataBlock block, bool isRequestData){
System.Diagnostics.Debugger.Launch();
OnData.Raise(this, new DataEventArgs<LivequoteRecord>(block, isRequestData, item));
}
public string TqlToGetAllSymbolsAndData(string exchange)
{
return string.Format("LIVEQUOTE;1003,6041;6023='{0}',1003='*'", exchange);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment