예제 #1
0
 void processhistory(int lHandle,BarRequest br)
 {
     int numbars = esig.get_GetNumBars(lHandle);
     if (numbars == 0)
     {
         verb("no bars available for reqhandle: " + lHandle);
         return;
     }
     numbars *= -1;
     for (int i = numbars; i<=0; i++)
     {
         try
         {
             BarData bd = esig.get_GetBar(lHandle, i);
             if (VerboseDebugging)
                 verb(br.symbol + " " + bd.dtTime.ToString() + " " + bd.dOpen + " " + bd.dHigh + " " + bd.dLow + " " + bd.dClose + " " + bd.dVolume);
             Bar b = new BarImpl((decimal)bd.dOpen, (decimal)bd.dHigh, (decimal)bd.dLow, (decimal)bd.dClose, (long)bd.dVolume, Util.ToTLDate(bd.dtTime), Util.ToTLTime(bd.dtTime), br.symbol, br.Interval);
             string msg = BarImpl.Serialize(b);
             if (!b.isValid && !AllowSendInvalidBars)
             {
                 debug("Not sending invalid bar: " + b.ToString()+" raw: "+msg);
                 continue;
             }
             tl.TLSend(msg, MessageTypes.BARRESPONSE, br.Client);
         }
         catch (Exception ex)
         {
             verb("error obtaining bar: " + i + " from: " + lHandle);
             verb(ex.Message + ex.StackTrace);
         }
     }
     if (ReleaseBarHistoryAfteRequest)
     {
         try
         {
             esig.ReleaseHistory(lHandle);
             _barhandle2barrequest.Remove(lHandle);
         }
         catch { }
     }
 }
예제 #2
0
        void OnReceiveHist(IAsyncResult result)
        {
            try
            {
                int bytesReceived = m_hist.EndReceive(result);
                var records = getrecords(m_buffhist, bytesReceived);

                // mark record processing status
                var lastrecordprocessed = false;
                var lastrecordidx = records.Length - 1;

                // process records
                for (int i = 0; i<records.Length; i++)
                {
                    // get record
                    var rec = records[i];
                    // mark it's status
                    lastrecordprocessed = false;
                    // skip empty data
                    if (string.IsNullOrWhiteSpace(rec))
                        continue;
                    // look for misc responses
                    if (checkhistproto && rec.Contains(expecthistproto))
                    {
                        checkhistproto = false;
                        rec = rec.Replace(expecthistproto, string.Empty);
                        records[i] = rec;
                        lastrecordprocessed = i == lastrecordidx;
                        v("hist protocol ack:" + expecthistproto);
                    }
                    string[] r = rec.Split(',');
                    // test for request id and response type
                    BarRequest br = new BarRequest();
                    var prev = (i == 0) ? string.Empty : records[i - 1];
                    if (isrecorddone(rec, r, prev, out br))
                    {
                        v("hist processed: " + rec);
                        lastrecordprocessed = i == lastrecordidx;
                        continue;
                    }
                    else if (i==lastrecordidx)
                    {
                        lastrecordprocessed = false;
                    }

                    Bar b = new BarImpl();

                    try
                    {
                        if (br.isValid)
                        {
                            if (br.BarInterval == BarInterval.Day)
                            {
                                DateTime dt = DateTime.MinValue;
                                if (!DateTime.TryParse(r[1], System.Globalization.CultureInfo.InvariantCulture, System.Globalization.DateTimeStyles.NoCurrentDateDefault, out dt))
                                {
                                    v("hist can't parse date: " + r[1] + " data: " + rec);
                                    continue;
                                }
                                decimal o, h, l, c;
                                long vol;
                                if (!decimal.TryParse(r[2], System.Globalization.NumberStyles.Number, System.Globalization.CultureInfo.InvariantCulture, out h))
                                {
                                    v("hist can't parse high: " + r[2] + " data: " + rec);
                                    continue;
                                }
                                else if (!decimal.TryParse(r[3], System.Globalization.NumberStyles.Number, System.Globalization.CultureInfo.InvariantCulture, out l))
                                {
                                    v("hist can't parse low: " + r[3] + " data: " + rec);
                                    continue;
                                }
                                else if (!decimal.TryParse(r[4], System.Globalization.NumberStyles.Number, System.Globalization.CultureInfo.InvariantCulture, out o))
                                {
                                    v("hist can't parse open: " + r[4] + " data: " + rec);
                                    continue;
                                }
                                else if (!decimal.TryParse(r[5], System.Globalization.NumberStyles.Number, System.Globalization.CultureInfo.InvariantCulture, out c))
                                {
                                    v("hist can't parse close: " + r[5] + " data: " + rec);
                                    continue;
                                }
                                else if (!long.TryParse(r[6], System.Globalization.NumberStyles.Number, System.Globalization.CultureInfo.InvariantCulture, out vol))
                                {
                                    v("hist can't parse vol: " + r[6] + " data: " + rec);
                                    continue;
                                }
                                // mark iqfeed processing status
                                lastrecordprocessed = i == lastrecordidx;
                                // build bar
                                b = new BarImpl(o, h, l, c, vol, Util.ToTLDate(dt), Util.ToTLTime(dt), br.symbol, br.Interval,br.CustomInterval,br.ID);
                                if (VerboseDebugging)
                                    debug("hist got bar: " + b);
                            }
                            else
                            {
                                DateTime dt = DateTime.MinValue;
                                if (!DateTime.TryParse(r[1], System.Globalization.CultureInfo.InvariantCulture, System.Globalization.DateTimeStyles.NoCurrentDateDefault, out dt))
                                {
                                    v("hist can't parse date: " + r[1] + " data: " + rec);
                                    continue;
                                }
                                decimal o, h, l, c;
                                long vol;
                                if (!decimal.TryParse(r[2], System.Globalization.NumberStyles.Number, System.Globalization.CultureInfo.InvariantCulture, out h))
                                {
                                    v("hist can't parse high: " + r[2] + " data: " + rec);
                                    continue;
                                }
                                else if (!decimal.TryParse(r[3], System.Globalization.NumberStyles.Number, System.Globalization.CultureInfo.InvariantCulture, out l))
                                {
                                    v("hist can't parse low: " + r[3] + " data: " + rec);
                                    continue;
                                }
                                else if (!decimal.TryParse(r[4], System.Globalization.NumberStyles.Number, System.Globalization.CultureInfo.InvariantCulture, out o))
                                {
                                    v("hist can't parse open: " + r[4] + " data: " + rec);
                                    continue;
                                }
                                else if (!decimal.TryParse(r[5], System.Globalization.NumberStyles.Number, System.Globalization.CultureInfo.InvariantCulture, out c))
                                {
                                    v("hist can't parse close: " + r[5] + " data: " + rec);
                                    continue;
                                }
                                else if (!long.TryParse(r[7], System.Globalization.NumberStyles.Number, System.Globalization.CultureInfo.InvariantCulture, out vol))
                                {
                                    v("hist can't parse vol: " + r[7] + " data: " + rec);
                                    continue;
                                }
                                // mark iqfeed processing status
                                lastrecordprocessed = i == lastrecordidx;
                                // build bar
                                b = new BarImpl(o, h, l, c, vol, Util.ToTLDate(dt), Util.ToTLTime(dt), br.symbol, br.Interval,br.CustomInterval,br.ID);
                                if (VerboseDebugging)
                                    debug("hist got bar: " + b);

                            }
                        }
                        
                    }
                    catch (Exception ex)
                    {
                        v("hist ohlc parse err: " + ex.Message + ex.StackTrace + " data: " + rec);
                        b = new BarImpl();
                    }
                    // send it
                    if (b.isValid)
                    {
                        var barserial = BarImpl.Serialize(b);
                        if (VerboseDebugging)
                            debug("hist sending response to: "+br.ID+" int: "+ b.Interval + " bar: " + b.ToString() + " to: " + br.Client + " using data: " + rec);
                        try
                        {
                            tl.TLSend(barserial, MessageTypes.BARRESPONSE, br.Client);
                            if (VerboseDebugging)
                                debug("hist sent " + b.Symbol + " bar in response to: " + br.ID);
                        }
                        catch (Exception ex)
                        {
                            debug("hist send error: " + ex.Message + ex.StackTrace + " on: " + barserial + " to: " + br.Client);
                        }
                    }
                    
                }
                if (!lastrecordprocessed)
                {
                    string lastrecord = records[lastrecordidx];
                    var endidx = lastrecord.IndexOf(HISTEND);
                    if (endidx >= 0)
                    {
                        lastrecord = lastrecord.Substring(endidx, HISTEND.Length);
                        v("hist got " + HISTEND);
                    }
                    if (!string.IsNullOrWhiteSpace(lastrecord))
                    {
                        _histbuff = lastrecord;
                        v("hist saving: " + _histbuff + " for more data.");
                    }
                }
                // wait for more historical data
                WaitForData(HISTSOCKET);
            }
            catch (SocketException)
            {
                v("hist connection closed.");
            }
            catch (Exception ex)
            {
                debug(ex.Message + ex.StackTrace);
            }
        }