Пример #1
0
 /// <summary>
 /// Create a new bar file.
 /// </summary>
 /// <param name="filePath"></param>
 /// <param name="market"></param>
 /// <param name="barType"></param>
 /// <param name="period"></param>
 public BarFileV1(string filePath, EnumMarket market, EnumBarType barType = EnumBarType.иооМ, int period = 1)
     : base(filePath, new FileHeader
 {
     FileVersion = FileVersion,
     Period      = period,
     Market      = market,
     BarType     = barType
 })
 {
 }
        public static BarProvider Create(DateTime tradeDay, InstrumentManager instmgr, string instid, int offset,
                                         int interval, EnumBarType bartype, BarSeries barlist)
        {
            List <TimeSlice> tradingtime = instmgr.GetTradingTime(tradeDay, instid);
            BarProviderInfo  prdinfo     = BaseBarHelper.Instance.GetBarProviderInfo(tradingtime, offset, interval, bartype);

            if (barlist != null)
            {
                return(new BarProvider(tradeDay, prdinfo, barlist));
            }
            else
            {
                return(new BarProvider(tradeDay, prdinfo));
            }
        }
        public static BarProviderInfo CreateBars(InstrumentManager instmgr, string instid, int offset,
                                                 int interval, EnumBarType bartype, DateTime tradingday, bool isFillNight = true)
        {
            var tradingtime = GetTradingTime(instmgr, instid, tradingday);

            if (tradingtime == null || tradingtime.Count == 0)
            {
                return(null);
            }
            if (isFillNight == false)
            {
                var item = from d in tradingtime where d.BeginTime.Hours != 21 && d.EndTime.Hours != 2 select d;
                tradingtime = item.ToList();
            }
            return(CreateBars(tradingtime, offset, interval, bartype));
        }
Пример #4
0
    // Use this for initialization
    void Start()
    {
        mInstance    = this;
        mObservers   = new ArrayList();
        mBarIterator = SingletonGameData.GetInstance().CreateBarIterator();
        //mSongIterator = SingletonGameData.GetInstance ().CreateSongIterator ();
        mListSameBarsAttribute = new List <BarAttribute> ();
        while (mBarIterator.HasNext())
        {
            mListSameBarsAttribute.Add(mBarIterator.Next() as BarAttribute);
        }
        mCurrentBarType = mListSameBarsAttribute[mCurrentBar].BarType;
        mSameBarNumber  = mListSameBarsAttribute [mCurrentBar].SameBarsNumber;
        mListCount      = mListSameBarsAttribute.Count;

        mSongAttribute = SingletonGameData.GetInstance().GetCurrentSongAttribute();
        mBeatSeconds   = (float)60 / mSongAttribute.BPM;
    }
Пример #5
0
    // Update is called once per frame
    void Update()
    {
        if (mIsGameStart)
        {
            mDeltaTime = Time.deltaTime;
            mBarIimer  = mBarIimer + mDeltaTime;
        }
        else
        {
        }
        //每过一个节拍的时间通知一次观察者
        if (mBarIimer > mBeatSeconds * mBeatTurningPoint)
        {
            mBeatTurningPoint++;
            switch (mCurrentBarType)
            {
            case EnumBarType.BEAT_1_4:
                mCurrentBeatType = EnumBeatType.DOWNBEAT;
                mTimes           = 1;
                if (mBarIimer + mBeatSeconds - mBeatSeconds * mSameBarNumber - mBarTurningPoint > 0)
                {
                    if (mCurrentBar < mListCount - 1)
                    {
                        mCurrentBarType = mListSameBarsAttribute[++mCurrentBar].BarType;
                        mSameBarNumber  = mListSameBarsAttribute[mCurrentBar].SameBarsNumber;
                    }
                    else
                    {
                        mIsGameOver = true;
                    }
                    mBarTurningPoint = mBarIimer + mBeatSeconds - mDeltaTime;
                }
                if (mBarIimer + mBeatSeconds - mBarTurningPoint > 0)
                {
                    mCurrentBarIndex++;
                }
                break;

            case EnumBarType.BEAT_2_4:
                switch (mTimes)
                {
                case 1:
                    mCurrentBeatType = EnumBeatType.DOWNBEAT;
                    mTimes++;
                    if (mBarIimer + 2 * mBeatSeconds - mBarTurningPoint > 0)
                    {
                        mCurrentBarIndex++;
                    }

                    break;

                case 2:
                    mCurrentBeatType = EnumBeatType.UPBEAT;
                    mTimes           = 1;
                    if (mBarIimer + mBeatSeconds - mBeatSeconds * 2 * mSameBarNumber - mBarTurningPoint > 0)
                    {
                        if (mCurrentBar < mListCount - 1)
                        {
                            mCurrentBarType = mListSameBarsAttribute[++mCurrentBar].BarType;
                            mSameBarNumber  = mListSameBarsAttribute[mCurrentBar].SameBarsNumber;
                        }
                        else
                        {
                            mIsGameOver = true;
                        }
                        mBarTurningPoint = mBarIimer + mBeatSeconds - mDeltaTime;
                    }
                    break;
                }
                break;

            case EnumBarType.BEAT_3_4:
                switch (mTimes)
                {
                case 1:
                    mCurrentBeatType = EnumBeatType.DOWNBEAT;
                    mTimes++;
                    if (mBarIimer + 3 * mBeatSeconds - mBarTurningPoint > 0)
                    {
                        mCurrentBarIndex++;
                    }
                    break;

                case 2:
                    mCurrentBeatType = EnumBeatType.UPBEAT;
                    mTimes++;
                    break;

                case 3:
                    mCurrentBeatType = EnumBeatType.UPBEAT;
                    mTimes           = 1;
                    if (mBarIimer + mBeatSeconds - mBeatSeconds * 3 * mSameBarNumber - mBarTurningPoint > 0)
                    {
                        if (mCurrentBar < mListCount - 1)
                        {
                            mCurrentBarType = mListSameBarsAttribute[++mCurrentBar].BarType;
                            mSameBarNumber  = mListSameBarsAttribute[mCurrentBar].SameBarsNumber;
                        }
                        else
                        {
                            mIsGameOver = true;
                        }
                        mBarTurningPoint = mBarIimer + mBeatSeconds - mDeltaTime;
                    }


                    break;
                }

                break;


            case EnumBarType.BEAT_3_8:
                switch (mTimes)
                {
                case 1:
                    mCurrentBeatType = EnumBeatType.DOWNBEAT;
                    mTimes++;
                    if (mBarIimer + 3 * mBeatSeconds - mBarTurningPoint > 0)
                    {
                        mCurrentBarIndex++;
                    }
                    break;

                case 2:
                    mCurrentBeatType = EnumBeatType.UPBEAT;
                    mTimes++;
                    break;

                case 3:
                    mCurrentBeatType = EnumBeatType.UPBEAT;
                    mTimes           = 1;
                    if (mBarIimer + mBeatSeconds - mBeatSeconds * 3 * mSameBarNumber - mBarTurningPoint > 0)
                    {
                        if (mCurrentBar < mListCount - 1)
                        {
                            mCurrentBarType = mListSameBarsAttribute[++mCurrentBar].BarType;
                            mSameBarNumber  = mListSameBarsAttribute[mCurrentBar].SameBarsNumber;
                        }
                        else
                        {
                            mIsGameOver = true;
                        }
                        mBarTurningPoint = mBarIimer + mBeatSeconds - mDeltaTime;
                    }

                    break;
                }

                break;

            case EnumBarType.BEAT_4_4:
                switch (mTimes)
                {
                case 1:
                    mCurrentBeatType = EnumBeatType.DOWNBEAT;
                    mTimes++;
                    if (mBarIimer + 4 * mBeatSeconds - mBarTurningPoint > 0)
                    {
                        mCurrentBarIndex++;
                    }
                    break;

                case 2:
                    mCurrentBeatType = EnumBeatType.UPBEAT;
                    mTimes++;
                    break;

                case 3:
                    mCurrentBeatType = EnumBeatType.OFFBEAT;
                    mTimes++;
                    break;

                case 4:
                    mCurrentBeatType = EnumBeatType.UPBEAT;
                    mTimes           = 1;
                    if (mBarIimer + mBeatSeconds - mBeatSeconds * 4 * mSameBarNumber - mBarTurningPoint > 0)
                    {
                        if (mCurrentBar < mListCount - 1)
                        {
                            mCurrentBarType = mListSameBarsAttribute[++mCurrentBar].BarType;
                            mSameBarNumber  = mListSameBarsAttribute[mCurrentBar].SameBarsNumber;
                        }
                        else
                        {
                            mIsGameOver = true;
                        }
                        mBarTurningPoint = mBarIimer + mBeatSeconds - mDeltaTime;
                    }
                    break;
                }
                break;

            case EnumBarType.BEAT_6_8:
                switch (mTimes)
                {
                case 1:
                    mCurrentBeatType = EnumBeatType.DOWNBEAT;
                    mTimes++;
                    if (mBarIimer + 6 * mBeatSeconds - mBarTurningPoint > 0)
                    {
                        mCurrentBarIndex++;
                    }
                    break;

                case 2:
                    mCurrentBeatType = EnumBeatType.UPBEAT;
                    mTimes++;
                    break;

                case 3:
                    mCurrentBeatType = EnumBeatType.UPBEAT;
                    mTimes++;
                    break;

                case 4:
                    mCurrentBeatType = EnumBeatType.OFFBEAT;
                    mTimes++;
                    break;

                case 5:
                    mCurrentBeatType = EnumBeatType.UPBEAT;
                    mTimes++;
                    break;

                case 6:
                    mCurrentBeatType = EnumBeatType.UPBEAT;
                    mTimes           = 1;
                    if (mBarIimer + mBeatSeconds - mBeatSeconds * 6 * mSameBarNumber - mBarTurningPoint > 0)
                    {
                        if (mCurrentBar < mListCount - 1)
                        {
                            mCurrentBarType = mListSameBarsAttribute[++mCurrentBar].BarType;
                            mSameBarNumber  = mListSameBarsAttribute[mCurrentBar].SameBarsNumber;
                        }
                        else
                        {
                            mIsGameOver = true;
                        }
                        mBarTurningPoint = mBarIimer + mBeatSeconds - mDeltaTime;
                    }

                    break;
                }

                break;

                //	case EnumBarType.OTHER_BEAT:
            }
            NotifyObserver(mCurrentBeatType);
            //print (mCurrentBarIndex);
            //print (IsGameOver);
        }
    }
        public static List <TimeSlice> CreateTimeSlices(List <TimeSlice> lstslice, int offset, int interval, EnumBarType bartype)
        {
            if (lstslice.Count == 0)
            {
                return(null);
            }

            TimeSpan offtime = new TimeSpan(0, 0, offset);
            TimeSpan sp      = default(TimeSpan);

            switch (bartype)
            {
            case EnumBarType.秒: sp = new TimeSpan(0, 0, interval); break;

            case EnumBarType.分钟: sp = new TimeSpan(0, interval, 0); break;

            case EnumBarType.小时: sp = new TimeSpan(interval, 0, 0); break;

            case EnumBarType.日线:
            {
                TimeSlice tsday = new TimeSlice();
                tsday.BeginTime = lstslice[0].BeginTime;
                tsday.EndTime   = lstslice[lstslice.Count - 1].EndTime;
                List <TimeSlice> daybars = new List <TimeSlice>();
                daybars.Add(tsday);
                return(daybars);
            }

            default: throw new Exception("不支持的k线:" + bartype.ToString());
            }

            TimeSpan         beginTime = lstslice[0].BeginTime;//开始时间
            List <TimeSlice> bars      = new List <TimeSlice>();

            if (offset > 0)
            {
                TimeSlice slice = new TimeSlice();
                slice.BeginTime = beginTime;
                slice.EndTime   = beginTime + offtime;
                if (slice.EndTime.Days > 0)
                {
                    slice.EndTime = slice.EndTime - OneDay;
                }

                bars.Add(slice);

                beginTime = slice.EndTime;
            }

            TimeSpan endTime = beginTime;//结束时间
            TimeSpan tsDiff  = default(TimeSpan);

            for (int i = 0; i < lstslice.Count; ++i)
            {
                TimeSlice ts = lstslice[i];
                while (true)
                {
                    TimeSpan tmp = ts.EndTime - endTime;
                    if (tmp < default(TimeSpan))
                    {
                        tmp = tmp + OneDay;
                    }

                    if (tmp + tsDiff < sp)
                    {
                        break;
                    }

                    endTime = endTime + (sp - tsDiff);
                    if (endTime >= OneDay)
                    {
                        endTime = endTime - OneDay;
                    }

                    TimeSlice slice = new TimeSlice();
                    slice.BeginTime = beginTime;
                    slice.EndTime   = endTime;
                    bars.Add(slice);

                    beginTime = endTime;
                    tsDiff    = default(TimeSpan);
                }

                if (i < lstslice.Count - 1)
                {
                    tsDiff = tsDiff + ts.EndTime - endTime;
                    if (tsDiff < default(TimeSpan))
                    {
                        tsDiff = tsDiff + OneDay;
                    }

                    endTime = lstslice[i + 1].BeginTime;

                    if (tsDiff == default(TimeSpan))
                    {
                        beginTime = endTime;
                    }
                }
                else
                {
                    if (beginTime < ts.EndTime)
                    {
                        TimeSlice slice = new TimeSlice();
                        slice.BeginTime = beginTime;
                        slice.EndTime   = ts.EndTime;
                        bars.Add(slice);
                    }
                }
            }

            return(bars);
        }
        public static BarProviderInfo CreateBars(List <TimeSlice> lstslice, int offset, int interval, EnumBarType bartype)
        {
            List <TimeSlice> bars = CreateTimeSlices(lstslice, offset, interval, bartype);

            if (bars == null)
            {
                return(null);
            }

            return(new BarProviderInfo()
            {
                Lstslice = bars,
                Bartype = bartype,
                Interval = interval,
                Offset = offset,
                Livingtime = new TimeSlice()
                {
                    BeginTime = lstslice[0].BeginTime,
                    EndTime = lstslice[lstslice.Count - 1].EndTime
                },
            });
        }
        public BarProviderInfo GetBarProviderInfo(List <TimeSlice> lstslice, int offset, int interval, EnumBarType period)
        {
            StringBuilder builder = new StringBuilder();

            foreach (TimeSlice slice in lstslice)
            {
                builder.Append(slice.BeginTime.ToString()).Append("_")
                .Append(slice.EndTime.ToString()).Append("_");
            }
            builder.Append(offset).Append("_")
            .Append(interval).Append("_")
            .Append(period.ToString());

            string          key = builder.ToString();
            BarProviderInfo tempbars;

            if (_templates.TryGetValue(key, out tempbars))
            {
                return(tempbars);
            }

            tempbars = CreateBars(lstslice, offset, interval, period);
            _templates.Add(key, tempbars);

            return(tempbars);
        }
Пример #9
0
    private int mSameBarsNumber;                //与该小节相同的小节数量

    // 构造函数
    public BarAttribute(EnumBarType barType, int sameBarsNumber)
    {
        mBarType        = barType;
        mSameBarsNumber = sameBarsNumber;
    }