Exemplo n.º 1
0
 /// <summary>
 /// Initializes a new instance of the <see cref="StockExchangeParameters"/> class.
 /// </summary>
 public StockExchangeParameters()
 {
     PeriodTypes        = new PeriodType[] { PeriodType.Day, PeriodType.Week, PeriodType.Month };
     EpisodeLength      = 7;
     NumberOfPeriods    = 14;
     InitialCapital     = 100000;
     TransactionCost    = 0.01;
     SimulationVelocity = 0;
     RewardCalculator   = RewardCalculator.Use(RewardCalculatorType.WinningsOverLoosings);
 }
Exemplo n.º 2
0
        /// <summary>
        /// Executes the specified action.
        /// </summary>
        /// <param name="action">The action.</param>
        /// <returns></returns>
        private double Execute(ActionType action, double inOutStrategy)
        {
            var actionPrice      = CurrentState.Today.Close;
            var capital          = CurrentState.Today.CurrentCapital;
            var position         = CurrentState.Today.ActualPosition;
            var previuosPosition = position;

            if (action == ActionType.Buy && capital > 0)
            {
                var availableMoney = inOutStrategy * capital;
                if (availableMoney >= actionPrice)
                {
                    VolumeOperated = (int)Math.Truncate(availableMoney / actionPrice);
                    var actionsCost = VolumeOperated * actionPrice;
                    TransactionCost = actionsCost * Parameters.TransactionCost;
                    var operationCost = actionsCost + TransactionCost;

                    if (operationCost <= capital)
                    {
                        CurrentState.Today.ActualPosition += VolumeOperated;
                        CurrentState.Today.CurrentCapital -= operationCost;
                    }
                }
            }
            else if (action == ActionType.Sell && position > 0)
            {
                VolumeOperated = (int)Math.Truncate(inOutStrategy * position);
                var actionsCost = VolumeOperated * actionPrice;

                if (VolumeOperated == 0)
                {
                    VolumeOperated = position;
                }

                TransactionCost = actionsCost * Parameters.TransactionCost;
                CurrentState.Today.ActualPosition -= VolumeOperated;
                CurrentState.Today.CurrentCapital += actionsCost - TransactionCost;
            }
            else if (action == ActionType.Wait)
            {
                VolumeOperated = 0;
            }

            // We need to calculate Earnings first before update the current position
            Earnings = previuosPosition * (CurrentState.Today.Close - CurrentState.Today.Open);


            return(RewardCalculator.Calculate(this));
        }
Exemplo n.º 3
0
        /// <summary>
        /// Initialize the agent and the stock exchange
        /// </summary>
        /// <param name="agentId"></param>
        protected void Initialize(long?agentId)
        {
            using (var DbContext = new DeepQStockContext())
            {
                if (agentId.HasValue)
                {
                    var agentParameters = DbContext.DeepRLAgentParameters
                                          .Include(a => a.QNetwork)
                                          .Include(a => a.StockExchange)
                                          .Single(a => a.Id == agentId.Value);

                    if (agentParameters.Status == AgentStatus.Completed)
                    {
                        DbContext.ClearAgent(agentParameters);
                        DbContext.SaveChanges();
                    }

                    Parameters       = agentParameters.StockExchange;
                    RewardCalculator = RewardCalculator.Use(RewardCalculatorType.WinningsOverLoosings);

                    DailyIndicators   = InitializeIndicators(DbContext, PeriodType.Day);
                    WeeklyIndicators  = InitializeIndicators(DbContext, PeriodType.Week);
                    MonthlyIndicators = InitializeIndicators(DbContext, PeriodType.Month);

                    Agent = new DeepRLAgent(agentParameters);
                    Agent.OnTrainingEpochComplete += (e, args) => RedisManager.Publish(RedisPubSubChannels.OnTrainingEpochComplete, JsonConvert.SerializeObject(args));

                    var experiences = DbContext.Experiences.Where(e => e.AgentId == agentParameters.Id).ToList();
                    Agent.SetExperiences(experiences);

                    DataProvider = new CsvDataProvider(Parameters.CsvDataFilePath, Parameters.EpisodeLength);

                    if (agentParameters.Status == AgentStatus.Paused)
                    {
                        CurrentState = DbContext.States.Include(s => s.InternalPeriods).Single(s => s.StockExchangeId == Parameters.Id);
                        DataProvider.Seek(CurrentState.Today.Date.AddDays(1));
                    }

                    if (agentParameters.Status == AgentStatus.Completed)
                    {
                        DbContext.ClearAgent(agentParameters);
                    }

                    agentParameters.Status = AgentStatus.Running;
                    DbContext.SaveChanges();
                }
            }
        }