Connect()

View as Markdown

Definition

Connects to a connection.

Syntax

Connection.Connect(ConnectOptions options)

Parameters

ParameterDescription
optionsThe connection option of what you want to connect to

Examples

1// Example of subscribing/unsubscribing to execution update events from an Add On. The concept can be carried over
2// to any NinjaScript object you may be working on.
3public class MyAddOnTab : NTTabPage
4{
5 private Connection connection;
6 public MyAddOnTab()
7 {
8 // Connect to Kinetick EOD
9 if (connection == null)
10 connection = Connect("Kinetick - End Of Day (Free)");
11 }
12
13 private Connection Connect(string connectionName)
14 {
15 // Output the execution
16 try
17 {
18 // Get the configured account connection
19 ConnectOptions connectOptions = null;
20 lock (Core.Globals.ConnectOptions)
21 connectOptions = Core.Globals.ConnectOptions.FirstOrDefault(o => o.Name == connectionName);
22
23 if (connectOptions == null)
24 {
25 NinjaTrader.Code.Output.Process("Could not connect. No connection found.", PrintTo.OutputTab1);
26 return null;
27 }
28
29 // If connection is not already connected, connect.
30 lock (Connection.Connections)
31 if (Connection.Connections.FirstOrDefault(c => c.Options.Name == connectionName) == null)
32 {
33 Connection connect = Connection.Connect(connectOptions);
34
35 // Only return connection if successfully connected
36 if (connect.Status == ConnectionStatus.Connected)
37 return connect;
38 else
39 return null;
40 }
41
42 return null;
43 }
44 catch (Exception error)
45 {
46 NinjaTrader.Code.Output.Process("Connect exception: " + error.ToString(), PrintTo.OutputTab1);
47 return null;
48 }
49 }
50
51 // Called by TabControl when tab is being removed or window is closed
52 public override void Cleanup()
53 {
54 // Disconnect from our connection
55 if (connection != null)
56 connection.Disconnect();
57 }
58
59 // Other required NTTabPage members left out for demonstration purposes. Be sure to add them in your own code.
60}