Esempio n. 1
0
        /// <inheritdoc />
        public override void Execute(BacktestDaemonState state)
        {
            var config    = new TemplateAlgorithmConfiguration(new[] { _pair }, 60);
            var container = state.ExchangeFactory.BuildBacktestingContainer <TemplateAlgorithm>(config);
            var timer     = (BacktestTimerProvider)container.TimerProvider;

            timer.SetCurrentTime(DateTimeOffset.FromUnixTimeMilliseconds(_epoch));
            var candles = container.DataProvider.GetCandles(_pair, _n);

            Console.WriteLine(_indicator(candles));
        }
Esempio n. 2
0
        private void ConfigureTimestampEdges(BacktestDaemonState state, StartBacktestCommandArguments args)
        {
            var(begin, end) = DatabaseUtilities.Instance.GetTimeStampEdges(_configuration.TradingPairs);

            if (args.BeginEpoch != 0)
            {
                if (args.BeginEpoch < 0)
                {
                    throw new InvalidCommandException(
                              $"Cannot set begin epoch to {DateTimeOffset.FromUnixTimeMilliseconds(args.BeginEpoch)}, " +
                              $"the database starts at {DateTimeOffset.FromUnixTimeMilliseconds(begin)}");
                }

                if (args.BeginEpoch < begin)
                {
                    throw new InvalidCommandException("begin_epoch was smaller than database start.");
                }

                if (args.BeginEpoch < begin + TimeSpan.FromDays(14).TotalMilliseconds)
                {
                    Console.WriteLine($"WARNING: Custom Begin Epoch is close to the beginning of the data, be careful" +
                                      $"not to read to far into the past.");
                }

                state.BeginTimeStamp = args.BeginEpoch;
            }
            else
            {
                state.BeginTimeStamp = begin + (long)TimeSpan.FromDays(14).TotalMilliseconds;
            }

            if (args.EndEpoch != 0)
            {
                if (args.EndEpoch > end)
                {
                    throw new InvalidCommandException(
                              $"Cannot set end epoch to {DateTimeOffset.FromUnixTimeMilliseconds(args.EndEpoch)}, " +
                              $"the database stops at {DateTimeOffset.FromUnixTimeMilliseconds(end)}");
                }

                end = args.EndEpoch;
            }

            state.EndTimeStamp = end;
        }
Esempio n. 3
0
        /// <inheritdoc/>
        public override void Execute(BacktestDaemonState state)
        {
            var allCommands = Reflections.GetAllSubtypes(typeof(BacktestCommand));

            // Get a list of tuples -> [(name(s), args, description)]
            var overview = allCommands
                           .Select(
                c => (string.Join(", ", c.GetCustomAttributes(true)
                                  .OfType <CommandName>().Select(x => x.Value)),
                      string.Join(",", c.GetCustomAttribute <CommandArguments>().Values),
                      c.GetCustomAttribute <CommandDescription>().Value));

            var table = new ConsoleTable("command", "arguments", "description");

            foreach (var(names, args, description) in overview)
            {
                table.AddRow(names, args, description);
            }

            table.Write(Format.MarkDown);
        }
Esempio n. 4
0
        /// <inheritdoc/>
        public override void Execute(BacktestDaemonState state)
        {
            var startStr = DateTimeOffset
                           .FromUnixTimeMilliseconds(BacktestDaemonService.Instance.State.BeginTimeStamp)
                           .ToString(CultureInfo.InvariantCulture);
            var endStr = DateTimeOffset
                         .FromUnixTimeMilliseconds(BacktestDaemonService.Instance.State.EndTimeStamp)
                         .ToString(CultureInfo.InvariantCulture);

            Configuration.Configuration.Instance.EnabledAlgorithm.ChangeAlgorithmConfiguration(_algo, _configuration);

            Console.WriteLine($"Starting backtest for {_algo.Name} from {startStr} to {endStr}");
            Console.WriteLine($"Configured Candle Width: {Configuration.Configuration.Instance.EnabledAlgorithm.AlgorithmConfiguration.CandleWidth}m");

            // Set custom id or increment.
            state.CurrentBacktestID = _args.ID == -1
                ? state.CurrentBacktestID + 1
                : _args.ID;
            state.CurrentBacktestConfigurationPath = _args.ConfigurationPath;

            // Reset the allocation for a new run.
            state.AllocationManager.SetInitialConfiguration(Configuration.Configuration.Instance.EnabledAlgorithm
                                                            .Allocation);

            // Backtests are run synchronously by design.
            var result = state.AlgorithmService.StartAlgorithm(_algo, _configuration);

            if (result.Success || result.Message == ResponseObject.OutOfFunds.Message)
            {
                // Notify third party applications that the backtest with their id has finished.
                Console.WriteLine($"BACKTEST_FINISHED={BacktestDaemonService.Instance.State.CurrentBacktestID}");
            }
            else
            {
                Console.WriteLine($"Algorithm Execution failed -> {result.Message}");
                Console.WriteLine($"BACKTEST_FAILED={BacktestDaemonService.Instance.State.CurrentBacktestID}");
            }
        }
Esempio n. 5
0
        #pragma warning restore CA1801

        /// <summary>
        /// Execute the command.
        /// </summary>
        /// <param name="state">The state of the backtest daemon.</param>
        public abstract void Execute(BacktestDaemonState state);
Esempio n. 6
0
 /// <inheritdoc/>
 public override void Execute(BacktestDaemonState state)
 {
     Program.ExitProgramWithCode(ExitCode.UserShutdown);
 }