Click or drag to resize

Getting started

The SampleBitStamp application shows an example of working with the BitStampTrader as shown in the figure below. The example source code is in the Samples/BitStamp folder of the installation package.

Bit Stamp Sample
  1. First, you need to create an instance of the BitStampTrader:

    C#
    Trader = new BitStampTrader();// { LogLevel = LogLevels.Debug };
    .....
    Trader.Key = Key.Text;
    Trader.Secret = Secret.Password;
  2. Next, you should subscribe on required IConnector events:

    C#
    // subscribe on successfully connect event
    Trader.Connected += () =>
    {
        // set flag (connection is established)
        _isConnected = true;
    
        // update gui labes
        this.GuiAsync(() => ChangeConnectStatus(true));
    };
    
    // subscribe on connection error event
    Trader.ConnectionError += error => this.GuiAsync(() =>
    {
        // update gui labes
        ChangeConnectStatus(false);
    
        MessageBox.Show(this, error.ToString(), "Connection was dropped.");    
    });
    
    // subscribe on successfully disconnect event
    Trader.Disconnected += () => this.GuiAsync(() => ChangeConnectStatus(false));
    
    // subscribe on error event
    Trader.Error += error => this.GuiAsync(() => MessageBox.Show(this, error.ToString(), "Data processing error"));
    
    // new securities received
    Trader.NewSecurity += security => _securitiesWindow.SecurityPicker.Securities.Add(security);
    
    // new own trades received
    Trader.NewMyTrade += trade => _myTradesWindow.TradeGrid.Trades.Add(trade);
    
    // new anonymous trades received
    Trader.NewTrade += trade => _tradesWindow.TradeGrid.Trades.Add(trade);
    
    // new orders received
    Trader.NewOrder += order => _ordersWindow.OrderGrid.Orders.Add(order);
    
    // new portofolios (=accounts) received
    Trader.NewPortfolio += portfolio =>
    {
        // subscribe on portfolio updates
        Trader.RegisterPortfolio(portfolio);
    
        _portfoliosWindow.PortfolioGrid.Portfolios.Add(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;
  3. Finally, you need to connect to BitStamp server:

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

  5. Instruments search (in more detail):

    Securities Lookup Window
  6. For updates on the selected instrument and ticks you need to use the RegisterSecurity(Security, NullableDateTimeOffset, NullableDateTimeOffset, NullableInt64, MarketDataBuildModes, NullableMarketDataTypes) and RegisterTrades(Security, NullableDateTimeOffset, NullableDateTimeOffset, NullableInt64, MarketDataBuildModes, NullableMarketDataTypes) methods, respectively:

    C#
    private void QuotesClick(object sender, RoutedEventArgs e)
    {
        var trader = MainWindow.Instance.Trader;
    
        foreach (var security in SecurityPicker.SelectedSecurities)
        {
            if (trader.RegisteredSecurities.Contains(security))
            {
                trader.UnRegisterSecurity(security);
                trader.UnRegisterTrades(security);
            }
            else
            {
                trader.RegisterSecurity(security);
                trader.RegisterTrades(security);
            }
        }
    }
  7. To request updates on the order books you need to use the RegisterMarketDepth(Security, NullableDateTimeOffset, NullableDateTimeOffset, NullableInt64, MarketDataBuildModes, NullableMarketDataTypes, NullableInt32) method:

    C#
    private void DepthClick(object sender, RoutedEventArgs e)
    {
        var trader = MainWindow.Instance.Trader;
    
        foreach (var security in SecurityPicker.SelectedSecurities)
        {
            var window = _quotesWindows.SafeAdd(security, s =>
            {
                trader.RegisterMarketDepth(security);
                var wnd = new QuotesWindow
                {
                    Title = security.Id + " " + LocalizedStrings.MarketDepth
                };
                wnd.MakeHideable();
                return wnd;
            });
    
            if (window.Visibility == Visibility.Visible)
                window.Hide();
            else
                window.Show();
    
            if (!_initialized)
            {
                TraderOnMarketDepthChanged(trader.GetMarketDepth(security));
                trader.MarketDepthChanged += TraderOnMarketDepthChanged;
                _initialized = true;
            }
        }
    }
  8. 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);
    }
  9. To withdraw funds in code need to create a conditional order, and fill necessary requisites:

    C#
    var order = new Order
    {
        Type = OrderTypes.Conditional,
        Condition = new BitStampOrderCondition
        {
            IsWithdraw = true,
            WithdrawInfo = new WithdrawInfo
            {
                // fill necessary requisites
                Comment = "My profit",
                //PaymentId = "45467dyjyttR8WBiTJXptyuTx4wbSerGZ5t45", //Riple
                CryptoAddress = "16zK3M53JBGnjs9ajTBGBfkRqoHtm4E573",
            }
        },
        Security = new Security() { Code = "BTC" },
    };
    
    Trader.RegisterOrder(order);