How To Create Your Own Python HFT Trading Bot

How To Create Your Own Python HFT Trading Bot
Photo by Pierre Borthiry - Peiobty / Unsplash

If you want to create your own high-frequency trading bot, here is a simple guide using Python. You will need a few libraries to get started.

The first is the Python Bloomberg API. You can find the installation instructions here.

The second is the Python Alpaca API. You can find the installation instructions here.

Once you have installed these libraries, you will need to create a file called bot.py. The code for the bot will go in this file.

The first thing you need to do is import the libraries you installed.

import blpapi import alpaca_trade_api

Next, you need to create a Bloomberg session. This will allow you to connect to the Bloomberg Terminal.

session = blpapi.Session() session.start()

Now you need to create an Alpaca API key. This will allow you to connect to the Alpaca brokerage.

APCA_API_KEY_ID = "your key id here" APCA_API_SECRET_KEY = "your secret key here" api = alpaca_trade_api.REST( APCA_API_KEY_ID, APCA_API_SECRET_KEY, api_version="v2" )

You will also need to create a list of tickers that you want your bot to trade. For this example, we will use the tickers for the SPDR S&P 500 ETF (SPY) and the iShares Russell 2000 ETF (IWM).

tickers = [ "SPY", "IWM" ]

Now we need to write the code for the bot. The bot will do the following:

  1. Query the Bloomberg Terminal for the last price of each ticker in the list.
  2. If the last price of SPY is greater than the last price of IWM, the bot will buy SPY and sell IWM.
  3. If the last price of SPY is less than the last price of IWM, the bot will sell SPY and buy IWM.
  4. The bot will repeat these steps every minute.

To do this, we will use the “get_last_price” function from the Alpaca API. This function takes a ticker symbol and returns the last trade price.

def get_last_price(ticker): return api.get_last_trade(ticker)['price']

Next, we need to write a function to get the last prices of all the tickers in our list. We will use the “map” function to apply the “get_last_price” function to each ticker in the list.

def get_last_prices(tickers): return map(get_last_price, tickers)

Now we need to write a function to compare the last prices of SPY and IWM. If the last price of SPY is greater than the last price of IWM, the function will return “SPY”. If the last price of SPY is less than the last price of IWM, the function will return “IWM”.

def get_ticker_to_buy(spy_price, iwm_price): if spy_price > iwm_price: return "SPY" else: return "IWM"

Finally, we need to write a function to place the trade. This function will take a ticker symbol and an order type (“buy” or “sell”). It will then place an order for that ticker using the Alpaca API.

def place_order(ticker, order_type): try: api.submit_order( symbol=ticker, side=order_type, type="market", quantity=100 ) except: print("Error placing order for {}".format(ticker))

Now we can put all of these functions together to create the bot.

def main(): while True: # Get the last prices of SPY and IWM spy_price = get_last_price("SPY") iwm_price = get_last_price("IWM") # Get the ticker to buy ticker_to_buy = get_ticker_to_buy(spy_price, iwm_price) # Place the trade place_order(ticker_to_buy, "buy") # Sleep for one minute time.sleep(60) if **name** == "**main**": main()

That’s it! You now have a working high-frequency trading bot.

Subscribe to The Poor Coder | Algorithm Solutions

Don’t miss out on the latest issues. Sign up now to get access to the library of members-only issues.
[email protected]
Subscribe