LiquidityProvider class

The LiquidityProvider class is the simplest of all the others. The goal of this component is to generate liquidities. Since we randomly generate liquidities, we just need to test whether the first liquidity that is sent by the LiquidityProvider class is well formed. We will create the generate_random_order function, which will randomly pick a side, a price, a quantity, and an action associated to this order. We will have three kinds of actions: create a new order, amend an order, and cancel an order. Since we are going to create a full trading system, we will also want to test the full system by inserting the order manually. Hence, this LiquidityProvider component will have a way to insert manual orders into the system.

The following code describes the LiquidityProvider class. We will use a pseudo random generator initialized by a seed. When you run your code several times, a seed will allow you to make the random number deterministic.

The generate_random_order function uses the lookup_orders function to determine whether the next order that will be generated already exists:

  1. In the code, we will create the LiquidityProvider class. The goal of this class is to act as a liquidity provider or an exchange. It will send price updates to the trading system. It will use the lp_2_gateway channel to send the price updates:
from random import randrange
from random import sample, seed

class LiquidityProvider:
def __init__(self, lp_2_gateway=None):
self.orders = []
self.order_id = 0
seed(0)
self.lp_2_gateway = lp_2_gateway
  1. Here, we create a utility function to look up orders in the list of orders:
     def lookup_orders(self,id):
count=0
for o in self.orders:
if o['id'] == id:
return o, count
count+=1
return None, None
  1. The insert_manual_order function will insert orders manually into the trading system. As shown, this function will be used for unit testing some components:
     def insert_manual_order(self,order):
if self.lp_2_gateway is None:
print('simulation mode')
return order
self.lp_2_gateway.append(order.copy())

The generate_random_order function will generate orders randomly. There will be three types of orders:

  • New (we will create a new order ID)
  • Modify (we will use the order ID of an order that was created and we will change the quantity)
  • Delete (we will use the order ID and we will delete the order)
  1. Each time we create a new order, we will need to increment the order ID. We will use thelookup_orders function as shown in the following code to check whether the order has already been created:
     def generate_random_order(self):
price=randrange(8,12)
quantity=randrange(1,10)*100
side=sample(['buy','sell'],1)[0]
order_id=randrange(0,self.order_id+1)
o=self.lookup_orders(order_id)

new_order=False
if
o is None:
action='new'
new_order=True
else
:
action=sample(['modify','delete'],1)[0]

ord = {
'id': self.order_id,
'price': price,
'quantity': quantity,
'side': side,
'action': action
}

if not new_order:
self.order_id+=1
self.orders.append(ord)

if not self.lp_2_gateway:
print('simulation mode')
return ord
self.lp_2_gateway.append(ord.copy())
  1. We test whether the LiquidityProvider class works correctly by using unit testing. Python has the unittest module . As shown, we will create the TestMarketSimulator class, inheriting from TestCase:
import unittest
from chapter7.LiquidityProvider import LiquidityProvider


class TestMarketSimulator(unittest.TestCase):
def setUp(self):
self.liquidity_provider = LiquidityProvider()

def test_add_liquidity(self):
self.liquidity_provider.generate_random_order()
self.assertEqual(self.liquidity_provider.orders[0]['id'],0)
self.assertEqual(self.liquidity_provider.orders[0]['side'], 'buy')
self.assertEqual(self.liquidity_provider.orders[0]['quantity'], 700)
self.assertEqual(self.liquidity_provider.orders[0]['price'], 11) OrderBook class

As shown, we have coded the test_add_liquidity function:

  • This function tests whether the random generation of a liquidity functions by comparing values generated by this function to expected values.
  • We used the functions belonging to this TestCase class to make a test fail if the returned values are not the expected ones.
  • This code will generate an order and test the order characteristics. If one field value is not the expected one, the unit test will fail.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.117.107.90