Ejemplo n.º 1
0
        protected override void OnBarOpen(Instrument instrument, Bar bar)
        {
            if (MaxBarSize == bar.Size)
            {
                DualPositionRecord       record = StrategyHelper.GetPositionRecord(instrument);
                InstrumentStrategyHelper InstrumentStrategyHelper = StrategyHelper.GetInstrumentStrategyHelper(Instrument, this);

                InstrumentStrategyHelper.ChangeTradingDay(record);
                // 2.同步持仓
                //SyncPosition();
            }
        }
Ejemplo n.º 2
0
        protected override void OnStrategyStart()
        {
            StrategyHelper = new StrategyHelper(framework, this);

            StrategyHelper.OpenCloseHelper.SeparateOrder = SeparateOrder.SeparateCloseOpen;
            StrategyHelper.OpenCloseHelper.DefaultClose  = DefaultClose.CloseToday;
            StrategyHelper.OpenCloseHelper.SendStyle     = SendStyle.OneByOne;

            if (Instrument != null)
            {
                InstrumentStrategyHelper InstrumentStrategyHelper = StrategyHelper.GetInstrumentStrategyHelper(Instrument, this);
                InstrumentStrategyHelper.MarketOrderType = SmartQuant.OrderType.Limit;
                InstrumentStrategyHelper.Tick            = 10;

                // 看情况是否要修正
                Console.WriteLine(string.Format("{0},{1}", Instrument.Symbol, InstrumentStrategyHelper.PriceHelper));
                // 如果是在模拟测试,需要修正此处理,防止出现需要用到交易所时间的情况下出错
                // 注意时区
                Console.WriteLine(string.Format("{0},{1}", Instrument.Symbol, InstrumentStrategyHelper.TimeHelper));
            }

            TrailingPrice = new TrailingPrice(framework, this);
        }
Ejemplo n.º 3
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;
            //        }
            //    }
            //}
        }
Ejemplo n.º 4
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);
        }