-
Notifications
You must be signed in to change notification settings - Fork 0
/
TDAmeritrade_user.py
157 lines (116 loc) · 5.85 KB
/
TDAmeritrade_user.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
#Functions to interact with the user of bot
# update_user - Gives an update message to user about current status of bot and transactions made.
import datetime,json,pickle,time
from TDAmeritrade_algorithm_centroids import track_buy_orders,track_sell_orders,check_request_times
from TDAmeritrade_API import get_access
def user_interface(lock,transactions,errors):
"""
User interaction. User can get information about the transactions
"""
user_input="Random"
while len(errors)==0:
try:
while user_input!="Check All" and user_input!="Stop" and user_input!="Check Ticker" \
and user_input!="Track Orders" and user_input!="Update":
user_input=input("Enter 'Check All', 'Check Ticker', 'Track Orders', 'Update' or 'Stop': ")
if user_input=="Check All":
lock.acquire()
print("--------------------")
print("--------------------")
print(json.dumps(transactions))
print("--------------------")
print("--------------------")
lock.release()
#Reset user_input
user_input="Random"
elif user_input=="Check Ticker":
lock.acquire()
ticker = input("Please enter the ticker to check: ")
print("--------------------")
print("--------------------")
if ticker in transactions:
print(json.dumps(transactions[ticker]))
print("--------------------")
print("--------------------")
lock.release()
#Reset user_input
user_input="Random"
elif user_input=="Update":
lock.acquire()
while user_input == "Update":
ticker = input("Please enter the ticker to update: ")
key = input("Please enter the key of the ticker dictionary to update: ")
value_temp = input("Please enter the value to change it to: ")
value_type = input("Please enter the type the value is (int,float,str): ")
if value_type=="int":
value=int(value_temp)
elif value_type=="float":
value=float(value_temp)
else:
value=value_temp
if ticker in transactions and key in transactions[ticker]:
transactions[ticker][key]=value
print(transactions[ticker][key])
user_input = input("Type 'Update' to keep updating or anything else to continue bot: ")
lock.release()
elif user_input=="Track Orders":
lock.acquire()
ticker = input("What ticker do you want to track orders for: ")
instruction = input("Do you want to track buy or sell orders (enter: buy or sell): ")
if ticker in transactions:
if instruction=='buy':
track_buy_orders(transactions,ticker)
elif instruction=='sell':
track_sell_orders(transactions,ticker)
lock.release()
#Reset user_input
user_input="Random"
elif user_input=="Stop":
errors.append('User asked to stop bot.')
except:
errors.append('Error in user interface.')
def update_transactions():
#Load transactions dictionary
transactions = pickle.load(open("Transactions.p","rb"))
#Prompt user for information on what they want to update.
user_input = 'Update'
while user_input == 'Update':
ticker = input('Please enter the ticker to update: ')
key = input('Please enter the key of the ticker to update: ')
value_temp = input("Please enter the value to change it to: ")
value_type = input("Please enter the type the value is (int,float,str): ")
if value_type=="int":
value=int(value_temp)
elif value_type=="float":
value=float(value_temp)
else:
value=value_temp
if ticker in transactions and key in transactions[ticker]:
transactions[ticker][key]=value
print(transactions[ticker][key])
user_input = input("Type 'Update' to keep updating or anything else stop: ")
#Save transactions dictionary
pickle.dump(transactions,open("Transactions.p","wb"))
def print_transactions():
#Load transactions dictionary
transactions = pickle.load(open("Transactions.p","rb"))
for ticker in transactions['Tickers']:
print('--------------------')
print(ticker)
print(transactions[ticker])
print('--------------------')
def update_all_orders():
#Load transactions dictionary
transactions = pickle.load(open("Transactions.p","rb"))
min_request_index=0
#Make sure the access token is valid
(transactions['Access Token'],transactions['Access Expire Time']) = get_access(transactions['Access Token'],transactions['Access Expire Time'])
for ticker in transactions['Tickers']:
track_buy_orders(transactions,ticker)
track_sell_orders(transactions,ticker)
transactions['Last Requests'][min_request_index]=time.time()
last_requests=transactions['Last Requests']
#Make sure we are not making too many requests to the TD Ameritrade API
min_request_index=check_request_times(last_requests)
#Save transactions dictionary
pickle.dump(transactions,open("Transactions.p","wb"))