forked from RudolfTheOne/ThetaTracker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
config_setup.py
150 lines (134 loc) · 5.45 KB
/
config_setup.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
import json
import logging
import os
# Define the paths to the config files
SYSTEM_CONFIG_PATH = "theta_tracker_system.conf"
USER_CONFIG_PATH = "theta_tracker_user.conf"
def read_tickers(file_path):
default_tickers = [("SPY", 0)] # Include index in tuple
try:
with open(file_path, 'r') as file:
tickers = file.read().splitlines()
# Check if the file is not empty
if not tickers:
print(f"No tickers found in the file. Using default: {default_tickers}")
return default_tickers
return [(ticker, idx+1) for idx, ticker in enumerate(tickers)] # Returns list of tuples
except FileNotFoundError:
print(f"File not found at path: {file_path}. Using default: {default_tickers}")
return default_tickers
except IOError:
print(f"Error reading file at path: {file_path}. Using default: {default_tickers}")
return default_tickers
def create_system_config():
"""Create the system config file with the API key and refresh interval."""
# Ask the user for the API key
api_key = input("Please enter your API key: ")
# Create the system config file
try:
with open(SYSTEM_CONFIG_PATH, "w") as f:
json.dump({"api_key": api_key, "refresh_interval": 300}, f)
except IOError:
raise IOError("Error: Unable to create system config file.")
def load_system_config():
"""Load the system configuration, creating it if necessary."""
if not os.path.exists(SYSTEM_CONFIG_PATH):
create_system_config()
try:
with open(SYSTEM_CONFIG_PATH, "r") as f:
return json.load(f)
except IOError:
raise IOError("Error: Unable to read system config file.")
def load_user_config():
"""Load the user configuration, creating it if necessary."""
if not os.path.exists(USER_CONFIG_PATH):
create_user_config()
try:
with open(USER_CONFIG_PATH, "r") as f:
return json.load(f)
except IOError:
raise IOError("Error: Unable to read user config file.")
except json.decoder.JSONDecodeError as e:
raise ValueError(f"Error: Unable to parse JSON data in user config file. JSONDecodeError: {e}")
def create_user_config():
# Ask the user for the config values
while True:
try:
max_delta = float(input("Please enter the maximum delta (0.0 - 1.0): "))
if not 0.0 <= max_delta <= 1.0:
print("Error: Delta range must be between 0.0 and 1.0.")
continue
dte_range_min = int(input("Please enter the DTE range minimum (0 - 365): "))
if not 0 <= dte_range_min <= 365:
print("Error: DTE range minimum must be between 0 and 365.")
dte_range_max = int(input("Please enter the DTE range maximum ({} - 365): ".format(dte_range_min)))
if not dte_range_min <= dte_range_max <= 365:
print("Error: DTE range maximum must be between {} and 365: ".format(dte_range_min))
continue
buying_power = float(input("Please enter the buying power (greater than $1000): "))
if buying_power <= 1000:
print("Error: Buying power must be greater than $1000.")
continue
break
except ValueError:
print("Error: Invalid input. Please try again.")
# Create the user config file
try:
with open(USER_CONFIG_PATH, "w") as f:
json.dump({
"max_delta": max_delta,
"dte_range_min": dte_range_min,
"dte_range_max": dte_range_max,
"buying_power": buying_power,
"default_sorting_method": "premium_usd",
"tickers": ["SPY"],
}, f)
except IOError:
print("Error: Unable to create user config file.")
exit(1)
def validate_max_delta(value):
try:
value = float(value)
logging.debug("konwersja do float udała się")
if 0.0 <= value <= 1.0:
logging.debug("wartość jest OK")
return True, None
else:
return False, "Delta range must be between 0.0 and 1.0."
except ValueError:
return False, "Invalid input. Please enter a valid number."
def validate_dte_range_min(value):
try:
value = int(value)
if 0 <= value <= 365:
return True, None
else:
return False, "Error: DTE range minimum must be between 0 and 365."
except ValueError:
return False, "Invalid input. Please enter a valid number."
def validate_dte_range_max(dte_range_min, value):
try:
value = int(value)
if int(dte_range_min) <= value <= 365:
return True, None
else:
return False, "Error: DTE range maximum must be between {} and 365: ".format(dte_range_min)
except ValueError:
return False, "Invalid input. Please enter a valid number."
def validate_buying_power(value):
try:
value = float(value)
if value >= 1000:
return True, None
else:
return False, "Buying power must be greater than or equal to $1000."
except ValueError:
return False, "Invalid input. Please enter a valid number."
def save_user_config(user_config):
# Save the user config file
try:
with open(USER_CONFIG_PATH, "w") as f:
json.dump(user_config, f)
except IOError:
print("Error: Unable to save user config file.")
exit(1)