Example #1
0
        protected override void OnBar(Instrument instrument, Bar bar)
        {
            StrategyHelper.StatisticsHelper.Update(instrument, bar.Close);

            DualPositionRecord       record = StrategyHelper.GetPositionRecord(instrument);
            InstrumentStrategyHelper InstrumentStrategyHelper = StrategyHelper.GetInstrumentStrategyHelper(Instrument, this);

            if (MaxBarSize == bar.Size)
            {
                bars86400.Add(bar);
                return;
            }
            else
            {
                Log(bar, barsGroup);
            }

            if (HH.Count > 0)
            {
                Log(HH.Last, hhGroup);
                Log(HC.Last, hcGroup);
                Log(LC.Last, lcGroup);
                Log(LL.Last, llGroup);
            }

            // Update performance.
            Portfolio.Performance.Update();

            // Log equity.
            Log(Portfolio.Value, equityGroup);


            do
            {
                // 尾盘平仓
                if (TimeHelper.GetTime(Clock.DateTime) > 1500 && TimeHelper.GetTime(Clock.DateTime) < 2100)
                {
                    record.TargetPosition = 0;
                    break;
                }

                double price  = bar.Close;
                double NetQty = record.CurrentPosition;
                if (NetQty == 0)
                {
                    if (price >= UpperBand)
                    {
                        record.TargetPosition = 1;
                    }
                    if (price <= LowerBand)
                    {
                        record.TargetPosition = -1;
                    }
                }
                else if (NetQty > 0)
                {
                    if (price <= LowerBand)
                    {
                        record.TargetPosition = -1;
                    }
                }
                else if (NetQty < 0)
                {
                    if (price >= UpperBand)
                    {
                        record.TargetPosition = 1;
                    }
                }

                // 策略改进:14:00以后了只让平仓,不让开仓
                if (TimeHelper.GetTime(Clock.DateTime) > 1400)
                {
                    record.FilterCloseOnly();
                }

                // 策略改进:达到当日亏损次数上限和当日连续亏损次数上限后不让开仓
                if (dailyNumOfLossTrades.TotalValue >= MaxLoss || dailyConsecutiveLossTrades.TotalValue >= MaxConsecutiveLoss)
                {
                    record.FilterCloseOnly();
                }
            } while (false);

            InstrumentStrategyHelper.Process(record, bar.Close);

            //// 价格达到涨跌停
            //if (bar.Close >= UpperLimitPrice)
            //{
            //    //// 这个地方方向是否有问题?会不会将我在这个地方的反方向开仓给平了?
            //    //StrategyHelper.ClosePosition(Position, "涨跌停平仓");
            //    //// 由于在这之前已经先达到了

            //    // 01.19 判断一下持仓方向,然后决定是否平仓(如涨停时,持有多仓则不应平仓)
            //    if (HasShortPosition(instrument))
            //    {
            //        StrategyHelper.ClosePosition(Position, "涨停平仓");
            //    }

            //    return;
            //}
            //else if (bar.Close <= LowerLimitPrice)
            //{
            //    // 01.19 判断一下持仓方向,然后决定是否平仓(如跌停时,持有空仓则不应平仓)
            //    if (HasLongPosition(instrument))
            //    {
            //        StrategyHelper.ClosePosition(Position, "跌停平仓");
            //    }
            //}

            // 开仓条件
            //if (bar.Close >= UpperBand)
            //{
            //    if (HasShortPosition(instrument))
            //    {
            //        Order order = StrategyHelper.BuyOrder(instrument, Position.Qty + Qty, "AA");
            //        order = StrategyHelper.RebuildOrder(order);
            //        StrategyHelper.Send(order);
            //        return;
            //    }

            //    if (!HasPosition(instrument))
            //    {
            //        if (!flgLongStopped)
            //        {
            //            Order order = StrategyHelper.BuyOrder(instrument, Position.Qty + Qty, "AA");
            //            order = StrategyHelper.RebuildOrder(order);
            //            StrategyHelper.Send(order);
            //            return;
            //        }

            //        if (bar.Close > TrailingPrice.HighestAfterEntry)
            //        {
            //            Order order = StrategyHelper.BuyOrder(instrument, Position.Qty + Qty, "AA");
            //            order = StrategyHelper.RebuildOrder(order);
            //            StrategyHelper.Send(order);
            //            return;
            //        }
            //    }
            //}

            //if (bar.Close <= LowerBand)
            //{
            //    if (HasLongPosition(instrument))
            //    {
            //        Order order = StrategyHelper.SellOrder(instrument, Position.Qty + Qty, "BB");
            //        order = StrategyHelper.RebuildOrder(order);
            //        StrategyHelper.Send(order);
            //        return;
            //    }

            //    if (!HasPosition(instrument))
            //    {
            //        if (!flgShortStopped)
            //        {
            //            Order order = StrategyHelper.SellOrder(instrument, Position.Qty + Qty, "BB");
            //            order = StrategyHelper.RebuildOrder(order);
            //            StrategyHelper.Send(order);
            //            return;
            //        }

            //        if (bar.Close < TrailingPrice.LowestAfterEntry)
            //        {
            //            Order order = StrategyHelper.SellOrder(instrument, Position.Qty + Qty, "BB");
            //            order = StrategyHelper.RebuildOrder(order);
            //            StrategyHelper.Send(order);
            //            return;
            //        }
            //    }
            //}
        }
Example #2
0
        protected override void OnBar(Instrument instrument, Bar bar)
        {
            DualPositionRecord       record = StrategyHelper.GetPositionRecord(instrument);
            InstrumentStrategyHelper InstrumentStrategyHelper = StrategyHelper.GetInstrumentStrategyHelper(Instrument, this);

            // Add bar to bar series.
            if (MaxBarSize == bar.Size)
            {
                bars86400.Add(bar);
                return;
            }
            else
            {
                bars60.Add(bar);
                Log(bar, bars60Group);
            }

            //if (!SuspendTrading)
            //{
            //    Console.WriteLine("fastSMA.Count = {0}", fastSMA.Count);
            //    Console.WriteLine("slowSMA.Count = {0}", slowSMA.Count);
            //}

            if (fastSMA.Count <= 0)
            {
                return;
            }
            Log(fastSMA.Last, fastSmaGroup);

            if (slowSMA.Count <= 0)
            {
                return;
            }
            Log(slowSMA.Last, slowSmaGroup);

            // Update performance.
            Portfolio.Performance.Update();

            // Log equity.
            Log(Portfolio.Value, equityGroup);

            if (SuspendTrading)
            {
                return;
            }

            do
            {
                // 尾盘平仓
                if (TimeHelper.GetTime(Clock.DateTime) > 1500 && TimeHelper.GetTime(Clock.DateTime) < 2055)
                {
                    record.TargetPosition = 0;
                    break;
                }

                // 开仓条件
                Cross cross = fastSMA.Crosses(slowSMA, bar.DateTime);
                switch (cross)
                {
                case Cross.Above:
                    record.TargetPosition = 1;
                    record.Text           = "上行";
                    break;

                case Cross.Below:
                    record.TargetPosition = -1;
                    record.Text           = "下行";
                    break;
                }
            }while(false);



            InstrumentStrategyHelper.Process(record, bar.Close);
        }