Table of Contents

Connectors

To work with exchanges and data sources in S# it is recommended to use the base class Connector.

Let's consider working with Connector. The example source codes are in the project Samples/Common/SampleConnection.

multiconnection main

We create an instance of the Connector class:

...
public Connector Connector;
...
public MainWindow()
{
	InitializeComponent();
	Connector = new Connector();
	InitConnector();
}
		

To configure ConnectorAPI has a special graphical interface in which you can configure several connections at once. How to use it see Graphical configuration.

...
private const string _connectorFile = "ConnectorFile";
...
private void Setting_Click(object sender, RoutedEventArgs e)
{
	if (Connector.Configure(this))
	{
		new XmlSerializer<SettingsStorage>().Serialize(Connector.Save(), _connectorFile);
	}
}
	  				

API GUI ConnectorWindow

Similarly, you can add connections directly from the code (without graphic windows) using the extension method TraderHelper.AddAdapter<TAdapter>(StockSharp.Algo.Connector connector, System.Action<TAdapter> init**)**:

...
connector.AddAdapter<BarChartMessageAdapter>(a => { });

You can add an unlimited number of connections to a single Connector object. Therefore, from the program you can simultaneously connect to several exchanges and brokers at once.

In the InitConnector method, we set the required IConnector event handlers:

private void InitConnector()
{
	// subscribe on connection successfully event
	Connector.Connected += () =>
	{
		this.GuiAsync(() => ChangeConnectStatus(true));
	};
	// subscribe on connection error event
	Connector.ConnectionError += error => this.GuiAsync(() =>
	{
		ChangeConnectStatus(false);
		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, msg, error) =>
		this.GuiAsync(() => MessageBox.Show(this, error.ToString(), LocalizedStrings.Str2956Params.Put(msg.DataType, security)))
	Connector.NewSecurity += _securitiesWindow.SecurityPicker.Securities.Add;
	Connector.NewTrade += _tradesWindow.TradeGrid.Trades.Add;
	Connector.NewOrder += _ordersWindow.OrderGrid.Orders.Add;
	Connector.NewStopOrder += _stopOrdersWindow.OrderGrid.Orders.Add;
	Connector.NewMyTrade += _myTradesWindow.TradeGrid.Trades.Add;
	
	Connector.NewPortfolio += _portfoliosWindow.PortfolioGrid.Portfolios.Add;
	Connector.NewPosition += _portfoliosWindow.PortfolioGrid.Positions.Add;
	// subscribe on error of order registration event
	Connector.OrderRegisterFailed += _ordersWindow.OrderGrid.AddRegistrationFail;
	// subscribe on error of order cancelling event
	Connector.OrderCancelFailed += OrderFailed;
	// subscribe on error of stop-order registration event
	Connector.OrderRegisterFailed += _stopOrdersWindow.OrderGrid.AddRegistrationFail;
	// subscribe on error of stop-order cancelling event
	Connector.StopOrderCancelFailed += OrderFailed;
	// set market data provider
	_securitiesWindow.SecurityPicker.MarketDataProvider = Connector;
	try
	{
		if (File.Exists(_settingsFile))
		{
			var ctx = new ContinueOnExceptionContext();
			ctx.Error += ex => ex.LogError();
			using (new Scope<ContinueOnExceptionContext> (ctx))
				Connector.Load(new XmlSerializer<SettingsStorage>().Deserialize(_settingsFile));
		}
	}
	catch
	{
	}
	ConfigManager.RegisterService<IExchangeInfoProvider>(new InMemoryExchangeInfoProvider());
	
	// needed for graphical settings
	ConfigManager.RegisterService<IMessageAdapterProvider>(new FullInMemoryMessageAdapterProvider(Connector.Adapter.InnerAdapters));
}

To save and load Connector settings to a file, see Save and load settings.

To create your own Connector, see Creating own connector.

For registering orders, see Orders management, Create new order, Create new stop order.

Graphical configuration