Ejemplo n.º 1
0
        private void SaveOandaCandle(CandlestickDao dao, OandaCandle oandaCandle)
        {
            var entity = dao.CreateNewEntity();

            entity.Instrument  = this.Instrument;
            entity.Granularity = this.Granularity;
            entity.DateTime    = oandaCandle.DateTime;
            entity.Open        = oandaCandle.openMid;
            entity.High        = oandaCandle.highMid;
            entity.Low         = oandaCandle.lowMid;
            entity.Close       = oandaCandle.closeMid;
            entity.Volume      = oandaCandle.volume;
            try {
                entity.Save();
            } catch (NpgsqlException e) {
                if (e.Code == "23505")
                {
                    Console.WriteLine(e.Message);
                    throw new RetryException();
                }
                else
                {
                    throw e;
                }
            }
        }
Ejemplo n.º 2
0
        private void SaveNullCandle(CandlestickDao dao, DateTime t)
        {
            var entity = dao.CreateNewEntity();

            entity.Instrument  = this.Instrument;
            entity.Granularity = this.Granularity;
            entity.DateTime    = t;
            entity.Open        = 0;
            entity.High        = 0;
            entity.Low         = 0;
            entity.Close       = 0;
            entity.Volume      = 0;

            try {
                entity.Save();
            } catch (NpgsqlException e) {
                if (e.Code == "23505")
                {
                    Console.WriteLine(e.Message);
                    throw new RetryException();
                }
                else
                {
                    throw e;
                }
            }
        }
Ejemplo n.º 3
0
        public IEnumerable <Candlestick> Execute()
        {
            List <Candlestick> result = new List <Candlestick>();

            TimeSpan granularitySpan = GetGranularitySpan();

            if (Count != -1)
            {
                End = Start.AddTicks(granularitySpan.Ticks * Count);
            }

            var dao = new CandlestickDao();

            while (true)
            {
                try {
                    using (var transaction = DBUtils.GetConnection().BeginTransaction()) {
                        DateTime t = GetAlignTime(Start);

                        foreach (var entity in dao.GetBy(Instrument, Granularity, t, End).ToList())
                        {
                            if (entity.DateTime != t)
                            {
                                foreach (var oandaCandle in GetCandles(t, entity.DateTime.AddSeconds(-1)))
                                {
                                    t = SaveAndAdd(result, granularitySpan, dao, t, oandaCandle);
                                }
                                FillNullCandles(result, granularitySpan, dao, t, entity.DateTime);
                                t = entity.DateTime;
                            }
                            result.Add(entity.Candlestick);
                            t = t.Add(granularitySpan);
                        }
                        if (t < End)
                        {
                            foreach (var oandaCandle in GetCandles(t, End))
                            {
                                t = SaveAndAdd(result, granularitySpan, dao, t, oandaCandle);
                            }
                            FillNullCandles(result, granularitySpan, dao, t, End);
                        }
                        transaction.Commit();
                    }
                    break;
                } catch (RetryException) {
                    continue;
                }
            }
            return(result);
        }
Ejemplo n.º 4
0
        private void SaveOandaCandle(CandlestickDao dao, OandaCandle oandaCandle)
        {
            var entity = dao.CreateNewEntity();

            entity.Instrument  = this.Instrument;
            entity.Granularity = this.Granularity;
            entity.DateTime    = oandaCandle.DateTime;
            entity.Open        = oandaCandle.openMid;
            entity.High        = oandaCandle.openMid;
            entity.Low         = oandaCandle.openMid;
            entity.Close       = oandaCandle.openMid;
            entity.Volume      = oandaCandle.volume;

            entity.Save();
        }
Ejemplo n.º 5
0
        public IEnumerable <Candlestick> Execute()
        {
            List <Candlestick> result = new List <Candlestick>();

            TimeSpan granularitySpan = GetGranularitySpan();

            if (Count != -1)
            {
                End = Start.AddTicks(granularitySpan.Ticks * Count);
            }

            var dao = new CandlestickDao();

            using (var transaction = DBUtils.GetConnection().BeginTransaction()) {
                DateTime t = GetAlignTime(Start);

                foreach (var entity in dao.GetBy(Instrument, Granularity, t, End).ToList())
                {
                    if (entity.DateTime != t)
                    {
                        foreach (var oandaCandle in GetCandles(t, entity.DateTime.AddSeconds(-1)))
                        {
                            SaveOandaCandle(dao, oandaCandle);
                            result.Add(oandaCandle.Candlestick);
                        }
                        t = entity.DateTime;
                    }
                    result.Add(entity.Candlestick);
                    t = t.Add(granularitySpan);
                }
                if (t < End)
                {
                    foreach (var oandaCandle in GetCandles(t, End))
                    {
                        SaveOandaCandle(dao, oandaCandle);
                        result.Add(oandaCandle.Candlestick);
                    }
                }
                transaction.Commit();
            }
            return(result);
        }
Ejemplo n.º 6
0
 private DateTime SaveAndAdd(List <Candlestick> result, TimeSpan granularitySpan, CandlestickDao dao, DateTime t, OandaCandle oandaCandle)
 {
     while (t < oandaCandle.DateTime)
     {
         SaveNullCandle(dao, t);
         result.Add(new Candlestick()
         {
             DateTime = t, Open = 0
         });
         t = t.Add(granularitySpan);
     }
     SaveOandaCandle(dao, oandaCandle);
     result.Add(oandaCandle.Candlestick);
     t = t.Add(granularitySpan);
     return(t);
 }
Ejemplo n.º 7
0
 private void FillNullCandles(List <Candlestick> result, TimeSpan granularitySpan, CandlestickDao dao, DateTime t, DateTime endTime)
 {
     while (t < endTime)
     {
         SaveNullCandle(dao, t);
         result.Add(new Candlestick()
         {
             DateTime = t, Open = 0
         });
         t = t.Add(granularitySpan);
     }
 }