示例#1
0
        /// <summary>
        /// Calculates things like win/loss percent, gain, etc. for the strategy used on the ticker.
        /// </summary>
        /// <param name="strategyName">Name of the strategy the statistics are for</param>
        /// <param name="orderType">Type of orders placed with this strategy (long or short)</param>
        /// <param name="tickerAndExchange">Ticker the strategy used</param>
        /// <param name="currentBar">Current bar of the simulation</param>
        /// <param name="maxBarsAgo">Maximum number of bars in the past to consider for calculating</param>
        /// <returns>Class holding the statistics calculated</returns>
        public StrategyStatistics GetStrategyStatistics(string strategyName, double orderType, TickerExchangePair tickerAndExchange, int currentBar, int maxBarsAgo)
        {
            // Orders that started less than this bar will not be considered.
            int cutoffBar = currentBar - maxBarsAgo;

            if (cutoffBar < 0)
            {
                cutoffBar = 0;
            }

            // Get the list of orders to search.
            StrategyStatistics stats     = new StrategyStatistics(strategyName, orderType);
            List <Order>       orderList = null;

            if (strategyName.Length > 0 && tickerAndExchange == null)
            {
                int strategyKey = strategyName.GetHashCode();
                if (StrategyDictionary.ContainsKey(strategyKey))
                {
                    orderList = StrategyDictionary[strategyKey].ToList();
                }
            }
            else if (tickerAndExchange != null)
            {
                int tickerHash = tickerAndExchange.GetHashCode();
                if (TickerDictionary.ContainsKey(tickerHash))
                {
                    orderList = TickerDictionary[tickerHash];
                }
            }

            if (orderList != null)
            {
                for (int i = orderList.Count - 1; i >= 0; i--)
                {
                    Order order = orderList[i];

                    if (order.IsFinished() &&
                        order.StrategyName == strategyName &&
                        order.BuyBar >= cutoffBar &&
                        order.Type == orderType &&
                        stats.NumberOfOrders < Simulator.Config.MaxLookBackOrders)
                    {
                        stats.AddOrder(order);
                    }
                }
            }

            if (stats.NumberOfOrders > Simulator.Config.MinRequiredOrders)
            {
                stats.CalculateStatistics();
            }
            else
            {
                stats = new StrategyStatistics(strategyName, orderType);
            }

            return(stats);
        }
示例#2
0
        /// <summary>
        /// Calculates things like win/loss percent, gain, etc. for the ticker.
        /// </summary>
        /// <param name="tickerAndExchange">Ticker to calculate for</param>
        /// <param name="currentBar">Current bar of the simulation</param>
        /// <param name="maxBarsAgo">Maximum number of bars in the past to consider for calculating</param>
        /// <returns>Class holding the statistics calculated</returns>
        public StrategyStatistics GetTickerStatistics(TickerExchangePair tickerAndExchange, int currentBar, int maxBarsAgo)
        {
            // Orders that started less than this bar will not be considered.
            int cutoffBar = currentBar - maxBarsAgo;

            if (cutoffBar < 0)
            {
                cutoffBar = 0;
            }

            // Order type doesn't matter here since we are just using this class to
            // output overall ticker info which could be from any order type. It will
            // get ignored on the web output display.
            StrategyStatistics stats = new StrategyStatistics(tickerAndExchange.ToString(), Order.OrderType.Long);

            int tickerHash = tickerAndExchange.GetHashCode();

            if (TickerDictionary.ContainsKey(tickerHash))
            {
                List <Order> tickerOrders = TickerDictionary[tickerHash];

                for (int i = tickerOrders.Count - 1; i >= 0; i--)
                {
                    Order order = tickerOrders[i];
                    if (order.BuyBar >= cutoffBar)
                    {
                        stats.AddOrder(order);
                    }
                }
            }

            // Only count the statistics if we have a bit more data to deal with.
            // We want to avoid having a strategy say it's 100% correct when it
            // only has 1 winning trade.
            if (stats.NumberOfOrders > Simulator.Config.MinRequiredOrders)
            {
                stats.CalculateStatistics();
            }
            else
            {
                // For the same reasons as earlier in this function, order type doesn't matter here.
                stats = new StrategyStatistics(tickerAndExchange.ToString(), Order.OrderType.Long);
            }

            return(stats);
        }