public Bar GetBar(int index, string symbol)
 {
     Bar b = new Bar();
     if (index >= _Count) return b;
     else if (index < 0)
     {
         index = _Count - 1 + index;
         if (index < 0) return b;
     }
     b = new Bar(opens[index], highs[index], lows[index], closes[index], vols[index], dates[index], orders[index], symbol, intervallength);
     if (index == Last()) b.IsNew = _isRecentNew;
     return b;
 }
Пример #2
0
 void _client_GotHistoricalBarDelegate(Bar obj)
 {
     _eventAggregator.GetEvent<HistBarEvent>().Publish(obj);
 }
Пример #3
0
 static void _iclient_GotHistoricalBarDelegate(Bar b)
 {
     // Find the security file
     if (_sec2bars.ContainsKey(b.FullSymbol))
     {
         DateTime d = Util.ToDateTime(b.Date, b.BarStartTime);
         DateTime d_local = DateTime.SpecifyKind(d, DateTimeKind.Local);         // to local time
         string line = d_local.ToString("yyyy/MM/dd HH:mm:ss") + "," +b.Open + "," +  b.High + "," + b.Low + "," + b.Close + "," + b.Volume;
         
         _sec2bars[b.FullSymbol].Add(line);
         _processedBars[b.FullSymbol]++;
     }
 }
Пример #4
0
        /// <summary>
        /// http://www.marketcalls.in/database/google-realtime-intraday-backfill-data.html
        /// http://www.codeproject.com/KB/IP/google_finance_downloader.aspx
        /// </summary>
        /// <param name="br"></param>
        public void RequestHistoricalData(BarRequest br, bool useRTH=false)
        {
            // Google always returns the most recent data. 
            // i is interval in seconds, set to 60s, ignore interval
            // ignore date; p: period
            // q is the symbol (AAPL)
            // x is the exchange (NASD)
            // sessions is the session requested (ext_hours)
            // p is the time period (5d = 5 days), set to 1d, ignore time span
            // f is the requested fields (d,c,v,o,h,l)
            // df = (cpct)
            // auto = (1)
            // ts is potentially a time stamp (1324323553 905) or time shift

            try
            {
                using (WebClient client = new WebClient())
                {
                    string google;
                    if (br.Interval != 86400)
                    {
                       google = @"https://www.google.com/finance/getprices?i="+br.Interval.ToString()+@"&p=1d&f=d,o,h,l,c,v&df=cpct&q=";
                    }
                    else   // for oneday, today is empty
                    {
                        google = @"https://www.google.com/finance/getprices?i=86400&p=2d&f=d,o,h,l,c,v&df=cpct&q=";
                    }

                    string[] symbol = br.FullSymbol.Split(' ');

                    System.IO.Stream data = client.OpenRead(google + symbol[0]);
                    System.IO.StreamReader read = new System.IO.StreamReader(data);

                    string[] lines = new string[] { read.ReadToEnd() };
                    string[] lines2 = lines[0].Split('\n');

                    // get time zone adjustment
                    // In line 6, GOOG has time zone offset = -240 which is new york time; 
                    //      while SPX has time zone offset = -300, which is chicago time.
                    // The following find the additional offset relative to local time.
                    /*
                    int localtimezonediffinminutes = (int)Util.GetUtcOffset(DateTime.Today).TotalMinutes;       // negative offset
                    string stime = lines[6];
                    int itime;
                    bool btime = Int32.TryParse(stime.Substring(stime.IndexOf('=') + 1), out itime);        // negative offset
                    int additionaloffset = localtimezonediffinminutes - itime;          // (-240) - (-300) = 60 mins
                    */

                    IEnumerable<string> history = lines2.Skip(7);        // skip the first 7 lines: header

                    int nlines = 0;                  // count of lines
                    string[] entries;
                    DateTime dstart = DateTime.Now;        // just for initialization
                    DateTime dt = DateTime.Now;

                    foreach (string line in history)
                    {
                        if (!string.IsNullOrEmpty(line))                 // skip empty lines, i.e., the last line
                        {
                            entries = line.Split(',');

                            if (nlines == 0)
                            {
                                // http://www.epochconverter.com/
                                dt = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
                                double secs = double.Parse(entries[0].Remove(0, 1));        // remove character 'a'
                                dt = dt.AddSeconds(secs); 
                                // GMT to EST
                                // dstart = TimeZoneInfo.ConvertTimeFromUtc(dt1, TimeZoneInfo.FindSystemTimeZoneById("Eastern Standard Time"));
                                dt = TimeZoneInfo.ConvertTimeFromUtc(dt, TimeZoneInfo.Local);
                                dstart = dt;
                            }
                            else
                            {
                                dstart = dt.AddSeconds(Int32.Parse(entries[0])*br.Interval);
                            }
                            
                            nlines++;
                            // write line to database
                            Bar bar = new Bar();
                            bar.Interval = 1;       // 1 sec

                            bar.FullSymbol = br.FullSymbol;

                            bar.Open = decimal.Parse(entries[4]);
                            bar.Date = Util.ToIntDate(dstart);
                            bar.BarOrderInADay = bar.GetOrder(Util.ToIntTime(dstart));
                            bar.High = decimal.Parse(entries[2]);
                            bar.Low = decimal.Parse(entries[3]);
                            bar.Close = decimal.Parse(entries[1]);
                            bar.Volume = long.Parse(entries[5]);

                            if (GotHistoricalBarDelegate != null)
                                GotHistoricalBarDelegate(bar);
                        }
                    }
                }
            }
            catch (Exception e)
            {
                Debug("Error in requesting historical data from Google client.");
                Debug(e.Message);
            }
        }
 /// <summary>
 /// Previous day daily bar
 /// </summary>
 private void ClientGotHistBar(Bar br)
 {
     foreach (StrategyItem si in _strategyitemlist)
     {
         si.S.GotHistoricalBar(br);
     }
 }
 public void AddBar(Bar mybar)
 {
     _Count++;
     closes.Add(mybar.Close);
     opens.Add(mybar.Open);
     dates.Add(mybar.Date);
     highs.Add(mybar.High);
     lows.Add(mybar.Low);
     vols.Add(mybar.Volume);
     orders.Add(GetOrder(mybar.BarStartTime,intervallength));
     ids.Add(GetBarId(mybar.BarStartTime, mybar.Date, intervallength));
 }
Пример #7
0
        public virtual void historicalData(int reqId, string date, double open, double high, double low, double close, int volume, int count, double WAP, bool hasGaps)
        {
            if (reqId < 0 || reqId > _historicalBarRequests.Count)
            {
                OnDebug("historical data request doesn't match");
                return;
            }
            // yyyyMMdd{space}{space}HH:mm:ss
            DateTime dt;
            if (!DateTime.TryParseExact(date, "yyyyMMdd  HH:mm:ss", CultureInfo.InvariantCulture, DateTimeStyles.None, out dt))
            {
                // only 1s and 1d are supported at this time. so it must be 1d
                dt = DateTime.ParseExact(date, "yyyyMMdd", CultureInfo.InvariantCulture);
            }

            int ndate = dt.Year * 10000 + dt.Month * 100 + dt.Day;
            int ntime = dt.Hour * 10000 + dt.Minute * 100 + dt.Second;

            Bar bar = new Bar((decimal)open, (decimal)high, (decimal)low, (decimal)close,
                    volume, ndate, ntime,
                    _historicalBarRequests[reqId].FullSymbol, _historicalBarRequests[reqId].Interval);
            bar.TradesInBar = count;
            // bar.WAP = (decimal)average;

            if (bar.IsValid)
            {
                OnGotHistoricalBar(bar);
            }
        }
Пример #8
0
 void OnGotHistoricalBar(Bar b)
 {
     if (GotHistoricalBarDelegate != null)
         GotHistoricalBarDelegate(b);
 }
Пример #9
0
 public static Tick[] ToTick(Bar bar)
 {
     List<Tick> list = new List<Tick>();
     list.Add(Tick.NewTrade(bar.FullSymbol, bar.Date, bar.BarStartTime, bar.Open,
         (int)((double)bar.Volume / 4)));
     list.Add(Tick.NewTrade(bar.FullSymbol, bar.Date, bar.BarStartTime, bar.High,
         (int)((double)bar.Volume / 4)));
     list.Add(Tick.NewTrade(bar.FullSymbol, bar.Date, bar.BarStartTime, bar.Low,
         (int)((double)bar.Volume / 4)));
     list.Add(Tick.NewTrade(bar.FullSymbol, bar.Date, bar.BarStartTime, bar.Close,
         (int)((double)bar.Volume / 4)));
     return list.ToArray();
 }
Пример #10
0
 public static Tick[] ToTick2(Bar bar)
 {
     List<Tick> list = new List<Tick>();
     DateTime dt = Util.ToDateTime(bar.Date, bar.BarStartTime).AddSeconds(bar.Interval);   // close at bartime+interval
     list.Add(Tick.NewTrade(bar.FullSymbol, Util.ToIntDate(dt), Util.ToIntTime(dt),
         bar.Close, (int)bar.Volume));
     return list.ToArray();
 }
Пример #11
0
 /// <summary>
 /// called when historical bar arrives.   
 /// </summary>
 public virtual void GotHistoricalBar(Bar b)
 {
 }
Пример #12
0
 internal static void AddBar(BarList b, Bar mybar, int instdataidx)
 {
     b._intervaldata[instdataidx].AddBar(mybar);
 }
Пример #13
0
        /// <summary>
        /// insert a bar at particular place in the list.
        /// REMEMBER YOU MUST REHANDLE GOTNEWBAR EVENT AFTER CALLING THIS.
        /// </summary>
        public static BarList InsertBar(BarList bl, Bar b, int position)
        {

            BarList copy = new BarList(bl.FullSymbol, bl.AvailableIntervals);
            for (int j = 0; j < bl.AvailableIntervals.Length; j++)
            {
                if (bl.AvailableIntervals[j] != b.Interval)
                    continue;
                int count = bl.IntervalCount(b.Interval);
                if (count > 0)
                {
                    for (int i = 0; i < count; i++)
                    {
                        if (i == position)
                        {
                            AddBar(copy, b, j);
                        }
                        AddBar(copy, bl[i, b.Interval], j);
                    }
                }
                else
                    AddBar(copy, b, 0);
            }
            return copy;
        }
Пример #14
0
 private void ProcessPreviousClose(Bar b)
 {
     _quoteupdateservice.InitTickerAndPreClose(new string[] { b.FullSymbol }, new decimal[] { b.Close });
 }
Пример #15
0
 public override void GotHistoricalBar(Bar b)
 {
     if (_precloseDict.ContainsKey(b.FullSymbol))
     {
         _precloseDict[b.FullSymbol] = b.Close;
     }
     else
     {
         _precloseDict.Add(b.FullSymbol, b.Close);
     }
 }