Click or drag to resize

Getting started

The SampleFxcm application shows an example of working with the FxcmTrader as shown in the figure below. The example source code is in the Samples/FXCM folder of the installation package.

Fxcm Sample
  1. First, you need to create an instance of the FxcmTrader:

    C#
    Trader = new FxcmTrader();
    .....
    Trader.Login = Login.Text;
    Trader.Password = Password.Password;
    Trader.Address = Address.Text.To<Uri>();
    Trader.IsDemo = IsDemo.IsChecked == true;
  2. Next, you should subscribe on required IConnector events:

    C#
    // subscribe on connection successfully event
    Trader.Connected += () =>
    {
        // set flag (connection is established)
        _isConnected = true;
    
        // update gui labes
        this.GuiAsync(() => ChangeConnectStatus(true));
    };
    Trader.Disconnected += () => this.GuiAsync(() => ChangeConnectStatus(false));
    
    // subscribe on connection error event
    Trader.ConnectionError += error => this.GuiAsync(() =>
    {
        // update gui labes
        ChangeConnectStatus(false);
    
        MessageBox.Show(this, error.ToString(), LocalizedStrings.Str2959);
    });
    
    // subscribe on error event
    Trader.Error += error =>
        this.GuiAsync(() => MessageBox.Show(this, error.ToString(), LocalizedStrings.Str2955));
    
    // subscribe on error of market data subscription event
    Trader.MarketDataSubscriptionFailed += (security, msg, error) =>
        this.GuiAsync(() => MessageBox.Show(this, error.ToString(), LocalizedStrings.Str2956Params.Put(msg.DataType, security)));
    
    Trader.NewSecurity += security => _securitiesWindow.SecurityPicker.Securities.Add(security);
    Trader.NewMyTrade += trade => _myTradesWindow.TradeGrid.Trades.Add(trade);
    Trader.NewTrade += trade => _tradesWindow.TradeGrid.Trades.Add(trade);
    Trader.NewOrder += order => _ordersWindow.OrderGrid.Orders.Add(order);
    Trader.NewStopOrder += order => _stopOrdersWindow.OrderGrid.Orders.Add(order);
    
    Trader.NewPortfolio += portfolio =>
    {
        _portfoliosWindow.PortfolioGrid.Portfolios.Add(portfolio);
    
        // subscribe on portfolio updates
        Trader.RegisterPortfolio(portfolio);
    };
    Trader.NewPosition += position => _portfoliosWindow.PortfolioGrid.Positions.Add(position);
    
    // subscribe on error of order registration event
    Trader.OrderRegisterFailed += _ordersWindow.OrderGrid.AddRegistrationFail;
    // subscribe on error of order cancelling event
    Trader.OrderCancelFailed += OrderFailed;
    
    // subscribe on error of stop-order registration event
    Trader.StopOrderRegisterFailed += _stopOrdersWindow.OrderGrid.AddRegistrationFail;
    // subscribe on error of stop-order cancelling event
    Trader.StopOrderCancelFailed += OrderFailed;
    
    Trader.MassOrderCancelFailed += (transId, error) =>
        this.GuiAsync(() => MessageBox.Show(this, error.ToString(), LocalizedStrings.Str716));
  3. Finally, you need to connect to FXCM server:

    C#
    Trader.Connect();
  4. Once connected, you can expect to trigger events:

  5. Instruments search (in more detail):

    Fxcm Securities
    C#
    private void FindClick(object sender, RoutedEventArgs e)
    {
        var wnd = new SecurityLookupWindow { Criteria = new Security { Code = "USD" } };
    
        if (!wnd.ShowModal(this))
            return;
    
        MainWindow.Instance.Trader.LookupSecurities(wnd.Criteria);
    }
  6. For updates on the selected instrument and ticks you need to use the RegisterSecurity(Security) method:

    C#
    private void Level1Click(object sender, RoutedEventArgs e)
    {
        var trader = MainWindow.Instance.Trader;
    
        foreach (var security in SecurityPicker.SelectedSecurities)
        {
            if (trader.RegisteredSecurities.Contains(security))
            {
                trader.UnRegisterSecurity(security);
            }
            else
            {
                trader.RegisterSecurity(security);
            }
        }
    }
  7. For orders registration the New Order button used. The RegisterOrder(Order) method sends an order to the server:

    C#
    private void NewOrderClick(object sender, RoutedEventArgs e)
    {
        var newOrder = new OrderWindow
        {
            Order = new Order { Security = SecurityPicker.SelectedSecurity },
            SecurityProvider = MainWindow.Instance.Trader,
            MarketDataProvider = MainWindow.Instance.Trader,
            Portfolios = new PortfolioDataSource(MainWindow.Instance.Trader),
        };
    
        if (newOrder.ShowModal(this))
            MainWindow.Instance.Trader.RegisterOrder(newOrder.Order);
    }