Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
wiseplat
GitHub Repository: wiseplat/python-code
Path: blob/master/ invest-robot-contest_S-TinkoffInvestRobot-main/main.py
5925 views
1
import tkinter as tk
2
from datetime import datetime
3
from tinkoff.invest import *
4
from tinkoff.invest import Client, RequestError
5
import time
6
from tinkoff.invest.services import SandboxService
7
import threading
8
def high():
9
global accaunt
10
global figi
11
global r
12
global end_bal
13
try:
14
with Client(TOKEN) as client:
15
do_pokypki = len(client.sandbox.get_sandbox_portfolio(account_id=accaunt).positions)
16
r = client.sandbox.post_sandbox_order(
17
figi=figi,
18
quantity=1,
19
account_id=accaunt,
20
order_id=datetime.now().strftime("%T-%m-%dT %H:%M:%S"),
21
direction=OrderDirection.ORDER_DIRECTION_BUY,
22
order_type=OrderType.ORDER_TYPE_MARKET
23
)
24
p = True
25
while p:
26
time.sleep(1)
27
try:
28
if(len(client.sandbox.get_sandbox_portfolio(account_id=accaunt).positions))>do_pokypki:
29
end_bal = client.sandbox.get_sandbox_portfolio(account_id=accaunt).positions[0].quantity.units+client.sandbox.get_sandbox_portfolio(account_id=accaunt).positions[0].quantity.nano*0.000000001
30
p=False
31
except:
32
pass
33
except:return(high)
34
def high_sell():
35
print("продали акцию")
36
global accaunt
37
global figi
38
try:
39
with Client(TOKEN) as client:
40
do_pokypki = len(client.sandbox.get_sandbox_portfolio(account_id=accaunt).positions)
41
r = client.sandbox.post_sandbox_order(
42
figi=figi,
43
quantity=1,
44
price=Quotation(units=126,nano=0),
45
account_id=accaunt,
46
order_id=datetime.now().strftime("%T-%m-%dT %H:%M:%S"),
47
direction=OrderDirection.ORDER_DIRECTION_SELL,
48
order_type=OrderType.ORDER_TYPE_LIMIT
49
)
50
p = True
51
while p:
52
time.sleep(1)
53
try:
54
if(len(client.sandbox.get_sandbox_portfolio(account_id=accaunt).positions))>do_pokypki:
55
p=False
56
except:
57
pass
58
except:return(high_sell)
59
def low():
60
global TOKEN
61
global price
62
global comissia
63
with Client(TOKEN) as client:
64
lol = int(price.last_prices[0].price.units*comissia)
65
cost = int(price.last_prices[0].price.nano*comissia)
66
client.sandbox.sandbox_pay_in(account_id = accaunt,amount=MoneyValue(units=lol,nano=int(cost),currency="usd"))
67
def low_sell():
68
global TOKEN
69
global price
70
global comissia
71
with Client(TOKEN) as client:
72
lol = int(price.last_prices[0].price.units*comissia)
73
cost = int(price.last_prices[0].price.nano*comissia)
74
client.sandbox.sandbox_pay_in(account_id = accaunt,amount=MoneyValue(units=lol,nano=int(cost),currency="usd"))
75
def str():
76
global stop_los
77
global nomer
78
global pos
79
global take_profit
80
global nomers_g
81
global nomers_r
82
global nomers_k
83
global candels_g
84
global candels_r
85
global candels_k
86
global figi
87
global end_bal
88
global TOKEN
89
global accaunt
90
global r
91
global price
92
global comissia
93
for i in range(2000):
94
time.sleep(1)
95
print(i)
96
with Client(TOKEN) as client:
97
try:
98
price = client.market_data.get_last_prices(figi=[figi])
99
cena = price.last_prices[0].price.units+price.last_prices[0].price.nano*0.000000001
100
vremia = price.last_prices[0].time.year,price.last_prices[0].time.month,price.last_prices[0].time.day,price.last_prices[0].time.hour,price.last_prices[0].time.minute,price.last_prices[0].time.second,price.last_prices[0].time.microsecond
101
grafs.append(cena)
102
print(cena)
103
if take_profit>stop_los:
104
if nomer ==0:
105
start_bal=client.sandbox.get_sandbox_portfolio(account_id=accaunt).positions[0].quantity.units+client.sandbox.get_sandbox_portfolio(account_id=accaunt).positions[0].quantity.nano*0.000000001
106
otkitie = grafs[0]
107
#покупка на повышение
108
th1 = threading.Thread(target = high)
109
th1.start()
110
#покупка на понижение
111
comissia = abs(((start_bal- end_bal)-(r.total_order_amount.units+r.total_order_amount.nano*0.000000001))/(r.total_order_amount.units+r.total_order_amount.nano*0.000000001)*100)
112
th6 = threading.Thread(target = low)
113
th6.start()
114
115
if pos ==1:
116
if cena <= otkitie-stop_los:
117
#g продажа на повышение
118
nomers_g.append(nomer)
119
candels_g.append(cena)
120
pos =2
121
th15 = threading.Thread(target = high_sell)
122
th15.start()
123
print("продали длинную")
124
if cena >= otkitie+stop_los:
125
#r продажа на понижение
126
nomers_r.append(nomer)
127
candels_r.append(cena)
128
pos =3
129
try:
130
th11 = threading.Thread(target = low_sell)
131
th11.start()
132
except:
133
pass
134
print("продали короткую")
135
if pos ==2:
136
#k продать на понижение
137
if cena <= otkitie-take_profit:
138
nomers_k.append(nomer)
139
candels_k.append(cena)
140
pos =1
141
otkitie=cena
142
try:
143
th12 = threading.Thread(target = low_sell)
144
th12.start()
145
except:
146
pass
147
print("продали короткую")
148
th2 = threading.Thread(target = high)
149
th2.start()
150
th7 = threading.Thread(target = low)
151
th7.start()
152
print("+")
153
print("купили 2")
154
elif cena >= otkitie+stop_los:
155
nomers_k.append(nomer)
156
candels_k.append(cena)
157
pos =1
158
otkitie=cena
159
try:
160
th13 = threading.Thread(target = low_sell)
161
th13.start()
162
except:
163
pass
164
print("продали короткую")
165
th3 = threading.Thread(target = high)
166
th3.start()
167
try:
168
th8 = threading.Thread(target = low)
169
th8.start()
170
except:
171
pass
172
print("-")
173
print("купили 2")
174
#k продать на повыщение
175
if pos ==3:
176
if cena >= otkitie+take_profit:
177
nomers_k.append(nomer)
178
candels_k.append(cena)
179
pos =1
180
otkitie=cena
181
th16 = threading.Thread(target = high_sell)
182
th16.start()
183
print("продали длинную")
184
th4 = threading.Thread(target = high)
185
th4.start()
186
try:
187
th9 = threading.Thread(target = low)
188
th9.start()
189
except:
190
pass
191
print("+")
192
print("купили 2")
193
elif cena <= otkitie-stop_los:
194
nomers_k.append(nomer)
195
candels_k.append(cena)
196
pos =1
197
otkitie=cena
198
th17 = threading.Thread(target = high_sell)
199
th17.start()
200
print("продали длинную")
201
th5 = threading.Thread(target = high)
202
th5.start()
203
try:
204
th10 = threading.Thread(target = low)
205
th10.start()
206
except:
207
pass
208
print("-")
209
print("купили 2")
210
except:
211
pass
212
#стратегия
213
def strotegy():
214
global window
215
window.destroy()
216
window = tk.Tk()
217
with Client(TOKEN) as client:
218
r = client.sandbox.get_sandbox_accounts().accounts
219
for i in range(len(r)):
220
tk.Button(
221
text=r[i].id,
222
command=lambda i=r[i].id:figi_com(i),
223
height = 4,
224
width =30).pack()
225
window.mainloop()
226
def stop_win():
227
global window
228
global TOKEN
229
global accaunt
230
but_1.pack_forget()
231
with Client(TOKEN) as client:
232
balance = client.sandbox.get_sandbox_portfolio(account_id=accaunt).positions[0].quantity
233
text_balance = tk.Label(text=balance)
234
text_balance.pack()
235
tk.Button(text="stop").pack()
236
for i in range(10000):
237
time.sleep(1)
238
with Client(TOKEN) as client:
239
text_balance['text'] = client.sandbox.get_sandbox_portfolio(account_id=accaunt).positions[0].quantity
240
def plts():
241
global col
242
global figi
243
global window
244
global figi
245
global accaunt
246
global col
247
global TOKEN
248
global grafs
249
global stop_los
250
global take_profit
251
global candels_g
252
global candels_r
253
global candels_k
254
global nomer
255
global nomers_g
256
global nomers_k
257
global nomers_r
258
global pos
259
grafs = []
260
stop_los = 0.5 ##################################
261
take_profit = 1 ##################################
262
candels_g = []
263
candels_r = []
264
candels_k = []
265
nomers_g = []
266
nomers_r = []
267
nomers_k = []
268
nomer=0
269
pos = 1
270
th1 = threading.Thread(target = stop_win)
271
th1.start()
272
time.sleep(1)
273
th = threading.Thread(target = str)
274
th.start()
275
def col_vo(j):
276
#figi
277
global col
278
global figi
279
global window
280
global but_1
281
window.destroy()
282
print(j)
283
figi = j
284
window = tk.Tk()
285
th1 = threading.Thread(target = plts)
286
but_1 = tk.Button(text=1,height = 4,
287
width =30,command=lambda:[th1.start()])
288
but_1.pack()
289
col = 1
290
window.mainloop()
291
def figi_com(i):
292
#acc
293
global accaunt
294
global figis
295
accaunt = i
296
figis = [["google","apple"],["BBG009S3NB30","BBG000B9XRY4"]]
297
global window
298
window.destroy()
299
window = tk.Tk()
300
for k in range(len(figis[0])):
301
tk.Button(
302
text=figis[0][k],
303
command=lambda j=figis[1][k]:col_vo(j),
304
height = 4,
305
width =30).pack()
306
window.mainloop()
307
window.mainloop()
308
309
310
311
312
#аккаунт
313
def creat():
314
global TOKEN
315
with Client(TOKEN) as client:
316
x = client.sandbox.open_sandbox_account()
317
id_ac = x.account_id #номер аккаунта
318
client.sandbox.sandbox_pay_in(account_id = id_ac,amount=MoneyValue(units=1000000,nano=0,currency="usd"))#пополнение счёта
319
print(client.sandbox.sandbox_pay_in(account_id = id_ac)) #вывод денег
320
print("аккаунт создан")
321
def remove():
322
with Client(TOKEN) as client:
323
r = client.sandbox.get_sandbox_accounts().accounts
324
for i in range(len(r)):
325
client.sandbox.close_sandbox_account(account_id=r[i].id)
326
print("аккаунты удалены")
327
def lists():
328
print("список аккаунтов")
329
with Client(TOKEN) as client:
330
r = client.sandbox.get_sandbox_accounts().accounts
331
for i in range(len(r)):
332
print(r[i])
333
def profil():
334
global window
335
window.destroy()
336
window = tk.Tk()
337
338
button_acc = tk.Button(
339
text="создать аккаунт",
340
command=creat,
341
height = 8,
342
width = 30)
343
button_accs = tk.Button(
344
text="список аккаунтов",
345
command=lists,
346
height = 8,
347
width = 30)
348
button_acc_rem = tk.Button(
349
text="удалить аккаунты",
350
command=remove,
351
height = 8,
352
width = 30)
353
button_bac = tk.Button(
354
text="назад",
355
command=token_com,
356
height = 8,
357
width = 30)
358
button_bac.pack()
359
button_acc_rem.pack()
360
button_accs.pack()
361
button_acc.pack()
362
363
window.mainloop()
364
365
#назад
366
def back():
367
global window
368
window.destroy()
369
main()
370
#главная
371
def token_com():
372
global window
373
global TOKEN
374
TOKEN = entry.get()
375
window.destroy()
376
window = tk.Tk()
377
378
button1 = tk.Button(
379
text="профиль",
380
command=profil,
381
height = 8,
382
width = 30
383
)
384
button2 = tk.Button(
385
text="стратегия",
386
command=strotegy,
387
height = 8,
388
width = 30
389
)
390
button3 = tk.Button(
391
text="назад к токену",
392
command=back,
393
height = 8,
394
width = 30
395
)
396
button1.pack()
397
button2.pack()
398
button3.pack()
399
400
window.mainloop()
401
#токен
402
def main():
403
global entry
404
global window
405
window = tk.Tk()
406
entry = tk.Entry(width = 35)
407
button = tk.Button(
408
text="Нажми на меня!",
409
command=token_com,
410
height = 8,
411
width = 30
412
)
413
entry.pack()
414
button.pack()
415
window.mainloop()
416
main()
417