private void HandlePositionOpened(PositionOpenedEventArgs args) { // Only watch positions created by this bot var position = args.Position; if (position.Label != Label) { return; } try { // Add the position to the set it belongs to, or begin a new set var id = Guid_.Parse(position.Comment); if (id != null) { if (PositionSets.ContainsKey(id.Value)) { PositionSets[id.Value].Add(position.Id); } else { PositionSets[id.Value] = new List <int>(new[] { position.Id }); } } // Notify position closed if (PositionOpened != null) { PositionOpened(this, args); } OnPositionOpened(position); } catch (Exception ex) { Debugging.Trace(ex.Message); Debugging.Trace(ex.StackTrace); } }
private void HandlePositionClosed(PositionClosedEventArgs args) { // Only watch positions created by this strategy var position = args.Position; if (position.Label != Label) { return; } // Close all positions in the same set var id = Guid_.Parse(position.Comment); if (id != null && PositionSets.ContainsKey(id.Value)) { var set = PositionSets[id.Value]; Broker.ClosePositions(Positions.Where(x => set.Contains(x.Id)), "Closing Set"); PositionSets.Remove(id.Value); } // Remove any position managers that are managing 'position' PositionManagers.RemoveIf(x => x.Position.Id == position.Id); try { // Notify position closed if (PositionClosed != null) { PositionClosed(this, args); } OnPositionClosed(position); } catch (Exception ex) { Debugging.Trace(ex.Message); Debugging.Trace(ex.StackTrace); } }