.. note::
    :class: sphx-glr-download-link-note

    Click :ref:`here <sphx_glr_download_generated_sphinx_gallery_run-01-subscribe-to-trading-channel.py>` to download the full example code
.. rst-class:: sphx-glr-example-title

.. _sphx_glr_generated_sphinx_gallery_run-01-subscribe-to-trading-channel.py:


============================
Subscribe to Trading Channel
============================

.. contents:: Table of Contents
    :local:
    :depth: 1

Initialise client
=================
By default all incoming messages are being logged at INFO level without
any modifications. So you would want to set logging accordingly in case
you need to visually inspect results.


.. code-block:: default

    import time
    import logging
    from pprint import pprint

    from bcx.client import BlockchainWebsocketClient

    logging.basicConfig(level=logging.INFO)

    client = BlockchainWebsocketClient()









Subscribe for trading
=====================
Actual trading and accessing balance of your account `requires authentication
<https://exchange.blockchain.com/api/#authenticated-channels>`_ with an API key.
All you need to do is to make sure that ``BLOCKCHAIN_API_SECRET`` environment
variable is present at script run time.

.. code-block:: shell

  export BLOCKCHAIN_API_SECRET="__ENTER_YOUR_API_SECRET_HERE__"

The client will handle the rest of the authentication process


.. code-block:: default

    client.subscribe_to_balances()
    time.sleep(2)

    client.subscribe_to_trading()
    time.sleep(2)

    pprint(client.connected_channels)
    time.sleep(2)






.. rst-class:: sphx-glr-script-out

 Out:

 .. code-block:: none

    [AuthChannel(is_subscribed=True),
     TradingChannel(is_subscribed=True),
     BalancesChannel(is_subscribed=True)]




Create market order
===================
Market order is an order that will match at any price available in the
market, starting from the best prices and filling up to the available
balance.

.. important ::
  At the moment, there is no logic in place which handles responses from
  the server when you create an order


.. code-block:: default

    time.sleep(2)
    client.create_market_order(
        order_id="my-order",
        symbol="BTC-USD",
        time_in_force="GTC",
        side="sell",
        quantity=0.000000000001,
    )









Create limit order
===================
Limit order is an order that has a price limit.

.. important ::
  At the moment, there is no logic in place which handles responses from
  the server when you create an order


.. code-block:: default

    time.sleep(2)
    client.create_limit_order(
        order_id="my-order",
        price=100000000000000.0,
        symbol="BTC-USD",
        time_in_force="GTC",
        side="sell",
        quantity=0.000000000001,
    )









Cancel all orders
=================
The client stores all necessary information about placed orders. This can
be used to track order execution status or cancel all of the in one go


.. code-block:: default

    time.sleep(2)
    client.cancel_all_orders()








.. rst-class:: sphx-glr-timing

   **Total running time of the script:** ( 0 minutes  12.632 seconds)


.. _sphx_glr_download_generated_sphinx_gallery_run-01-subscribe-to-trading-channel.py:


.. only :: html

 .. container:: sphx-glr-footer
    :class: sphx-glr-footer-example



  .. container:: sphx-glr-download

     :download:`Download Python source code: run-01-subscribe-to-trading-channel.py <run-01-subscribe-to-trading-channel.py>`



  .. container:: sphx-glr-download

     :download:`Download Jupyter notebook: run-01-subscribe-to-trading-channel.ipynb <run-01-subscribe-to-trading-channel.ipynb>`


.. only:: html

 .. rst-class:: sphx-glr-signature

    `Gallery generated by Sphinx-Gallery <https://sphinx-gallery.github.io>`_
