public static ArbitrageTimeStatistics Instance(string symbol) { ArbitrageTimeStatistics value = null; if (!_dictInstance.TryGetValue(symbol, out value)) { lock (o) { if (!_dictInstance.TryGetValue(symbol, out value)) { _dictInstance.Add(symbol, new ArbitrageTimeStatistics(symbol)); value = _dictInstance[symbol]; } } } return(value); }
private void Run(SingleAdviser <int> first, SingleAdviser <int> second, Level2 f, Level2 s, ref Arbitrage arb) { if (f.Bids.Count == 0 || s.Asks.Count == 0 || s.Ask - f.Bid > -float.Epsilon) { if (null != arb) { arb.EndDateTime = DateTime.UtcNow; arb.Duration = (arb.EndDateTime - arb.StartDateTime).TotalMilliseconds; ArbitrageDetail prevArbDetail = arb.ArbitrageDetails.Last(); prevArbDetail.EndTickTime = arb.EndDateTime; prevArbDetail.duration = (prevArbDetail.EndTickTime - prevArbDetail.StartTickTime).TotalMilliseconds; double test = Math.Abs(arb.ArbitrageDetails.Sum(p => p.duration)); if (test - arb.Duration > 0.0000000001) { Console.WriteLine("DurationError"); } Arbitrage refArbitrage = arb; //Task.Factory.StartNew(() => //{ // using (Model.ArbitrageContext arbContext = new Model.ArbitrageContext()) // { // arbContext.Arbitrages.Add(refArbitrage); // arbContext.SaveChanges(); // } //}); arb = null; if (!dictSPairs.Any(p => p.Value.m_firstBidSecondAskArbitrage != null)) { ArbitrageTimeStatistics ars = ArbitrageTimeStatistics.Instance(f.Symbol); ars.TotalSecondsArbitradge += (DateTime.UtcNow - ars.StartArbitrageDateTime).TotalSeconds; ars.StartArbitrageDateTime = DateTime.MinValue; Console.WriteLine("Symbol={3}; ArbitrageTime={0} seconds; Percent={1}%; InHourSeconds={2}", ars.TotalSecondsArbitradge, ars.TotalSecondsArbitradge * 100 / (DateTime.UtcNow - ArbitrageTimeStatistics.StartAppDateTime).TotalSeconds, ars.TotalSecondsArbitradge * 3600 / (DateTime.UtcNow - ArbitrageTimeStatistics.StartAppDateTime).TotalSeconds, f.Symbol); } } } else { DateTime utcNow = DateTime.UtcNow; if (arb == null) { arb = new Arbitrage(utcNow, first.Tag, second.Tag, m_symbol); } ArbitrageDetail arbDetail = new ArbitrageDetail(utcNow); if (f.Asks.Count > 0) { arbDetail.B1Ask1 = f.Asks[0].Price; arbDetail.B1Ask1Volume = f.Asks[0].Volume; } arbDetail.B1Bid1 = f.Bids[0].Price; arbDetail.B1Bid1Volume = f.Bids[0].Volume; arbDetail.B2Ask1 = s.Asks[0].Price; arbDetail.B2Ask1Volume = s.Asks[0].Volume; if (s.Bids.Count > 0) { arbDetail.B2Bid1 = s.Bids[0].Price; arbDetail.B2Bid1Volume = s.Bids[0].Volume; } if (arb.ArbitrageDetails.Count != 0) { ArbitrageDetail previos = arb.ArbitrageDetails.Last(); previos.EndTickTime = arbDetail.StartTickTime; previos.duration = (previos.EndTickTime - previos.StartTickTime).TotalMilliseconds; } arbDetail.spread = s.Ask - f.Bid; arb.ArbitrageDetails.Add(arbDetail); if (ArbitrageTimeStatistics.Instance(f.Symbol).StartArbitrageDateTime == DateTime.MinValue) { ArbitrageTimeStatistics.Instance(f.Symbol).StartArbitrageDateTime = DateTime.UtcNow; } //CheckDoubleArbitrage(first.Tag, second.Tag); } }