Exemplo n.º 1
0
        static Statistics GetSessionData(string filename)
        {
            using (var tf = TeaFile <Tick> .OpenRead(filename, ItemDescriptionElements.None))
            {
                var    stats     = new Statistics();
                Tick   firstTick = tf.Items.First();
                double minPrice  = firstTick.Price;
                double maxPrice  = firstTick.Price;
                var    session   = new TradingSession(firstTick.Time);
                stats.Sessions.Add(session);
                int n = 0;
                foreach (var tick in tf.Items)
                {
                    if (tick.Time >= session.End)
                    {
                        session = new TradingSession(tick.Time);
                        stats.Sessions.Add(session);
                    }
                    session.TickCount++;

                    minPrice = Math.Min(tick.Price, minPrice);
                    maxPrice = Math.Max(tick.Price, maxPrice);
                    n++;
                }
                stats.MinPrice = minPrice;
                stats.MaxPrice = maxPrice;
                return(stats);
            }
        }
Exemplo n.º 2
0
        private List <TradingSession> GetTradingSessionDataResult(List <TradingSession> updatedTradingSessionList, List <TradingSession> updateTradingSessionList)
        {
            HashSet <int>         set_TradingDay = new HashSet <int>();
            List <TradingSession> result         = new List <TradingSession>();

            if (updatedTradingSessionList != null)
            {
                for (int i = 0; i < updatedTradingSessionList.Count; i++)
                {
                    TradingSession session = updatedTradingSessionList[i];
                    if (set_TradingDay.Contains(session.TradingDay))
                    {
                        continue;
                    }
                    set_TradingDay.Add(session.TradingDay);
                    result.Add(session);
                }
            }
            if (updateTradingSessionList == null)
            {
                return(result);
            }
            for (int i = 0; i < updateTradingSessionList.Count; i++)
            {
                TradingSession session = updateTradingSessionList[i];
                if (set_TradingDay.Contains(session.TradingDay))
                {
                    continue;
                }
                set_TradingDay.Add(session.TradingDay);
                result.Add(session);
            }
            return(result);
        }
Exemplo n.º 3
0
 static Statistics GetSessionDataMemMapped(string filename)
 {
     using (var mf = RawMemoryMapping <Tick> .OpenRead(filename))
         unsafe
         {
             var    stats     = new Statistics();
             Tick * firstTick = (Tick *)mf.ItemAreaStart;
             double minPrice  = firstTick->Price;
             double maxPrice  = firstTick->Price;
             var    session   = new TradingSession(firstTick->Time);
             stats.Sessions.Add(session);
             for (var t = firstTick; t != mf.ItemAreaEnd; t++)
             {
                 if (t->Time >= session.End)
                 {
                     session = new TradingSession(t->Time);
                     stats.Sessions.Add(session);
                 }
                 session.TickCount++;
                 minPrice = Math.Min(t->Price, minPrice);
                 maxPrice = Math.Max(t->Price, maxPrice);
             }
             stats.MinPrice = minPrice;
             stats.MaxPrice = maxPrice;
             return(stats);
         }
 }
Exemplo n.º 4
0
        public TradingEngine(MarketDataChannel chnnls, TradingSession sess, string companyId, string traderId, string locationId = null)
        {
            senderCompId     = companyId;
            senderTraderId   = traderId;
            senderLocationId = locationId;

            channels = chnnls;
            if (channels != null)
            {
                snapshotConnection    = channels.Connections.Find(x => x.Type == MarketDataChannelConnectionType.Snapshot && x.Feed == "A");
                incrementalConnection = channels.Connections.Find(x => x.Type == MarketDataChannelConnectionType.Incremental && x.Feed == "A");

                snapshotServer    = new FixUdpServer(snapshotConnection);
                incrementalServer = new FixUdpServer(incrementalConnection);
            }
            else
            {
                snapshotServer = new FixUdpServer(new MarketDataChannelConnection()
                {
                    IPAddress = IPAddress.Loopback, Port = 7998
                });
                incrementalServer = new FixUdpServer(new MarketDataChannelConnection()
                {
                    IPAddress = IPAddress.Loopback, Port = 7999
                });
            }

            session          = sess;
            session.Changed += HandleSessionChanged;
        }
Exemplo n.º 5
0
        public async Task <string> CreateExperiment(ForexExperiment experiment)
        {
            experiment.starttime = DateTime.Now.ToLongTimeString();
            await AddExperiment(_mapper.Map <ForexExperimentMongo>(experiment));

            List <Strategy> _strategies = experiment.GetStrategies();
            int             counter     = 0;

            foreach (Strategy _strategy in _strategies)
            {
                TradingSession session = new TradingSession();
                session.Name            = $"{experiment.name}_{counter}";
                session.StartDate       = experiment.startdate;
                session.EndDate         = experiment.enddate;
                session.TradingStrategy = _mapper.Map <StrategyMongo>(_strategy);
                session.Read            = false;
                session.ExperimentId    = experiment.name;
                session.StartAmount     = experiment.startamount;
                await PushTradingStrategySession(session);

                counter++;
            }

            return($"{experiment.name} added");
        }
Exemplo n.º 6
0
        private bool IsTimeInTradingSession(int tradingDay, double time)
        {
            if (tradingDay < 0)
            {
                return(false);
            }
            TradingSession tradingSession = GetTradingSession(tradingDay);

            if (tradingSession == null)
            {
                return(false);
            }
            return(time >= tradingSession.StartTime && time <= tradingSession.EndTime);
        }
Exemplo n.º 7
0
        public TradingSession ToTradingSession()
        {
            var ts = new TradingSession();

            foreach (var date in Dates)
            {
                foreach (var sess in date.Sessions)
                {
                    // TODO: split out no cancel
                    ts.Add(sess.StartTime, sess.Status);
                }
            }

            return(ts);
        }
Exemplo n.º 8
0
        public int GetRecentTradingDay(double time, bool forward)
        {
            if (dic_StartTime_TradingDay.ContainsKey(time))
            {
                return(dic_StartTime_TradingDay[time]);
            }
            int            tradingDay     = (int)time;
            TradingSession tradingSession = GetTradingSession(tradingDay);

            if (tradingSession != null)
            {
                if (time >= tradingSession.StartTime && time <= tradingSession.EndTime)
                {
                    return(tradingDay);
                }
                if (forward)
                {
                    if (time > tradingSession.EndTime)
                    {
                        return(GetTradingDayCache().GetNextTradingDay(tradingDay));
                    }
                    return(tradingDay);
                }
                else
                {
                    if (time < tradingSession.StartTime)
                    {
                        return(GetTradingDayCache().GetPrevTradingDay(tradingDay));
                    }
                    return(tradingDay);
                }
            }

            if (forward)
            {
                return(GetTradingDayCache().GetNextTradingDay(tradingDay));
            }

            int nextTradingDay = GetTradingDayCache().GetNextTradingDay(tradingDay);

            if (IsTimeInThisDay(nextTradingDay, time))
            {
                return(nextTradingDay);
            }

            return(GetTradingDayCache().GetPrevTradingDay(tradingDay));
        }
Exemplo n.º 9
0
 public CacheUtils_TradingSession(string instrumentId, List <TradingSession> tradingSessionList)
 {
     this.instrumentId                  = instrumentId;
     this.tradingSessionList            = tradingSessionList;
     this.tradingDays                   = new List <int>(tradingSessionList.Count);
     this.startTimes                    = new List <double>(tradingSessionList.Count);
     this.dic_TradingDay_TradingSession = new Dictionary <int, TradingSession>(tradingSessionList.Count);
     this.dic_StartTime_TradingDay      = new Dictionary <double, int>(tradingSessionList.Count);
     for (int i = 0; i < tradingSessionList.Count; i++)
     {
         TradingSession tradingSession = tradingSessionList[i];
         this.tradingDays.Add(tradingSession.TradingDay);
         this.startTimes.Add(tradingSession.StartTime);
         this.dic_TradingDay_TradingSession.Add(tradingSession.TradingDay, tradingSession);
         this.dic_StartTime_TradingDay.Add(tradingSession.StartTime, tradingSession.TradingDay);
     }
 }
Exemplo n.º 10
0
        private IKLineData GetData_Minute(KLinePeriod period)
        {
            if (period.PeriodType != KLineTimeType.MINUTE)
            {
                return(null);
            }

            int day = beforeBarCount * period.Period / 225;

            int                beforeStartDate    = dataReader.TradingDayReader.GetPrevTradingDay(startDate, day);
            IKLineData         klineData          = dataReader.KLineDataReader.GetData(code, beforeStartDate, endDate, period);
            KLineData_RealTime klineData_RealTime = new KLineData_RealTime(klineData);
            TradingSession     session            = tradingSessionReader.GetTradingSession(startDate);
            int                index = klineData_RealTime.IndexOfTime(session.StartTime);

            klineData_RealTime.BarPos = index;

            return(klineData_RealTime);
        }
Exemplo n.º 11
0
        public static List <TradingSession> LoadByLines(string[] lines)
        {
            List <TradingSession> data = new List <TradingSession>(lines.Length);

            for (int i = 0; i < lines.Length; i++)
            {
                String line = lines[i].Trim();
                if (line.Equals(""))
                {
                    continue;
                }
                String[]       dataArr   = line.Split(',');
                TradingSession startTime = new TradingSession();
                startTime.TradingDay = int.Parse(dataArr[0]);
                startTime.StartTime  = double.Parse(dataArr[1]);
                startTime.EndTime    = double.Parse(dataArr[2]);
                data.Add(startTime);
            }
            return(data);
        }
Exemplo n.º 12
0
 private void BuildInstrumentTradingSessions(List <Protocal.TradingInstrument.InstrumentDayOpenCloseParams> instrumentParams)
 {
     foreach (var eachInstrumentParams in instrumentParams)
     {
         List <TradingSession> sessions;
         if (!_InstrumentTradingSessionDict.TryGetValue(eachInstrumentParams.Id, out sessions))
         {
             sessions = new List <TradingSession>();
             _InstrumentTradingSessionDict.Add(eachInstrumentParams.Id, sessions);
         }
         foreach (var eachSession in eachInstrumentParams.TradingSessions)
         {
             TradingSession session = new TradingSession
             {
                 BeginTime = eachSession.BeginTime,
                 EndTime   = eachSession.EndTime
             };
             sessions.Add(session);
         }
     }
 }
Exemplo n.º 13
0
        public static void Run()
        {
            Security sec = new Security()
            {
                Id = 1, Type = SecurityType.Future, Group = "GC", Product = "GC", Contract = "GCZ6"
            };

            var tod = DateTime.UtcNow.TimeOfDay;

            var ts = new TradingSession();

            Random rand = new Random();
            int    port = 7000 + rand.Next(0, 100);

            TradingEngine exc = new TradingEngine(null, ts, "CME", "G");

            exc.AddSecurity(sec);
            exc.Start(IPAddress.Loopback, port);

            ts.Update(SecurityTradingStatus.Open);

            Thread.Sleep(100);

            var client = new Client("ABC123N", "Operator1", "IE", "CME", "G", null, "Acc1");

            client.Connect(IPAddress.Loopback, port);

            Thread.Sleep(100);
            client.Logon("password");

            int oid1 = client.CreateLimitOrder(sec, Side.Buy, 3, 100);

            Thread.Sleep(100); // we need to wait to hear the ack to get an OrderId
            client.UpdateOrder(oid1, sec, OrderType.Limit, TimeInForce.Day, null, Side.Buy, 105, null, 5, null, null);
            client.DeleteOrder(oid1, sec, Side.Buy);

            Thread.Sleep(100);
        }
Exemplo n.º 14
0
        public int GetRecentOpenDate(double time)
        {
            int date = GetOpenDate(time);

            if (date >= 0)
            {
                return(date);
            }
            date = (int)time;
            if (dicOpenTime.ContainsKey(date))
            {
                TradingSession dayOpenTime = GetOpenTime(date);
                if (time < dayOpenTime.StartTime)
                {
                    return(date);
                }
                return(cache.GetNextTradingDay((int)time));
            }
            if (date >= cache.LastTradingDay)
            {
                return(-1);
            }
            return(cache.GetNextTradingDay((int)time));
        }
Exemplo n.º 15
0
 static Statistics GetSessionDataMemMapped(string filename)
 {
     using (var mf = RawMemoryMapping<Tick>.OpenRead(filename))
     unsafe
     {
         var stats = new Statistics();
         Tick* firstTick = (Tick*)mf.ItemAreaStart;
         double minPrice = firstTick->Price;
         double maxPrice = firstTick->Price;
         var session = new TradingSession(firstTick->Time);
         stats.Sessions.Add(session);
         for (var t = firstTick; t != mf.ItemAreaEnd; t++)
         {
             if (t->Time >= session.End)
             {
                 session = new TradingSession(t->Time);
                 stats.Sessions.Add(session);
             }
             session.TickCount++;
             minPrice = Math.Min(t->Price, minPrice);
             maxPrice = Math.Max(t->Price, maxPrice);
         }
         stats.MinPrice = minPrice;
         stats.MaxPrice = maxPrice;
         return stats;
     }
 }
Exemplo n.º 16
0
 public async Task PushTradingStrategySession(TradingSession item)
 {
     await _context.TradingSessionQueue.InsertOneAsync(item);
 }
Exemplo n.º 17
0
        static Statistics GetSessionData(string filename)
        {
            using (var tf = TeaFile<Tick>.OpenRead(filename, ItemDescriptionElements.None))
            {
                var stats = new Statistics();
                Tick firstTick = tf.Items.First();
                double minPrice = firstTick.Price;
                double maxPrice = firstTick.Price;
                var session = new TradingSession(firstTick.Time);
                stats.Sessions.Add(session);
                int n = 0;
                foreach (var tick in tf.Items)
                {
                    if (tick.Time >= session.End)
                    {
                        session = new TradingSession(tick.Time);
                        stats.Sessions.Add(session);
                    }
                    session.TickCount++;

                    minPrice = Math.Min(tick.Price, minPrice);
                    maxPrice = Math.Max(tick.Price, maxPrice);
                    n++;
                }
                stats.MinPrice = minPrice;
                stats.MaxPrice = maxPrice;
                return stats;
            }
        }