Intro Exercises to Optibook

These exercises are designed to familiarize yourself with Optibook, and how to use it. They will walk you through the different "interactions" that are possible with Optibook. You should have successfully completed the setup instructions before you tackle these problems.

Important: All problems are solvable using the manual.ipynb notebook, so make sure to have that open. For your reference, the Python client package source code can be viewed in ~/environment/optibook_client. For example, to view the available fields in the different objects that the exchange client returns, you can look at ~/environment/optibook_client/common_types.py. If you are lazy, can also looks at the docs.

Problem Set A

Problem 1

Buy 10 lots of the instrument of your choice at any current price, and use the Optibook client to double check your position. You should have +10 lots.

Problem 2

Wait a minute or two, and then sell 10 lots of the same instrument at any current price, and use the Optibook client to double check your position. You should now have 0 lots. Go ahead and play around with inserting orders at different prices into the order book. Insert an order and see what happens in the visualizer! Try to make a few trades.

Only move on to problem 3 once you are comfortable with the idea of buying and selling. Also make sure your position is 0 when you move on to problem 3, it will make the problem a lot easier.

Problem 3

Get the trade info of the trades you made, and calculate the "PnL" (Profit & Loss) of your trades. If you are unsure how to do this, reason through this on paper first, what price did you buy your instrument at, and how many lots did you buy? At what price did you sell it again, and how many lots did you sell? Did you make any money? Now turn this into code.

Use the e.get_pnl() function to verify that your calculation is correct.

Problem 4

Insert 10 orders to sell the instrument of your choice at a random number higher than 100,000. This order should not trade, as the price is way too high. Now, check your outstanding orders, do you see it? Next, amend your order to only trade 5 lots, and check your outstanding orders again, do you see the change?

Finally, cancel all outstanding orders, and check again to make sure they have been cancelled correctly.

Problem 5

Look at all public trades that have happened since the exchange has started, for all users, and print them!

Problem Set B

Problem 6

Get the current order book using e.get_last_price_book(instrument_id) and write a function to print it with descending prices, in the "common" order book representation. This would be an example:

bid | price | ask
    |  100  | 2
    |  99   | 1
 1  |  98   | 

To print, simply do something like this (this is purely meant to give you some inspiration, your solution does not have to look like this):

print("bid | price | ask")

for level in processed_order_book:
  print(f"{level.bid_volume} | {level.price_level} | {level.ask_volume}")

Problem 7

Write a generic PnL function that calculates your current PnL. This is similar to Problem 3, but you will need to take a few more things into account:

  1. What happens if you have an outstanding position? You need to value it against the current market price. Use the last traded price as a valuation, in order to get the same result as our get_pnl() function. But be aware that you can also value a position differently (e.g. mid-market rate, vwap, etc.)

  2. What happens if you have a short position?

  3. As you enter trades, you will accumulate "cash", you can see this in the visualizer as well, next to "Pnl" and "Position". This is the amount of money you spent, or received upon entering a trade. For example, say you buy 100 lots at 11 dollars each. Then you will have a position of 100, and a cash amount of (100 * -11) = -1,100. The 11 is negative, because you are paying money. You will have spent 1,100 dollars to aquire 100 lots. When you short-sell, it is the other way around! You will have negative positions, but a positive cash, as the counterparty pays you. So say you sell 10 lots at 11 dollars, then your position will be -10, but you will have received (100 * 11) = 1,100 cash in return. Depending on how you build your PnL function, you might have to take this into account.

Double check that your PnL is the same as the PnL returned to you from the get_pnl() function.

Problem 8

By now you should have a pretty good idea of how you can interact with Optibook. Try to think of how you can combine all this information into an "automated" trading algorithm. The goal is not to make profit here, but rather to come up with a structured approach of how you might trade in an automated way, regardless of the strategy employed.

Write some pseudo-code as well as a paragraph or two explaining your thought process, and why you are doing what you are doing. Also try to think of some risks involved, and what you can do to avoid going bankrupt.

Keep in mind that the exchange is dynamic and things change constantly. Your algorithm is highly dependent on that of others. Here are some concrete scenarios to take into account:

  • What if you shoot for an order but it does not execute?

  • What if you breach a limit?

  • What if you are doing more trades than you anticipated?

  • What if you suddenly get disconnected?

  • What if the exchange sends you an error message?

  • What if your algorithm is much less profitable than you expect it to be?

  • What if your algorithm is much more profitable than you expect it to be?

A word of advice

Don't build all functionality in one go. Nobody get's it right the first time. Start by analyzing data, and don't trade right away. Instead of sending an order, print a message to the command line first. In fact, print print and print more! Print is your best friend and will help you understand what is going on. You should always strive to fully understand every detail of what your algorithm is doing, that is the most important bit by far!

When you do start trading, start with small volumes (e.g. 1 lot) until you are sure the algorithm works. It goes without saying: test your code.

Contact optibook@optiver.com for any questions