/// <summary> /// Argument Constructor /// </summary> /// <param name="tradeSide">Represents the side which was responsible for starting the Trade</param> /// <param name="tradeSize">Size of Trade depending upon the Order Size which strated the Trade</param> /// <param name="executionPrice">Price at which the order is executed</param> /// <param name="executionProvider">Order Execution Provider On which the executions take place</param> /// <param name="executionId">Execution ID for Order which initiated the Trade</param> /// <param name="security">Security/Symbol for which the Order Executions are occuring</param> /// <param name="startTime">Execution time to be used for Trade Start Time</param> public Trade(TradeSide tradeSide, int tradeSize, decimal executionPrice, string executionProvider, string executionId, Security security, DateTime startTime) { // Save Information _tradeSide = tradeSide; _tradeSize = tradeSize; _executionProvider = executionProvider; _security = security; _startTime = startTime; // Initialize _executionDetails = new Dictionary <string, int>(); // Set initial Position value _position = tradeSize; // Set initial PnL value _profitAndLoss = executionPrice * tradeSize; // Set value to 'Negative' if the Trade side is 'SELL' if (tradeSide.Equals(Constants.TradeSide.Sell)) { _position *= -1; _tradeSize *= -1; _profitAndLoss *= -1; } // Add initial values to the local Map _executionDetails.Add(executionId, _position); }
/// <summary> /// Add new execution details to the current Trade /// </summary> /// <param name="executionId">Unique Execution ID</param> /// <param name="executionSize">Shares filled in the current execution</param> /// <param name="executionPrice">Price at which the order is executed</param> /// <param name="executionTime">Execution Time</param> /// <returns>Remaining amount from the Execution Size after adding it into the current Trade</returns> public int Add(string executionId, int executionSize, decimal executionPrice, DateTime executionTime) { // Process only if the Trade is still Open if (!IsComplete()) { // Process Execution Size to be added to the current Trade // If the start Trade is BUY incoming executions will be 'Negative' if (_tradeSide.Equals(Constants.TradeSide.Buy)) { executionSize *= -1; } // Get remaining Position size int remainingPosition = _position + executionSize; // Check if the Trade is complete. // Check if incoming execution can be fully utilized. if (remainingPosition == 0 || HaveSameSign(_position, remainingPosition)) { // Trade is complete if Remaining Position equals '0' else Update the current Position _position = remainingPosition; // Add Execution Details to local Map _executionDetails.Add(executionId, executionSize); // Update PnL value _profitAndLoss += (executionPrice * executionSize); if (Logger.IsDebugEnabled) { Logger.Debug("Trade Updated with Size: " + executionSize + " ExecutionID: " + executionId, _type.FullName, "Add"); Logger.Debug(this.ToString(), _type.FullName, "Add"); } // Check if the Trade is complete if (IsComplete()) { // Set Execution Time as Completion for the Trade _completionTime = executionTime; } // Indicates the incoming execution size was fully utilized return(0); } // Incoming execution is more than whats need to complete the trade else { // Get execution size which can be utilized int executionSizeToUse = Math.Abs(executionSize) - (Math.Abs(remainingPosition)); // Update Position _position = 0; // If the start Trade is BUY execution size to use will be 'Negative' if (_tradeSide.Equals(Constants.TradeSide.Buy)) { executionSizeToUse *= -1; } // Add Execution Details to local Map _executionDetails.Add(executionId, executionSizeToUse); if (Logger.IsDebugEnabled) { Logger.Debug("Trade Updated with Size: " + executionSize + " ExecutionID: " + executionId, _type.FullName, "Add"); Logger.Debug(this.ToString(), _type.FullName, "Add"); } // Update PnL value _profitAndLoss += (executionPrice * executionSizeToUse); // Check if the Trade is complete if (IsComplete()) { // Set Execution Time as Completion for the Trade _completionTime = executionTime; } // Return the execution size which was not utilized return(Math.Abs(remainingPosition)); } } // Indicates nothing was utilized return(executionSize); }