Results 1 to 10 of 17

Hybrid View

  1. #1
    Administrator
    Join Date
    Jul 2006
    Location
    Milan
    Posts
    1,091
    This sample class "simulates" a broadcast feed, which is one of the most common types of data feed, especially in a trading scenario.
    Once you connect to such a feed, you immediately receive the current real-time update flow for all available data (the IExternalFeedListener interface).
    Some broacast feed may also allow you to request the current state of a data item. Our sample feed class does support this (the SendCurrentValues method).

    In our sample feed class, all available data consists of 30 fake stocks, which is what is needed by the StockList demo front-end. For each of the above stocks/items, we create a specific object, of type ExternalFeedProducer, whose job is to create a thread and generate the updates for the item. We collect all these objects in the _stockGenerators hash table.

    In order to manage SendCurrentValues request, the feed does not return the current state to the caller, but it rather sends a redundant event to the IExternalFeedListener interface (i.e. an event which does not carry updated data but just the current state). We do this in a separate thread (we call this thread _snapshotSender; it runs the Run method reported above) and use the _snapshotQueue queue to send the SendCurrentValues requests to that thread.

    Hope This Helps
    Dario

  2. #2
    Senior Member
    Join Date
    Oct 2007
    Location
    HoChiMinh
    Posts
    69
    Hi Dario,
    You mean, StockListDemo work-flow :
    Step 1: Get feed data (simulate) from "ExternalFeed" class

    public ExternalFeedSimulator() {
    _stockGenerators= new Hashtable();
    _snapshotQueue= new ArrayList();
    ...
    }

    Step 2 : Starts generating update events for the stocks. attaching and reading from an external broadcast feed (in step 1).

    public void Start()
    {
    if (_snapshotSender != null) return;
    //all available data consists of 30 fake stocks, which is what is needed by the StockList demo front-end.
    for (int i = 0; i < 30; i++)
    {
    ....
    //For each of the above stocks/items, you create a specific object, of type ExternalFeedProducer, whose job is to create a thread and generate the updates for the item.
    ExternalFeedProducer myProducer = new ExternalFeedProducer(itemName,
    _openprices[i], _refprices[i], _minprices[i], _maxprices[i],
    _updateTimeMeans[i], _updateTimeStdDevs[i], _stockNames[i]);

    _stockGenerators[itemName]= myProducer;
    myProducer.SetFeedListener(_listener);
    myProducer.Start();
    }
    ...
    }


    private void Run()
    {
    ...
    }


    Now, I want to call my function every 5 seconds, it starts generating update events for the stocks, attaching and reading from an external broadcast feed (in step 1). How do I do?

    Can u help me?

  3. #3
    Administrator
    Join Date
    Jul 2006
    Location
    Milan
    Posts
    1,091
    Provided that you still think that the demo architecture fits your needs, in order to send your custom data, you should customize only the "ExternalFeedProducer" class.
    Each object instance of this class owns an update thread, which waits for randomly generated times, produced by the "ComputeNextWaitTime" method; you can rewrite the latter to always return 5000 milliseconds.
    Then, you should only rewrite the "ComputeNewValues" and "GetCurrentValues" methods. The first one produces new data for the instance-related stock and stores it in the object; the second one collects the stored data in an "IDictionary", suitable for being sent forward.

    If, however, you can only read data for all your stocks, from outside, as an atomic operation, then some adjustments would be needed (it is still unclear to me which are your requirements).

    Dario

  4. #4
    Senior Member
    Join Date
    Oct 2007
    Location
    HoChiMinh
    Posts
    69
    Hi Dario,
    I tried follow your instruction, but it has error : Out of memory.

    Here my changed
    -------

    public int ComputeNextWaitTime()
    {
    lock (this)
    {
    return 5000;
    }
    }

    public void ComputeNewValues()
    {
    ISecurityStructReader reader;
    reader = new ReadSecurityStruct("C:\\TEMP\\BACKUP14\\SECURITY.D AT");

    nStock = 0;

    if (reader.Open())
    {
    arr = reader.Read();

    nStock = arr.Count;

    _stockSymbol = new string[nStock];

    for (int i=0; i< nStock ;i++)
    {
    Struct_Security item = (Struct_Security)arr[i];
    //_stockNames.Add(item.SecurityName);
    _openprices[i]= item.OpenPrice;
    _stockSymbol[i]= item.StockSymbol;
    ...

    string itemName= "item" + (i + 1);
    ExternalFeedProducer myProducer = new ExternalFeedProducer(itemName, m_openprices[i], m_refprices[i], m_minprices[i], m_maxprices[i], m_stockSymbol[i], m_stockNames[i], m_ceiling[i], m_floor[i], m_bid1[i], m_bid2[i], m_bid3[i], m_bid1vol[i], m_bid2vol[i], m_bid3vol[i], m_ask1[i], m_ask2[i], m_ask3[i],m_ask1vol[1], m_ask2vol[i], m_ask3vol[i], m_last[i], m_lastVal[i], m_lastVol[i], m_projectOpen[i]);

    _stockGenerators[itemName]= myProducer;

    myProducer.SetFeedListener(_listener);
    myProducer.Start();
    }
    ...
    }

    }

    -----------
    My step is correct?

    In my case, stock data i get from outside and it is a binary file and has a structure. I read it and store it in array list.

    In your code, u stored data in an "IDictionay" because you change only 01 value of item in the same time, but my data has many change in many item (in the same time).

    Example :
    10:00 AM I have :
    stock_symbol[1] = STB , price[1] = 100, volume[1] = 99
    stock_symbol[2]= ACB, price[2] = 80, volume[2] = 89

    10:01 AM, I have
    stock_symbol[1] = STB , price[1] = 101, volume[1] = 90
    stock_symbol[2]= ACB, price[2] = 82, volume[2] = 80

    How to rewrite "GetCurrentValues" and "ComputeNewValues" to store this? Can u help me?

  5. #5
    Administrator
    Join Date
    Jul 2006
    Location
    Milan
    Posts
    1,091
    I assume from the above that the answer to my doubt
    If, however, you can only read data for all your stocks, from outside, as an atomic operation, then some adjustments would be needed (it is still unclear to me which are your requirements).
    is yes; you can only read updates for several or all stocks alltogether from a file. Then I'm afraid that the demo Data Adapter architecture is not suitable for you as a starting point.

    What do you exactly read from your files?
    Do you get values for all stocks or only for the modified stocks?
    And, for each stock read, do you get values for all fields or only for the modified fields?

    Dario

  6. #6
    Senior Member
    Join Date
    Oct 2007
    Location
    HoChiMinh
    Posts
    69
    Dear Dario,
    Let's me explain : Stock Exchange send data file (binary file) to the Broker company, this file has information for all stocks (stock symbol, stock name, price, volume, ceiling, floor, percent change,...). If some stock has information changed, they send this file again to the Broker company. So every 5-10 seconds, I need read this file again to get values for all stocks, because i don't know which stock modified.

    Exactly I need to read from file :
    - Get values for all stocks : stock symbol, stock name, stock type (bond or common stock) ceiling price, floor price, open price, split (means stock has divident or not), benifit (means stock has action coporation), prior Close price, last price, last volume, last values, percent change, highest price, lowest price, Best 1 bid, Best 1 bid volume, Best 2 bid, Best 2 bid volume, Best 3 bid, Best 3 bid volume, Best 1 Offer, Best 1 Offer volume, Best 2 Offer, Best 2 Offer volume, Best 3 Offer, Best 3 Offer volume.

    for each stock read, I want to get values for all fields :
    -------
    last price, last volume, last values, percent change, highest price, lowest price, Best 1 bid, Best 1 bid volume, Best 2 bid, Best 2 bid volume, Best 3 bid, Best 3 bid volume, Best 1 Offer, Best 1 Offer volume, Best 2 Offer, Best 2 Offer volume, Best 3 Offer, Best 3 Offer volume

    Here struct binary file, it has contain 504 stocks :
    --------
    [StructLayout(LayoutKind.Sequential, Pack = 1)]
    public struct Struct_Security
    {
    //221: SECURITY.DAT
    //Ma CK dang so (2 bytes)
    public short StockNo;

    //Ma CK dang chuoi (8 bytes)
    [MarshalAs(UnmanagedType.ByValTStr, SizeConst =8)]
    public string StockSymbol;

    //Loai CK (S:Co phieu, D:Trai phieu, U:Chung chi quy) (1 byte)
    [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 1)]
    public string StockType;

    //Gia tran (4 bytes)
    public int Ceiling;
    //Gia san (4 bytes)
    public int Floor;

    //
    public double BigLotValue;


    //Ten day du cua CK
    [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 25)]
    public string SecurityName;

    [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 1)]
    public string SectorNo;

    [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 1)]
    //
    public string Designated;

    [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 1)]
    //CK bi ngung GD (Null: GD binh thuong, S: Bi ngung GD)
    public string Suspension;

    [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 1)]
    public string Delist;
    [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 1)]
    public string HaltResumeFlag;
    [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 1)]
    public string Split;
    [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 1)]
    public string Benefit;
    [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 1)]
    public string Meeting;
    [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 1)]
    public string Notice;

    [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 1)]
    public string ClientIDRequired;
    public short CouponRate;

    [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 6)]
    public string IssueDate;

    [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 6)]
    public string MatureDate;

    public int AvrPrice;

    public short ParValue;

    [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 1)]
    public string SDCFlag;

    public int PriorClosePrice;

    [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 6)]
    public string PriorCloseDate;

    public int ProjectOpen;

    public int OpenPrice;
    public int Last;
    public int LastVol;
    public double LastVal;
    public int Highest;
    public int Lowest;

    public double Totalshares;
    public double TotalValue;
    public short AccumulateDeal;
    public short BigDeal;
    public int BigVol;
    public double BigVal;
    public short OddDeal;
    public int OddVol;
    public double OddVal;

    //Single
    public int Best1Bid;
    public int Best1BidVolume;
    //Single
    public int Best2Bid;
    public int Best2BidVolume;
    //Single
    public int Best3Bid;
    public int Best3BidVolume;

    //Single
    public int Best1Offer;
    public int Best1OfferVolume;
    //Single
    public int Best2Offer;
    public int Best2OfferVolume;
    //Single
    public int Best3Offer;
    public int Best3OfferVolume;

    public short BoardLost;

    public static System.Collections.ArrayList ReaderBlock2Array(BinaryReader br)
    {
    System.Collections.ArrayList arr = new System.Collections.ArrayList();

    br.BaseStream.Position = 0;
    while (br.BaseStream.Position < br.BaseStream.Length)
    {
    byte[] buff = br.ReadBytes(TSSize.Size);
    GCHandle handle = GCHandle.Alloc(buff, GCHandleType.Pinned);
    Struct_Security s = (Struct_Security)Marshal.PtrToStructure(handle.Add rOfPinnedObject(), typeof(Struct_Security));

    handle.Free();
    arr.Add(s);
    }

    return arr;
    }
    }

    internal sealed class TSSize
    {
    public static int _size;

    static TSSize()
    {
    _size = Marshal.SizeOf(typeof(Struct_Security));

    }

    public static int Size
    {
    get
    {
    return _size;
    }
    }
    }


    Can u help me?

  7. #7
    Administrator
    Join Date
    Jul 2006
    Location
    Milan
    Posts
    1,091
    You should definitely rewrite the ExternalFeedSimulator class and get rid of the included ExternalFeedProducer class.
    The Data Adapter should receive all data and detect the changed data in order to send them to Lightstreamer.

    However, to keep the Data Adapter simple for demo purpose, the Data Adapter can also forward all data to Lightstreamer, which will be responsible for detecting the changed data and only send them to the client. Please consider that this is not recommended in a production scenario.
    The simplest structure might be:

    Code:
    constructor:
       create a map to hold all snapshots
          (let's create an IDictionary and call it _snaps)
    
    method Start:
       start a new thread with the following behaviour:
          every 5 seconds:
             read the file
             for each stock:
                create an IDictionary to store all field name/value pairs
                associate the IDictionary to the stock name in _snaps
                   (the previous association, if any, should be garbage collected)
                call onEvent on the listener and send the IDictionary 
    
    method SendCurrentValues:
       get the IDictionary associated to the requested item name in _snap
       if found:
          call onEvent on the listener and send the IDictionary
       else
          call onEvent on the listener with an empty IDictionary

 

 

Similar Threads

  1. Replies: 8
    Last Post: May 7th, 2008, 09:53 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
All times are GMT +1. The time now is 08:34 PM.