Path: blob/master/ invest-robot-contest_S-TinkoffInvestRobot-main/main.py
5925 views
import tkinter as tk1from datetime import datetime2from tinkoff.invest import *3from tinkoff.invest import Client, RequestError4import time5from tinkoff.invest.services import SandboxService6import threading7def high():8global accaunt9global figi10global r11global end_bal12try:13with Client(TOKEN) as client:14do_pokypki = len(client.sandbox.get_sandbox_portfolio(account_id=accaunt).positions)15r = client.sandbox.post_sandbox_order(16figi=figi,17quantity=1,18account_id=accaunt,19order_id=datetime.now().strftime("%T-%m-%dT %H:%M:%S"),20direction=OrderDirection.ORDER_DIRECTION_BUY,21order_type=OrderType.ORDER_TYPE_MARKET22)23p = True24while p:25time.sleep(1)26try:27if(len(client.sandbox.get_sandbox_portfolio(account_id=accaunt).positions))>do_pokypki:28end_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.00000000129p=False30except:31pass32except:return(high)33def high_sell():34print("продали акцию")35global accaunt36global figi37try:38with Client(TOKEN) as client:39do_pokypki = len(client.sandbox.get_sandbox_portfolio(account_id=accaunt).positions)40r = client.sandbox.post_sandbox_order(41figi=figi,42quantity=1,43price=Quotation(units=126,nano=0),44account_id=accaunt,45order_id=datetime.now().strftime("%T-%m-%dT %H:%M:%S"),46direction=OrderDirection.ORDER_DIRECTION_SELL,47order_type=OrderType.ORDER_TYPE_LIMIT48)49p = True50while p:51time.sleep(1)52try:53if(len(client.sandbox.get_sandbox_portfolio(account_id=accaunt).positions))>do_pokypki:54p=False55except:56pass57except:return(high_sell)58def low():59global TOKEN60global price61global comissia62with Client(TOKEN) as client:63lol = int(price.last_prices[0].price.units*comissia)64cost = int(price.last_prices[0].price.nano*comissia)65client.sandbox.sandbox_pay_in(account_id = accaunt,amount=MoneyValue(units=lol,nano=int(cost),currency="usd"))66def low_sell():67global TOKEN68global price69global comissia70with Client(TOKEN) as client:71lol = int(price.last_prices[0].price.units*comissia)72cost = int(price.last_prices[0].price.nano*comissia)73client.sandbox.sandbox_pay_in(account_id = accaunt,amount=MoneyValue(units=lol,nano=int(cost),currency="usd"))74def str():75global stop_los76global nomer77global pos78global take_profit79global nomers_g80global nomers_r81global nomers_k82global candels_g83global candels_r84global candels_k85global figi86global end_bal87global TOKEN88global accaunt89global r90global price91global comissia92for i in range(2000):93time.sleep(1)94print(i)95with Client(TOKEN) as client:96try:97price = client.market_data.get_last_prices(figi=[figi])98cena = price.last_prices[0].price.units+price.last_prices[0].price.nano*0.00000000199vremia = 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.microsecond100grafs.append(cena)101print(cena)102if take_profit>stop_los:103if nomer ==0:104start_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.000000001105otkitie = grafs[0]106#покупка на повышение107th1 = threading.Thread(target = high)108th1.start()109#покупка на понижение110comissia = 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)111th6 = threading.Thread(target = low)112th6.start()113114if pos ==1:115if cena <= otkitie-stop_los:116#g продажа на повышение117nomers_g.append(nomer)118candels_g.append(cena)119pos =2120th15 = threading.Thread(target = high_sell)121th15.start()122print("продали длинную")123if cena >= otkitie+stop_los:124#r продажа на понижение125nomers_r.append(nomer)126candels_r.append(cena)127pos =3128try:129th11 = threading.Thread(target = low_sell)130th11.start()131except:132pass133print("продали короткую")134if pos ==2:135#k продать на понижение136if cena <= otkitie-take_profit:137nomers_k.append(nomer)138candels_k.append(cena)139pos =1140otkitie=cena141try:142th12 = threading.Thread(target = low_sell)143th12.start()144except:145pass146print("продали короткую")147th2 = threading.Thread(target = high)148th2.start()149th7 = threading.Thread(target = low)150th7.start()151print("+")152print("купили 2")153elif cena >= otkitie+stop_los:154nomers_k.append(nomer)155candels_k.append(cena)156pos =1157otkitie=cena158try:159th13 = threading.Thread(target = low_sell)160th13.start()161except:162pass163print("продали короткую")164th3 = threading.Thread(target = high)165th3.start()166try:167th8 = threading.Thread(target = low)168th8.start()169except:170pass171print("-")172print("купили 2")173#k продать на повыщение174if pos ==3:175if cena >= otkitie+take_profit:176nomers_k.append(nomer)177candels_k.append(cena)178pos =1179otkitie=cena180th16 = threading.Thread(target = high_sell)181th16.start()182print("продали длинную")183th4 = threading.Thread(target = high)184th4.start()185try:186th9 = threading.Thread(target = low)187th9.start()188except:189pass190print("+")191print("купили 2")192elif cena <= otkitie-stop_los:193nomers_k.append(nomer)194candels_k.append(cena)195pos =1196otkitie=cena197th17 = threading.Thread(target = high_sell)198th17.start()199print("продали длинную")200th5 = threading.Thread(target = high)201th5.start()202try:203th10 = threading.Thread(target = low)204th10.start()205except:206pass207print("-")208print("купили 2")209except:210pass211#стратегия212def strotegy():213global window214window.destroy()215window = tk.Tk()216with Client(TOKEN) as client:217r = client.sandbox.get_sandbox_accounts().accounts218for i in range(len(r)):219tk.Button(220text=r[i].id,221command=lambda i=r[i].id:figi_com(i),222height = 4,223width =30).pack()224window.mainloop()225def stop_win():226global window227global TOKEN228global accaunt229but_1.pack_forget()230with Client(TOKEN) as client:231balance = client.sandbox.get_sandbox_portfolio(account_id=accaunt).positions[0].quantity232text_balance = tk.Label(text=balance)233text_balance.pack()234tk.Button(text="stop").pack()235for i in range(10000):236time.sleep(1)237with Client(TOKEN) as client:238text_balance['text'] = client.sandbox.get_sandbox_portfolio(account_id=accaunt).positions[0].quantity239def plts():240global col241global figi242global window243global figi244global accaunt245global col246global TOKEN247global grafs248global stop_los249global take_profit250global candels_g251global candels_r252global candels_k253global nomer254global nomers_g255global nomers_k256global nomers_r257global pos258grafs = []259stop_los = 0.5 ##################################260take_profit = 1 ##################################261candels_g = []262candels_r = []263candels_k = []264nomers_g = []265nomers_r = []266nomers_k = []267nomer=0268pos = 1269th1 = threading.Thread(target = stop_win)270th1.start()271time.sleep(1)272th = threading.Thread(target = str)273th.start()274def col_vo(j):275#figi276global col277global figi278global window279global but_1280window.destroy()281print(j)282figi = j283window = tk.Tk()284th1 = threading.Thread(target = plts)285but_1 = tk.Button(text=1,height = 4,286width =30,command=lambda:[th1.start()])287but_1.pack()288col = 1289window.mainloop()290def figi_com(i):291#acc292global accaunt293global figis294accaunt = i295figis = [["google","apple"],["BBG009S3NB30","BBG000B9XRY4"]]296global window297window.destroy()298window = tk.Tk()299for k in range(len(figis[0])):300tk.Button(301text=figis[0][k],302command=lambda j=figis[1][k]:col_vo(j),303height = 4,304width =30).pack()305window.mainloop()306window.mainloop()307308309310311#аккаунт312def creat():313global TOKEN314with Client(TOKEN) as client:315x = client.sandbox.open_sandbox_account()316id_ac = x.account_id #номер аккаунта317client.sandbox.sandbox_pay_in(account_id = id_ac,amount=MoneyValue(units=1000000,nano=0,currency="usd"))#пополнение счёта318print(client.sandbox.sandbox_pay_in(account_id = id_ac)) #вывод денег319print("аккаунт создан")320def remove():321with Client(TOKEN) as client:322r = client.sandbox.get_sandbox_accounts().accounts323for i in range(len(r)):324client.sandbox.close_sandbox_account(account_id=r[i].id)325print("аккаунты удалены")326def lists():327print("список аккаунтов")328with Client(TOKEN) as client:329r = client.sandbox.get_sandbox_accounts().accounts330for i in range(len(r)):331print(r[i])332def profil():333global window334window.destroy()335window = tk.Tk()336337button_acc = tk.Button(338text="создать аккаунт",339command=creat,340height = 8,341width = 30)342button_accs = tk.Button(343text="список аккаунтов",344command=lists,345height = 8,346width = 30)347button_acc_rem = tk.Button(348text="удалить аккаунты",349command=remove,350height = 8,351width = 30)352button_bac = tk.Button(353text="назад",354command=token_com,355height = 8,356width = 30)357button_bac.pack()358button_acc_rem.pack()359button_accs.pack()360button_acc.pack()361362window.mainloop()363364#назад365def back():366global window367window.destroy()368main()369#главная370def token_com():371global window372global TOKEN373TOKEN = entry.get()374window.destroy()375window = tk.Tk()376377button1 = tk.Button(378text="профиль",379command=profil,380height = 8,381width = 30382)383button2 = tk.Button(384text="стратегия",385command=strotegy,386height = 8,387width = 30388)389button3 = tk.Button(390text="назад к токену",391command=back,392height = 8,393width = 30394)395button1.pack()396button2.pack()397button3.pack()398399window.mainloop()400#токен401def main():402global entry403global window404window = tk.Tk()405entry = tk.Entry(width = 35)406button = tk.Button(407text="Нажми на меня!",408command=token_com,409height = 8,410width = 30411)412entry.pack()413button.pack()414window.mainloop()415main()416417