Exemple #1
0
 private void initializeClass(List <int> intevals, DateTime?firstBarTime = null)
 {
     _lastClose    = -1;
     _todayOpen    = -1;
     _raisePercent = 0;
     intevals.Sort();
     _Live_Bars = new List <LiveBars>();
     for (int i = 0; i < intevals.Count; i++)
     {
         LiveBars bar = new LiveBars(intevals[i], _security.TimeRange, firstBarTime);
         bar.LiveBar_Arrival += LiveBar_Arrival;
         _Live_Bars.Add(bar);
         if (intevals[i] == 60)
         {
             this._bar1M = bar;
         }
         else if (intevals[i] == 15)
         {
             this._bar15S = bar;
         }
         else if (intevals[i] == 300)
         {
             this._bar5M = bar;
         }
     }
     //bar1M = new LiveBars(60);
     //_bar1M.LiveBar_Arrival += LiveBar_Arrival;
     _currentTick = new TickData();
 }
Exemple #2
0
        public static TickData ConvertFromDataRow(DataRow dr)
        {
            TickData td = new TickData();

            td.Code = dr["tick_code"].ToString();
            //td.market = System.Convert.ToByte(dr["tick_market"]);
            td.secInfo  = GlobalValue.GetFutureByCode(td.code)[0];
            td.Time     = System.Convert.ToDateTime(dr["tick_time"]);
            td.Preclose = 0;
            td.Open     = 0;
            td.High     = 0;
            td.Low      = 0;
            td.Ask      = System.Convert.ToDouble(dr["tick_ask1"]);
            td.Bid      = System.Convert.ToDouble(dr["tick_bid1"]);
            td.Last     = Math.Round((td.Ask + td.Bid) / 2, 2, MidpointRounding.AwayFromZero);
            td.Volume   = 0;
            td.Amt      = 0;

            td.isReal = false;
            for (int i = 0; i < 10; i++)
            {
                td.Asks[i]     = System.Convert.ToDouble(dr[string.Format("tick_ask{0}", i + 1)]);
                td.Bids[i]     = System.Convert.ToDouble(dr[string.Format("tick_bid{0}", i + 1)]);
                td.Asksizes[i] = System.Convert.ToDouble(dr[string.Format("tick_asks{0}", i + 1)]);
                td.Bidsizes[i] = System.Convert.ToDouble(dr[string.Format("tick_bids{0}", i + 1)]);
            }
            return(td);
        }
        private void UpdateCurrentBar(TickData tickdata, TimeSpan CurrentTickTime)
        {
            if (_currentBar.Open == 0)
            {
                _currentBar.Open  = tickdata.Last;
                _currentBar.High  = tickdata.Last;
                _currentBar.Close = tickdata.Last;
                _currentBar.Low   = tickdata.Last;
            }
            else
            {
                _currentBar.Close = tickdata.Last;
                _currentBar.High  = _currentBar.High < tickdata.Last ? tickdata.Last : _currentBar.High;
                _currentBar.Low   = _currentBar.Low > tickdata.Last ? tickdata.Last : _currentBar.Low;
            }
            _currentBar.Volumn = tickdata.Volume - _lastVolumn;
            double seconds = (CurrentTickTime - _currentOpenTimeSpan).TotalSeconds;

            if (seconds == 0)
            {
                seconds = 1;
            }
            _currentBar.EstVolumn = System.Convert.ToInt64(_currentBar.Volumn / seconds * Inteval);
            _barTickPriceQueue.Add(tickdata.Last);
            MakeRaiseType();
            //return CurrentTickTime;
        }
 public void Update(StockData.TickData td)
 {
     //ParameterizedThreadStart pts = new ParameterizedThreadStart(UpdateLabel);
     //Thread th = new Thread(pts);
     //th.Start(td);
     UpdateLabel(td);
 }
Exemple #5
0
 public static void Update(SecurityInfo sinfo, TickData tick)
 {
     if (currentStockdatas.Keys.Contains(sinfo.Key))
     {
         currentStockdatas[sinfo.Key].tick = tick;
     }
     else
     {
         currentStockdatas.Add(sinfo.Key, new CurrentStockData(sinfo, tick));
     }
 }
Exemple #6
0
        public virtual void ReceiveTick(TickData tickdata)
        {
            //if(SecurityMarket.isLive(tickdata.Time.TimeOfDay))
            {
                //if(tickdata.Time.TimeOfDay == _lastTime)
                //{
                //    _dataisOk = false;
                //    return;
                //}
                if (tickdata.Last != 0)
                {
                    if (_lastClose == -1)
                    {
                        _lastClose = tickdata.Last;
                        _todayOpen = tickdata.Last;
                    }
                    //if (tickdata.Preclose != 0)
                    //{
                    //    this._raisePercent = Math.Round((tickdata.Last - tickdata.Preclose) / tickdata.Preclose * 100, 2, MidpointRounding.AwayFromZero);
                    //}

                    //int seconds = _security.calculateSeconds(tickdata.Time.TimeOfDay);

                    //if(seconds != 0)
                    //{
                    //    this._averageDealVolumn = tickdata.Volume / seconds * 60;
                    //}

                    //try
                    //{
                    //    this._averageDealVolumn = tickdata.Volume / (SecurityMarket.calculateSeconds(new TimeSpan(9, 30, 0), tickdata.Time.TimeOfDay) / 60);
                    //}
                    //catch
                    //{
                    //    this._averageDealVolumn = 0;
                    //}
                    //LastTime = tickdata.Time.TimeOfDay;
                    //_bar1M.ReceiveTick(tickdata);
                    foreach (LiveBars bar in _Live_Bars)
                    {
                        bar.ReceiveTick(tickdata);
                    }
                    //_dataisOk = true;
                }
                else
                {
                    //_dataisOk = false;
                }
            }
        }
Exemple #7
0
 public virtual void Reset()
 {
     //_lastTime = new TimeSpan(9, 30, 0);
     _lastClose    = -1;
     _todayOpen    = -1;
     _raisePercent = 0;
     foreach (LiveBars bar in _Live_Bars)
     {
         bar.Reset();
     }
     //_bar1M.Reset();// = new LiveBars(60);
     //_bar1M.LiveBar_Arrival += LiveBar_Arrival;
     _currentTick = new TickData();
     //_dataisOk = false;
 }
Exemple #8
0
        public static TickData ConvertFromArray(string[] datas)
        {
            TickData td = new TickData();

            if (datas[8] == "1")
            {
                td.code = string.Empty;
                return(td);
            }
            try
            {
                td.time = PhraseTimeSpan(datas[8]);
            }
            catch
            {
                td.code = string.Empty;
                return(td);
            }
            //td.market = System.Convert.ToByte(datas[0]);
            td.code    = datas[1];
            td.secInfo = new SecurityInfo(td.code, string.Empty, "", 0, 0, 0, "", "");
            td.name    = datas[1];
            //td.amt = System.Convert.ToDouble(datas[12]);
            td.ask      = System.Convert.ToDouble(datas[18]);
            td.bid      = System.Convert.ToDouble(datas[17]);
            td.high     = System.Convert.ToDouble(datas[6]);
            td.last     = System.Convert.ToDouble(datas[3]);
            td.low      = System.Convert.ToDouble(datas[7]);
            td.open     = System.Convert.ToDouble(datas[5]);
            td.preclose = System.Convert.ToDouble(datas[4]);
            td.volume   = System.Convert.ToInt32(datas[10]);
            td.isReal   = true;
            for (int j = 0; j < 5; j++)
            {
                td.asks[j]     = System.Convert.ToDouble(datas[18 + 4 * j]);
                td.bids[j]     = System.Convert.ToDouble(datas[17 + 4 * j]);
                td.Asksizes[j] = System.Convert.ToInt32(datas[20 + 4 * j]);
                td.bidsizes[j] = System.Convert.ToInt32(datas[19 + 4 * j]);
            }
            for (int j = 5; j < 10; j++)
            {
                td.asks[j]     = System.Convert.ToDouble(datas[43 + 4 * (j - 5)]);
                td.bids[j]     = System.Convert.ToDouble(datas[42 + 4 * (j - 5)]);
                td.Asksizes[j] = System.Convert.ToInt32(datas[45 + 4 * (j - 5)]);
                td.bidsizes[j] = System.Convert.ToInt32(datas[44 + 4 * (j - 5)]);
            }
            return(td);
        }
Exemple #9
0
 public CurrentStockData(SecurityInfo sinfo, TickData tick)
 {
     // TODO: Complete member initialization
     this.sinfo = sinfo;
     this.tick  = tick;
 }
        protected override void dataReceiver_Data_Arrival(object sender, StockData.TickData tickdata)
        {
            if (tickdata.Code == string.Empty)
            {
                IsSimulateFinished = true;
                HistoryFinished();
                LiveDataUpdate(tickdata);
            }
            else
            {
                if (tickdata.Time.TimeOfDay >= new TimeSpan(15, 00, 00))
                {
                    foreach (var item in tps)
                    {
                        PTradePoints tp = item.Value;
                        if (!tp.Finished)
                        {
                            if (tp.EnterPoint.OpenType == OpenType.KaiDuo)
                            {
                                Leave(tickdata.Bid, parameter.qty, tp.TradeGuid);
                            }
                            if (tp.EnterPoint.OpenType == OpenType.KaiKong)
                            {
                                Leave(tickdata.Ask, parameter.qty, tp.TradeGuid);
                            }
                        }
                    }
                }
                if (currentDay != tickdata.Time.Date)
                {
                    currentDay = tickdata.Time.Date;
                    this.Reset();
                }
                currentTick = tickdata;
                liveDataProcessor.ReceiveTick(tickdata);

                if (this.SecInfo.isLive(tickdata.Time.TimeOfDay))
                {
                    if (IsSimulateFinished || (!IsSimulateFinished && !tickdata.IsReal))
                    {
                        if (parameter.DebugModel)
                        {
                            if (CanOpen)
                            {
                                if (tickdata.Bid >= MarkHigh.Price * 0.999 && KaiKong)
                                {
                                    KaiKong = false;
                                    Open(tickdata.Bid, parameter.qty, OpenType.KaiKong);
                                }
                                if (tickdata.Ask <= MarkLow.Price * 1.001 && KaiDuo)
                                {
                                    KaiDuo = false;
                                    Open(tickdata.Ask, parameter.qty, OpenType.KaiDuo);
                                }
                            }
                            foreach (var item in tps)
                            {
                                PTradePoints tp = item.Value;
                                if (!tp.Finished)
                                {
                                    if (tp.EnterPoint.OpenType == OpenType.KaiDuo)
                                    {
                                        if (tickdata.Bid >= tp.Y)
                                        {
                                            KaiDuo = true;
                                            Leave(tickdata.Bid, parameter.qty, tp.TradeGuid);
                                        }
                                    }
                                    if (tp.EnterPoint.OpenType == OpenType.KaiKong)
                                    {
                                        if (tickdata.Ask <= tp.Y)
                                        {
                                            KaiKong = true;
                                            Leave(tickdata.Ask, parameter.qty, tp.TradeGuid);
                                        }
                                    }
                                }
                            }
                        }
                        #region 止盈、止损
                        foreach (var item in tps)
                        {
                            PTradePoints tp = item.Value;
                            if (!tp.Finished)
                            {
                                double qty = tp.EnterPoint.DealQty - (tp.OutPoint != null ? tp.OutPoint.OpenQty : 0);
                                if (qty > 0)
                                {
                                    if (tp.EnterPoint.OpenType == OpenType.KaiDuo)
                                    {
                                        if (tickdata.Bid <= tp.ZhiSun)
                                        {
                                            if (tp.OutPoint != null)
                                            {
                                                for (int i = 0; i < tp.OutPoint.OpenPointWeiTuo.Count; i++)
                                                {
                                                    if (tp.OutPoint.OpenPointWeiTuo[i].OpenQty > tp.OutPoint.OpenPointWeiTuo[i].DealQty)
                                                    {
                                                        string wtbh = tp.OutPoint.OpenPointWeiTuo[i].Weituobianhao;
                                                        NeedCheDanWeiTuos.Add(wtbh, wtbh);
                                                    }
                                                }
                                            }
                                            KaiDuo = true;
                                            Leave(tickdata.Bid, qty, tp.TradeGuid);
                                        }
                                    }
                                    else if (tp.EnterPoint.OpenType == OpenType.KaiKong)
                                    {
                                        if (tickdata.Ask >= tp.ZhiSun)
                                        {
                                            if (tp.OutPoint != null)
                                            {
                                                for (int i = 0; i < tp.OutPoint.OpenPointWeiTuo.Count; i++)
                                                {
                                                    if (tp.OutPoint.OpenPointWeiTuo[i].OpenQty > tp.OutPoint.OpenPointWeiTuo[i].DealQty)
                                                    {
                                                        string wtbh = tp.OutPoint.OpenPointWeiTuo[i].Weituobianhao;
                                                        NeedCheDanWeiTuos.Add(wtbh, wtbh);
                                                    }
                                                }
                                            }
                                            KaiKong = true;
                                            Leave(tickdata.Ask, qty, tp.TradeGuid);
                                        }
                                    }
                                }
                            }
                        }
                        #endregion
                    }
                }
                LiveDataUpdate(tickdata);
            }
        }
Exemple #11
0
 public BarArrivalEventArgs(TimeSpan ts, TickData td)
 {
     this.barTimeSpan  = ts;
     this.lastTickData = td;
 }
        protected override void dataReceiver_Data_Arrival(object sender, StockData.TickData tickdata)
        {
            if (tickdata.Code == string.Empty)
            {
                IsSimulateFinished = true;
                HistoryFinished();
                LiveDataUpdate(tickdata);
            }
            else
            {
                if (currentDay != tickdata.Time.Date)
                {
                    while (LiveBars["close"].Count > 120)
                    {
                        foreach (var item in LiveBars)
                        {
                            LiveBars[item.Key].RemoveAt(0);
                        }
                    }
                    currentDay = tickdata.Time.Date;
                    StopTime   = tickdata.Time.Date.AddHours(14).AddMinutes(59);
                    this.Reset();
                }
                if (tickdata.Time > StopTime)
                {
                    foreach (var item in tps)
                    {
                        PTradePoints tp = item.Value;
                        if (!tp.Finished)
                        {
                            if (tp.EnterPoint.OpenType == OpenType.KaiDuo)
                            {
                                double qty = tp.EnterPoint.DealQty - (tp.OutPoint != null ? tp.OutPoint.OpenQty : 0);
                                if (qty > 0)
                                {
                                    Leave(currentTick.Bid, qty, tp.TradeGuid);
                                }
                            }
                            else
                            {
                                double qty = tp.EnterPoint.DealQty - (tp.OutPoint != null ? tp.OutPoint.OpenQty : 0);
                                if (qty > 0)
                                {
                                    Leave(currentTick.Ask, qty, tp.TradeGuid);
                                }
                            }
                        }
                    }
                }
                currentTick = tickdata;
                liveDataProcessor.ReceiveTick(tickdata);

                if (this.SecInfo.isLive(tickdata.Time.TimeOfDay))
                {
                    if (IsSimulateFinished || (!IsSimulateFinished && !tickdata.IsReal))
                    {
                    }
                }
                LiveDataUpdate(tickdata);
            }
        }
Exemple #13
0
 protected override void dataReceiver_Data_Arrival(object sender, StockData.TickData tickdata)
 {
     if (tickdata.Code == string.Empty)
     {
         IsSimulateFinished = true;
         HistoryFinished();
         LiveDataUpdate(tickdata);
     }
     else
     {
         currentTick = tickdata;
         liveDataProcessor.ReceiveTick(tickdata);
         if (tickdata.Time > StopTradeTime)
         {
             StartTrade = false;
         }
         if (currentDay != tickdata.Time.Date)
         {
             currentDay = tickdata.Time.Date;
             this.Reset();
         }
         if (tickdata.Last == 0)
         {
             return;
         }
         if (tickdata.Time >= NextOpenTime)
         {
             foreach (var item in tps)
             {
                 PTradePoints tp = item.Value;
                 if (tickdata.IsReal ? !tp.Finished : !tp.Closed)
                 {
                     double qty = tp.EnterPoint.DealQty - (tp.OutPoint != null ? tp.OutPoint.OpenQty : 0);
                     if (qty > 0)
                     {
                         Leave(tp.EnterPoint.OpenType == OpenType.KaiDuo ? tickdata.Ask : tickdata.Bid, qty, tp.TradeGuid);
                     }
                 }
             }
             NextOpenTime = NextOpenTime.AddHours(parameter.TimeCycle);
             this.resetArgs();
         }
         if (this.SecInfo.isLive(tickdata.Time.TimeOfDay))
         {
             if (IsSimulateFinished || (!IsSimulateFinished && !tickdata.IsReal))
             {
                 if (StartTrade)
                 {
                     if (tickdata.Last > ZHighPrice)
                     {
                         ZHighPrice = tickdata.Last;
                     }
                     if (tickdata.Last < ZLowPrice)
                     {
                         ZLowPrice = tickdata.Last;
                     }
                     if (tickdata.Last > RegionHighPrice && !arriveHigh)
                     {
                         RegionHighPrice = tickdata.Last;
                         arriveHigh      = true;
                     }
                     if (tickdata.Last < RegionLowPrice && !arriveLow)
                     {
                         RegionLowPrice = tickdata.Last;
                         arriveLow      = true;
                     }
                     if (K1 && OpenDuoTimes < parameter.JiaCangCiShu)
                     {
                         if (tickdata.Last > RegionHighPrice)
                         {
                             Qty = parameter.qty;
                             if (OpenDuoTimes > 0)
                             {
                                 for (int j = 1; j <= OpenDuoTimes; j++)
                                 {
                                     Qty = Qty * parameter.JiaCangJiaGeBeiShu;
                                 }
                             }
                             OpenDuoTimes += 1;
                             Open(tickdata.Ask, Qty, OpenType.KaiDuo);
                         }
                     }
                     if (K2 && OpenKongTimes < parameter.JiaCangCiShu)
                     {
                         if (tickdata.Last < RegionLowPrice)
                         {
                             Qty = parameter.qty;
                             if (OpenKongTimes > 0)
                             {
                                 for (int j = 1; j <= OpenKongTimes; j++)
                                 {
                                     Qty = Qty * parameter.JiaCangJiaGeBeiShu;
                                 }
                             }
                             OpenKongTimes += 1;
                             Open(tickdata.Bid, Qty, OpenType.KaiKong);
                         }
                     }
                     #region 止损
                     if (tickdata.Last < DuoChuChangPrice)
                     {
                         foreach (var item in tps)
                         {
                             PTradePoints tp = item.Value;
                             if (tickdata.IsReal ? !tp.Finished : !tp.Closed)
                             {
                                 if (tp.EnterPoint.OpenType == OpenType.KaiDuo)
                                 {
                                     double qty = tp.EnterPoint.DealQty - (tp.OutPoint != null ? tp.OutPoint.OpenQty : 0);
                                     if (qty > 0)
                                     {
                                         Leave(tickdata.Bid, qty, tp.TradeGuid);
                                     }
                                 }
                             }
                         }
                     }
                     if (tickdata.Last > KongChuChangPrice)
                     {
                         foreach (var item in tps)
                         {
                             PTradePoints tp = item.Value;
                             if (tickdata.IsReal ? !tp.Finished : !tp.Closed)
                             {
                                 if (tp.EnterPoint.OpenType == OpenType.KaiKong)
                                 {
                                     double qty = tp.EnterPoint.DealQty - (tp.OutPoint != null ? tp.OutPoint.OpenQty : 0);
                                     if (qty > 0)
                                     {
                                         Leave(tickdata.Ask, qty, tp.TradeGuid);
                                     }
                                 }
                             }
                         }
                     }
                     #endregion
                     #region 止盈
                     foreach (var item in tps)
                     {
                         PTradePoints tp = item.Value;
                         if (tickdata.IsReal ? !tp.Finished : !tp.Closed)
                         {
                             double qty = tp.EnterPoint.DealQty - (tp.OutPoint != null ? tp.OutPoint.OpenQty : 0);
                             if (qty > 0)
                             {
                                 if (!tp.StartZhiYing)
                                 {
                                     if (tp.EnterPoint.OpenType == OpenType.KaiDuo)
                                     {
                                         if (tickdata.Last >= tp.Y)
                                         {
                                             tp.StartZhiYing = true;
                                         }
                                     }
                                     else if (tp.EnterPoint.OpenType == OpenType.KaiKong)
                                     {
                                         if (tickdata.Last <= tp.Y)
                                         {
                                             tp.StartZhiYing = true;
                                         }
                                     }
                                 }
                                 else
                                 {
                                     double beiShu = 0;
                                     double huice  = 0.6;
                                     if (tp.EnterPoint.OpenType == OpenType.KaiDuo)
                                     {
                                         beiShu = (tickdata.Last - tp.EnterPoint.OpenPrice) / (tp.Y - tp.EnterPoint.OpenPrice);
                                     }
                                     else
                                     {
                                         beiShu = (tp.EnterPoint.OpenPrice - tickdata.Last) / (tp.EnterPoint.OpenPrice - tp.Y);
                                     }
                                     if (beiShu >= 4)
                                     {
                                         huice = 0.3;
                                     }
                                     else if (beiShu >= 3.5)
                                     {
                                         huice = 0.35;
                                     }
                                     else if (beiShu >= 3)
                                     {
                                         huice = 0.4;
                                     }
                                     else if (beiShu >= 2.5)
                                     {
                                         huice = 0.45;
                                     }
                                     else if (beiShu >= 2)
                                     {
                                         huice = 0.5;
                                     }
                                     else if (beiShu >= 1.5)
                                     {
                                         huice = 0.55;
                                     }
                                     if (tp.EnterPoint.OpenType == OpenType.KaiDuo)
                                     {
                                         if ((tp.Y - tickdata.Last) / (tp.Y - tp.EnterPoint.OpenPrice) > huice)
                                         {
                                             Leave(tickdata.Bid, qty, tp.TradeGuid);
                                         }
                                     }
                                     else
                                     {
                                         if ((tickdata.Last - tp.Y) / (tp.EnterPoint.OpenPrice - tp.Y) > huice)
                                         {
                                             Leave(tickdata.Ask, qty, tp.TradeGuid);
                                         }
                                     }
                                 }
                             }
                         }
                     }
                     #endregion
                     #region 出场
                     #endregion
                 }
             }
         }
     }
 }
        public void ReceiveTick(TickData tickdata)
        {
            if (_lastDayClose == 0)
            {
                _lastDayClose = tickdata.Preclose;
                _todayOpen    = tickdata.Open;
            }


            if (_currentHigh < tickdata.Last)
            {
                _currentHigh = tickdata.Last;
                _reachHigh   = true;
            }

            //TimeSpan closeTimeSpan = getNextCloseTimeSpan(_currentOpenTimeSpan);
            //TimeSpan closeTimeSpan = _currentBar.BarCloseTime;
            //TimeSpan LastTickTime = _currentBar.LastTickTime;
            TimeSpan CurrentTickTime = tickdata.Time.TimeOfDay;

            //TimeSpan NextTickTime = tickdata.Time.TimeOfDay.Add(TimeSpan.FromSeconds(_tickInteval));


            //.Log(string.Format("{0}-{1}",currentTickTime,closeTimeSpan));
            if (CurrentTickTime <= _currentBar.BarCloseTime) //当前时间小于等于本根bar的收盘时间
            {
                UpdateCurrentBar(tickdata, CurrentTickTime);
                //if (CurrentTickTime > _currentBar.LastTickTime)
                //{
                //    _currentBar.EstVolumn = _currentBar.Volumn;
                //    _lastVolumn += _currentBar.Volumn;
                //    CalcuateBar();
                //    PushBarArrivalEvent(_currentBar);
                //    _currentOpenTimeSpan = _currentBar.NextOpenTime;
                //    if (Bars.ContainsKey(_currentOpenTimeSpan))
                //    {
                //        _currentBar = Bars[_currentOpenTimeSpan];
                //    }
                //}

                //PushBarArrivalEvent(_currentBar);
            }
            else //当前时间大于本根bar收盘时间
            {
                while (CurrentTickTime > _currentBar.BarCloseTime)
                {
                    LiveBar nextBar;
                    if (_currentBar.Open == 0)
                    {
                        _currentBar.Open  = tickdata.Last;
                        _currentBar.High  = tickdata.Last;
                        _currentBar.Close = tickdata.Last;
                        _currentBar.Low   = tickdata.Last;
                    }
                    //_lastVolumn += _currentBar.Volumn;
                    //_currentBar.EstVolumn = _currentBar.Volumn;
                    //CalcuateBar();
                    //MakeRaiseType();
                    //PushBarArrivalEvent(_currentBar);

                    _currentOpenTimeSpan = _currentBar.NextOpenTime;
                    if (Bars.ContainsKey(_currentOpenTimeSpan))
                    {
                        nextBar = Bars[_currentOpenTimeSpan];
                    }
                    else
                    {
                        return;
                    }
                    //LastTickTime = Bars[_currentOpenTimeSpan].LastTickTime;
                    //closeTimeSpan = getNextCloseTimeSpan(closeTimeSpan);
                    if (CurrentTickTime <= nextBar.BarCloseTime)    //当前时间小于等于下一根bar收盘时间
                    {
                        if (CurrentTickTime > nextBar.LastTickTime) //当前时间大于最后tick时间
                        {
                            //push nextbar
                            CalcuateBar();
                            MakeRaiseType();
                            _lastVolumn          += _currentBar.Volumn;
                            _currentBar           = Bars[_currentOpenTimeSpan];
                            _currentBar.Open      = tickdata.Last;
                            _currentBar.High      = tickdata.Last;
                            _currentBar.Close     = tickdata.Last;
                            _currentBar.Low       = tickdata.Last;
                            _currentBar.EstVolumn = _currentBar.Volumn;
                            CalcuateBar();
                            MakeRaiseType();
                            _currentBar.Finish = true;
                            PushBarArrivalEvent(_currentBar);
                            _currentOpenTimeSpan = _currentBar.NextOpenTime;
                            if (Bars.ContainsKey(_currentOpenTimeSpan))
                            {
                                _currentBar = Bars[_currentOpenTimeSpan];
                            }
                        }
                        else // 当前时间小于最后tick时间
                        {
                            //push currentbar
                            CalcuateBar();
                            MakeRaiseType();
                            _lastVolumn       += _currentBar.Volumn;
                            _currentBar.Finish = true;
                            PushBarArrivalEvent(_currentBar);
                            _currentBar       = Bars[_currentOpenTimeSpan];
                            _currentBar.Open  = tickdata.Last;
                            _currentBar.High  = tickdata.Last;
                            _currentBar.Close = tickdata.Last;
                            _currentBar.Low   = tickdata.Last;
                        }
                    }
                    else
                    {
                        CalcuateBar();
                        MakeRaiseType();
                        _currentBar           = Bars[_currentOpenTimeSpan];
                        _currentBar.Open      = tickdata.Last;
                        _currentBar.High      = tickdata.Last;
                        _currentBar.Close     = tickdata.Last;
                        _currentBar.Low       = tickdata.Last;
                        _currentBar.EstVolumn = _currentBar.Volumn;
                        CalcuateBar();
                        MakeRaiseType();
                        _lastVolumn += _currentBar.Volumn;
                        //PushBarArrivalEvent(_currentBar);
                        _currentOpenTimeSpan = _currentBar.NextOpenTime;
                        if (Bars.ContainsKey(_currentOpenTimeSpan))
                        {
                            _currentBar = Bars[_currentOpenTimeSpan];
                        }
                    }
                }
            }
        }
        protected override void dataReceiver_Data_Arrival(object sender, StockData.TickData tickdata)
        {
            if (tickdata.Code == string.Empty)
            {
                IsSimulateFinished = true;
                HistoryFinished();
                LiveDataUpdate(tickdata);
            }
            else
            {
                if (currentDay != tickdata.Time.Date)
                {
                    loseArr.Add(loseCount);
                    currentDay = tickdata.Time.Date;
                    this.Reset();
                }
                currentTick = tickdata;
                SecondTick  = CurrentStockData.GetTick(SecondSi);
                liveDataProcessor.ReceiveTick(tickdata);

                if (this.SecInfo.isLive(tickdata.Time.TimeOfDay))
                {
                    if (IsSimulateFinished || (!IsSimulateFinished && !tickdata.IsReal))
                    {
                        if (CanOpen())
                        {
                            if (Math.Abs((tickdata.Time - SecondTick.Time).Seconds) <= 1)
                            {
                                double a = tickdata.Bid - SecondTick.Ask;
                                double b = (tickdata.Bid * parameter.Fee + SecondTick.Ask * parameter.SecondFee + tickdata.Last * parameter.Lirun);
                                double c = SecondTick.Bid - tickdata.Ask;
                                double d = (tickdata.Ask * parameter.Fee + SecondTick.Bid * parameter.SecondFee + tickdata.Last * parameter.Lirun);
                                List <CurrentBanalce> ACBS = CurrentBalances.getCurrentBalance(SecInfo.Market);
                                List <CurrentBanalce> BCBS = CurrentBalances.getCurrentBalance(parameter.SecondMarket);
                                foreach (CurrentBanalce aCBS in ACBS)
                                {
                                    if (aCBS.Code.ToUpper() == parameter.Code.ToUpper() || aCBS.Code.ToUpper() == parameter.Code.ToUpper() + "-SPOT")
                                    {
                                        if (aCBS.Ava > parameter.Qty * 2)
                                        {
                                            Code1AtoB = true;
                                        }
                                        else
                                        {
                                            Code1AtoB = false;
                                        }
                                    }
                                    if (aCBS.Code.ToUpper() == parameter.Code2.ToUpper() || aCBS.Code.ToUpper() == parameter.Code2.ToUpper() + "-SPOT")
                                    {
                                        if (aCBS.Ava > parameter.Qty * tickdata.Last * 2)
                                        {
                                            Code2BtoA = true;
                                        }
                                        else
                                        {
                                            Code2BtoA = false;
                                        }
                                    }
                                }
                                foreach (CurrentBanalce bCBS in BCBS)
                                {
                                    if (bCBS.Code.ToUpper() == parameter.Code.ToUpper() || bCBS.Code.ToUpper() == parameter.Code.ToUpper() + "-SPOT")
                                    {
                                        if (bCBS.Ava > parameter.Qty * 2)
                                        {
                                            Code1BtoA = true;
                                        }
                                        else
                                        {
                                            Code1BtoA = false;
                                        }
                                    }
                                    if (bCBS.Code.ToUpper() == parameter.Code2.ToUpper() || bCBS.Code.ToUpper() == parameter.Code2.ToUpper() + "-SPOT")
                                    {
                                        if (bCBS.Ava > parameter.Qty * tickdata.Last * 2)
                                        {
                                            Code2AtoB = true;
                                        }
                                        else
                                        {
                                            Code2AtoB = false;
                                        }
                                    }
                                }
                                if (a > b)
                                {
                                    if (Code1AtoB && Code2AtoB)
                                    {
                                        firstDeal  = false;
                                        secondDeal = false;
                                        string   tr = DateTime.Now.ToString("yyyyMMddHHmmss") + Rand6Int().ToString();
                                        OpenArgs oa = new OpenArgs(
                                            tickdata.Bid - parameter.HuaDian,
                                            parameter.qty,
                                            OpenType.Sell,
                                            SecInfo,
                                            tickdata,
                                            tr);
                                        OpenDelete od = new OpenDelete(OpenThread);
                                        od.BeginInvoke(oa, null, null);
                                        OpenArgs oa2 = new OpenArgs(
                                            SecondTick.Ask + parameter.HuaDian,
                                            parameter.qty,
                                            OpenType.Buy,
                                            SecondSi,
                                            SecondTick,
                                            tr);
                                        OpenDelete od2 = new OpenDelete(OpenThread);
                                        od2.BeginInvoke(oa2, null, null);
                                    }
                                    else
                                    {
                                        loseCount += 1;
                                    }
                                }
                                if (c > d)
                                {
                                    if (Code1BtoA && Code2BtoA)
                                    {
                                        firstDeal  = false;
                                        secondDeal = false;
                                        string   tr = DateTime.Now.ToString("yyyyMMddHHmmss") + Rand6Int().ToString();
                                        OpenArgs oa = new OpenArgs(
                                            tickdata.Ask + parameter.HuaDian,
                                            parameter.qty,
                                            OpenType.Buy,
                                            SecInfo,
                                            tickdata,
                                            tr);
                                        OpenDelete od = new OpenDelete(OpenThread);
                                        od.BeginInvoke(oa, null, null);
                                        OpenArgs oa2 = new OpenArgs(
                                            SecondTick.Bid - parameter.HuaDian,
                                            parameter.qty,
                                            OpenType.Sell,
                                            SecondSi,
                                            SecondTick,
                                            tr);
                                        OpenDelete od2 = new OpenDelete(OpenThread);
                                        od2.BeginInvoke(oa2, null, null);
                                    }
                                    else
                                    {
                                        loseCount += 1;
                                    }
                                }
                            }
                        }
                    }
                }
                LiveDataUpdate(tickdata);
            }
        }
Exemple #16
0
        protected override void dataReceiver_Data_Arrival(object sender, StockData.TickData tickdata)
        {
            if (tickdata.Code == string.Empty)
            {
                IsSimulateFinished = true;
                HistoryFinished();
                LiveDataUpdate(tickdata);
            }
            else
            {
                if (currentDay != tickdata.Time.Date)
                {
                    currentDay = tickdata.Time.Date;
                    this.Reset();
                }
                currentTick = tickdata;
                liveDataProcessor.ReceiveTick(tickdata);

                if (this.SecInfo.isLive(tickdata.Time.TimeOfDay))
                {
                    if (IsSimulateFinished || (!IsSimulateFinished && !tickdata.IsReal))
                    {
                        if (MarkHigh != 0 && MarkLow != 0)
                        {
                            if (tickdata.Last > MarkHigh && MarkHigh > MarkLow)
                            {
                                if (DuoHands + FrozenDuoHands == 0)
                                {
                                    if (KongHands > 0)
                                    {
                                        foreach (var item in tps)
                                        {
                                            PTradePoints tp = item.Value;
                                            if (!tp.Finished)
                                            {
                                                if (tp.EnterPoint.OpenType == OpenType.KaiKong)
                                                {
                                                    double qty = tp.EnterPoint.DealQty - (tp.OutPoint != null ? tp.OutPoint.OpenQty : 0);
                                                    if (qty > 0)
                                                    {
                                                        Leave(tickdata.Ask, qty, tp.TradeGuid);
                                                    }
                                                }
                                            }
                                        }
                                    }
                                    FrozenDuoHands += (Decimal)parameter.qty;
                                    Open(tickdata.Bid, parameter.qty, OpenType.KaiDuo);
                                }
                            }
                            if (tickdata.Last < MarkLow && MarkHigh > MarkLow)
                            {
                                if (KongHands + FrozenKongHands == 0)
                                {
                                    if (DuoHands > 0)
                                    {
                                        foreach (var item in tps)
                                        {
                                            PTradePoints tp = item.Value;
                                            if (!tp.Finished)
                                            {
                                                if (tp.EnterPoint.OpenType == OpenType.KaiDuo)
                                                {
                                                    double qty = tp.EnterPoint.DealQty - (tp.OutPoint != null ? tp.OutPoint.OpenQty : 0);
                                                    if (qty > 0)
                                                    {
                                                        Leave(tickdata.Bid, qty, tp.TradeGuid);
                                                    }
                                                }
                                            }
                                        }
                                    }
                                    FrozenKongHands += (Decimal)parameter.qty;
                                    Open(tickdata.Ask, parameter.qty, OpenType.KaiKong);
                                }
                            }
                        }
                        #region 止盈、止损
                        foreach (var item in tps)
                        {
                            PTradePoints tp = item.Value;
                            if (!tp.Finished)
                            {
                                double qty = tp.EnterPoint.DealQty - (tp.OutPoint != null ? tp.OutPoint.OpenQty : 0);
                                if (qty > 0)
                                {
                                    if (!tp.StartZhiYing)
                                    {
                                        if (tp.EnterPoint.OpenType == OpenType.KaiDuo)
                                        {
                                            if (tickdata.Last >= tp.Y)
                                            {
                                                tp.StartZhiYing = true;
                                            }
                                        }
                                        else if (tp.EnterPoint.OpenType == OpenType.KaiKong)
                                        {
                                            if (tickdata.Last <= tp.Y)
                                            {
                                                tp.StartZhiYing = true;
                                            }
                                        }
                                    }
                                    else
                                    {
                                        if (tp.EnterPoint.OpenType == OpenType.KaiDuo)
                                        {
                                            if (tickdata.Last > tp.Y)
                                            {
                                                tp.Y = tickdata.Last;
                                            }
                                            if ((tp.Y - tickdata.Last) / (tp.Y - tp.EnterPoint.OpenPrice) > parameter.HuiCheBiLi)
                                            {
                                                Leave(tickdata.Ask, qty, tp.TradeGuid);
                                            }
                                        }
                                        else
                                        {
                                            if (tickdata.Last < tp.Y)
                                            {
                                                tp.Y = tickdata.Last;
                                            }
                                            if ((tickdata.Last - tp.Y) / (tp.EnterPoint.OpenPrice - tp.Y) > parameter.HuiCheBiLi)
                                            {
                                                Leave(tickdata.Bid, qty, tp.TradeGuid);
                                            }
                                        }
                                    }
                                    if (tp.EnterPoint.OpenType == OpenType.KaiDuo)
                                    {
                                        if (tickdata.Last <= tp.ZhiSun)
                                        {
                                            Leave(tickdata.Ask, qty, tp.TradeGuid);
                                        }
                                    }
                                    else if (tp.EnterPoint.OpenType == OpenType.KaiKong)
                                    {
                                        if (tickdata.Last >= tp.ZhiSun)
                                        {
                                            Leave(tickdata.Bid, qty, tp.TradeGuid);
                                        }
                                    }
                                }
                            }
                        }
                        #endregion
                    }
                }
                LiveDataUpdate(tickdata);
            }
        }
Exemple #17
0
        protected override void dataReceiver_Data_Arrival(object sender, StockData.TickData tickdata)
        {
            if (tickdata.Code == string.Empty)
            {
                IsSimulateFinished = true;
                HistoryFinished();
                LiveDataUpdate(tickdata);
            }
            else
            {
                if (currentDay != tickdata.Time.Date)
                {
                    currentDay = tickdata.Time.Date;
                    this.Reset();
                }
                if (tickdata.Last == 0)
                {
                    return;
                }

                currentTick = tickdata;
                //dataAnalisys.ReceiveTick(tickdata);

                liveDataProcessor.ReceiveTick(tickdata);
                if (this.SecInfo.isLive(tickdata.Time.TimeOfDay))
                {
                    if (IsSimulateFinished || (!IsSimulateFinished && !tickdata.IsReal))
                    {
                        #region 4MA策略
                        double bid1      = tickdata.Bid;
                        double ask1      = tickdata.Ask;
                        double mid_price = (tickdata.Ask + tickdata.Bid) / 2;
                        if (open_complete == 0 && close_complete == 0)
                        {
                            //开仓
                            if (last_maefast > last_maeslow && last_maxfast > last_maxslow && mid_price >= last_high)
                            {
                                if (long_position == 0)
                                {//开多
                                    Open(ask1, parameter.qty, OpenType.KaiDuo);
                                }
                            }
                            else if (last_maefast < last_maeslow && last_maxfast < last_maxslow && mid_price <= last_low)
                            {
                                if (short_position == 0)
                                {//开空
                                    Open(bid1, parameter.qty, OpenType.KaiKong);
                                }
                            }
                            //平仓
                            if ((last_maxfast < last_maxslow) || (last_maefast < last_maeslow && last_maxfast < last_maxslow && mid_price <= last_low))
                            {
                                if (long_position > 0)
                                {//平多
                                    foreach (TradePoints tp in tps)
                                    {
                                        if (!tp.Finished)
                                        {
                                            Leave(long_position, bid1, tp.TradeGuid);
                                        }
                                    }
                                }
                            }
                            else if ((last_maxfast > last_maxslow) || (last_maefast > last_maeslow && last_maxfast > last_maxslow && mid_price >= last_high))
                            {
                                if (short_position > 0)
                                {//平空
                                    foreach (TradePoints tp in tps)
                                    {
                                        if (!tp.Finished)
                                        {
                                            Leave(short_position, ask1, tp.TradeGuid);
                                        }
                                    }
                                }
                            }
                        }
                        #endregion
                    }
                }
            }
        }
        protected override void dataReceiver_Data_Arrival(object sender, StockData.TickData tickdata)
        {
            if (tickdata.Code == string.Empty)
            {
                IsSimulateFinished = true;
                HistoryFinished();
                LiveDataUpdate(tickdata);
            }
            else
            {
                if (currentDay != tickdata.Time.Date)
                {
                    currentDay = tickdata.Time.Date;
                    this.Reset();
                }
                if (tickdata.Last == 0)
                {
                    return;
                }

                currentTick = tickdata;
                eosusdt     = CurrentStockData.GetTick(eosusdtsi);
                eosbtc      = CurrentStockData.GetTick(eosbtcsi);

                liveDataProcessor.ReceiveTick(tickdata);
                if (this.SecInfo.isLive(tickdata.Time.TimeOfDay))
                {
                    if (IsSimulateFinished || (!IsSimulateFinished && !tickdata.IsReal))
                    {
                        if (Math.Abs((tickdata.Time - eosbtc.Time).Seconds) <= 1 && Math.Abs((tickdata.Time - eosusdt.Time).Seconds) <= 1 && Math.Abs((eosusdt.Time - eosbtc.Time).Seconds) <= 1)
                        {
                            if (CanOpen())
                            {
                                double a = eosusdt.Bid - tickdata.Ask * eosbtc.Ask;
                                double b = tickdata.Bid * eosbtc.Bid - eosusdt.Ask;
                                double ChengBenAndLiRun = (eosusdt.Last) * (0.0006 + 0.0002);
                                if (a > ChengBenAndLiRun)
                                {
                                    Console.WriteLine(string.Format("{3}-a开仓,时间:{0},价格A:{1},成本C:{2}", DateTime.Now.ToLongTimeString(), a, ChengBenAndLiRun, SecInfo.Code + "%" + eosusdtsi.Code + "%" + eosbtc.Code));
                                    //EosBtcStatus = false;
                                    //OpenArgs oa = new OpenArgs(
                                    //            eosbtc.Ask,
                                    //            parameter.qty,
                                    //            OpenType.Buy,
                                    //            eosbtcsi,
                                    //            eosbtc,
                                    //            "");
                                    //OpenDelete od = new OpenDelete(OpenThread);
                                    //od.BeginInvoke(oa, null, null);
                                }
                                if (b > ChengBenAndLiRun)
                                {
                                    Console.WriteLine(string.Format("{3}-b开仓,时间:{0},价格B:{1},成本C:{2}", DateTime.Now.ToLongTimeString(), b, ChengBenAndLiRun, SecInfo.Code + "%" + eosusdtsi.Code + "%" + eosbtc.Code));
                                    //EosBtcStatus = false;
                                    //OpenArgs oa = new OpenArgs(
                                    //            eosbtc.Bid,
                                    //            parameter.qty,
                                    //            OpenType.Sell,
                                    //            eosbtcsi,
                                    //            eosbtc,
                                    //            "");
                                    //OpenDelete od = new OpenDelete(OpenThread);
                                    //od.BeginInvoke(oa, null, null);
                                }
                            }
                        }
                    }
                }
                LiveDataUpdate(tickdata);
                if (parameter.IsReal)
                {
                    try
                    {
                        UpdateChart();
                    }
                    catch { }
                }
            }
        }
        protected override void dataReceiver_Data_Arrival(object sender, StockData.TickData tickdata)
        {
            //Console.WriteLine("openSide:{0},signInHighPrice:{1},signInLowPrice:{2},tickdata.Last:{3},dealState:{4},openComplete:{5},closeComplete:{6}", openSide, signInHighPrice, signInLowPrice, tickdata.Last, dealState, openComplete, closeComplete);
            //Console.WriteLine("Ratio:{0}", Ratio);
            if (tickdata.Code == string.Empty)
            {
                IsSimulateFinished = true;
                HistoryFinished();
                LiveDataUpdate(tickdata);
            }
            else
            {
                if (currentDay != tickdata.Time.Date)
                {
                    currentDay = tickdata.Time.Date;
                    this.Reset();
                }
                if (tickdata.Last == 0)
                {
                    return;
                }
                liveDataProcessor.ReceiveTick(tickdata);
                currentTick = tickdata;
                if (parameter.StartTime <= tickdata.Time.TimeOfDay && parameter.EndTime >= tickdata.Time.TimeOfDay && init)
                {
                    if (IsSimulateFinished || (!IsSimulateFinished && !tickdata.IsReal))
                    {
                        if (dealState == true && openComplete && closeComplete)
                        {
                            dealState = false;
                        }

                        if (dealState == false)
                        {
                            if (openHands > 0)
                            {
                            }
                            if (tickdata.Last > signInHighPrice && openSide != 1)
                            {
                                if (openSide == 2)
                                {
                                    foreach (TradePoints tp in tps)
                                    {
                                        if (!tp.Finished)
                                        {
                                            if (!tickdata.IsReal)
                                            {
                                                tp.Finished = true;
                                                Leave(tp.EnterPoint.OpenQty, tickdata.Last, tp.TradeGuid);
                                            }
                                            else
                                            {
                                                //平空
                                                closeComplete = false;
                                                dealState     = true;
                                                CloseDelete od = new CloseDelete(CloseThread);
                                                od.BeginInvoke(parameter.qty, tickdata.Last, tp.TradeGuid, null, null);
                                            }
                                        }
                                    }
                                }

                                if (Ratio <= parameter.Ratio)
                                {
                                    if (!tickdata.IsReal)
                                    {
                                        openSide     = 1;
                                        openComplete = false;
                                        dealState    = true;
                                        openHands    = parameter.qty;
                                        OpenArgs oa = new OpenArgs(
                                            tickdata.Last,
                                            parameter.qty,
                                            OpenType.KaiDuo,
                                            SecInfo,
                                            tickdata,
                                            -1,
                                            "");
                                        OpenDelete od = new OpenDelete(OpenThread);
                                        od.BeginInvoke(oa, null, null);
                                    }
                                    else
                                    {
                                        openSide   = 1;
                                        holdHands += (decimal)parameter.Qty;
                                        Open(tickdata.Last, parameter.qty, OpenType.KaiDuo, SecInfo, tickdata, -1, "");
                                    }
                                }
                            }
                            else if (tickdata.Last < signInLowPrice && openSide != 2)
                            {
                                if (openSide == 1)
                                {
                                    foreach (TradePoints tp in tps)
                                    {
                                        if (!tp.Finished)
                                        {
                                            if (!tickdata.IsReal)
                                            {
                                                tp.Finished = true;
                                                Leave(tp.EnterPoint.OpenQty, tickdata.Last, tp.TradeGuid);
                                            }
                                            else
                                            {
                                                //平多
                                                closeComplete = false;
                                                dealState     = true;
                                                CloseDelete od = new CloseDelete(CloseThread);
                                                od.BeginInvoke(parameter.qty, tickdata.Last, tp.TradeGuid, null, null);
                                            }
                                        }
                                    }
                                }

                                if (Ratio <= parameter.Ratio)
                                {
                                    if (!tickdata.IsReal)
                                    {
                                        openSide     = 2;
                                        openComplete = false;
                                        dealState    = true;
                                        openHands    = parameter.qty;
                                        OpenArgs oa = new OpenArgs(
                                            tickdata.Last,
                                            parameter.qty,
                                            OpenType.KaiKong,
                                            SecInfo,
                                            tickdata,
                                            -1,
                                            "");
                                        OpenDelete od = new OpenDelete(OpenThread);
                                        od.BeginInvoke(oa, null, null);
                                    }
                                    else
                                    {
                                        openSide = 2;
                                        Open(tickdata.Last, parameter.qty, OpenType.KaiKong, SecInfo, tickdata, -1, "");
                                    }
                                }
                            }
                        }
                    }
                }
                LiveDataUpdate(tickdata);
                if (parameter.IsReal)
                {
                    try
                    {
                        UpdateChart();
                    }
                    catch { }
                }
            }
        }