Ejemplo n.º 1
0
        /// <summary>
        /// Initializes a new instance of the <see cref="OandaRestApiBase"/> class.
        /// </summary>
        /// <param name="symbolMapper">The symbol mapper.</param>
        /// <param name="orderProvider">The order provider.</param>
        /// <param name="securityProvider">The holdings provider.</param>
        /// <param name="environment">The Oanda environment (Trade or Practice)</param>
        /// <param name="accessToken">The Oanda access token (can be the user's personal access token or the access token obtained with OAuth by QC on behalf of the user)</param>
        /// <param name="accountId">The account identifier.</param>
        /// <param name="agent">The Oanda agent string</param>
        protected OandaRestApiBase(OandaSymbolMapper symbolMapper, IOrderProvider orderProvider, ISecurityProvider securityProvider, Environment environment, string accessToken, string accountId, string agent)
            : base("Oanda Brokerage")
        {
            SymbolMapper     = symbolMapper;
            OrderProvider    = orderProvider;
            SecurityProvider = securityProvider;
            Environment      = environment;
            AccessToken      = accessToken;
            AccountId        = accountId;
            Agent            = agent;

            PricingConnectionHandler = new DefaultConnectionHandler {
                MaximumIdleTimeSpan = TimeSpan.FromSeconds(20)
            };
            PricingConnectionHandler.ConnectionLost     += OnPricingConnectionLost;
            PricingConnectionHandler.ConnectionRestored += OnPricingConnectionRestored;
            PricingConnectionHandler.ReconnectRequested += OnPricingReconnectRequested;
            PricingConnectionHandler.Initialize(null);

            TransactionsConnectionHandler = new DefaultConnectionHandler {
                MaximumIdleTimeSpan = TimeSpan.FromSeconds(20)
            };
            TransactionsConnectionHandler.ConnectionLost     += OnTransactionsConnectionLost;
            TransactionsConnectionHandler.ConnectionRestored += OnTransactionsConnectionRestored;
            TransactionsConnectionHandler.ReconnectRequested += OnTransactionsReconnectRequested;
            TransactionsConnectionHandler.Initialize(null);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Creates an instance of a websockets brokerage
        /// </summary>
        /// <param name="wssUrl">Websockets base url</param>
        /// <param name="websocket">Websocket client instance</param>
        /// <param name="restClient">Rest client instance</param>
        /// <param name="apiKey">Brokerage api auth key</param>
        /// <param name="apiSecret">Brokerage api auth secret</param>
        /// <param name="websocketMaximumIdle">The maximum amount of time the socket can go idle before triggering a reconnect</param>
        /// <param name="name">Name of brokerage</param>
        protected BaseWebsocketsBrokerage(string wssUrl, IWebSocket websocket, IRestClient restClient, string apiKey, string apiSecret, TimeSpan websocketMaximumIdle, string name) : base(name)
        {
            JsonSettings = new JsonSerializerSettings {
                FloatParseHandling = FloatParseHandling.Decimal
            };
            CachedOrderIDs     = new ConcurrentDictionary <int, Orders.Order>();
            _connectionHandler = new DefaultConnectionHandler {
                MaximumIdleTimeSpan = websocketMaximumIdle
            };

            WebSocket = websocket;
            WebSocket.Initialize(wssUrl);
            WebSocket.Message += (sender, message) =>
            {
                OnMessage(sender, message);
                _connectionHandler.KeepAlive(LastHeartbeatUtcTime);
            };

            _connectionHandler.Initialize(Guid.NewGuid().ToString());
            _connectionHandler.ReconnectRequested += (sender, args) =>
            {
                Reconnect();
            };

            RestClient = restClient;
            ApiSecret  = apiSecret;
            ApiKey     = apiKey;
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Initializes a new instance of the <see cref="OandaRestApiBase"/> class.
        /// </summary>
        /// <param name="symbolMapper">The symbol mapper.</param>
        /// <param name="orderProvider">The order provider.</param>
        /// <param name="securityProvider">The holdings provider.</param>
        /// <param name="aggregator">Consolidate ticks</param>
        /// <param name="environment">The Oanda environment (Trade or Practice)</param>
        /// <param name="accessToken">The Oanda access token (can be the user's personal access token or the access token obtained with OAuth by QC on behalf of the user)</param>
        /// <param name="accountId">The account identifier.</param>
        /// <param name="agent">The Oanda agent string</param>
        protected OandaRestApiBase(OandaSymbolMapper symbolMapper, IOrderProvider orderProvider, ISecurityProvider securityProvider, IDataAggregator aggregator, Environment environment, string accessToken, string accountId, string agent)
            : base("Oanda Brokerage")
        {
            SymbolMapper         = symbolMapper;
            OrderProvider        = orderProvider;
            SecurityProvider     = securityProvider;
            Environment          = environment;
            AccessToken          = accessToken;
            AccountId            = accountId;
            Agent                = agent;
            Aggregator           = aggregator;
            _subscriptionManager = new EventBasedDataQueueHandlerSubscriptionManager();
            _subscriptionManager.SubscribeImpl   += (s, t) => Refresh();
            _subscriptionManager.UnsubscribeImpl += (s, t) => Refresh();

            PricingConnectionHandler = new DefaultConnectionHandler {
                MaximumIdleTimeSpan = TimeSpan.FromSeconds(20)
            };
            PricingConnectionHandler.ConnectionLost     += OnPricingConnectionLost;
            PricingConnectionHandler.ConnectionRestored += OnPricingConnectionRestored;
            PricingConnectionHandler.ReconnectRequested += OnPricingReconnectRequested;
            PricingConnectionHandler.Initialize(null);

            TransactionsConnectionHandler = new DefaultConnectionHandler {
                MaximumIdleTimeSpan = TimeSpan.FromSeconds(20)
            };
            TransactionsConnectionHandler.ConnectionLost     += OnTransactionsConnectionLost;
            TransactionsConnectionHandler.ConnectionRestored += OnTransactionsConnectionRestored;
            TransactionsConnectionHandler.ReconnectRequested += OnTransactionsReconnectRequested;
            TransactionsConnectionHandler.Initialize(null);

            Task.Factory.StartNew(
                () =>
            {
                do
                {
                    _refreshEvent.WaitOne();
                    Thread.Sleep(SubscribeDelay);

                    if (!_isConnected)
                    {
                        continue;
                    }

                    _refreshEvent.Reset();

                    var symbolsToSubscribe = SubscribedSymbols;
                    // restart streaming session
                    SubscribeSymbols(symbolsToSubscribe);
                } while (!_streamingCancellationTokenSource.IsCancellationRequested);
            },
                TaskCreationOptions.LongRunning
                );
        }