/// <summary> /// Ported from bool operator()(const Edge& e) const /// </summary> public bool Filter(Commodity comm1, Commodity comm2, PriceGraphEdge edge) { Logger.Current.Debug("history.find", () => String.Format(" reftime = {0}", RefTime)); if (!Oldest.IsNotADateTime()) { Logger.Current.Debug("history.find", () => String.Format(" oldest = {0}", Oldest)); } if (edge.Prices.Count == 0) { Logger.Current.Debug("history.find", () => " prices map is empty for this edge"); return(false); } IEnumerable <DateTime> priceDates = edge.Prices.Keys; // [DM] The collection is ordered. if (priceDates.First() > RefTime) { Logger.Current.Debug("history.find", () => " don't use this edge"); return(false); } else { DateTime low = priceDates.Last(pd => pd <= RefTime); if (low > RefTime) { throw new InvalidOperationException("assert(((*low).first <= reftime));"); } if (!Oldest.IsNotADateTime() && low < Oldest) { Logger.Current.Debug("history.find", () => " edge is out of range"); return(false); } var secs = (RefTime - low).TotalSeconds; if (secs < 0) { throw new InvalidOperationException("assert(secs >= 0);"); } edge.Weight = RefTime - low; edge.PricePoint = new PricePoint(low, edge.Prices[low]); Logger.Current.Debug("history.find", () => String.Format(" using edge at price point {0} {1}", low, edge.Prices[low])); return(true); } }
public void AddPrice(Commodity source, DateTime when, Amount price) { if (source == price.Commodity) { throw new InvalidOperationException("Source commodity should not be equal to price's commodity"); } PriceGraphEdge edge = PriceGraph.FindEdge(source, price.Commodity); if (edge == null) { edge = new PriceGraphEdge(); PriceGraph.AddEdge(source, price.Commodity, edge); } edge.Prices[when] = price; }
public void RemovePrice(Commodity source, Commodity target, DateTime date) { if (source == target) { throw new InvalidOperationException("Source commodity cannot be equal to target commodity"); } PriceGraphEdge edge = PriceGraph.FindEdge(source, target); if (edge != null) { if (edge.Prices.ContainsKey(date)) { edge.Prices.Remove(date); } if (edge.Prices.Count == 0) { PriceGraph.RemoveEdge(source, target); } } }