Example #1
0
        private void Insert()
        {
            {
                try
                {
                    var sw = Stopwatch.StartNew();

                    Time t = DateTime.Now;
                    Task.Run(
                        () =>
                    {
                        if (!System.IO.File.Exists(ConnectionString))
                        {
                            using (var tf = TeaFile <Tick> .Create(ConnectionString))
                            {
                                tf.Write(
                                    Enumerable.Range(1, 100).Select(
                                        i => new Tick
                                {
                                    Time   = t.AddDays(i),
                                    Price  = i * 101.0,
                                    Volume = i * 1000
                                }));
                            }
                        }
                        else
                        {
                            using (var tf = TeaFile <Tick> .Append(ConnectionString))
                            {
                                tf.Write(
                                    Enumerable.Range(1, 100).Select(
                                        i => new Tick
                                {
                                    Time   = t.AddDays(i),
                                    Price  = i * 101.0,
                                    Volume = i * 1000
                                }));
                            }
                        }
                    })
                    .ContinueWith(x =>
                    {
                        sw.Stop();
                    }).ContinueWith(x => ExecutionTime.Value = sw.ElapsedMilliseconds, TaskScheduler.FromCurrentSynchronizationContext());

                    sw.Stop();
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }
            }
        }
Example #2
0
        public void Scan()
        {
            foreach (var underlying in _underlyings)
            {
                var contracts = OptionChainProvider.GetOptionContractList(underlying, Time);

                var underlyingPrice = Portfolio[underlying].Price;

                var specificContracts = (from contract in contracts
                                         where contract.ID.OptionStyle == OptionStyle.American &&
                                         contract.ID.OptionRight == OptionRight.Call &&
                                         contract.ID.StrikePrice > underlyingPrice &&
                                         contract.ID.Date > Time.AddDays(15) &&
                                         contract.ID.Date < Time.AddDays(60) &&
                                         contract.ID.Date == new DateTime(2017, 10, 20)
                                         select contract).OrderBy(c => c.ID.StrikePrice).FirstOrDefault();

                if (specificContracts != null)
                {
                    var optionContract = AddOptionContract(specificContracts, Resolution.Minute);
                    Manager[underlying] = new Position()
                    {
                        Ticker          = underlying,
                        Security        = optionContract,
                        OptionPrice     = optionContract.Close,
                        Symbol          = optionContract.Symbol,
                        UnderlyingPrice = underlyingPrice
                    };
                }
            }
        }
Example #3
0
        static void Main(string[] args)
        {
            try
            {
                if (args.Count() != 2)
                {
                    Console.WriteLine("Usage: CreateTicks <filename> <number of ticks>");
                    return;
                }
                string filename = args[0];
                int    n        = int.Parse(args[1]);

                var sw = Stopwatch.StartNew();

                Time t = DateTime.Now;
                using (var tf = TeaFile <Tick> .Create(filename))
                {
                    tf.Write(Enumerable.Range(1, n).Select(i => new Tick {
                        Time = t.AddDays(i), Price = i * 101.0, Volume = i * 1000
                    }));
                }

                sw.Stop();
                Console.WriteLine("execution time = " + sw.Elapsed.TotalMilliseconds + "ms");
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }
Example #4
0
        public override void Initialize()
        {
            SetStartDate(2020, 1, 4);
            SetEndDate(2020, 1, 6);

            _es20h20 = AddFutureContract(
                QuantConnect.Symbol.CreateFuture(Futures.Indices.SP500EMini, Market.CME, new DateTime(2020, 3, 20)),
                Resolution.Minute).Symbol;

            _es19m20 = AddFutureContract(
                QuantConnect.Symbol.CreateFuture(Futures.Indices.SP500EMini, Market.CME, new DateTime(2020, 6, 19)),
                Resolution.Minute).Symbol;

            var optionChains = OptionChainProvider.GetOptionContractList(_es20h20, Time.AddDays(1))
                               .Concat(OptionChainProvider.GetOptionContractList(_es19m20, Time));

            foreach (var optionContract in optionChains)
            {
                _expectedSymbolsReceived.Add(AddFutureOptionContract(optionContract, Resolution.Minute).Symbol);
            }

            if (_expectedSymbolsReceived.Count == 0)
            {
                throw new InvalidOperationException("Expected Symbols receive count is 0, expected >0");
            }
        }
Example #5
0
        public LogLineItem(string ns, string line, DateTime date)
        {
            Namespace = ns;
            Line      = line;
            DateTime time;

            Time = (line.Length > 20 && DateTime.TryParse(Line.Substring(2, 16), out time)) ? date.Date.Add(time.TimeOfDay) : date;
            if (Time > DateTime.Now)
            {
                Time = Time.AddDays(-1);
            }
        }
Example #6
0
        public void ShouldAddDays()
        {
            // Given
            const double value = 123.456;
            var          time  = new Time(12345);

            // When
            var result = time.AddDays(value);

            // Then
            Assert.AreEqual(time.TotalSeconds + value * 24 * 60 * 60, result.TotalSeconds, TimeInaccuracy);
        }
Example #7
0
 static void CreateSampleFile(string filename, int n)
 {
     r = new Random(1);
     var t = new Time(2000, 1, 1);
     File.Delete(filename);
     using (var tf = TeaFile<Tick>.Create(filename))
     {
         while (n-- > 0)
         {
             bool isGoodDay = DrawRandom(90);
             WriteDailyTicks(tf, t, isGoodDay);
             t = t.AddDays(1);
         }
     }
 }
        static void CreateSampleFile(string filename, int n)
        {
            r = new Random(1);
            var t = new Time(2000, 1, 1);

            File.Delete(filename);
            using (var tf = TeaFile <Tick> .Create(filename))
            {
                while (n-- > 0)
                {
                    bool isGoodDay = DrawRandom(90);
                    WriteDailyTicks(tf, t, isGoodDay);
                    t = t.AddDays(1);
                }
            }
        }
        public void Scan()
        {
            foreach (var underlying in _underlyings)
            {
                var contracts = OptionChainProvider.GetOptionContractList(underlying, Time);

                var underlyingPrice = Portfolio[underlying].Price;

                var specificContract = (from contract in contracts
                                        where contract.ID.OptionStyle == OptionStyle.American &&
                                        contract.ID.OptionRight == OptionRight.Call &&
                                        contract.ID.StrikePrice > underlyingPrice &&
                                        contract.ID.Date > Time.AddDays(15) &&
                                        contract.ID.Date < Time.AddDays(60) &&
                                        contract.ID.Date == new DateTime(2017, 10, 20)
                                        select contract).OrderBy(c => c.ID.StrikePrice).FirstOrDefault();

                if (specificContract != null)
                {
                    Option optionContract = null;
                    if ((Securities.ContainsKey(specificContract) && (Securities[specificContract].Invested)))
                    {
                        optionContract = (Option)Securities[specificContract];
                    }
                    else
                    {
                        optionContract = AddOptionContract(specificContract, Resolution.Minute);
                    }
                    Manager[underlying] = new CoveredCallPosition()
                    {
                        Ticker           = underlying,
                        OptionSecurity   = optionContract,
                        OptionPrice      = optionContract.Close,
                        UnderlyingSymbol = optionContract.Symbol,
                        UnderlyingPrice  = underlyingPrice
                    };


                    // Do Stuff
                    if (!optionContract.Invested)
                    {
                        RemoveSecurity(optionContract.Symbol);
                    }
                }
            }
        }
Example #10
0
        public LogEntry(string logFile, string line)
        {
            LogFile = logFile;
            Line    = line;
            Match match = LogEntryRegex.LogEntryStructure.Match(line);

            if (match.Success)
            {
                DateTime time;
                var      ts = match.Groups["timestamp"].Value;
                if (DateTime.TryParse(ts, out time))
                {
                    Time = DateTime.Today.Add(time.TimeOfDay);
                    if (Time > DateTime.Now)
                    {
                        Time = Time.AddDays(-1);
                    }
                }
                LineContent = match.Groups["line"].Value;
            }
        }
Example #11
0
        public LogLineItem(string ns, string line)
        {
            Namespace = ns;
            Line      = line;
            var regex = new Regex("^(D|W) (?<ts>([\\d:.]+)) (?<line>(.*))$");
            var match = regex.Match(line);

            if (match.Success)
            {
                DateTime time;
                var      ts = match.Groups["ts"].Value;
                if (DateTime.TryParse(ts, out time))
                {
                    Time = DateTime.Today.Add(time.TimeOfDay);
                    if (Time > DateTime.Now)
                    {
                        Time = Time.AddDays(-1);
                    }
                }
                LineContent = match.Groups["line"].Value;
            }
        }
Example #12
0
        public Line(string logName, string line)
        {
            LogName = logName;
            RawLine = line ?? string.Empty;
            Text    = line ?? string.Empty;
            Time    = DateTime.Now;

            var match = LineRegex.Match(line);

            if (match.Success)
            {
                Text = match.Groups["text"].Value;
                if (DateTime.TryParse(match.Groups["ts"].Value, out var time))
                {
                    Time = DateTime.Today.Add(time.TimeOfDay);
                    if (Time > DateTime.Now)
                    {
                        Time = Time.AddDays(-1);
                    }
                }
            }
            IsValid = match.Success;
        }
Example #13
0
        private void LiquidateExpiredOptions()
        {
            foreach (var option in Portfolio.Values)
            {
                if (!option.Invested)
                {
                    continue;
                }

                if (option.Type != SecurityType.Option)
                {
                    Liquidate(option.Symbol);
                    continue;
                }

                var contract = (Option)Portfolio.Securities[option.Symbol];
                if (contract.Expiry < Time.AddDays(1))
                {
                    Debug($"Date: {Time} - Liquidating {option.Symbol}");
                    Liquidate(contract.Symbol);
                }
            }
        }
        public void NextCycle()
        {
            _stopWatch.Restart();
            MatingTime();
            _stopWatch.Stop();

            _totalMatingTimeInMilliseconds += _stopWatch.ElapsedMilliseconds;

            _stopWatch.Restart();
            BirthTime();
            _stopWatch.Stop();

            _totalBirthTimeTimeInMilliseconds += _stopWatch.ElapsedMilliseconds;

            _stopWatch.Restart();
            DeathTime();
            _stopWatch.Stop();

            _totalDeathTimeTimeInMilliseconds += _stopWatch.ElapsedMilliseconds;

            Time = Time.AddDays(1);

            CycleCounter++;
        }
Example #15
0
 public void IOException()
 {
     var stream = new TestStream();
     using (var tf = TeaFile<Event<int>>.Create(stream))
     {
         Time t = new Time(2000, 1, 1);
         tf.Write(Enumerable.Range(1, 5).Select(i => new Event<int> {Time = t.AddDays(i), Value = i}));
     }
     stream.Position = 0;
     stream.FailAfterPosition = stream.Length - 2;
     using (var tf = TeaFile<Event<int>>.OpenRead(stream))
     {
         tf.Count.Should().Be(5); // the count is still 5, the stream length has not changed
         tf.Read().Value.Should().Be(1);
         tf.Read().Value.Should().Be(2);
         tf.Read().Value.Should().Be(3);
         tf.Read().Value.Should().Be(4);
         Executing.This(() => tf.Read()).Should().Throw<IOException>();
     }
 }
Example #16
0
        public void AddDays()
        {
            Time.Scale = Timescale.Net;
            Time t = new Time(2010, 2, 3);
            Time t2 = t.AddDays(5);
            (t2.NetTime - t.NetTime).TotalDays.Should().Be(5);

            Time.Scale = Timescale.Java;
            t = new Time(2010, 2, 3);
            t2 = t.AddDays(5);
            (t2.NetTime - t.NetTime).TotalDays.Should().Be(5);
        }