示例#1
0
        public void Finish()
        {
            foreach (var component in _components)
            {
                component.Finish();
            }

            if (_activeInstructions.Count > 0)
            {
                foreach (var id in _activeInstructions.Keys)
                {
                    _context.Log(string.Format("unexecuted instruction {0}.", id));
                }
            }
        }
示例#2
0
        public void NotifyTransactionStatus(Transaction transaction)
        {
            Instruction instruction;

            if (!_activeInstructions.TryGetValue(transaction.InstructionId, out instruction))
            {
                throw new InvalidOperationException(
                          string.Format("can't find instruction {0} associated with the transaction.", transaction.InstructionId));
            }

            if (transaction.Action == TradingAction.OpenLong)
            {
                if (transaction.Succeeded)
                {
                    // update the stop loss and risk for new positions
                    var code = transaction.Code;
                    if (!_context.ExistsPosition(code))
                    {
                        throw new InvalidOperationException(
                                  string.Format("There is no position for {0} when calling this function", code));
                    }

                    var positions = _context.GetPositionDetails(code);

                    if (!positions.Any())
                    {
                        throw new InvalidProgramException("Logic error");
                    }

                    OpenInstruction openInstruction = instruction as OpenInstruction;

                    // set stop loss and initial risk for all new positions
                    if (positions.Count() == 1)
                    {
                        var position = positions.Last();
                        if (!position.IsStopLossPriceInitialized())
                        {
                            position.SetStopLossPrice(openInstruction.StopLossPriceForBuying);

                            _context.Log(
                                string.Format(
                                    "Set stop loss for position {0}/{1} as {2:0.000}",
                                    position.Id,
                                    position.Code,
                                    openInstruction.StopLossPriceForBuying));
                        }
                    }
                    else
                    {
                        // set stop loss for positions created by PositionAdjusting component
                        if (Math.Abs(openInstruction.StopLossPriceForBuying) > 1e-6)
                        {
                            var lastPosition     = positions.Last();
                            var newStopLossPrice = openInstruction.StopLossPriceForBuying;

                            // now set the new stop loss price for all positions
                            foreach (var position in positions)
                            {
                                if (!position.IsStopLossPriceInitialized() ||
                                    position.StopLossPrice < newStopLossPrice)
                                {
                                    position.SetStopLossPrice(newStopLossPrice);

                                    _context.Log(
                                        string.Format(
                                            "PositionAdjusting:IncreaseStopLoss: Set stop loss for position {0}/{1} as {2:0.000}",
                                            position.Id,
                                            position.Code,
                                            newStopLossPrice));
                                }
                            }
                        }
                    }
                }
            }

            // remove the instruction from active instruction collection.
            _activeInstructions.Remove(instruction.Id);
        }