-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcall_price.py
120 lines (108 loc) · 3.49 KB
/
call_price.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
import requests
import os
import math
import time
from place_order import place_sell_order, modify_sell_order
expiry = '24APR'
usdinr_lotsize = 1000
def sell_call_price(sell_strike):
quotes_price_url = "https://api.upstox.com/v2/market-quote/quotes?instrument_key=" + sell_strike[
'instrument_key']
payload = {}
headers = {
'Accept': 'application/json',
'Authorization': 'Bearer ' + os.environ['ACCESS_TOKEN']
}
response = requests.request("GET",
quotes_price_url,
headers=headers,
data=payload)
response_data = response.json()
sell_strike_price = sell_strike['strike_price']
frac = math.modf(sell_strike_price)
if frac[0] == 0.0:
sell_strike_price = math.trunc(sell_strike_price)
instrument_name = sell_strike['segment'] \
+ ':' + sell_strike['underlying_symbol'] \
+ expiry + str(sell_strike_price) \
+sell_strike['instrument_type']
sell_prices=response_data\
.get('data')\
.get(instrument_name)\
.get('depth')\
.get('sell')
sell_price = sell_prices[0]['price'] * usdinr_lotsize
# TODO: smart pricing
order_id = None
for i, price in reversed(list(enumerate(sell_prices))):
# Place order
if i == (len(sell_prices) - 1):
order_placed = place_sell_order(price['price'], sell_strike)
order_id = order_placed.json().get('data').get('order_id')
# Modify order
else:
order_status_api = "https://api.upstox.com/v2/order/details?order_id=" + str(
order_id)
response = requests.request("GET", order_status_api, headers=headers)
pending_quantity = response.json().get('data').get('pending_quantity')
if pending_quantity > 0:
order_modified = modify_sell_order(price['price'], order_id)
print(order_modified.json())
order_id = order_modified.json().get('data').get('order_id')
time.sleep(5)
# check for spread width
ask = response_data\
.get('data')\
.get(instrument_name)\
.get('depth')\
.get('sell')[0]['price']
bid = response_data\
.get('data')\
.get(instrument_name)\
.get('depth')\
.get('buy')[0]['price']
spread = ask - bid
illiquid = False
if spread > 0.25:
illiquid = True
return sell_price, sell_strike_price, illiquid
def buy_call_price(buy_strike):
quotes_price_url = "https://api.upstox.com/v2/market-quote/quotes?instrument_key=" + buy_strike[
'instrument_key']
payload = {}
headers = {
'Accept': 'application/json',
'Authorization': 'Bearer ' + os.environ['ACCESS_TOKEN']
}
response = requests.request("GET",
quotes_price_url,
headers=headers,
data=payload)
response_data = response.json()
buy_strike_price = buy_strike['strike_price']
frac = math.modf(buy_strike_price)
if frac[0] == 0.0:
buy_strike_price = math.trunc(buy_strike_price)
instrument_name = buy_strike['segment'] \
+ ':' + buy_strike['underlying_symbol'] \
+ expiry + str(buy_strike_price) \
+buy_strike['instrument_type']
buy_prices=response_data\
.get('data')\
.get(instrument_name)\
.get('depth')\
.get('sell')
buy_price = buy_prices[0]['price'] * usdinr_lotsize
# TODO: calculate bid ask spread
ask = response_data\
.get('data')\
.get(instrument_name)\
.get('depth')\
.get('sell')[0]['price']
bid = response_data\
.get('data')\
.get(instrument_name)\
.get('depth')\
.get('buy')[0]['price']
spread = ask - bid
return buy_price, buy_strike_price