public override T GetDefaultStrategy <T>(string key) { if (StrategyDictionary.ContainsKey(key)) { return((T)StrategyDictionary[key].First()); } return(default(T)); }
public override List <T> GetStrategys <T>(string key) { if (StrategyDictionary.ContainsKey(key)) { return(StrategyDictionary[key] as List <T>); } return(null); }
/// <summary> /// 添加策略 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="t"></param> public override T AddStrategy <T>(string key, T t) { if (StrategyDictionary.ContainsKey(key)) { StrategyDictionary[key].Add(t); } else { var newValue = new List <IStrategyBase>() { t }; StrategyDictionary.AddOrUpdate(key, newValue, (tKey, existingVal) => { return(newValue); }); } return(t); }
/// <summary> /// Adds an order to all the dictionaries for searching by multiple key types. /// </summary> /// <param name="order">The order to add</param> /// <param name="dependentIndicators">Indicators used when making a decision to place this order</param> /// <param name="currentBar">Current bar the order is being added in</param> public void AddOrder(Order order, List <Indicator> dependentIndicators, int currentBar) { // Save the main order in the regular strategy dictionary since we want to // save all of it's orders with no weird processing. if (order.GetType() == typeof(MainStrategyOrder)) { int mainKey = order.StrategyName.GetHashCode(); if (StrategyDictionary.ContainsKey(mainKey) == false) { StrategyDictionary[mainKey] = new ConcurrentBag <Order>(); } StrategyDictionary[mainKey].Add(order); } else { // If this is the first time we've seen this ticker, need a new strategy dictionary // for this ticker. int tickerKey = order.Ticker.TickerAndExchange.GetHashCode(); if (TickerStrategyOrders.ContainsKey(tickerKey) == false) { TickerStrategyOrders[tickerKey] = new Dictionary <int, List <Order> >(); } Dictionary <int, List <Order> > tickerDictionary = TickerStrategyOrders[tickerKey]; // If this is the first time we've seen this strategy for this ticker, create a // new list of orders to track it. int strategyKey = order.StrategyName.GetHashCode(); if (tickerDictionary.ContainsKey(strategyKey) == false) { tickerDictionary[strategyKey] = new List <Order>(); } // Remove any old orders before adding the new one to the list. List <Order> orders = tickerDictionary[strategyKey]; RemoveOldOrders(orders, currentBar); orders.Add(order); } SaveSnapshot(order, dependentIndicators, currentBar); }
/// <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 tickerKey = tickerAndExchange.GetHashCode(); if (TickerStrategyOrders.ContainsKey(tickerKey)) { Dictionary <int, List <Order> > tickerDictionary = TickerStrategyOrders[tickerKey]; int strategyKey = strategyName.GetHashCode(); if (tickerDictionary.ContainsKey(strategyKey)) { orderList = tickerDictionary[strategyKey]; } } } if (orderList != null) { for (int i = orderList.Count - 1; i >= 0; i--) { Order order = orderList[i]; // Add orders that are newer than the maximum lookback and only keep a set // amount of orders. 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); }