Introduction to Algorithmic Trading with Delta REST Client

Introduction to Algorithmic Trading with Delta REST Client

Are You Ready to Automate Your Trading?

Welcome to the exciting world of algorithmic trading! If you’re interested in leveraging technology to enhance your trading strategies, this blog is your starting point. We’ll explore how you can use Python and the Delta REST Client to automate your trades on Delta Exchange.

By the end of this read, you will be able to

  1. Code your own strategies and place orders on your delta exchange account through API
  2. Get a Python script to place a limit order at the mark price of your desired option contract and move the bid automatically as the mark price of the contract changes
  3. Delta Rest Client Documentation

Setting the Stage: Python and API Trading

Are You Familiar with Python?

Python is a versatile programming language favoured for its simplicity and readability, making it perfect for algorithmic trading. If you’re not familiar with Python, it’s recommended that you start with a basic Python setup guide. Python’s extensive libraries and community support make it ideal for developing trading algorithms.

Understanding API Trading

API trading connects your trading decisions to the digital market through programmable interfaces. By generating an API key on Delta Exchange, you integrate your Python scripts with your trading account, enabling automated, real-time decision-making.

Creating an API Key

If you haven’t created an API key before, you can follow the steps from this blog:
https://www.delta.exchange/blog/how-to-create-an-api-key-on-delta-exchange?category=research

Getting Started with Delta REST Client

Installation

To begin, install the Delta REST Client using the following command:

pip install delta-rest-client

Implementing a Simple Trading Strategy

Automating Limit Orders

Let’s implement a basic strategy: We will place a limit order at the mark price of an option contract and continuously update it to match the current mark price. Here’s a simple outline to get you started:

  1. Initialize the Delta REST Client.
  2. Fetch the current mark price of your chosen contract.
  3. Place a limit order at that price.
  4. Use a loop to constantly check and update the order price to the current mark price.
  5. Break when the order is filled.

This strategy exemplifies how you can use Python to make dynamic trading decisions, adapting to real-time market changes.

You can copy the Python script from here


from delta_rest_client import DeltaRestClient
import pandas as pd
import time

delta_client = DeltaRestClient(
  base_url='https://api.delta.exchange',
  api_key='your_api_key',
  api_secret='your_api_secret'
)
symbol = 'P-BTC-46400-110124'
ticker = delta_client.get_ticker(symbol)
mark_price = ticker['mark_price']
product_id = ticker['product_id']
print(mark_price)
qty = 1
while True:
    # Place Limit Order at Mark Price
    order_response = delta_client.place_order(
        product_id=product_id,
        size=qty,
        side='buy',
        limit_price=mark_price
        )
    print(order_response)
    print('Order Placed for 0.001 BTC at Mark Price of ', mark_price,' Contract Symbol: ', symbol)
    break
    time.sleep(5)
    # check if the order is unfilled
    open_orders = delta_client.get_live_orders(product_id=product_id)
    if len(open_orders) > 0:
        for order in open_orders:
            if order['id'] == order_response['id']:
                print('Order is still open')
                qty = order['unfilled_size']
                # Cancel the Order and wait for loop to replace it with a new order
                delta_client.cancel_order(
                    order_id=order_response['id'],
                    product_id=product_id,
                )
                break
    else:
        # Order is filled
        print('Order is filled')
        break
    

To have your questions about Algo Trading answered, you can schedule a call with us by clicking here