-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.py
403 lines (320 loc) · 12.4 KB
/
util.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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
import json
import os
import threading
from datetime import datetime
import mysql.connector
from colorama import Fore, Back
from fyers_apiv3 import fyersModel
from playsound import playsound
from prettytable import PrettyTable
from common import get_object_from_list, get_comma_separated_symbols, write_log
from constants import *
fyers_copy = {}
connection = None
def load_properties():
f = open('property.json')
props = json.load(f)
f.close()
return props
def get_access_token(props):
write_log("\n")
access_token = ""
now = datetime.now()
d = now.strftime("%d-%m-%Y")
file_name = "access_token/access_token_" + d + ".txt"
if not os.path.exists(file_name):
session = fyersModel.SessionModel(client_id=props["app_id"], secret_key=props["secret_id"],
redirect_uri=props["redirect_url"],
response_type="code", grant_type="authorization_code")
response = session.generate_authcode()
write_log("Login url : " + response)
auth_code = input("Enter Auth Code : ")
session.set_token(auth_code)
access_token = session.generate_token()['access_token']
with open(file_name, "w") as f:
f.write(access_token)
else:
with open(file_name, "r") as f:
access_token = f.read()
write_log("Access Token : " + access_token)
return access_token
def get_available_balance(props, fyers, title):
data = fyers.funds()
write_log("Get Available Funds : Response : " + json.dumps(data))
obj = get_object_from_list(data["fund_limit"], "title", title)
return obj["equityAmount"]
def create_order_single(props, fyers, symbol, qty, type, side, limit_price, stop_price, stop_loss, take_profit,
strategy):
request = {
"symbol": symbol,
"qty": qty,
"type": type,
"side": side,
"productType": props["productType"],
"limitPrice": limit_price,
"stopPrice": stop_price,
"validity": props["validity"],
"disclosedQty": props["disclosedQty"],
"offlineOrder": props["offlineOrder"],
"stopLoss": stop_loss,
"takeProfit": take_profit
}
retry_count = 1
response = {}
while retry_count <= strategy["retry_count_create_order"]:
write_log("Create Order : Retry_count : " + str(retry_count) + " : Request : " + json.dumps(request))
response = fyers.place_order(request)
write_log("Create Order : Retry_count : " + str(retry_count) + " : Response : " + json.dumps(response))
verify_response("PLACE_ORDER", response)
retry_count = retry_count + 1
if response["s"] == SUCCESS:
print("Order successfully Placed...")
break
return response
def get_stocks_info(props, fyers, symbols):
comma_seperated_symbols = get_comma_separated_symbols(symbols)
request = {"symbols": comma_seperated_symbols}
write_log("Get Stock Info : Request : " + json.dumps(request))
response = fyers.quotes(request)
write_log("Get Stock Info : Response : " + json.dumps(response))
return response["d"]
def modify_order(props, fyers, order_id, limit_price, stop_price, qty, type):
request = {
"id": order_id
}
if limit_price != "NULL":
request["limitPrice"] = limit_price
if stop_price != "NULL":
request["stopPrice"] = stop_price
if qty != "NULL":
request["qty"] = qty
if type != "NULL":
request["type"] = type
response = {}
write_log("Modify order : Request : " + json.dumps(request))
response = fyers.modify_order(request)
write_log("Modify order : Response : " + json.dumps(response))
verify_response("STOP_LOSS_UPDATE", response)
def cancel_order(props, fyers, order_id):
request = {"id": 'order_id'}
response = {}
write_log("Cancel order : Request : " + json.dumps(request))
response = fyers.cancel_order(request)
write_log("Cancel order : Response : " + json.dumps(response))
verify_response("CANCEL_ORDER", response)
def exit_position(props, fyers, order_id):
request = {}
if order_id != "NULL":
request["id"] = order_id
write_log("Exit Position : Request : " + json.dumps(request))
response = fyers.exit_positions(request)
write_log("Exit Position : Response : " + json.dumps(response))
verify_response("EXIT_POSITION", response)
def verify_response(action_type, response):
audio = ""
if action_type == "PLACE_ORDER" and response["s"] == SUCCESS:
audio = ORDER_PLACED_SUCCESS
elif action_type == "PLACE_ORDER":
audio = ORDER_PLACED_FAILURE
if action_type == "ORDER_MODIFY" and response["s"] == SUCCESS:
audio = MODIFY_ORDER_SUCCESS
elif action_type == "ORDER_MODIFY":
audio = MODIFY_ORDER_FAILURE
if action_type == "STOP_LOSS_UPDATE" and response["s"] == SUCCESS:
audio = STOP_LOSS_UPDATED_SUCCESS
elif action_type == "STOP_LOSS_UPDATE":
audio = STOP_LOSS_UPDATED_FAILURE
if action_type == "CANCEL_ORDER" and response["s"] == SUCCESS:
audio = ORDER_CANCELLED_SUCCESS
elif action_type == "CANCEL_ORDER":
audio = ORDER_CANCELLED_FAILURE
if action_type == "EXIT_POSITION" and response["s"] == SUCCESS:
audio = ORDER_EXIT_SUCCESS
elif action_type == "EXIT_POSITION":
audio = ORDER_EXIT_FAILURE
# if audio != "":
# playsound(audio)
def get_connection():
# global connection
# if connection == None:
connection = mysql.connector.connect(host="10.0.0.85", user="stsuser", password="stsuser@6Dtech",
database="QUESTION_MANAGEMENT")
return connection
def get_trades(type, props, LTP_DICT):
connection = get_connection()
query = "SELECT * FROM TRADE_MASTER "
if type == "pending":
query += "where STATUS=1"
elif type == "trading":
query += "where STATUS=2"
if props["show_logs"]:
print(query)
mycursor = connection.cursor()
mycursor.execute(query)
myresult = mycursor.fetchall()
trades = []
for record in myresult:
trade = {}
trade["id"] = record[0]
trade["symbolName"] = record[1]
trade["strike"] = record[2]
trade["type"] = record[3]
trade["side"] = record[4]
trade["status"] = record[5]
trade["aboveOrBelow"] = record[6]
trade["price"] = record[7]
trade["profitPrice"] = record[8]
trade["slPrice"] = record[9]
trade["qty"] = record[10]
trades.append(trade)
if props["show_logs"]:
print("Trades : ", trades)
for trade in trades:
now = datetime.now()
dt_string = now.strftime("%H:%M:%S.%f")[:-3]
print(dt_string, " : ", type, " Strike:", trade["strike"], ", Price:", trade["price"])
if trade["symbolName"] in LTP_DICT:
print("Current Price : ", LTP_DICT[trade["symbolName"]])
else:
print("Current Price : Not Available")
return trades
def update_trade_status(status, id):
connection = get_connection()
mycursor = connection.cursor()
sql = "UPDATE TRADE_MASTER SET STATUS = " + str(status) + " WHERE ID = " + str(id)
print(sql)
mycursor.execute(sql)
connection.commit()
print(mycursor.rowcount, "record(s) affected")
def get_current_active_positions(positions):
active_positions = []
for position in positions:
if position["buyQty"] != position["sellQty"]:
active_positions.append(position)
return active_positions
def get_obj_from_array(list, field_name, value):
for obj in list:
if obj[field_name] == value:
return obj
return ""
def play_audio(file_path):
try:
playsound(file_path)
except Exception as e:
log_message("Error While playing alert", "WARNING")
def alertUser(file_path):
audio_thread = threading.Thread(target=play_audio, args=(file_path,))
audio_thread.start()
# Define your log levels and associated colors
log_levels = {
'INFO': Fore.BLUE,
'WARNING': Fore.YELLOW,
'ERROR': Fore.RED,
'CRITICAL': Back.RED + Fore.WHITE,
}
def log_message(message, level='INFO'):
# Get the color for the log level
color = log_levels.get(level, Fore.RESET)
# Display the log message with color
print(f'{color}[{level}] {message}')
def filter_pending_status_rows():
pending_rows = []
file_path = "Trade_notepad.txt"
with open(file_path, 'r') as file:
lines = file.readlines()
file.close()
header = [x.strip() for x in lines[0].replace(" ", "").split('|')]
for line in lines[1:]:
parts = [x.strip() for x in line.replace(" ", "").split('|')]
row = dict(zip(header, parts))
date_to_check = datetime.strptime(row['date'], "%d-%m-%Y")
if row['status'] == 'Pending' and date_to_check.date() == datetime.now().date():
# Convert ID to an integer
row['id'] = int(row['id'])
# Remove 'Status' key as it's not needed in the output
# del row['Status']
pending_rows.append(row)
return pending_rows
def update_status_by_id(id_to_update, new_status):
file_path = "Trade_notepad.txt"
# Read the text file
with open(file_path, 'r') as file:
lines = file.readlines()
file.close()
updated_lines = []
for line in lines:
parts = line.replace(" ", "").split('|')
if len(parts) >= 6:
current_id = parts[0]
current_status = parts[4]
if current_id == str(id_to_update):
parts[4] = new_status
updated_line = ' | '.join(parts)
print("Before Update: ", updated_line)
updated_lines.append(updated_line)
else:
updated_lines.append(line)
# Write the updated content back to the file
with open(file_path, 'w') as file:
file.writelines(updated_lines)
file.close()
print(f"Updated Status for ID {id_to_update} to '{new_status}'.")
def print_in_table(array):
table = PrettyTable()
all_keys = set(key for item in array for key in item.keys())
table.field_names = list(all_keys)
for item in array:
table.add_row([item.get(key, "") for key in all_keys])
print(table)
def calculate_strike(props, trade, LTP_DATA):
print("HHH Trade : ", trade)
print("HHH LTP_DATA : ", LTP_DATA)
current_price = LTP_DATA[trade["symbol"]]
symbol_stock_info_list = props["symbol_stock_info"]
symbol_info = ""
strike = trade["strike"]
for symbol_stock_info in symbol_stock_info_list:
if symbol_stock_info["symbol"] == trade["symbol"]:
symbol_info = symbol_stock_info
coieficient = int(current_price / symbol_info["strike_displacement"])
if trade["operator"] == ">=":
strike_val = (coieficient - int(trade["from_ATM"])) * symbol_info["strike_displacement"]
strike += str(strike_val) + "PE"
elif trade["operator"] == "<=":
strike_val = (coieficient + int(trade["from_ATM"]) + 1) * symbol_info["strike_displacement"]
strike += str(strike_val) + "CE"
print("strike : ", strike)
return strike
def get_available_fund(fyers):
json = fyers.funds()
print(json)
available_balance_object = next((item for item in json['fund_limit'] if item['title'] == 'Available Balance'), None)
print(available_balance_object)
return available_balance_object["equityAmount"]
def calculate_qty(fyers, props, trade):
qty = 1
capital_per = float(trade["capital_per"])
if "NIFTY" in trade["symbol"]:
# Assuming trade in options
if trade["qty"] == "AUTO":
available_capital = get_available_fund(fyers)
if capital_per > 50:
capital_per = 50
tradable_capital = (capital_per * available_capital) / 100
lot_size = get_lot_size(props, trade["symbol"])
price = float(trade["price"])
qty_lot = int((tradable_capital / price) / lot_size)
qty = qty_lot * lot_size
else:
qty = trade["qty"]
else:
qty = trade["qty"]
qty = int(qty)
print("QTY : ", qty)
return qty
def get_lot_size(props, symbol):
symbol_list = props["symbol_stock_info"]
for obj in symbol_list:
if obj["option_prefix"] in symbol:
print("Lot Size : ", obj["lot_size"])
return int(obj["lot_size"])