using System; using System.Timers; using TradeIdeas.MiscSupport; using TradeIdeas.TIProData.Interfaces; namespace TradeIdeas.TIProData { public static class TIQUtilities { //This is a fake data generator that's especially tailored for the RBI/GBI custom windows that's currently being developed in the TIProDevExtensions. Most of this //code copied from the TIQ, but with a number of changes for our particular case. It's crude, but it does furnish the fake stuff. // private static Timer _timer; private static IConnectionMaster _connectionMaster; private static readonly DateTime REFERENCE = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc); //http://stackoverflow.com/questions/7227278/net-datetime-to-time-t-in-seconds public static void StartFakeData(IConnectionMaster connectionMaster) { _connectionMaster = connectionMaster; _timer = new Timer(20000); // Set up the timer (milliseconds) _timer.Elapsed += new ElapsedEventHandler(_timer_Elapsed); _timer.Enabled = true; // Enable it } static void _timer_Elapsed(object sender, ElapsedEventArgs e) { string symbol = GetRandomString(4); string entry = getRandomPriceString(); string stop = getRandomPriceString(); string price = getRandomPriceString(); string prevClose = getRandomPriceString(); string volume = getRandomVolumeString(); string avVol = getRandomVolumeString(); string posRange = getRandomPriceString(); string postionInRange = getRandomPriceString(); string high = getRandomPriceString(); string low = (Double.Parse(high) - Double.Parse(high) / 3.0).ToString(); string date = (ServerFormats.ToTimeT(DateTime.Now)).ToString(); string finalString = "symbol=" + symbol + "&TIME=" + date + "&price=" + price + "&entry=" + entry + "&stop=" + stop + "&prev_close=" + prevClose + "&tvol=" + volume + "&advol=" + avVol + "&high=" + high + "&low=" + low + "&test=1"; _connectionMaster.TraditionalAlertsManager.SendMessage(GetCategory(), finalString); } public static void StopFakeData() { if (_timer != null) { _timer.Stop(); } } private static string GetRandomString(int length) { //code from http://blog.codeeffects.com/Article/Generate-Random-Numbers-And-Strings-C-Sharp string[] array = new string[26] { "A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z" }; System.Text.StringBuilder sb = new System.Text.StringBuilder(); for (int i = 0; i < length; i++) sb.Append(array[GetRandomNumber(25)]); return sb.ToString(); } private static string getRandomPriceString() { double tens = GetRandomNumber(99); double fraction = GetRandomNumber(99); return (tens + fraction / 100).ToString(); } private static string GetCategory() { string[] array = new String[6] { "tipro-GBI5","tipro-GBI15","tipro-GBI30","tipro-RBI5","tipro-RBI15","tipro-RBI30" }; return array[GetRandomNumber(6)]; } private static string getRandomVolumeString() { byte[] b = new byte[4]; new System.Security.Cryptography.RNGCryptoServiceProvider().GetBytes(b); int seed = (b[0] & 0x7f) << 24 | b[1] << 16 | b[2] << 8 | b[3]; System.Random r = new System.Random(seed); return (r.Next(50000, 101000)).ToString(); } private static int GetRandomNumber(int maxNumber) { //code from http://blog.codeeffects.com/Article/Generate-Random-Numbers-And-Strings-C-Sharp if (maxNumber < 1) throw new System.Exception("The maxNumber value should be greater than 1"); byte[] b = new byte[4]; new System.Security.Cryptography.RNGCryptoServiceProvider().GetBytes(b); int seed = (b[0] & 0x7f) << 24 | b[1] << 16 | b[2] << 8 | b[3]; System.Random r = new System.Random(seed); return r.Next(1, maxNumber); } } }