Exemplo n.º 1
0
        /// <summary>
        /// 根据日线创建周线
        /// </summary>
        /// <param name="begin"></param>
        /// <param name="end"></param>
        /// <returns></returns>
        public KLine CreateWeek(DateTime begin, DateTime end)
        {
            KLine weekKLine = new KLine(this.Code, TimeUnit.week);

            DateTime d = CalendarUtils.GetWeek(begin, DayOfWeek.Friday);

            while (d <= end)
            {
                KLineItem weekItem = new KLineItem();
                DateTime  t        = d;
                KLineItem t1       = this.GetNearest(t, false);
                if (t1 == null)
                {
                    d = d.AddDays(7); continue;
                }
                weekItem.SetValue <double>("close", t1.CLOSE);
                t1 = this.GetNearest(t.AddDays(-4), true, 0);
                if (t1 == null)
                {
                    d = d.AddDays(7); continue;
                }
                weekItem.SetValue <double>("close", t1.OPEN);

                int    index = this.IndexOf(t1);
                double high  = 0;
                double low   = 0;
                while (index < this.Count && this[index].Date <= d)
                {
                    if (high == 0 || high < this[index].HIGH)
                    {
                        high = this[index].HIGH;
                    }
                    if (low > this[index].LOW)
                    {
                        low = this[index].LOW;
                    }
                    index += 1;
                }
                if (low == 0 || high == 0)
                {
                    d = d.AddDays(7);
                    continue;
                }

                weekItem.SetValue <double>("high", high);
                weekItem.SetValue <double>("low", low);
                weekItem.SetValue <DateTime>("time", d);
                weekItem.SetValue <String>("code", Code);
                weekKLine.Add(weekItem);

                d = d.AddDays(7);
            }
            return(weekKLine);
        }
Exemplo n.º 2
0
            public override TradeRecords DoBuy(TimeSerialsDataSet ds, Properties strategyParam, BacktestParameter backtestParam)
            {
                TimeSeries <ITimeSeriesItem <List <double> > > dayFunds  = ds.FundTrendCreateOrLoad(TimeUnit.day);
                TimeSeries <ITimeSeriesItem <List <double> > > weekFunds = ds.FundTrendCreateOrLoad(TimeUnit.week);
                TimeSeries <ITimeSeriesItem <double> >         dayCross  = ds.FundTrendCrossCreateOrLoad(TimeUnit.day);
                TimeSeries <ITimeSeriesItem <double> >         weekCross = ds.FundTrendCrossCreateOrLoad(TimeUnit.day);

                if (dayFunds == null || dayFunds.Count <= 0 || weekFunds == null || weekFunds.Count <= 0 || dayCross == null || dayCross.Count <= 0 || weekCross == null || weekCross.Count <= 0)
                {
                    return(null);
                }

                TradeRecords tr          = new TradeRecords(ds.Code);
                DateTime     begin       = backtestParam.BeginDate;
                DateTime     end         = backtestParam.EndDate;
                double       p_day_low   = strategyParam.Get <double>("day_low");
                double       p_day_bias  = strategyParam.Get <double>("day_bias");
                double       p_week_low  = strategyParam.Get <double>("week_low");
                double       p_week_bias = strategyParam.Get <double>("week_bias");
                GetInMode    p_getinMode = (GetInMode)strategyParam.Get <Object>("getinMode");

                for (int i = 0; i < dayFunds.Count; i++)
                {
                    ITimeSeriesItem <List <double> > dayFundItem = dayFunds[i];

                    if (dayFundItem == null)
                    {
                        continue;
                    }
                    if (dayFundItem.Date < begin || dayFundItem.Date >= end)
                    {
                        continue;
                    }
                    if ((dayFundItem.Value[0] - dayFundItem.Value[1]) < p_day_bias)
                    {
                        continue;
                    }

                    DateTime td = CalendarUtils.GetWeek(dayFundItem.Date, DayOfWeek.Friday);
                    ITimeSeriesItem <List <double> > weekFundItem = weekFunds[td];
                    if (weekFundItem == null)
                    {
                        continue;
                    }
                    if ((weekFundItem.Value[0] - weekFundItem.Value[1]) < p_week_bias)
                    {
                        continue;
                    }

                    KLine dayLine = ds.DayKLine;
                    if (dayLine == null)
                    {
                        continue;
                    }
                    KLineItem dayLineItem = dayLine[dayFundItem.Date];
                    if (dayLineItem == null)
                    {
                        continue;
                    }

                    TradeBout bout = new TradeBout(ds.Code);
                    bout.RecordTrade(1, dayFundItem.Date, TradeDirection.Buy, dayLineItem.CLOSE, (int)(p_getinMode.Value / dayLineItem.CLOSE), 0, 0, Name);
                    tr.Bouts.Add(bout);
                }
                return(tr);
            }