Exemplo n.º 1
0
        /// <summary>
        /// Get all instance of agents
        /// </summary>
        /// <returns></returns>
        public DeepRLAgentParameters GetById(long id)
        {
            DeepRLAgentParameters agent = null;

            using (var ctx = new DeepQStockContext())
            {
                ctx.Configuration.LazyLoadingEnabled   = false;
                ctx.Configuration.ProxyCreationEnabled = false;

                agent = ctx.DeepRLAgentParameters.SingleOrDefault(a => a.Id == id);

                ctx.Entry(agent).Reference(a => a.QNetwork).Load();
                ctx.Entry(agent).Reference(a => a.StockExchange).Load();
                ctx.Entry(agent).Collection(a => a.Decisions).Load();

                var lastDecision = agent.Decisions.LastOrDefault();

                if (lastDecision != null)
                {
                    ctx.Entry(lastDecision).Reference(d => d.Period).Load();
                }
            }

            return(agent);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Shutdowns this instance.
        /// </summary>
        public void Shutdown()
        {
            using (var DbContext = new DeepQStockContext())
            {
                var agent = DbContext.DeepRLAgentParameters.Single(a => a.Id == Agent.Parameters.Id);

                if (agent.Status == AgentStatus.Removed)
                {
                    DbContext.RemoveAgent(agent);
                }
                else if (agent.Status == AgentStatus.Paused || agent.Status == AgentStatus.Completed)
                {
                    Agent.SaveQNetwork();
                    CurrentState.StockExchangeId = Parameters.Id;


                    foreach (var p in CurrentState.InternalPeriods)
                    {
                        if (p.Id > 0 && !DbContext.IsAttached(p))
                        {
                            DbContext.Entry(p).State = EntityState.Unchanged;
                        }
                        else
                        {
                            DbContext.Periods.AddOrUpdate(p);
                        }
                    }

                    if (CurrentState.Id > 0)
                    {
                        var currentStateInDb = DbContext.States.Single(s => s.Id == CurrentState.Id);
                        DbContext.Entry(currentStateInDb).CurrentValues.SetValues(CurrentState);
                        DbContext.Entry(currentStateInDb).State = EntityState.Modified;
                    }
                    else
                    {
                        var oldState = DbContext.States.SingleOrDefault(s => s.StockExchangeId == Parameters.Id);
                        if (oldState != null)
                        {
                            DbContext.States.Remove(oldState);
                        }

                        DbContext.States.Add(CurrentState);
                    }

                    foreach (var indicator in DailyIndicators.Concat(WeeklyIndicators).Concat(MonthlyIndicators))
                    {
                        indicator.Save(DbContext);
                    }
                }

                DbContext.SaveChanges();
            }
        }
        /// <summary>
        /// Save the indicator
        /// </summary>
        /// <param name="ctx"></param>
        public virtual void Save(DeepQStockContext ctx)
        {
            var set   = ctx.Set(System.Type.GetType(ClassType));
            var dbObj = set.Find(Id);

            if (dbObj == null)
            {
                set.Add(this);
            }
            else
            {
                ctx.Entry(dbObj).CurrentValues.SetValues(this);
                ctx.Entry(dbObj).State = EntityState.Modified;
            }
        }
Exemplo n.º 4
0
        /// <summary>
        /// Save the indicator
        /// </summary>
        /// <param name="ctx"></param>
        public override void Save(DeepQStockContext ctx)
        {
            var dbObj = ctx.RSIs.Find(Id);

            if (dbObj == null)
            {
                ctx.RSIs.Add(this);
            }
            else
            {
                ctx.Entry(dbObj).CurrentValues.SetValues(this);
                ctx.Entry(dbObj).State = EntityState.Modified;

                dbObj.PreviousPeriod = PreviousPeriod;
            }
        }
Exemplo n.º 5
0
        /// <summary>
        /// Generates the a mini batch of randoms sample for train and update Q-network wegihts.
        /// </summary>
        /// <returns></returns>
        private IList <Experience> GenerateMiniBatch()
        {
            var experiences = new List <Experience>();

            if (MemoryReplay.Count <= Parameters.MiniBatchSize)
            {
                experiences = MemoryReplay.ToList();
            }
            else
            {
                var indexes = Enumerable.Range(0, MemoryReplay.Count - 1).OrderBy(x => RandomGenerator.Next());
                experiences = MemoryReplay.Where((e, i) => indexes.Contains(i)).ToList();
            }

            using (var ctx = new DeepQStockContext())
            {
                foreach (var experience in experiences)
                {
                    if (experience.Id > 0 && !ctx.IsAttached(experience))
                    {
                        ctx.Experiences.Attach(experience);
                    }

                    if (experience.From == null)
                    {
                        ctx.Entry(experience).Reference(e => e.From).Load();
                    }

                    if (experience.To == null)
                    {
                        ctx.Entry(experience).Reference(e => e.To).Load();
                    }
                }
            }

            return(experiences.ToList());
        }
Exemplo n.º 6
0
        /// <summary>
        /// Remove an state if exists
        /// </summary>
        /// <param name="state"></param>
        public void RemoveState(State state)
        {
            if (state != null)
            {
                if (state.Id > 0)
                {
                    using (var ctx = new DeepQStockContext())
                    {
                        if (!ctx.IsAttached(state))
                        {
                            state.InternalPeriods = null;
                            ctx.States.Attach(state);
                        }

                        ctx.Entry(state).State = EntityState.Deleted;
                        ctx.SaveChanges();
                    }
                }
            }
        }