Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DEV-13427 Allocation Service Examples #675

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2,005 changes: 2,005 additions & 0 deletions examples/use-cases/allocation-service/AllocationService-1.ipynb

Large diffs are not rendered by default.

2,065 changes: 2,065 additions & 0 deletions examples/use-cases/allocation-service/AllocationService-2.ipynb

Large diffs are not rendered by default.

2,006 changes: 2,006 additions & 0 deletions examples/use-cases/allocation-service/AllocationService-3.ipynb

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions examples/use-cases/allocation-service/data/1/blocks.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
block_id,order_ids,instrument_name,client_internal,isin,figi,quantity,side,type,time_in_force,created_date
BLOCK001,"ORD001,ORD002",Amazon_Nasdaq_AMZN,imd_346345343,US0231351067,BBG000BVPXP1,20,buy,limit,GTC,2023-04-13T11:15:00+00:00
3 changes: 3 additions & 0 deletions examples/use-cases/allocation-service/data/1/executions.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
execution_id,placement_id,instrument_name,client_internal,isin,figi,quantity,state,side,type,price,currency,settlement_currency,settlement_currency_fx_rate,counterparty,created_date
EXEC001,PLC001,Amazon_Nasdaq_AMZN,imd_346345343,US0231351067,BBG000BVPXP1,5,some_state,buy,limit,1550,USD,USD,1.0,some_counterparty,2023-04-13T11:15:00+00:00
EXEC002,PLC001,Amazon_Nasdaq_AMZN,imd_346345343,US0231351067,BBG000BVPXP1,5,some_state,buy,limit,1560,USD,USD,1.0,some_counterparty,2023-04-13T11:20:00+00:00
2 changes: 2 additions & 0 deletions examples/use-cases/allocation-service/data/1/instruments.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
instrument_name,client_internal,currency,isin,figi,ticker
Amazon_Nasdaq_AMZN,imd_34634534,USD,US0231351067,BBG000BVPXP1,AMZN
3 changes: 3 additions & 0 deletions examples/use-cases/allocation-service/data/1/orders.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
portfolio,instrument_name,client_internal,isin,figi,quantity,price,currency,order_id,side,type,state,date
PORT001,Amazon_Nasdaq_AMZN,imd_346345343,US0231351067,BBG000BVPXP1,10,1550,USD,ORD001,buy,limit,new,2023-04-13T11:10:00+00:00
PORT002,Amazon_Nasdaq_AMZN,imd_346345343,US0231351067,BBG000BVPXP1,10,1550,USD,ORD002,buy,limit,new,2023-04-13T11:15:00+00:00
2 changes: 2 additions & 0 deletions examples/use-cases/allocation-service/data/1/placements.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
placement_id,block_id,instrument_name,client_internal,isin,figi,quantity,state,side,type,time_in_force,created_date
PLC001,BLOCK001,Amazon_Nasdaq_AMZN,imd_346345343,US0231351067,BBG000BVPXP1,20,some_state,buy,limit,GTC,2023-04-13T11:15:00+00:00
3 changes: 3 additions & 0 deletions examples/use-cases/allocation-service/data/1/portfolios.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
portfolio_name,portfolio_code,currency,created_date
Allocation Service - Example 1 - Portfolio A,PORT001,USD,2023-04-10T12:00:00+00:00
Allocation Service - Example 2 - Portfolio B,PORT002,USD,2023-04-10T12:00:00+00:00
3 changes: 3 additions & 0 deletions examples/use-cases/allocation-service/data/2/allocations.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
allocation_id,instrument_name,client_internal,isin,figi,quantity,price,currency,portfolio_id,order_id,execution_ids,placement_ids,date
ALLOC001,Amazon_Nasdaq_AMZN,imd_346345343,US0231351067,BBG000BVPXP1,2,1560,USD,PORT001,ORD001,EXEC002,PLC001,2023-04-11T11:20:00+00:00
ALLOC002,Amazon_Nasdaq_AMZN,imd_346345343,US0231351067,BBG000BVPXP1,3,1560,USD,PORT002,ORD002,EXEC002,PLC001,2023-04-11T11:20:00+00:00
2 changes: 2 additions & 0 deletions examples/use-cases/allocation-service/data/2/blocks.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
block_id,order_ids,instrument_name,client_internal,isin,figi,quantity,side,type,time_in_force,created_date
BLOCK001,"ORD001,ORD002",Amazon_Nasdaq_AMZN,imd_346345343,US0231351067,BBG000BVPXP1,20,buy,limit,GTC,2023-04-11T11:15:00+00:00
3 changes: 3 additions & 0 deletions examples/use-cases/allocation-service/data/2/executions.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
execution_id,placement_id,instrument_name,client_internal,isin,figi,quantity,state,side,type,price,currency,settlement_currency,settlement_currency_fx_rate,counterparty,created_date
EXEC001,PLC001,Amazon_Nasdaq_AMZN,imd_346345343,US0231351067,BBG000BVPXP1,5,some_state,buy,limit,1560,USD,USD,1.0,some_counterparty,2023-04-11T11:30:00+00:00
EXEC002,PLC001,Amazon_Nasdaq_AMZN,imd_346345343,US0231351067,BBG000BVPXP1,5,some_state,buy,limit,1550,USD,USD,1.0,some_counterparty,2023-04-12T11:30:00+00:00
2 changes: 2 additions & 0 deletions examples/use-cases/allocation-service/data/2/instruments.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
instrument_name,client_internal,currency,isin,figi,ticker
Amazon_Nasdaq_AMZN,imd_34634534,USD,US0231351067,BBG000BVPXP1,AMZN
3 changes: 3 additions & 0 deletions examples/use-cases/allocation-service/data/2/orders.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
portfolio,instrument_name,client_internal,isin,figi,quantity,price,currency,order_id,side,type,state,date
PORT001,Amazon_Nasdaq_AMZN,imd_346345343,US0231351067,BBG000BVPXP1,10,1550,USD,ORD001,buy,limit,new,2023-04-11T11:15:00+00:00
PORT002,Amazon_Nasdaq_AMZN,imd_346345343,US0231351067,BBG000BVPXP1,10,1550,USD,ORD002,buy,limit,new,2023-04-11T11:10:00+00:00
2 changes: 2 additions & 0 deletions examples/use-cases/allocation-service/data/2/placements.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
placement_id,block_id,instrument_name,client_internal,isin,figi,quantity,state,side,type,time_in_force,created_date
PLC001,BLOCK001,Amazon_Nasdaq_AMZN,imd_346345343,US0231351067,BBG000BVPXP1,20,some_state,buy,limit,GTC,2023-04-11T11:15:00+00:00
3 changes: 3 additions & 0 deletions examples/use-cases/allocation-service/data/2/portfolios.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
portfolio_name,portfolio_code,currency,created_date
Allocation Service - Example 2 - Portfolio A,PORT001,USD,2023-04-10T12:00:00+00:00
Allocation Service - Example 2 - Portfolio B,PORT002,USD,2023-04-10T12:00:00+00:00
2 changes: 2 additions & 0 deletions examples/use-cases/allocation-service/data/2/transactions.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
transaction_id,portfolio_id,type,instrument_name,client_internal,isin,figi,transaction_date,settlement_date,quantity,price,cost,currency,order_id,allocation_id
TXN001,PORT001,Buy,Amazon_Nasdaq_AMZN,imd_346345343,US0231351067,BBG000BVPXP1,2023-04-11T18:00:00+00:00,2023-04-13T18:00:00+00:00,2,1560,3120,USD,ORD001,ALLOC001
3 changes: 3 additions & 0 deletions examples/use-cases/allocation-service/data/3/allocations.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
allocation_id,instrument_name,client_internal,isin,figi,quantity,price,currency,portfolio_id,order_id,execution_ids,placement_ids,date
ALLOC001,Amazon_Nasdaq_AMZN,imd_346345343,US0231351067,BBG000BVPXP1,8,1550,USD,PORT001,ORD001,EXEC001,PLC001,2023-04-11T11:20:00+00:00
ALLOC002,Amazon_Nasdaq_AMZN,imd_346345343,US0231351067,BBG000BVPXP1,2,1550,USD,PORT002,ORD002,EXEC001,PLC001,2023-04-11T11:15:00+00:00
2 changes: 2 additions & 0 deletions examples/use-cases/allocation-service/data/3/blocks.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
block_id,order_ids,instrument_name,client_internal,isin,figi,quantity,side,type,time_in_force,created_date
BLOCK001,"ORD001,ORD002",Amazon_Nasdaq_AMZN,imd_346345343,US0231351067,BBG000BVPXP1,20,buy,limit,GTC,2023-04-11T11:15:00+00:00
3 changes: 3 additions & 0 deletions examples/use-cases/allocation-service/data/3/executions.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
execution_id,placement_id,instrument_name,client_internal,isin,figi,quantity,state,side,type,price,currency,settlement_currency,settlement_currency_fx_rate,counterparty,created_date
EXEC001,PLC001,Amazon_Nasdaq_AMZN,imd_346345343,US0231351067,BBG000BVPXP1,10,some_state,buy,limit,1550,USD,USD,1.0,some_counterparty,2023-04-12T11:30:00+00:00
EXEC002,PLC002,Amazon_Nasdaq_AMZN,imd_346345343,US0231351067,BBG000BVPXP1,8,some_state,buy,limit,1560,USD,USD,1.0,some_counterparty,2023-04-12T11:45:00+00:00
2 changes: 2 additions & 0 deletions examples/use-cases/allocation-service/data/3/instruments.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
instrument_name,client_internal,currency,isin,figi,ticker
Amazon_Nasdaq_AMZN,imd_34634534,USD,US0231351067,BBG000BVPXP1,AMZN
3 changes: 3 additions & 0 deletions examples/use-cases/allocation-service/data/3/orders.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
portfolio,instrument_name,client_internal,isin,figi,quantity,price,currency,order_id,side,type,state,date
PORT001,Amazon_Nasdaq_AMZN,imd_346345343,US0231351067,BBG000BVPXP1,10,1550,USD,ORD001,buy,limit,new,2023-04-11T11:10:00+00:00
PORT002,Amazon_Nasdaq_AMZN,imd_346345343,US0231351067,BBG000BVPXP1,10,1550,USD,ORD002,buy,limit,new,2023-04-11T11:15:00+00:00
3 changes: 3 additions & 0 deletions examples/use-cases/allocation-service/data/3/placements.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
placement_id,block_id,instrument_name,client_internal,isin,figi,quantity,state,side,type,time_in_force,created_date
PLC001,BLOCK001,Amazon_Nasdaq_AMZN,imd_346345343,US0231351067,BBG000BVPXP1,10,some_state,buy,limit,GTC,2023-04-11T11:15:00+00:00
PLC002,BLOCK001,Amazon_Nasdaq_AMZN,imd_346345343,US0231351067,BBG000BVPXP1,10,some_state,buy,limit,GTC,2023-04-11T11:15:00+00:00
3 changes: 3 additions & 0 deletions examples/use-cases/allocation-service/data/3/portfolios.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
portfolio_name,portfolio_code,currency,created_date
Allocation Service - Example 2 - Portfolio A,PORT001,USD,2023-04-10T12:00:00+00:00
Allocation Service - Example 2 - Portfolio B,PORT002,USD,2023-04-10T12:00:00+00:00
191 changes: 191 additions & 0 deletions examples/use-cases/allocation-service/helpers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,191 @@
import datetime
from collections import defaultdict

from lusid import models


class AllocationServiceHelpers:
@staticmethod
def create_order_set_request_from_df(df, scope):
order_requests = list()

for index, order in df.iterrows():
order_requests.append(
models.OrderRequest(
id=models.ResourceId(
scope=scope,
code=order['order_id']
),
quantity=order['quantity'],
side=order['side'],
instrument_identifiers={
'Instrument/default/Figi': order['figi']
},
properties={},
portfolio_id=models.ResourceId(
scope=scope,
code=order['portfolio']
),
state=order['state'],
type=order['type'],
date=datetime.datetime.fromisoformat(order['date']),
price=models.CurrencyAndAmount(amount=order['quantity'], currency=order['currency'])
)
)

return models.OrderSetRequest(order_requests=order_requests)

@staticmethod
def create_block_set_request_from_df(df, scope):
block_requests = list()

for index, block in df.iterrows():
order_ids = list()
for x in block['order_ids'].split(","):
order_ids.append(models.ResourceId(scope=scope, code=x))

block_requests.append(
models.BlockRequest(
id=models.ResourceId(
scope=scope,
code=block['block_id']
),
order_ids=order_ids,
instrument_identifiers={
'Instrument/default/Figi': block['figi']
},
quantity=block['quantity'],
side=block['side'],
type=block['type'],
time_in_force=block['time_in_force'],
created_date=datetime.datetime.fromisoformat(block['created_date'])
)
)

return models.BlockSetRequest(requests=block_requests)

@staticmethod
def create_placement_set_request_from_df(df, scope):
placement_requests = list()

for index, placement in df.iterrows():
placement_requests.append(
models.PlacementRequest(
id=models.ResourceId(
scope=scope,
code=placement['placement_id']
),
block_ids=[models.ResourceId(scope=scope, code=placement['block_id'])],
instrument_identifiers={
'Instrument/default/Figi': placement['figi']
},
quantity=placement['quantity'],
state=placement['state'],
side=placement['side'],
type=placement['type'],
time_in_force=placement['time_in_force'],
created_date=datetime.datetime.fromisoformat(placement['created_date'])
)
)

return models.PlacementSetRequest(requests=placement_requests)

@staticmethod
def create_execution_set_request_from_df(df, scope):
execution_requests = list()

for index, execution in df.iterrows():
execution_requests.append(
models.ExecutionRequest(
id=models.ResourceId(
scope=scope,
code=execution['execution_id']
),
placement_id=models.ResourceId(
scope=scope,
code=execution['placement_id']
),
instrument_identifiers={
'Instrument/default/Figi': execution['figi']
},
quantity=execution['quantity'],
state=execution['state'],
side=execution['side'],
type=execution['type'],
price=models.CurrencyAndAmount(amount=execution['price'], currency=execution['currency']),
settlement_currency=execution['settlement_currency'],
settlement_currency_fx_rate=execution['settlement_currency_fx_rate'],
counterparty=execution['counterparty'],
created_date=datetime.datetime.fromisoformat(execution['created_date'])
)
)

return models.ExecutionSetRequest(requests=execution_requests)

@staticmethod
def create_allocation_set_request_from_df(df, scope):
allocation_requests = list()

for index, allocation in df.iterrows():
execution_ids = list()
placement_ids = list()
for x in allocation['execution_ids'].split(","):
execution_ids.append(models.ResourceId(scope=scope, code=x))
for x in allocation['placement_ids'].split(","):
placement_ids.append(models.ResourceId(scope=scope, code=x))

allocation_requests.append(
models.AllocationRequest(
instrument_identifiers={
'Instrument/default/Figi': allocation['figi']
},
quantity=allocation['quantity'],
portfolio_id=models.ResourceId(
scope=scope,
code=allocation['portfolio_id']
),
allocated_order_id=models.ResourceId(
scope=scope,
code=allocation['order_id']
),
id=models.ResourceId(
scope=scope,
code=allocation['allocation_id']
),
price=models.CurrencyAndAmount(amount=allocation['price'], currency=allocation['currency']),
execution_ids=execution_ids,
placement_ids=placement_ids,
date=datetime.datetime.fromisoformat(allocation['date'])
)
)
return models.AllocationSetRequest(allocation_requests=allocation_requests)

@staticmethod
def create_transaction_request_dict_from_df(df, scope):
txn_requests = defaultdict(list)

for index, txn in df.iterrows():
txn_requests[txn['portfolio_id']].append(
models.TransactionRequest(
transaction_id=txn['transaction_id'],
type=txn['type'],
instrument_identifiers={
'Instrument/default/Figi': txn['figi']
},
transaction_date=txn['transaction_date'],
settlement_date=txn['settlement_date'],
units=txn['quantity'],
transaction_price=models.TransactionPrice(txn['price'], "Price"),
total_consideration=models.CurrencyAndAmount(txn['cost'], txn['currency']),
order_id=models.ResourceId(
scope=scope,
code=txn['order_id']
),
allocation_id=models.ResourceId(
scope=scope,
code=txn['allocation_id']
)
)
)

return txn_requests