// // // ************************************************************* // **** ValidatePrices() **** // ************************************************************* /// <summary> /// Called prior to updating prices in a strategy to make sure our prices are valid. /// /// This overload is called when the user does NOT have the book. /// </summary> /// <param name="desiredPrice"></param> /// <param name="mktSide"></param> /// <param name="instrName"></param> /// <returns>true if prices are valid.</returns> public bool ValidatePrices(double desiredPrice, int mktSide, InstrumentName instrName) { bool isValid = false; UV.Lib.BookHubs.Book aBook; if (m_Market.TryEnterReadBook(out aBook)) { isValid = ValidatePrices(desiredPrice, mktSide, instrName, aBook); m_Market.ExitReadBook(aBook); } return(isValid); }
// // // // // // ********************************************************* // **** CreateBar() **** // ********************************************************* /// <summary> /// This is called each time a bar must be created from a snapshot of the current market. /// New bars are pushed into the BarEventArg.BarList queue and handed to the QueryBuilder /// for query creation and writing. /// Called by internal hub thread. /// </summary> private void CreateBar(DateTime barTime) { //Log.NewEntry(LogLevel.Minor, "DataHub: CreateBar - Bar Creation Started"); BarEventArgs eArg = m_BarEventFactory.Get(); // new version GetBarEventArg(); eArg.unixTime = (int)Utilities.QTMath.DateTimeToEpoch(barTime.ToUniversalTime()); //round this to the floor. Log.NewEntry(LogLevel.Minor, "CreateBar: Attempting to create bar for timestamp {0} - miliseconds = {1}", eArg.unixTime, barTime.Millisecond); if ((eArg.unixTime - m_LastBarTimeStamp) > 1) { // we have stepped through time in some interval greater than a second....create email alert for debugging purposes DatabaseWriterEventArgs emailEvent = new DatabaseWriterEventArgs(); emailEvent.Request = DatabaseWriterRequests.SendEmail; emailEvent.QueryBase.AppendFormat("Data Hub Missed Timestamp. Current timestamp={0} and last timestamp={1} difference is {2} seconds", eArg.unixTime, m_LastBarTimeStamp, eArg.unixTime - m_LastBarTimeStamp); m_DatabaseWriterHub.HubEventEnqueue(emailEvent); } m_LastBarTimeStamp = eArg.unixTime; // // Get markets now. // UV.Lib.BookHubs.Book aBook; if (m_Market.TryEnterReadBook(out aBook)) { foreach (KeyValuePair <int, UV.Lib.BookHubs.Market> aBookMarket in aBook.Instruments) { int mySQLID = -1; if (m_InstrumentsRequested.Contains(aBookMarket.Value.Name) && m_InstrToMySQLID.TryGetValue(aBookMarket.Value.Name, out mySQLID)) { // we would like to record data for this instrument if (aBookMarket.Value.Qty[(int)UV.Lib.Utilities.QTMath.BidSide][0] == 0 || aBookMarket.Value.Qty[(int)UV.Lib.Utilities.QTMath.AskSide][0] == 0) {// we have bad data this can happen sometimes in between sessions.. //Log.NewEntry(LogLevel.Major, "CreateBar: Bid Or Ask qty for {0} is equal To zero, skipping bar", aBookMarket.Value.Name); continue; } Bar aBar = m_BarFactory.Get(); // grab a bar! aBar.mysqlID = mySQLID; // set instrument id aBar.bidPrice = aBookMarket.Value.Price[(int)UV.Lib.Utilities.QTMath.BidSide][0]; // set best bid aBar.askPrice = aBookMarket.Value.Price[(int)UV.Lib.Utilities.QTMath.AskSide][0]; // set best ask aBar.bidQty = aBookMarket.Value.Qty[(int)UV.Lib.Utilities.QTMath.BidSide][0]; // set best bidqty aBar.askQty = aBookMarket.Value.Qty[(int)UV.Lib.Utilities.QTMath.AskSide][0]; // set best askqty aBar.lastTradePrice = aBookMarket.Value.LastPrice; aBar.sessionVolume = aBookMarket.Value.Volume[(int)UV.Lib.Utilities.QTMath.LastSide]; aBar.longVolume = aBookMarket.Value.Volume[(int)UV.Lib.Utilities.QTMath.BidSide]; aBar.shortVolume = aBookMarket.Value.Volume[(int)UV.Lib.Utilities.QTMath.AskSide]; aBar.totalVolume = aBar.longVolume + aBar.shortVolume + aBookMarket.Value.Volume[(int)UV.Lib.Utilities.QTMath.UnknownSide]; aBar.sessionCode = Convert.ToInt32(aBookMarket.Value.IsMarketGood); // flag for trading ==1 or not trading==0 eArg.BarList.Enqueue(aBar); } } m_Market.ExitReadBook(aBook); } else { // something went wrong here! Log.NewEntry(LogLevel.Error, " ********* CreateBar: FAILED TO OBTAIN READ FOR BOOK! *********"); } if (eArg.BarList.Count > 0 && !m_IsDebugMode) // do not write to db in debug mode. { m_QueryBuilderHub.HubEventEnqueue(eArg); } }//CreateBar().