0

I'm still learning C# .Net 4 and this is my first WinForms so please be kind.

Continuing in my project, my financial DataFeed is streaming into my application by use of 'Asynchronous Sockets?'. Anyway, the data I am getting is tick per tick data, which is basically 1 order/transaction. So now I need to build bars with this tick by tick data, in particular Range Bars.

My problem is I don't want to go to the database and grab this data, so I am looking to do this in memory, like a list variable. Eventually, this system on the main server will do all the number crunching etc... and will have clients connected via Sockets to interrogate or set their own predefined algos on the in coming data and build their own charts using different ranges and indicators.

I wouldn't want to offload this to the client because I would like to keep the indicators technology proprietary.

How would I go about implementing this?

I already have my class called Tick

class Tick
{
    public double Last { get; set; }
    public double Bid { get; set; }
    public double Ask { get; set; }
    public double BidSize { get; set; }
    public double AskSize { get; set; }
    public DateTime TimeStampInternal { get; set; }
    public int DTNTickID { get; set; }
    public int UpdateTypeID { get; set; }
} 

I'm thinking of a

Static List<Tick> Ticks 

but I don't think this is the way to go because

  1. I need to be able to hold only a certain amount of ticks and as new data comes in, old data gets thrown away, FIFO to keep memory usage down.
  2. I will only be able to hold 1 Static List and I need something dynamic, e.g. have a List for each user that connects which would be identifiable to them only.

Please help me architect this correctly with best practices for speed and efficiency.

John Saunders
  • 160,644
  • 26
  • 247
  • 397
DeveloperDavid
  • 157
  • 2
  • 7
  • 16
  • We would need more information about how you interact with your datafeed to help you architect. Meaning, when you app communicates with the feed are you getting incremental changes? or all the latest data for all 'Ticks'? – Glenn Ferrie Jul 11 '11 at 19:03
  • I connect to my DataFeed by use of asynchronous TCP Sockets, essentially, whenever data is sent to me it fires off a method in my C# application with tick data, from LastX, Bid, Ask, BidSize, AskSize etc... in total, there are over 40 properties, too long to list here. But essentially, each tick data is self contained, it contains a snap shot of the market at that particular instant. I hope this helps. – DeveloperDavid Jul 11 '11 at 19:09
  • and at that point you want to see if you have a historic record for that tick or if its new and either replace it in your in-memory data store or add it? – Glenn Ferrie Jul 11 '11 at 19:13
  • At the moment, I am not interested in seeing historic data, I only really want to see the last 1000 ticks which has been captured live via the stream. Yes I record data into my Database but that is only for back testing purposes. My current situation requires that I use the real live streaming data for the purposes for the system to automatically enter and exit positions based on rules and patterns that have been matched with data coming in live. Every millisecond counts as bid and ask prices can change within a 10th of a second. – DeveloperDavid Jul 11 '11 at 19:22

2 Answers2

1

Sounds like a circular buffer is what you're looking for.

http://circularbuffer.codeplex.com/

Or perhaps a queue.

Robert Harvey
  • 178,213
  • 47
  • 333
  • 501
  • I was thinking of using a Queue but I'm not sure because there wouldn't be a unique identifier for each record in a Queue like an index would there? Because as new data comes in, the oldest gets thrown away, but maybe I will have to use my own ID on this??? – DeveloperDavid Jul 11 '11 at 19:17
  • I assume you are referring to `DTNTickID`? If not, just add your unique identifier to the record. I also assume that, once you purge a record from the queue, that you no longer need to look it up from the queue, by any means. There's no free lunch, so it is available in the queue, until it isn't – Robert Harvey Jul 11 '11 at 19:21
  • That's correct, once I have purged it from a queue I will no longer need it or it's data. I'm looking at the Cicularbuffer but I think it's too complicated for my head! lol Regarding the DTNTickID, unfortunately I don't think I will be able to use this because although each DTNTickID from the live feed is unique per trade, there could be as most often is unfortunately several ticks with the same DTNTickID, simply because when a Tick's Bid or Ask price has changed I receive a new snapshot, it's still the same tick, but the Bid and Ask has changed, therefore it keeps the same DTNTickID. – DeveloperDavid Jul 11 '11 at 19:27
  • Right, so you will need to add your own unique ID. If you don't need to understand how CircularBuffer works internally, it should be fairly simple to use. It's just a queue with an upper size limit, where the discarded slots in the queue are re-used to enqueue new items. All you should need to do is set it to a size of 1000, enqueue and dequeue. Note that most applications like this need two threads; one on the queue side, and one on the dequeue site. This is known as a ... – Robert Harvey Jul 11 '11 at 19:30
  • ...producer/consumer pattern. There are some simple blocking queue implementations on the internet that can help you understand this pattern. In particular, have a look at Marc Gravell's implementation of a blocking queue here: http://stackoverflow.com/questions/530211/creating-a-blocking-queuet-in-net/530228#530228 – Robert Harvey Jul 11 '11 at 19:32
0

I hope that I correctly understand what you want, so here is very pseudocode :

public class User {

     private UserTickList<Tick> _userTicks = new UserTickList<Tick>();

     public void AddUserTick(Tick t) {
              _userTicks.Add(t);
     }

     /*remove, update if need*/
}

 public class UserTickList {

    private List<Tick> _list = new List<Tick>();

    public void AddTick(Tick tick) {
         if(_list.Count == 10){
               /*perform what you need*/
         }
         else 
           _list.Add(tick);
    }
 }

I repeat this probabbly will not compile, but just to give an idea what it can look like.

Hope this helps.

Tigran
  • 61,654
  • 8
  • 86
  • 123
  • I didn't downvote you, but this doesn't really answer the OP's question. He's asking for architectural advice, and you're giving him some basic code that manipulates a list. – Robert Harvey Jul 11 '11 at 19:25