-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
195 lines (179 loc) · 11 KB
/
main.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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
import os
from time import sleep
import datetime
from webdriver import *
START_TIME = datetime.time(10, 0, 0)
END_TIME = datetime.time(16, 54, 0)
URL_CLEAR_LOGIN = "https://login.clear.com.br/pit/login/"
URL_CLEAR_TESOURO = "https://pro.clear.com.br/#renda-fixa/tesouro-direto"
URL_CLEAR_PEDIDO_LIMITE = "https://forms.office.com/Pages/ResponsePage.aspx?id=BeRWz7DSZkKyEKoEY2thYYvZn_U3ldBGtrKRP1Fh5WxUMVU1OVg0MEFBVDdHNlNSR1c0MjJGS1FOMC4u"
URL_CLEAR = "https://pro.clear.com.br/#renda-variavel/swing-trade"
CODIGO_CLIENTE=os.getenv("CODIGO_CLIENTE")
CPF=str(os.getenv("CPF"))
DATA_NASCIMENTO=os.getenv("DATA_NASCIMENTO")
SENHA=os.getenv("SENHA")
ASSINATURA=os.getenv("ASSINATURA")
SALDO_EXTERNO = float(str(os.getenv("SALDO_EXTERNO")))
VALOR_COMPRA = 13.65
VALOR_VENDA = 14.35
PRECO_HIGH_2A = 19
PRECO_LOW_2A = 12.81
DIFF_2A = PRECO_HIGH_2A - PRECO_LOW_2A
QUANTIDADE = 0
def time_in_range(start, end, current):
"""Returns whether current is in the range [start, end]"""
return start <= current <= end
def main():
STEP = int(str(os.getenv("STEP")))
driver = get_selenium_webdriver(headless=False)
try:
compra = venda = 0
driver.get(URL_CLEAR_LOGIN)
cpf_field = WebDriverWait(driver, 15).until(EC.presence_of_element_located((By.NAME, 'Username')))
cpf_field.send_keys(CPF)
contents = driver.find_elements(By.XPATH, '//h2[@class="sub_title"]')
for content in contents:
print(content.get_attribute('innerHTML'))
data_nascimento_field = driver.find_element(By.NAME, 'DoB')
data_nascimento_field.send_keys(DATA_NASCIMENTO)
senha_field = driver.find_element(By.NAME, "Password")
senha_field.send_keys(SENHA)
acessar = driver.find_element(By.CLASS_NAME, "bt_signin")
acessar.click()
menu = WebDriverWait(driver, 300).until(EC.presence_of_element_located((By.CLASS_NAME, 'menu')))
sleep(1)
#Pega o saldo do tesouro direto
driver.get(URL_CLEAR_TESOURO)
WebDriverWait(driver, 60).until(EC.presence_of_element_located((By.CLASS_NAME, "site")))
driver.switch_to.frame(driver.find_element(By.NAME, "content-page"))
driver.find_element(By.XPATH, "//a[@class='btn-top-res btn-results btn-icon btn-text']").click()
sleep(2)
saldo_tesouro = float(driver.find_element(By.XPATH, "//div[@class='text-value position-value-net']").get_attribute('innerHTML').replace("R$ ","").replace(".", "").replace(",","."))
print(saldo_tesouro)
#Solicita o limite se ele não estiver sido solicitado antes
# if saldo_clear < saldo_tesouro:
# driver.get(URL_CLEAR_PEDIDO_LIMITE)
# campos = driver.find_elements(By.XPATH, "//input[@class='office-form-question-textbox office-form-textfield-input form-control office-form-theme-focus-border border-no-radius']")
# campos[0].send_keys(CODIGO_CLIENTE)
# campos[1].send_keys(CPF.replace(".", "").replace("-", ""))
# campos[2].send_keys(str(saldo_tesouro).replace(".", ","))
#Muda para o swing trade e começa as operações
driver.get(URL_CLEAR)
WebDriverWait(driver, 60).until(EC.presence_of_element_located((By.CLASS_NAME, "site")))
driver.switch_to.frame(driver.find_element(By.NAME, "content-page"))
WebDriverWait(driver, 60).until(EC.presence_of_element_located((By.XPATH, "//div[@class='cont_detail detail-deal-book']")))
#mostra o livro de ofertas
sleep(15)
driver.find_element(By.XPATH, "//div[@class='AssetListItem ui-sortable-handle']").click()
#Salva a assinatura eletrônica
driver.find_element(By.XPATH, "//label[@class='checkbox bt-toggle-signature']/span[@class='check']").click()
sleep(1)
driver.find_element(By.XPATH, "//input[@class='relocate-signature-input']").send_keys(ASSINATURA)
sleep(1)
driver.find_element(By.XPATH, "//a[@class='bt-docket save-signature xsmall']").click()
quantidade = int(str(driver.find_element(By.XPATH, "//div[@class='value detailed-net-qty']").get_attribute('innerHTML')).split(" ")[0].replace(".", ""))
print(f"Quantidade: {quantidade}")
preco_medio = float(str(driver.find_element(By.XPATH, "//div[@class='value detailed-net-average']").get_attribute('innerHTML')).replace("R$ ","").replace(".", "").replace(",","."))
print(f"Preço médio: R${preco_medio}")
preco_atual = float(driver.find_element(By.XPATH, "//span[@class='symbol-price']").get_attribute('innerHTML').replace(".","").replace(",","."))
print(f"Preço atual: R$ {preco_atual}")
driver.switch_to.default_content()
#Clica uma vez no saldo para aparecer os elementos do saldo. Clica de novo para tirar o saldo da tela
driver.find_element(By.XPATH, "//a[@data-wa='pit;topo-fixo;saldo-conta']").click()
driver.find_element(By.XPATH, "//a[@data-wa='pit;topo-fixo;saldo-conta']").click()
saldo_clear = driver.find_elements(By.XPATH, "//soma-paragraph[@class='total-amount total_val elipsed-val soma-paragraph hydrated small-text']")
if saldo_clear:
saldo_clear = float(str(saldo_clear[0].get_attribute('innerHTML')).replace("R$ ","").replace(".", "").replace(",","."))
else:
saldo_clear = float(str(driver.find_element(By.XPATH, "//soma-paragraph[@class='total-amount total_val elipsed-val soma-paragraph hydrated']").get_attribute('innerHTML')).replace("R$ ","").replace(".", "").replace(",","."))
print(f"Saldo projetado Clear: R${saldo_clear}")
saldo = saldo_clear + SALDO_EXTERNO
saldo_abev3 = quantidade*preco_atual
print(f"Saldo total dinheiro: R${saldo}")
patrimonio = saldo + saldo_abev3
print(f"Patrimônio total: R${patrimonio}")
diff_percentual = 1-((preco_atual-PRECO_LOW_2A)/DIFF_2A)
QUANTIDADE = ((patrimonio*diff_percentual)-(quantidade*preco_medio))/preco_atual
print(f"Rebalanceamento (quantidade): {QUANTIDADE}")
print(f"Step: {STEP}")
driver.switch_to.frame(driver.find_element(By.NAME, "content-page"))
except Exception as e:
driver.close()
print(e)
main()
while True:
sleep(2)
if not time_in_range(START_TIME, END_TIME, datetime.datetime.now().time()) or datetime.datetime.now().weekday() > 4:
print(f"Pregão fechado. Horário: {datetime.datetime.now()}")
continue
is_leilao = driver.find_elements(By.XPATH, "//div[@data-symbol='ABEV3']/div[@class='widget-list auction active']")
if is_leilao:
print(f"ABEV3 em Leilão")
continue
abev3_book_prices = driver.find_elements(By.XPATH, "//tbody[@class='itens']/tr/td[@class='buy-amount buy']/a")
for key, element in enumerate(abev3_book_prices):
if key == 0:
compra = float(str(element.get_attribute('innerHTML')).replace('<var>', '').replace('</var>', '').replace('Aber', '0'))
print(f"ABEV3 compra: {compra}")
abev3_book_prices = driver.find_elements(By.XPATH, "//tbody[@class='itens']/tr/td[@class='sell-amount sell']/a")
for key, element in enumerate(abev3_book_prices):
if key == 0:
venda = float(str(element.get_attribute('innerHTML')).replace('<var>', '').replace('</var>', '').replace('Aber', '0'))
print(f"ABEV3 venda: {venda}")
quantidade = int(str(driver.find_element(By.XPATH, "//div[@class='value detailed-net-qty']").get_attribute('innerHTML')).split(" ")[0].replace(".", ""))
print(f"Quantidade: {quantidade}")
preco_medio = float(str(driver.find_element(By.XPATH, "//div[@class='value detailed-net-average']").get_attribute('innerHTML')).replace("R$ ","").replace(".", "").replace(",","."))
print(f"Preço médio: R${preco_medio}")
try:
if 0 not in (venda, compra) and saldo_clear >= saldo_tesouro:
if QUANTIDADE >= STEP:
abev3_venda = driver.find_element(By.XPATH, "//li[@class='action-item buy']/a")
abev3_venda.click()
abev3_qtde = driver.find_element(By.XPATH, "//input[@class='xbig input-quantity id-input-quantity ui-spinner-input']")
abev3_qtde.clear()
abev3_qtde.send_keys(STEP)
# driver.find_element(By.XPATH, "//button[@class='btn-checkout bt-docket buy']").click()
print(f"Efetuada a compra de {STEP} ABEV3")
sleep(5)
if QUANTIDADE <= -STEP and compra <= preco_medio:
abev3_venda = driver.find_element(By.XPATH, "//li[@class='action-item sell']/a")
abev3_venda.click()
abev3_qtde = driver.find_element(By.XPATH, "//input[@class='xbig input-quantity id-input-quantity ui-spinner-input']")
abev3_qtde.clear()
abev3_qtde.send_keys(STEP)
# driver.find_element(By.XPATH, "//button[@class='btn-checkout bt-docket sell']").click()
print(f"Efetuada a venda de {STEP} ABEV3")
sleep(5)
except Exception as e:
print(e)
raise
driver.switch_to.default_content()
try:
#Clica uma vez no saldo para aparecer os elementos do saldo. Clica de novo para tirar o saldo da tela
driver.find_element(By.XPATH, "//a[@data-wa='pit;topo-fixo;saldo-conta']").click()
driver.find_element(By.XPATH, "//a[@data-wa='pit;topo-fixo;saldo-conta']").click()
saldo_clear = driver.find_elements(By.XPATH, "//soma-paragraph[@class='total-amount total_val elipsed-val soma-paragraph hydrated small-text']")
if saldo_clear:
saldo_clear = float(str(saldo_clear[0].get_attribute('innerHTML')).replace("R$ ","").replace(".", "").replace(",","."))
else:
saldo_clear = float(str(driver.find_element(By.XPATH, "//soma-paragraph[@class='total-amount total_val elipsed-val soma-paragraph hydrated']").get_attribute('innerHTML')).replace("R$ ","").replace(".", "").replace(",","."))
saldo = saldo_clear + SALDO_EXTERNO
saldo_abev3 = quantidade*compra
patrimonio = saldo + saldo_abev3
diff_percentual = 1-((compra-PRECO_LOW_2A)/DIFF_2A)
QUANTIDADE = ((patrimonio*diff_percentual)-(quantidade*preco_medio))/compra
STEP = int(str(os.getenv("STEP")))
print(f"Saldo Tesouro Direto: R${saldo_tesouro}")
print(f"Saldo projetado Clear: R${saldo_clear}")
if saldo_clear < saldo_tesouro: print("Limite ainda não liberado")
print(f"Saldo total dinheiro: R${saldo}")
print(f"Patrimônio total: R${patrimonio}")
print(f"Rebalanceamento (quantidade): {QUANTIDADE}")
print(f"Step: {STEP}")
except Exception as e:
driver.close()
print(e)
main()
driver.switch_to.frame(driver.find_element(By.NAME, "content-page"))
if __name__ == "__main__":
main()