public IOrder AdjustRisk(IPortfolio portfolio) { var p = portfolio.PositionList.FirstOrDefault(v => v.InstrumentTicker == TargetInstrumentTicker); if (p == null) { return(null); } if (p.DataTime < StartTime || p.DataTime > EndTime) { return(null); } if (p.IsProfit && ((p.MaxPrice - p.CurrentPrice) / p.MaxPrice > StopProfitPercent)) { var o = portfolio.GenerateOrder(TargetInstrumentTicker, p.CurrentPrice, (int)p.Shares, OrderType.Sell); return(o); } if (p.IsLoss && (p.Cost - p.CurrentPrice) / p.Cost > StopLossPercent) { var o = portfolio.GenerateOrder(TargetInstrumentTicker, p.CurrentPrice, (int)p.Shares, OrderType.Sell); return(o); } if (p.KeepTime > MaxPositionTime) { var o = portfolio.GenerateOrder(TargetInstrumentTicker, p.CurrentPrice, (int)p.Shares, OrderType.Sell); return(o); } var w = portfolio.GetWeight(TargetInstrumentTicker); if (w > MaxWeightPercent && p.KeepTime >= MinPositionTime) { var capital = (w - MaxWeightPercent) * portfolio.PositionCapital.Number; var o = portfolio.GenerateOrderByCapital(TargetInstrumentTicker, p.CurrentPrice, capital, OrderType.Sell); return(o); } if (w < MinWeightPercent) { var capital = (MinWeightPercent - w) * portfolio.PositionCapital.Number; var o = portfolio.GenerateOrderByCapital(TargetInstrumentTicker, p.CurrentPrice, capital, OrderType.Buy); return(o); } return(null); }