示例#1
0
        private static void PredicateStrategy(
            PredicationContext context,
            ITradingStrategy strategy,
            IDictionary <ParameterAttribute, object> parameterValues,
            DateTime startDate,
            double initialCapital,
            double currentCapital,
            IEnumerable <Position> activePositions,
            ITradingDataProvider dataProvider,
            StockBlockRelationshipManager relationshipManager,
            int positionFrozenDays)
        {
            var predicator = new TradingStrategyPredicator(
                initialCapital,
                currentCapital,
                strategy,
                parameterValues,
                dataProvider,
                relationshipManager,
                positionFrozenDays,
                activePositions,
                context.Logger);

            try
            {
                predicator.Predicate();

                var auxiliaryData = predicator.PredicatedTransactions
                                    .Select(
                    transaction =>
                {
                    var codeIndex = dataProvider.GetIndexOfTradingObject(transaction.Code);
                    Bar lastBar;

                    if (!dataProvider.GetLastEffectiveBar(codeIndex, transaction.SubmissionTime, out lastBar))
                    {
                        lastBar.OpenPrice    = 0.0;
                        lastBar.HighestPrice = 0.0;
                        lastBar.LowestPrice  = 0.0;
                        lastBar.ClosePrice   = 0.0;
                    }

                    return(new AuxiliaryData()
                    {
                        Code = transaction.Code,
                        Name = transaction.Name,
                        OpenPrice = lastBar.OpenPrice,
                        ClosePrice = lastBar.ClosePrice,
                        HighestPrice = lastBar.HighestPrice,
                        LowestPrice = lastBar.LowestPrice
                    });
                });

                context.SaveResults(
                    dataProvider,
                    parameterValues,
                    predicator.ActivePositions,
                    predicator.PredicatedTransactions,
                    auxiliaryData);
            }
            catch (Exception ex)
            {
                Console.WriteLine();
                Console.WriteLine("{0}", ex);
                return;
            }
        }
示例#2
0
        static void Run(Options options)
        {
            // check the validation of options
            CheckOptions(options);

            // load settings from files
            var combinedStrategySettings = CombinedStrategySettings.LoadFromFile(options.CombinedStrategySettingsFile);
            var stockDataSettings        = ChinaStockDataSettings.LoadFromFile(options.StockDataSettingsFile);
            var positions = LoadPositions(options.PositionFile);

            // load codes and stock name table
            var stockNameTable = new StockNameTable(stockDataSettings.StockNameTableFile);
            var codes          = LoadCodeOfStocks(options.CodeFile);

            // load stock block relationship if necessary, and filter codes
            StockBlockRelationshipManager stockBlockRelationshipManager = null;

            if (!string.IsNullOrWhiteSpace(options.StockBlockRelationshipFile))
            {
                stockBlockRelationshipManager = LoadStockBlockRelationship(options.StockBlockRelationshipFile);

                // filter stock block relationship for loaded codes only
                stockBlockRelationshipManager = stockBlockRelationshipManager.CreateSubsetForStocks(codes);

                // codes will be updated according to stock-block relationships
                codes = stockBlockRelationshipManager.Stocks;
            }

            var allDataFiles = codes
                               .Select(stockDataSettings.BuildActualDataFilePathAndName)
                               .ToArray();

            // initialize data provider
            var dataProvider
                = new ChinaStockDataProvider(
                      stockNameTable,
                      allDataFiles,
                      options.StartDate,
                      options.EndDate,
                      options.WarmupPeriods);

            var finalCodes = dataProvider.GetAllTradingObjects().Select(to => to.Code);
            var filteredStockBlockRelationshipManager = stockBlockRelationshipManager == null
                ? null
                : stockBlockRelationshipManager.CreateSubsetForStocks(finalCodes);

            if (filteredStockBlockRelationshipManager != null)
            {
                var filteredCodes = filteredStockBlockRelationshipManager.Stocks;

                var filteredDataFiles = filteredCodes
                                        .Select(stockDataSettings.BuildActualDataFilePathAndName)
                                        .ToArray();

                // rebuild data provider according to filtered codes
                dataProvider = new ChinaStockDataProvider(
                    stockNameTable,
                    filteredDataFiles,
                    options.StartDate,
                    options.EndDate,
                    options.WarmupPeriods);
            }

            // initialize combined strategy assembler
            var combinedStrategyAssembler = new CombinedStrategyAssembler(combinedStrategySettings, false);

            var strategyInstances
                = new List <Tuple <CombinedStrategy, IDictionary <ParameterAttribute, object> > >();

            IDictionary <ParameterAttribute, object> values;

            while ((values = combinedStrategyAssembler.GetNextSetOfParameterValues()) != null)
            {
                var strategy = combinedStrategyAssembler.NewStrategy();

                strategyInstances.Add(Tuple.Create(strategy, values));
            }

            if (strategyInstances.Count != 1)
            {
                throw new InvalidDataException("Strategy has more or less than one instance, please check strategy settings");
            }

            string predictionContextDirectory = options.PredicationName + "_" + DateTime.Now.ToString("yyyyMMddTHHmmss");

            if (!Directory.Exists(predictionContextDirectory))
            {
                Directory.CreateDirectory(predictionContextDirectory);
            }

            using (PredicationContext context = new PredicationContext(predictionContextDirectory))
            {
                PredicateStrategy(
                    context,
                    strategyInstances.First().Item1,
                    strategyInstances.First().Item2,
                    options.StartDate,
                    options.InitialCapital,
                    options.CurrentCapital,
                    positions,
                    dataProvider,
                    filteredStockBlockRelationshipManager,
                    options.PositionFrozenDays);
            }

            Console.WriteLine();
            Console.WriteLine("Done.");
        }