Click or drag to resize

Multiple connections

S# has a mechanism to work simultaneously with multiple connections (connectors). For example, the algorithm can simultaneously perform trade through Interactive Brokers and OpenECry, or work with multiple Interactive Brokers terminals. This mechanism is implemented at the message level (see Messages). Behind the scenes of this mechanism is the use of the BasketMessageAdapter class, which allows you to sequentially operate multiple adapters connected to various trading systems. The Connector class has the Adapter property of this type.

It is possible to organize work with several connectors downloading the settings from the special configuration file, in which the connector settings are saved, including adapters settings. Or it is possible to use the ConnectorWindow component, which is a graphical service for the connector configuration.

The following is an example of creating multiple connection. Source codes of the example are in the Samples/Common/SampleMultiConnection project.

multiconnection main

Creating multiple connection

  1. In the main window constructor we call the InitConnector method in which the connector is configured, as well as subscription to the required events is performed.

    public MainWindow()
        Instance = this;
        Title = Title.Put("Multi connection");
        var logManager = new LogManager();
        logManager.Listeners.Add(new FileLogListener("sample.log"));
  2. The subscription to the connector events is performed in the InitConnector method, as well as connector settings loading from the configuration file using the ConnectorLoad(SettingsStorage) method.

    private void InitConnector()
        // subscribe on connection successfully event
        Connector.Connected += () =>
            this.GuiAsync(() => ChangeConnectStatus(true));
        // subscribe on connection error event
        Connector.ConnectionError += error => this.GuiAsync(() =>
            MessageBox.Show(this, error.ToString(), LocalizedStrings.Str2959);
        Connector.Disconnected += () => this.GuiAsync(() => ChangeConnectStatus(false));
        // subscribe on error event
        Connector.Error += error =>
            this.GuiAsync(() => MessageBox.Show(this, error.ToString(), LocalizedStrings.Str2955));
        // subscribe on error of market data subscription event
        Connector.MarketDataSubscriptionFailed += (security, type, error) =>
            this.GuiAsync(() => MessageBox.Show(this, error.ToString(), LocalizedStrings.Str2956Params.Put(type, security)));
        Connector.NewSecurities += securities => _securitiesWindow.SecurityPicker.Securities.AddRange(securities);
        Connector.NewOrders += orders => _ordersWindow.OrderGrid.Orders.AddRange(orders);
        // subscribe on error of order registration event
        Connector.OrdersRegisterFailed += OrdersFailed;
        // subscribe on error of order cancelling event
        Connector.OrdersCancelFailed += OrdersFailed;
        // set market data provider
        _securitiesWindow.SecurityPicker.MarketDataProvider = Connector;
            if (File.Exists(_settingsFile))
                Connector.Load(new XmlSerializer<SettingsStorage>().Deserialize(_settingsFile));
  3. The following is the code of the Settings button Click event handler. The Configure(Connector, Window) method is called in the handler. This method opens the Connection Settings window (see below). Also in the handler the connector settings are saved to the configuration file. Further this file can be used to configure the connector without calling the Connection Settings window

    private void SettingsClick(object sender, RoutedEventArgs e)
        new XmlSerializer<SettingsStorage>().Serialize(Connector.Save(), _settingsFile);
  4. The connection settings window is shown here. It is necessary to select the required adapters from the drop-down list (it is opened by clicking '+') and to set their properties in the Properties window on the right. For example, for OpenECry it is required to specify the server address, login and password.

    multiconnection settings
    Note Note

    The valid of the connection can be checked using the Test button.

    The result of this window will be creating and adding the adapters to the list of the ConnectorAdapter property internal adapters.