Пример #1
0
        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);
        }
Пример #2
0
        public List <IOrder> GetConsistentOrderList(double capital, IPortfolio portfolio, OrderType orderType)
        {
            var ol = new List <IOrder>();
            var wl = portfolio.GetWeightList();

            if (wl == null || wl.Count == 0)
            {
                return(ol);
            }
            foreach (var kv in wl)
            {
                var p = portfolio.PositionList.FirstOrDefault(v => v.InstrumentTicker == kv.Key);
                var o = portfolio.GenerateOrderByCapital(kv.Key, p.CurrentPrice, capital, orderType);
                if (o != null)
                {
                    ol.Add(o);
                }
            }
            return(ol);
        }