static void StackExecutions(BitFlyerClient client, ICacheFactory factory, BfProductCode productCode, int count) { using (var cache = factory.GetExecutionCache(productCode)) { var recs = cache.GetManageTable(); cache.CommitCount *= 100; var completed = new ManualResetEvent(false); var recordCount = 0; var sw = new Stopwatch(); sw.Start(); new HistoricalExecutionSource(client, productCode, recs[0].EndExecutionId + count, recs[0].EndExecutionId).Subscribe(exec => { cache.Add(exec); if ((++recordCount % 10000) == 0) { Console.WriteLine("{0} {1} Completed {2} Elapsed", exec.ExecutedTime.ToLocalTime(), recordCount, sw.Elapsed); } }, () => { cache.SaveChanges(); sw.Stop(); completed.Set(); }); completed.WaitOne(); cache.SaveChanges(); } }
public void Login(string apiKey, string apiSecret) { if (string.IsNullOrEmpty(apiKey) || string.IsNullOrEmpty(apiSecret)) { throw new ArgumentException("Invalid API key or secret."); } if (Client == null) { Client = new BitFlyerClient(apiKey, apiSecret); } else { Client.ApplyApiKeyAndSecrets(apiKey, apiSecret); } // Check API permissions var permissions = Client.GetPermissions().GetResult(); if (!permissions.Where(e => e.Contains("v1/me/")).Any()) { throw new BitFlyerDotNetException("Any of enabled private API permission is not found."); } Initialize(); }
static void FillGaps(BitFlyerClient client, ICacheFactory factory, BfProductCode productCode) { using (var cache = factory.GetExecutionCache(productCode)) { cache.CommitCount *= 100; var completed = new ManualResetEvent(false); var recordCount = 0; var sw = new Stopwatch(); sw.Start(); cache.FillGaps(client).Subscribe(exec => { if ((++recordCount % 10000) == 0) { Console.WriteLine("{0} {1} Completed {2} Elapsed", exec.ExecutedTime.ToLocalTime(), recordCount, sw.Elapsed); } }, () => { cache.SaveChanges(); sw.Stop(); completed.Set(); }); completed.WaitOne(); } }
public static void Classinitialize(TestContext context) { _productCode = Enum.Parse <BfProductCode>(context.Properties["ProductCode"].ToString()); _key = context.Properties["ApiKey"].ToString(); _secret = context.Properties["ApiSecret"].ToString(); _client = new BitFlyerClient(_key, _secret); _cacheDirectoryPath = context.Properties["CacheDirectoryPath"].ToString(); _connStr = "data source=" + Path.Combine(_cacheDirectoryPath, "TradingApiTests.db3"); //_connStr = "data source=" + Path.Combine(_cacheDirectoryPath, "account.db3"); }
public void Initialize() { _client = new BitFlyerClient(_key, _secret); _client.ConfirmCallback = (apiName, json) => { var jobject = JsonConvert.DeserializeObject(json); json = JsonConvert.SerializeObject(jobject, Formatting.Indented, BitFlyerClient.JsonSerializeSettings); _requestJson = $"{apiName}: " + Environment.NewLine + json; return(_enableSendOrder); // Disable send order to market }; }
public ExecutionCachedSource(BitFlyerClient client, IExecutionCache cache, BfProductCode productCode, int before) { _client = client; _productCode = productCode; _cache = cache; Debug.WriteLine($"{nameof(ExecutionCachedSource)} constructed Before={before}"); var manageRecords = _cache.GetManageTable(); _source = (manageRecords.Count == 0) ? CreateSimpleCopySource(before, 0) : CreateMergedSource(manageRecords, before); }
public void Initialize() { if (Client == null) { Client = new BitFlyerClient(); } if (RealtimeSource == null) { RealtimeSource = new RealtimeSourceFactory(Client); } RealtimeSource.AvailableMarkets.ForEach(productCode => { _markets.Add(productCode, new BfTradingMarket(this, productCode)); }); }
public BfxAccount(string apiKey, string apiSecret) { if (string.IsNullOrEmpty(apiKey) || string.IsNullOrEmpty(apiKey)) { Client = new BitFlyerClient().AddTo(_disposables); RealtimeSource = RealtimeSourceFactory.Singleton; } else { Client = new BitFlyerClient(apiKey, apiSecret).AddTo(_disposables); RealtimeSource = RealtimeSourceFactory.Singleton; RealtimeSource.Authenticate(apiKey, apiSecret); } RealtimeSource.ConnectionResumed += OnRealtimeConnectionResumed; }
static void Main(string[] args) { // ログファイル出力設定 // Time / JSON LoadRunsettings(args[0]); var key = Properties["ApiKey"]; var secret = Properties["ApiSecret"]; _client = new BitFlyerClient(key, secret); _factory = new RealtimeSourceFactory(key, secret); _factory.MessageReceived += OnRealtimeMessageReceived; _factory.Error += (error) => Console.WriteLine("Error: {0} Socket Error = {1}", error.Message, error.SocketError); _factory.GetTickerSource(ProductCode).Subscribe(ticker => { _ticker = ticker; //Console.WriteLine($"Ask:{_ticker.BestAsk} Bid:{_ticker.BestBid}"); }).AddTo(_disposables); _factory.GetChildOrderEventsSource().Subscribe(OnChildOrderEvent).AddTo(_disposables); _factory.GetParentOrderEventsSource().Subscribe(OnParentOrderEvent).AddTo(_disposables); while (true) { Console.WriteLine("========================================================================"); Console.WriteLine("C)hild order >"); Console.WriteLine("P)arent order >"); Console.WriteLine(""); Console.WriteLine("Q)uit"); Console.WriteLine("========================================================================"); switch (GetCh()) { case 'C': ChildOrderMain(); break; case 'P': ParentOrderMain(); break; case 'Q': _disposables.Dispose(); return; } } }
public HistoricalExecutionSource(BitFlyerClient client, BfProductCode productCode, int before, int after, int readCount = ReadCountMax) { readCount = Math.Min(readCount, ReadCountMax); _source = Observable.Create <IBfExecution>(observer => { return(Task.Run(() => { while (true) { var result = client.GetExecutions(productCode, ReadCountMax, before, 0); if (result.IsError) { if (result.StatusCode == HttpStatusCode.BadRequest) // no more records { observer.OnCompleted(); return; } Thread.Sleep(ErrorInterval); continue; } Thread.Sleep(ReadInterval); var elements = result.GetResult(); foreach (var element in elements) { if (_cancel.IsCancellationRequested) { observer.OnCompleted(); return; } if (element.ExecutionId <= after) { observer.OnCompleted(); return; } observer.OnNext(element); } before = elements.Last().ExecutionId; } })); }); }
public IObservable <IBfExecution> UpdateRecents(BitFlyerClient client) { var after = 0L; var manageRec = _dbctx.ManageTable.ToList(); if (manageRec.Count > 0) { after = manageRec[0].EndExecutionId; } return(new HistoricalExecutionSource(client, _productCode, 0, after) .Select(exec => { Add(exec); return exec; }) .Finally(() => { SaveChanges(); })); }
public void CacheFillGapTest() { var client = new BitFlyerClient(); var cacheFactory = new SqlServerCacheFactory(@"server=(local);Initial Catalog=bitflyer;Integrated Security=True"); var cache = cacheFactory.GetExecutionCache(BfProductCode.FXBTCJPY); var completed = new ManualResetEvent(false); var recentTime = DateTime.UtcNow; cache.FillGaps(client).Subscribe(exec => { if (exec.ExecutedTime.Day != recentTime.Day) { recentTime = exec.ExecutedTime; Console.WriteLine("{0} Completed", recentTime.ToLocalTime().Date); } }, () => { completed.Set(); }); completed.WaitOne(); }
public IObservable <IBfExecution> FillGaps(BitFlyerClient client) { return(_dbctx.ManageTable.Buffer(2, 1).SkipLast(1).Select(rec => { var count = 0; return new HistoricalExecutionSource(client, _productCode, rec[0].StartExecutionId, rec[1].EndExecutionId) .Select(exec => { count++; return exec; }) .Finally(() => { if (count == 0) { InsertGap(rec[0].StartExecutionId, rec[1].EndExecutionId); } }); }).Concat().Select(exec => { Add(exec); return exec; }) .Finally(() => { SaveChanges(); })); }
public BfxOrderCache(BitFlyerClient client, BfProductCode productCode) { _client = client; _productCode = productCode; }
static void Main(string[] args) { var productCode = BfProductCode.FXBTCJPY; var client = new BitFlyerClient(); #if SQLSERVER var connStr = @"server=(local);Initial Catalog=bitflyer;Integrated Security=True"; var cacheFactory = new SqlServerCacheFactory(connStr); #elif SQLITE var folderPath = Path.Combine( Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), Path.GetFileNameWithoutExtension(Process.GetCurrentProcess().ProcessName) ); var cacheFactory = new SqliteCacheFactory(folderPath); #endif Console.WriteLine("BitFlyerDotNet cache management utilities"); Console.WriteLine("Copyright (C) 2017-2018 Fiats Inc."); try { while (true) { Console.WriteLine(); Console.WriteLine("S)elect product"); Console.WriteLine("U)pdate recent historical executions in cache"); Console.WriteLine("F)ill fragmentations"); Console.WriteLine("A)dd specified count"); Console.WriteLine("R)ange updates"); Console.WriteLine("O)ptimize manage table"); //Console.WriteLine("I)mport SQLite cache"); Console.WriteLine(); Console.WriteLine("Hit Q key to exit."); switch (GetCh()) { case 'Q': return; case 'S': productCode = SelectProduct(); break; case 'U': UpdateRecent(client, cacheFactory, productCode); Console.WriteLine("Completed."); break; case 'F': FillGaps(client, cacheFactory, productCode); Console.WriteLine("Completed."); break; case 'A': { Console.Write("Count : "); var count = int.Parse(Console.ReadLine()); Console.Write("Are you sure to start? (y/n)"); if (GetCh() == 'Y') { StackExecutions(client, cacheFactory, productCode, count); Console.WriteLine("Completed."); } } break; case 'R': { Console.Write("Before : "); var before = int.Parse(Console.ReadLine()); Console.Write("After : "); var after = int.Parse(Console.ReadLine()); Console.Write("Are you sure to start? (y/n)"); if (GetCh() == 'Y') { GetExecutionsRange(client, cacheFactory, productCode, before, after); Console.WriteLine("Completed."); } } break; case 'O': OptimizaManageTable(cacheFactory, productCode); break; case 'I': //ImportCache(sqliteCacheFactory, sqlserverCacheFactory, productCode); break; } } } catch (Exception ex) { Console.WriteLine(ex.Message); Console.WriteLine("Hit Q key to exit."); while (GetCh() != 'Q') { ; } } finally { } }
public void Initialize() { _client = new BitFlyerClient(); }
static void Main(string[] args) { var productCode = BfProductCode.FXBTCJPY; var client = new BitFlyerClient(); var config = new ConfigurationBuilder() .SetBasePath(Directory.GetCurrentDirectory()) .AddJsonFile("appsettings.json", true, true) .Build(); var connStr = config.GetConnectionString("bitflyer"); var cacheFactory = new SqlServerCacheFactory(connStr); if (args.Length > 0) { productCode = Enum.Parse <BfProductCode>(args[0]); UpdateRecent(client, cacheFactory, productCode); FillGaps(client, cacheFactory, productCode); GenerateOhlc(cacheFactory, productCode); return; } Console.WriteLine("BitFlyerDotNet cache management utilities"); Console.WriteLine("Copyright (C) 2017-2021 Fiats Inc."); try { while (true) { Console.WriteLine(); Console.WriteLine("S)elect product"); Console.WriteLine("U)pdate recent historical executions in cache"); Console.WriteLine("F)ill fragmentations"); Console.WriteLine("O)ptimize manage table"); Console.WriteLine("G)enerate OHLC"); Console.WriteLine(); Console.WriteLine("Hit Q key to exit."); switch (GetCh()) { case 'Q': return; case 'S': productCode = SelectProduct(); break; case 'U': UpdateRecent(client, cacheFactory, productCode); Console.WriteLine("Completed."); break; case 'F': FillGaps(client, cacheFactory, productCode); Console.WriteLine("Completed."); break; case 'O': OptimizaManageTable(cacheFactory, productCode); break; case 'G': GenerateOhlc(cacheFactory, productCode); break; } } } catch (Exception ex) { Console.WriteLine(ex); Console.WriteLine("Hit Q key to exit."); while (GetCh() != 'Q') { ; } } finally { } }
public BfTradingAccount(BitFlyerClient client, RealtimeSourceFactory realtimeSource) { Client = client; RealtimeSource = realtimeSource; }
public ExecutionCachedSourceFactory(BitFlyerClient client, ICacheFactory cacheFactory) { _client = client; _cacheFactory = cacheFactory; }
public void Initialize() { _client = new BitFlyerClient(_key, _secret); }