sysstra 0.1.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
src/data/__init__.py ADDED
@@ -0,0 +1,2 @@
1
+ api_key = "123asdf"
2
+ data_url = "http://127.0.0.1:5001"
src/data/historical.py ADDED
@@ -0,0 +1,85 @@
1
+ import requests
2
+ from src.data import api_key, data_url
3
+
4
+
5
+ def fetch_eod_candles(exchange, symbol, start_date, end_date):
6
+ """ Function to fetch End of Day Candles for symbol """
7
+ try:
8
+ headers = {"x-api-key": api_key, "content-type": "application/json"}
9
+ request_data = {"exchange": exchange, "symbol": symbol, "from_date": start_date, "to_date": end_date}
10
+ request_url = f"{data_url}/fetch-eod-data"
11
+ eod_data = requests.post(url=request_url, headers=headers, json=request_data)
12
+ return eod_data
13
+ except Exception as e:
14
+ print(f"Exception in fetching eod candles : {e}")
15
+ return []
16
+
17
+
18
+ def fetch_index_candles(exchange, symbol, start_date, end_date, granularity=1):
19
+ """ Function to fetch candles for the respective date """
20
+ try:
21
+ headers = {"x-api-key": api_key, "content-type": "application/json"}
22
+ request_data = {"exchange": exchange, "symbol": symbol, "from_date": start_date, "to_date": end_date,
23
+ "granularity": granularity}
24
+ request_url = f"{data_url}/fetch-index-data"
25
+ candles_data = requests.post(url=request_url, headers=headers, json=request_data)
26
+ return candles_data
27
+ except Exception as e:
28
+ print(f"Exception in fetching date candles : {e}")
29
+ return []
30
+
31
+
32
+ def fetch_option_candles(exchange, underlying_symbol, start_date, end_date, option_type, strike_price, expiry="near", granularity=1, timestamp=None):
33
+ """ Function to Fetch Options Trade Data """
34
+ try:
35
+ headers = {"x-api-key": api_key, "content-type": "application/json"}
36
+ request_data = {"exchange": exchange, "underlying_symbol": underlying_symbol, "from_date": start_date,
37
+ "to_date": end_date, "option_type": option_type, "strike_price": strike_price,
38
+ "expiry": expiry, "granularity": granularity}
39
+ request_url = f"{data_url}/fetch-options-data"
40
+ candles_data = requests.post(url=request_url, headers=headers, json=request_data)
41
+ return candles_data
42
+ except Exception as e:
43
+ print(f"Exception in fetching options candle : {e}")
44
+ return []
45
+
46
+
47
+ def fetch_option_candles_by_symbol(exchange, symbol, start_date, end_date, granularity=1):
48
+ """ Function to Fetch Options Trade Data """
49
+ try:
50
+ headers = {"x-api-key": api_key, "content-type": "application/json"}
51
+ request_data = {"exchange": exchange, "symbol": symbol, "from_date": start_date, "to_date": end_date, "granularity": granularity}
52
+ request_url = f"{data_url}/fetch-options-data-by-symbol"
53
+ candles_data = requests.post(url=request_url, headers=headers, json=request_data)
54
+ return candles_data
55
+
56
+ except Exception as e:
57
+ print(f"Exception in fetching options candle : {e}")
58
+ return []
59
+
60
+
61
+ def fetch_option_candle_by_timestamp(exchange, underlying_symbol, strike_price, option_type, timestamp, expiry="near"):
62
+ """ Function to Fetch Order Candle based on timestamp """
63
+ try:
64
+ headers = {"x-api-key": api_key, "content-type": "application/json"}
65
+ request_data = {"exchange": exchange, "underlying_symbol": underlying_symbol, "option_type": option_type,
66
+ "strike_price": strike_price, "timestamp": timestamp, "expiry": expiry}
67
+ request_url = f"{data_url}/fetch-option-data-by-timestamp"
68
+ candles_data = requests.post(url=request_url, headers=headers, json=request_data)
69
+ return candles_data
70
+
71
+ except Exception as e:
72
+ print(f"Exception in fetching order candle : {e}")
73
+ return []
74
+
75
+
76
+ if __name__ == '__main__':
77
+ # eod_candles = fetch_eod_candles(exchange="NSE", symbol="NIFTY 50", start_date="01/01/2024", end_date="05/01/2024")
78
+ # candles = fetch_index_candles(exchange="NSE", symbol="NIFTY 50", start_date="01/01/2024", end_date="05/01/2024")
79
+ # candles = fetch_option_candles(exchange="NSE", underlying_symbol="SENSEX", start_date="19/12/2024",
80
+ # end_date="19/12/2024", strike_price=80200, option_type="CE", expiry="near")
81
+ # candles = fetch_option_candles_by_symbol(exchange="NSE", symbol="SENSEX24D2080200CE", start_date="19/12/2024",
82
+ # end_date="19/12/2024")
83
+ candle = fetch_option_candle_by_timestamp(exchange="NSE", underlying_symbol="SENSEX", strike_price=80200, option_type="CE", expiry="near",
84
+ timestamp="2024-12-19 14:45:00")
85
+ print(candle)
src/data/live.py ADDED
@@ -0,0 +1,77 @@
1
+ def fetch_ticks(rdb_cursor, stocks_list):
2
+ """Function to fetch new ticks from Redis Cursor"""
3
+ try:
4
+ print("Fetching New Ticks from Redis")
5
+ sub = rdb_cursor.pubsub()
6
+ sub.subscribe(stocks_list)
7
+ for message in sub.listen():
8
+ if message is not None and isinstance(message, dict):
9
+ print("message : {}".format(message))
10
+ except Exception as e:
11
+ print("Exception in fetching ticks : {}".format(e))
12
+ pass
13
+
14
+
15
+ def fetch_current_day_open(rdb_cursor, symbol):
16
+ """ Function to Fetch Current Day Open """
17
+ try:
18
+ logger.info(msg="fetching current day open price*****")
19
+ candles_list = rdb_cursor.lrange(symbol, 0, 1)
20
+ if candles_list:
21
+ candles_dict = json.loads(candles_list[0])
22
+ logger.info(msg="current day open : {}".format(candles_dict["open"]))
23
+ return candles_dict["open"]
24
+ except Exception as e:
25
+ logger.exception(msg="Exception in fetching current day open : {}".format(e))
26
+ return None
27
+
28
+
29
+ def fetch_live_option_candle(symbol, strike_price, option_type):
30
+ """Function to fetch recent option candle"""
31
+ try:
32
+ logger.info(msg="fetching live option candle")
33
+ if symbol == "NIFTY BANK":
34
+ poc_result = rdb_cursor.get('poc')
35
+ elif symbol == "NIFTY 50":
36
+ poc_result = rdb_cursor.get('n50_poc')
37
+ else:
38
+ poc_result = rdb_cursor.get('poc')
39
+
40
+ options_dict = json.loads(poc_result)
41
+
42
+ symbol_ = symbol.replace(" ", "_")
43
+ key_name = f"{symbol_}_{str(int(strike_price))}_{option_type}"
44
+ option_candle = options_dict[key_name]
45
+
46
+ # Manipulating Tick Values
47
+ option_candle["close"] = option_candle["last_price"]
48
+ option_candle["timestamp"] = option_candle["exchange_timestamp"]
49
+ option_candle["timestamp"] = datetime.datetime.strptime(str(option_candle["timestamp"]), '%Y-%m-%d %H:%M:%S')
50
+ option_candle["date"] = datetime.datetime.strptime(str(option_candle["timestamp"].date()), '%Y-%m-%d')
51
+ # option_candle["expiry"] = datetime.datetime.today().date().isoformat()
52
+
53
+ logger.info(msg="live option_candle : {}".format(option_candle))
54
+ return option_candle
55
+ except Exception as e:
56
+ logger.exception(msg="Exception Fetching Option Candle : {}".format(e))
57
+ return None
58
+
59
+
60
+ def fetch_live_option_candles(symbol, strike_price, option_type):
61
+ """ Function to fetch current-day option candles """
62
+ try:
63
+ logger.info("fetchin day option candles")
64
+ symbol_ = symbol.replace(" ", "_")
65
+ key_name = f"{symbol_}_{str(int(strike_price))}_{option_type}"
66
+ logger.info(f"key_name : {key_name}")
67
+ candles = rdb_cursor.lrange(key_name, 0, -1)
68
+ if candles:
69
+ candles_list = [json.loads(i) for i in candles]
70
+ logger.info(f"total live options candles till now : {len(candles_list)}")
71
+ return candles_list
72
+ else:
73
+ return None
74
+ except Exception as e:
75
+ print(f"Exception in fetching day option candles : {e}")
76
+ return None
77
+
src/orders/__init__.py ADDED
File without changes
src/orders/kite.py ADDED
@@ -0,0 +1,36 @@
1
+ import json
2
+ import requests
3
+
4
+
5
+ def place_live_order(credential_id, order_details):
6
+ """Function to place live trade order via REST API"""
7
+ try:
8
+ print("Placing Live Trade Order")
9
+ request_dict = {"credential_id": str(credential_id),
10
+ "order_details": json.dumps(order_details)}
11
+ print("request_dict : {}".format(request_dict))
12
+
13
+ response = requests.post(url=orders_url+"place_order", params=request_dict)
14
+ print("******** Order Placement Response *********")
15
+ print(response.json())
16
+ return response.json()
17
+ except Exception as e:
18
+ print("Exception in placing live trade order : {}".format(e))
19
+ return None
20
+
21
+
22
+ def modify_live_order(credential_id, order_details):
23
+ """Function to Modify Live Order"""
24
+ try:
25
+ print("* Modifying Live Order")
26
+ request_dict = {"credential_id": str(credential_id),
27
+ "order_details": json.dumps(order_details)}
28
+ print("request_dict : {}".format(request_dict))
29
+
30
+ response = requests.post(url=orders_url + "modify_order", params=request_dict)
31
+ print("******** Order Placement Response *********")
32
+ print(response.json())
33
+ return response.json()
34
+ except Exception as e:
35
+ print("Exception in Modify Live Order : {}".format(e))
36
+ pass