public unsafe void AccessItemsViaMemoryMapping() { const string filename = "RawMemoryMappingTest_AccessItemsViaMemoryMapping.tea"; using (var f = TeaFile <Tick> .Create(filename)) { var dt = new DateTime(2000, 1, 1); for (int i = 0; i < 27; i++) { f.Write(new Tick { Time = dt.AddDays(i), Volume = i * 10 }); } } using (var view = TeaFile <Tick> .OpenRawMemoryMapping(filename)) { Tick *tick = (Tick *)view.ItemAreaStart; var dt = new DateTime(2000, 1, 1); for (int i = 0; i < 27; i++) { tick->Volume.Should().Be(i * 10); tick->Time.Should().Be(dt.AddDays(i)); tick++; } } }
/// <summary> /// Запись в БД Command, новой команды для бота и времени ёё выполнения, возвращает 1, если команда успешно записана. /// </summary> /// <param name="command">новая команда</param> /// <returns></returns> public static int WriteCommand(TradeTaskRequest command) { var fileName = DecimalTradeDBhelper.CreateFileName(command.currencyPair); try { if (!File.Exists(fileName)) { DecimalTradeDBhelper.InitDbCommandResponse(fileName); } using (var tf = TeaFile <TradeTaskRequest> .Append(fileName)) { // if the stream is a filestream that was opened in FileMode.Append, this call is redundant. // this line is here for the allowed case were the stream and headerstream point to the same stream. //https://github.com/discretelogics/TeaFiles.Net-time-series-storage-in-flat-files/blob/938e3fd46e9c55b80e81163495cdd30ebd0112c4/TeaFiles/TeaFileT.cs //tf.SetFilePointerToEnd(); tf.Write(command); // TODO транзакция } return(1); } catch (Exception ex) { //Logger.Log.Error("Произошла ошибка! Команда не была записана"); } return(0); }
/// <summary> /// Чтение из файла БД данных о цене в заданный промежуток времени /// </summary> /// <param name="begin">врямя начала чтения</param> /// <param name="end">время окончания чтения</param> /// <param name="pair">пара валют, напр. BTC_USD </param> /// <returns></returns> public static TimeCurrencyPair[] ReadHistory(DateTime begin, DateTime end, MoneyPair pair) { Time teaTimeBegin = begin; Time teaTimeEnd = end; IEnumerable <TimeCurrencyPair> teaCurrencyPairs = new List <TimeCurrencyPair>(); var fileName = DecimalTradeDBhelper.CreateFileName(pair); try { if (!File.Exists(fileName)) { DecimalTradeDBhelper.InitDb(fileName); } using (var tf = TeaFile <TimeCurrencyPair> .OpenRead(fileName)) { teaCurrencyPairs = tf.Items.Where(p => (p.currentTime > teaTimeBegin) && (p.currentTime < teaTimeEnd)).ToArray(); Console.WriteLine(teaCurrencyPairs); } } catch (Exception ex) { //Logger.Log.Error("Произошла ошибка! Не удалось прочитать истрические данные"); Console.Write(ex.Message); } return(teaCurrencyPairs.ToArray()); }
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); } }
public void MeasureTeaFile(Runner runner) { long N = runner.N; File.Delete(filename); using (var tf = TeaFile <double> .Create(filename)) { runner.Measure("fill teafile<double>", () => { for (int i = 0; i < N; i++) { tf.Write(i); // conversion } }); } using (var tf = TeaFile <double> .OpenRead(filename)) { runner.Measure("sum teafile<double>", () => { double sum = 0; for (int i = 0; i < N; i++) { sum += tf.Read(); } Console.WriteLine(sum); if (sum != runner.TargetValue) { throw new Exception("wrong result"); } }); } }
public unsafe void MeasureTeaFileRawMemoryMappedTick(Runner runner) { long N = runner.N; RawMemoryMapping <Tick> view = null; File.Delete(filename); using (var tf = TeaFile <Tick> .Create(filename)) { for (int i = 0; i < N; i++) { tf.Write(new Tick { Price = i }); } } runner.Measure("memmap raw sum using ticks", () => { using (view = TeaFile <Tick> .OpenRawMemoryMapping(filename)) { double sum = 0d; Tick *tick = (Tick *)view.ItemAreaStart; for (int i = 0; i < N; i++) { sum += tick->Price; tick++; } Console.WriteLine(sum); if (sum != runner.TargetValue) { throw new Exception("wrong result"); } } }); }
public unsafe void AccessItemsViaMemoryMappingPointers() { const string filename = "AccessItemsViaMemoryMappingPointers.tea"; using (var f = TeaFile <Tick> .Create(filename)) { var dt = new DateTime(2000, 1, 1); for (int i = 0; i < 3; i++) { f.Write(new Tick { Time = dt.AddDays(i), Volume = i * 10 }); } } using (var view = TeaFile <Tick> .OpenRawMemoryMapping(filename)) { Tick *tick = (Tick *)view.ItemAreaStart; Tick *end = (Tick *)view.ItemAreaEnd; ((int)tick).Should().Be.LessThanOrEqualTo((int)end); int i = 0; var dt = new DateTime(2000, 1, 1); while (tick < end) { tick->Volume.Should().Be(i * 10); tick->Time.Should().Be(dt.AddDays(i)); tick++; i++; } } }
static void Main(string[] args) { try { if (args.Count() != 2) { Console.WriteLine("Usage: CreateTicks <filename> <number of ticks>"); return; } string filename = args[0]; int n = int.Parse(args[1]); var sw = Stopwatch.StartNew(); Time t = DateTime.Now; using (var tf = TeaFile <Tick> .Create(filename)) { tf.Write(Enumerable.Range(1, n).Select(i => new Tick { Time = t.AddDays(i), Price = i * 101.0, Volume = i * 1000 })); } sw.Stop(); Console.WriteLine("execution time = " + sw.Elapsed.TotalMilliseconds + "ms"); } catch (Exception ex) { Console.WriteLine(ex.Message); } }
public static void InitDb(string fileName) { try { TimeCurrencyPair initPair = new TimeCurrencyPair() { currentTime = DateTime.Now, highestBid = 3333, last = 3333, lowestAsk = 3333, lastbaseVolume = 8888, quoteVolume = 8888, isFrozen = 0 }; Stopwatch watch = Stopwatch.StartNew(); using (var tf = TeaFile <TimeCurrencyPair> .Create(fileName)) { tf.Write(initPair); } watch.Stop(); } catch (Exception ex) { // TODO log } }
static double Run(string filename) { using (var tf = TeaFile <Tick> .OpenRead(filename)) { return(tf.Items.Sum(item => item.Price)); } }
public void MeasureTeaFileMemoryMapped(Runner runner) { long N = runner.N; ManagedMemoryMapping <double> view = null; runner.Measure("TeaFile<double>.OpenMemoryMapping", () => { view = TeaFile <double> .OpenMemoryMapping(filename); }); double sum = 0d; runner.Measure("memmap sum += view[i]", () => { for (int i = 0; i < N; i++) { sum += view[i]; } }); Console.WriteLine(sum); runner.Measure("memmap sum += view[i] 2nd run", () => { for (int i = 0; i < N; i++) { sum += view[i]; } }); view.Dispose(); view = null; }
static void Main(string[] args) { try { if (args.Count() != 1) { throw new Exception("Usage: CreateTicks <filename>"); } string filename = args.First(); using (var tf = TeaFile <Tick> .OpenRead(filename)) { Console.WriteLine(tf.Description); Console.WriteLine("ItemAreaStart={0}", tf.ItemAreaStart); Console.WriteLine("ItemAreaEnd={0}", tf.ItemAreaEnd); Console.WriteLine("ItemAreaSize={0}", tf.ItemAreaSize); foreach (Tick tick in tf.Items.Take(5)) { Console.WriteLine(tick); } } } catch (Exception ex) { Console.WriteLine(ex.Message); } }
static void Main() { // clean up from previous runs File.Delete("acme.tea"); // create file using (var tf = TeaFile <Tick> .Create("acme.tea")) { tf.Write(new Tick { Time = DateTime.Now, Price = 12 }); tf.Write(new Tick { Time = DateTime.Now.AddDays(1), Price = 15 }); tf.Write(new Tick { Time = DateTime.Now.AddDays(2), Price = 18 }); } // read file using (var tf = TeaFile <Tick> .OpenRead("acme.tea")) { foreach (var tick in tf.Items) { Console.WriteLine(tick); } tf.Write(new Tick { Time = DateTime.Now.AddDays(1), Price = 15 }); tf.Write(new Tick { Time = DateTime.Now.AddDays(2), Price = 18 }); } // create file with description File.Delete("acme.tea"); using (var tf = TeaFile <Tick> .Create("acme.tea", "this file holds acme prices", NameValueCollection.From("decimals", 2, "datafeed", "bloomfield"))) { tf.Write(new Tick { Time = DateTime.Now, Price = 12 }); tf.Write(new Tick { Time = DateTime.Now.AddDays(1), Price = 15 }); tf.Write(new Tick { Time = DateTime.Now.AddDays(2), Price = 18 }); } // get the file description by filename using (var tf = TeaFile <Tick> .OpenRead("acme.tea")) { Console.WriteLine(tf.Description.ContentDescription); Console.WriteLine(tf.Description.NameValues.GetValue <int>("decimals")); Console.WriteLine(tf.Description.NameValues.GetValue <string>("datafeed")); } }
private static void LexingWithAFile() { var tea = new TeaFile("Resources/LoginWithFailingUsername.tea"); Console.WriteLine(tea.URL); Console.WriteLine(tea.TestCase); tea.ActionLines.ForEach(line => Console.WriteLine(line.ToString())); }
public TeaFilePointList(DateTime[] x, double[] y) { tf = TeaFile <point> .Create(Path.GetRandomFileName() + ".tea"); for (var i = 0; i < x.Length; i++) { tf.Write(new point() { x = x[i], y = y[i] }); } }
public bool AddTeaFile(TeaFile f) { DBSession db = new DBSession(); if (tfdal.Add(f) != null) { db.SaveChanges(); return(true); } ; return(false); }
public IEnumerable <Tick> Get(Instrument instr) { var fileName = getFileBy(instr); using (var tf = TeaFile <Tick> .OpenWrite(fileName)) { foreach (var item in tf.Items) { yield return(item); } } }
private void Insert() { { try { var sw = Stopwatch.StartNew(); Time t = DateTime.Now; Task.Run( () => { if (!System.IO.File.Exists(ConnectionString)) { using (var tf = TeaFile <Tick> .Create(ConnectionString)) { tf.Write( Enumerable.Range(1, 100).Select( i => new Tick { Time = t.AddDays(i), Price = i * 101.0, Volume = i * 1000 })); } } else { using (var tf = TeaFile <Tick> .Append(ConnectionString)) { tf.Write( Enumerable.Range(1, 100).Select( i => new Tick { Time = t.AddDays(i), Price = i * 101.0, Volume = i * 1000 })); } } }) .ContinueWith(x => { sw.Stop(); }).ContinueWith(x => ExecutionTime.Value = sw.ElapsedMilliseconds, TaskScheduler.FromCurrentSynchronizationContext()); sw.Stop(); } catch (Exception ex) { Console.WriteLine(ex.Message); } } }
static unsafe double RunMemoryMapped(string filename) { double sum = 0; using (var fm = TeaFile <Tick> .OpenRawMemoryMapping(filename)) { for (Tick *tick = (Tick *)fm.ItemAreaStart; tick != fm.ItemAreaEnd; ++tick) { sum += tick->Price; } } return(sum); }
static void Run(string filename, bool memoryMapped, bool displayValues) { using (var tf = TeaFile <Tick> .OpenRead(filename, ItemDescriptionElements.None)) { if (tf.Items.Count == 0) { throw new Exception("File holds no items."); } } Statistics stats; if (memoryMapped) { stats = GetSessionDataMemMapped(filename); } else { stats = GetSessionData(filename); } int minTransactions = int.MaxValue; int maxTransactions = int.MinValue; foreach (var session in stats.Sessions) { minTransactions = Math.Min(minTransactions, session.TickCount); maxTransactions = Math.Max(maxTransactions, session.TickCount); } Console.WriteLine("min price = " + stats.MinPrice); Console.WriteLine("max price = " + stats.MaxPrice); Console.WriteLine("min ticks per session = " + minTransactions); Console.WriteLine("max ticks per session = " + maxTransactions); var tickCounts = stats.Sessions.Select(s => s.TickCount).ToArray(); Array.Sort(tickCounts); int median = tickCounts[tickCounts.Length / 2]; Console.WriteLine("median = " + median); if (displayValues) { double minimumExpectedTicksPerSession = median / 2.0; Console.WriteLine("First 10 sessions:"); foreach (var s in stats.Sessions.Take(10)) { Console.WriteLine(s + " " + ((s.TickCount >= minimumExpectedTicksPerSession) ? "OK" : "QUESTIONABLE")); } } }
/// <summary> /// Сохраняем в хранилище полученные пару валют за текущее время /// </summary> /// <param name="currentPair">Пара валют</param> /// <param name="fileName">Названии файла</param> public static void WriteTick(TimeCurrencyPair currentPair, string fileName) { try { using (var tf = TeaFile <TimeCurrencyPair> .Append(fileName)) { tf.Write(currentPair); } } catch (Exception ex) { Console.WriteLine(ex.Message); } }
private Tick[] Refresh() { IItemCollection <Tick> ticks = null; if (System.IO.File.Exists(ConnectionString)) { using (var tf = TeaFile <Tick> .OpenRead(ConnectionString, ItemDescriptionElements.None)) { ticks = tf.Items; return(ticks.ToArray()); } } return(null); }
public void SnapshotReturnsCorrectValue() { const string filename = "snapshotreturnscorrectvalue.tea"; using (var tf = TeaFile <int> .Create(filename)) { tf.Write(Enumerable.Range(3, 5)); } var s = TeaTime.TeaFile.GetSnapshot(filename); s.First.Values[0].Should().Be(3); s.Last.Values[0].Should().Be(7); s.Count.Should().Be(5); }
/// <summary> /// Сохраняем статус аккаунта после выполниения команды от нейросети /// </summary> /// <param name="fileName">Название файла-хранилища </param> /// <returns></returns> public static void WriteResponseLastCommand(TradeTaskResponse response, string fileName) { TradeTaskResponse lastCommandResponse = new TradeTaskResponse(); try { using (var tf = TeaFile <TradeTaskResponse> .Append(fileName)) { tf.Write(lastCommandResponse); } } catch (Exception ex) { // TODO log } }
internal static void InitDbCommandResponse(string fileNameCommandResponse) { TradeTaskResponse lastCommandResponse = new TradeTaskResponse(); try { using (var tf = TeaFile <TradeTaskResponse> .Create(fileNameCommandResponse)) { tf.Write(lastCommandResponse); } } catch (Exception ex) { //Logger.Log.Error("Ошибка! Не удалось создать файл базы данных кошелька"); } }
static void CreateSampleFile(string filename, int n) { r = new Random(1); var t = new Time(2000, 1, 1); File.Delete(filename); using (var tf = TeaFile <Tick> .Create(filename)) { while (n-- > 0) { bool isGoodDay = DrawRandom(90); WriteDailyTicks(tf, t, isGoodDay); t = t.AddDays(1); } } }
internal static void InitDbCommandResponse(string fileNameCommandResponse) { TradeTaskResponse lastCommandResponse = new TradeTaskResponse(); try { using (var tf = TeaFile <TradeTaskResponse> .Create(fileNameCommandResponse)) { tf.Write(lastCommandResponse); } } catch (Exception ex) { // TODO log } }
public void CanRunASeleniumTest() { var tea = new TeaFile("./../../../Resources/LoginWithFailingUsername.tea"); WebBrowser browser = new LoggingBrowser(new WebBrowser()); browser.Start(BrowserList.Chrome); browser.GoTo(tea.URL); foreach (var line in tea.ActionLines) { DoAction doAction = new DoAction(ref browser, line.Action); ActionStrategy strategy = doAction.SelectBy(line.By); strategy.Execute(line.Text); } browser.Quit(); }
static void WriteDailyTicks(TeaFile <Tick> tf, Time day, bool isGoodDay) { Time t = day.AddHours(9); // start trading session at 09:00 Time end = day.AddHours(17.5); // end trading session at 17:30 while (t < end) { // on a good day, we write many ticks, on a bad day however only 1 percent as much if (isGoodDay || DrawRandom(1)) { double p = r.NextDouble() * 100000.0; tf.Write(new Tick { Time = t, Price = p, Volume = 10 }); } t = t.AddSeconds(15 + r.Next(0, 20)); } }
public void Add(Instrument instr, Tick tick) { var fileName = getFileBy(instr); if (!File.Exists(fileName)) { using (var tf = TeaFile <Tick> .Create(fileName)) { tf.Write(tick); } } else { using (var tf = TeaFile <Tick> .Append(fileName)) { tf.Write(tick); } } }