예제 #1
0
        /// <summary>
        /// Inits the values from already calculated statistics.
        /// </summary>
        /// <param name="stats">Other stattistics object.</param>
        public void InitFromStrategyTickerPairStatistics(StrategyTickerPairStatistics stats)
        {
            WinPercent            = stats.WinPercent;
            LossPercent           = stats.LossPercent;
            ProfitTargetPercent   = stats.ProfitTargetPercent;
            StopLossPercent       = stats.StopLossPercent;
            LengthExceededPercent = stats.LengthExceededPercent;
            Gain           = stats.Gain;
            NumberOfOrders = stats.NumberOfOrders;

            LargestWinner        = stats.LargestWinner;
            LargestWinnerBuyDate = stats.LargestWinnerBuyDate;
            LargestLoser         = stats.LargestLoser;
            LargestLoserBuyDate  = stats.LargestLoserBuyDate;

            MostConsecutiveLosers     = stats.MostConsecutiveLosers;
            MostConsecutiveLosersDate = stats.MostConsecutiveLosersDate;
            MaxDrawdown         = stats.MaxDrawdown;
            MaxDrawdownDate     = stats.MaxDrawdownDate;
            ProfitFactor        = stats.ProfitFactor;
            ProfitFactorLargest = stats.ProfitFactorLargest;

            LongWinPercent            = stats.LongWinPercent;
            LongLossPercent           = stats.LongLossPercent;
            LongProfitTargetPercent   = stats.LongProfitTargetPercent;
            LongStopLossPercent       = stats.LongStopLossPercent;
            LongLengthExceededPercent = stats.LongLengthExceededPercent;
            LongNumberOfOrders        = stats.LongNumberOfOrders;
            LongWinAvg         = stats.LongWinAvg;
            LongWinAvgPercent  = stats.LongWinAvgPercent;
            LongLossAvg        = stats.LongLossAvg;
            LongLossAvgPercent = stats.LongLossAvgPercent;

            ShortWinPercent            = stats.ShortWinPercent;
            ShortLossPercent           = stats.ShortLossPercent;
            ShortProfitTargetPercent   = stats.ShortProfitTargetPercent;
            ShortStopLossPercent       = stats.ShortStopLossPercent;
            ShortLengthExceededPercent = stats.ShortLengthExceededPercent;
            ShortNumberOfOrders        = stats.ShortNumberOfOrders;
            ShortWinAvg         = stats.ShortWinAvg;
            ShortWinAvgPercent  = stats.ShortWinAvgPercent;
            ShortLossAvg        = stats.ShortLossAvg;
            ShortLossAvgPercent = stats.ShortLossAvgPercent;

            AverageOrderLength       = stats.AverageOrderLength;
            AverageProfitOrderLength = stats.AverageProfitOrderLength;
            AverageStopOrderLength   = stats.AverageStopOrderLength;
        }
예제 #2
0
		/// <summary>
		/// Outputs the all the statistics for all strategies and the overall stats for each.
		/// </summary>
		void OutputStrategyStats()
		{
			if (Simulator.Config.UseAbbreviatedOutput == true)
			{
				return;
			}

			ConcurrentBag<StrategyStatistics> allStrategyStatistics = new ConcurrentBag<StrategyStatistics>();

#if DEBUG			
			foreach (KeyValuePair<int, ConcurrentBag<Order>> strategy in Simulator.Orders.StrategyDictionary)
#else
			Parallel.ForEach(Simulator.Orders.StrategyDictionary, strategy =>
#endif
			{
				string jsonOutput;
				string folderName;
				string filename;

				ConcurrentBag<Order> orders = strategy.Value;
				if (orders.Count > Simulator.Config.MinRequiredOrders)
				{
					// Get the strategy name but skip the main strategy as it gets process differently.
					string strategyName = orders.First().StrategyName;
					if (strategyName == "MainStrategy")
					{
#if DEBUG
						continue;
#else
						return;
#endif
					}

					Dictionary<int, StrategyTickerPairStatistics> tickersForThisStrategy = new Dictionary<int, StrategyTickerPairStatistics>();
					StrategyStatistics stratStats = new StrategyStatistics(strategyName, orders.First().Type);

					// Catagorize and total all the orders for this strategy by the ticker they are associated with.
					foreach (Order order in orders)
					{
						int tickerHash = order.Ticker.TickerAndExchange.GetHashCode();
						string tickerName = order.Ticker.TickerAndExchange.ToString();

						// If we haven't created the output for this ticker then create it.
						if (!tickersForThisStrategy.ContainsKey(tickerHash))
						{
							tickersForThisStrategy[tickerHash] = new StrategyTickerPairStatistics(strategyName, tickerName, order.DependentIndicatorNames);
						}

						tickersForThisStrategy[tickerHash].AddOrder(order);
						stratStats.AddOrder(order);
					}

					stratStats.CalculateStatistics();
					allStrategyStatistics.Add(stratStats);

					// Output the info about each ticker for this strategy.
					List<StrategyStatistics> overallList = new List<StrategyStatistics>();
					foreach (KeyValuePair<int, StrategyTickerPairStatistics> item in tickersForThisStrategy)
					{
						// This hasn't been calculated yet.
						item.Value.CalculateStatistics();

						// Save the info for this strategy by the ticker.
						jsonOutput = JsonConvert.SerializeObject(item.Value);
						folderName = _outputFolder + "strategies\\" + strategyName;
						filename = folderName + "\\" + item.Value.TickerName + ".json";
						Directory.CreateDirectory(folderName);
						File.WriteAllText(filename, jsonOutput);

						// Save for the overall stats to be outputted later.
						// Order type doesn't matter for tickers and will get ignored in the web display.
						StrategyStatistics tickerStats = new StrategyStatistics(item.Value.TickerName, Order.OrderType.Long);
						tickerStats.InitFromStrategyTickerPairStatistics(item.Value);
						overallList.Add(tickerStats);
					}

					// Output the overall stats for this strategy.
					jsonOutput = JsonConvert.SerializeObject(overallList);
					folderName = _outputFolder + "strategies\\" + strategyName;
					filename = folderName + "\\overall.json";
					Directory.CreateDirectory(folderName);
					File.WriteAllText(filename, jsonOutput);

				}
#if DEBUG
			}
#else
			});
#endif

			string overallJsonOutput = JsonConvert.SerializeObject(allStrategyStatistics.ToArray());
			string overallFilename = _outputFolder + "overall-strategies.json";
			File.WriteAllText(overallFilename, overallJsonOutput);
		}