Ejemplo n.º 1
0
        public override ConnectionResult Connect(ConnectRequestParameters connectRequestParameters)
        {
            if (!NetworkInterface.GetIsNetworkAvailable())
            {
                return(ConnectionResult.CreateFail("Network does not available"));
            }

            var settingItem = connectRequestParameters.ConnectionSettings.GetItemByPath(LOGIN_PARAMETER_GROUP, CONNECTION);

            if (settingItem == null || !(settingItem.Value is SelectItem selectItem))
            {
                return(ConnectionResult.CreateFail("Can't find connection parameters"));
            }

            MarketDataVendor hitBTCVendor = null;

            if (selectItem.Value.ToString() == CONNECTION_INFO)
            {
                hitBTCVendor = new MarketDataVendor();
            }
            else
            {
                hitBTCVendor = new TradingVendor();
            }

            hitBTCVendor.NewMessage += (m) => this.PushMessage(m);

            this.vendor = hitBTCVendor;

            return(this.vendor.Connect(connectRequestParameters));
        }
Ejemplo n.º 2
0
        public override ConnectionResult Connect(ConnectRequestParameters connectRequestParameters)
        {
            if (connectRequestParameters.CancellationToken.IsCancellationRequested)
            {
                return(ConnectionResult.CreateCancelled());
            }

            if (this.client.Connect(connectRequestParameters.CancellationToken, out string error))
            {
                var symbols = this.client.SymbolsProvider.GetSymbols(OKExInstrumentType.Spot, OKExInstrumentType.Swap, OKExInstrumentType.Futures, OKExInstrumentType.Option, OKExInstrumentType.Index).ToList();

                foreach (var s in symbols)
                {
                    this.allSymbolsCache[s.UniqueInstrumentId] = s;
                }

                return(ConnectionResult.CreateSuccess());
            }
            else
            {
                return(ConnectionResult.CreateFail(error ?? "Unknown error"));
            }
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Called when user decides to connect to this particular integration via Connections Screen
        /// </summary>
        public override ConnectionResult Connect(ConnectRequestParameters connectRequestParameters)
        {
            if (!NetworkInterface.GetIsNetworkAvailable())
            {
                return(ConnectionResult.CreateFail(loc._("Network does not available")));
            }

            this.socketApi.Connect();

            if (this.socketApi.ConnectionState != BitfinexConnectionState.Connected)
            {
                return(ConnectionResult.CreateFail(loc._("Can't connect via socket")));
            }

            var symbols = this.restApi.GetSymbolsDetails().Result;

            foreach (var symbol in symbols)
            {
                this.symbolsCache.Add(symbol.Pair, symbol);
            }

            return(ConnectionResult.CreateSuccess());
        }
Ejemplo n.º 4
0
        public override ConnectionResult Connect(ConnectRequestParameters connectRequestParameters)
        {
            // Initialize
            var config = this.HitConfig;

            this.restApi   = new HitRestApi(config);
            this.socketApi = new HitSocketApi(config);
            this.socketApi.ConnectionStateChanged += this.SocketApi_ConnectionStateChanged;
            this.socketApi.Notification           += this.SocketApi_Notification;

            this.currenciesCache = new Dictionary <string, HitCurrency>();
            this.symbolsCache    = new Dictionary <string, HitSymbol>();

            this.ping    = new Ping();
            this.pingUri = new Uri("https://api.hitbtc.com/api/2");

            this.lastsTimeCache = new Dictionary <string, long>();

            this.aggressorFlagCalculator = new AggressorFlagCalculator();

            // Connect
            var token = connectRequestParameters.CancellationToken;

            this.socketApi.ConnectAsync().Wait(token);

            if (token.IsCancellationRequested)
            {
                return(ConnectionResult.CreateCancelled());
            }

            if (this.socketApi.ConnectionState != HitConnectionState.Connected)
            {
                return(ConnectionResult.CreateFail("Can't connect to socket API"));
            }

            var currencies = this.CheckHitResponse(this.restApi.GetCurrenciesAsync(token).Result, out HitError hitError);

            if (hitError != null)
            {
                return(ConnectionResult.CreateFail(hitError.Format()));
            }

            foreach (var currency in currencies)
            {
                this.currenciesCache.Add(currency.Id, currency);
            }

            var symbols = this.CheckHitResponse(this.restApi.GetSymbolsAsync(token).Result, out hitError);

            if (hitError != null)
            {
                return(ConnectionResult.CreateFail(hitError.Format()));
            }

            foreach (var symbol in symbols)
            {
                this.symbolsCache.Add(symbol.Id, symbol);
            }

            return(ConnectionResult.CreateSuccess());
        }
Ejemplo n.º 5
0
        public override ConnectionResult Connect(ConnectRequestParameters connectRequestParameters)
        {
            var token = connectRequestParameters.CancellationToken;

            // read settings
            string apiKey = null;
            string secret = null;

            var settingItem = connectRequestParameters.ConnectionSettings.GetItemByPath(LOGIN_PARAMETER_GROUP, HitBTCVendor.PARAMETER_API_KEY);

            if (settingItem != null)
            {
                apiKey = settingItem.Value?.ToString();
            }

            if (string.IsNullOrEmpty(apiKey))
            {
                return(ConnectionResult.CreateFail("API key is empty"));
            }

            settingItem = connectRequestParameters.ConnectionSettings.GetItemByPath(LOGIN_PARAMETER_GROUP, HitBTCVendor.PARAMETER_SECRET_KEY);
            if (settingItem != null && settingItem.Value is PasswordHolder passwordHolder)
            {
                secret = passwordHolder.Password;
            }

            if (string.IsNullOrEmpty(secret))
            {
                return(ConnectionResult.CreateFail("Secret key is empty"));
            }

            // create config
            this.hitConfig = new HitConfig
            {
                ApiKey = apiKey,
                Secret = secret
            };

            if (token.IsCancellationRequested)
            {
                return(ConnectionResult.CreateCancelled());
            }

            // connect
            var result = base.Connect(connectRequestParameters);

            if (result.State != ConnectionState.Connected || result.Cancelled)
            {
                return(result);
            }

            // login
            this.CheckHitResponse(this.socketApi.LoginAsync(token).Result, out var error);
            if (error != null)
            {
                return(ConnectionResult.CreateFail(error.Format()));
            }

            if (token.IsCancellationRequested)
            {
                return(ConnectionResult.CreateCancelled());
            }

            // subscribe reports
            this.CheckHitResponse(this.socketApi.SubscribeReportsAsync(token).Result, out error);
            if (error != null)
            {
                return(ConnectionResult.CreateFail(error.Format()));
            }

            if (token.IsCancellationRequested)
            {
                return(ConnectionResult.CreateCancelled());
            }

            // get balances
            this.UpdateBalances(token, out error);
            if (error != null)
            {
                return(ConnectionResult.CreateFail(error.Format()));
            }

            if (token.IsCancellationRequested)
            {
                return(ConnectionResult.CreateCancelled());
            }

            return(ConnectionResult.CreateSuccess());
        }
Ejemplo n.º 6
0
        public override ConnectionResult Connect(ConnectRequestParameters connectRequestParameters)
        {
            if (!NetworkInterface.GetIsNetworkAvailable())
            {
                return(ConnectionResult.CreateFail(loc._("Network does not available")));
            }

            var settingItem = connectRequestParameters.ConnectionSettings.GetItemByPath(LOGIN_PARAMETER_GROUP, CONNECTION);

            if (settingItem == null || settingItem.Value is not SelectItem connection)
            {
                return(ConnectionResult.CreateFail(loc._("Can't find connection parameters")));
            }

            switch (connection.Value)
            {
            case OKExConsts.CONNECTION_INFO:
            {
                var client = new OKExMarketClient(OKExConsts.RealServer);
                this.currentVendor = new OKExMarketVendor(client);
                break;
            }

            case OKExConsts.CONNECTION_TRADING:
            {
                var parameters = connectRequestParameters.ConnectionSettings;
                var apikey     = string.Empty;
                var secretId   = string.Empty;
                var passphase  = string.Empty;

                // apikey
                settingItem = parameters.GetItemByPath(LOGIN_PARAMETER_GROUP, OKExConsts.PARAMETER_API_KEY);
                if (settingItem?.Value != null)
                {
                    apikey = settingItem.Value.ToString();
                }

                if (string.IsNullOrEmpty(apikey))
                {
                    return(ConnectionResult.CreateFail(loc._("ApiKey is empty")));
                }

                // secret id
                settingItem = parameters.GetItemByPath(LOGIN_PARAMETER_GROUP, OKExConsts.PARAMETER_SECRET_ID);
                if (settingItem?.Value != null)
                {
                    secretId = ((PasswordHolder)settingItem.Value)?.Password?.ToString();
                }

                if (string.IsNullOrEmpty(secretId))
                {
                    return(ConnectionResult.CreateFail(loc._("Secret id is empty")));
                }

                // passphase
                settingItem = parameters.GetItemByPath(LOGIN_PARAMETER_GROUP, OKExConsts.PARAMETER_PASSPHRASE_ID);
                if (settingItem?.Value != null)
                {
                    passphase = ((PasswordHolder)settingItem.Value)?.Password?.ToString();
                }

                if (string.IsNullOrEmpty(secretId))
                {
                    return(ConnectionResult.CreateFail(loc._("Passphase is empty")));
                }

                var client = new OKExTradingClient(apikey, secretId, passphase, OKExConsts.RealServer);
                this.currentVendor = new OKExTradingVendor(client);

                break;
            }
            }

            this.currentVendor.NewMessage += this.CurrentVendor_NewMessage;
            return(this.currentVendor?.Connect(connectRequestParameters));
        }