-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathusecase_02_range.py
47 lines (39 loc) · 1.44 KB
/
usecase_02_range.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# USECASE [02] Iterating through a range of prices to plot maker pnl
from exchange_single_maker import ExchangeSingleMaker
from makers.maker_zero_knowledge import MakerZeroKnowledge
from utils import compute_maker_pnls
import pandas as pd
import numpy as np
import plotly.graph_objects as go
px_init = 100
tick = 0.5
numBids = numOffers = int(40/tick)
size = 2 / (numBids+numOffers)
maker = MakerZeroKnowledge(initMidPrice=px_init, tickSize=tick, numBids=numBids, sizeBid=size, numOffers=numOffers, sizeOffer=size)
exchange = ExchangeSingleMaker(maker)
while True:
tx = exchange.buy_at_first_rank()
if not tx:
break
pnl_up = compute_maker_pnls(exchange.transactions)
# sell until px_min
maker = MakerZeroKnowledge(initMidPrice=px_init, tickSize=tick, numBids=numBids, sizeBid=size, numOffers=numOffers, sizeOffer=size)
exchange = ExchangeSingleMaker(maker)
while True:
tx = exchange.sell_at_first_rank()
if not tx:
break
pnl_down = compute_maker_pnls(exchange.transactions)
taker_pnls = pd.concat([pnl_down, pnl_up], axis=0).sort_values("price")
x = taker_pnls["price"]
y = taker_pnls["pnl"]
# simple projection on 2nd order polynomial
m = np.poly1d(np.polyfit(x, y, 2))(x)
fig = go.Figure()
fig.add_trace(go.Scatter(x=x, y=y,
mode='lines',
name='pnl'))
fig.add_trace(go.Scatter(x=x, y=m,
mode='markers',
name='poly2'))
fig.show()