mtcli-risco 2.1.2__py3-none-any.whl → 2.2.1__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- mtcli_risco/commands/checar.py +68 -67
- mtcli_risco/commands/monitorar.py +57 -56
- mtcli_risco/commands/{lucro.py → trades.py} +54 -53
- mtcli_risco/models/{risco.py → checar_model.py} +102 -100
- mtcli_risco/models/{lucro.py → trades_model.py} +77 -58
- mtcli_risco/plugin.py +6 -17
- {mtcli_risco-2.1.2.dist-info → mtcli_risco-2.2.1.dist-info}/METADATA +1 -1
- mtcli_risco-2.2.1.dist-info/RECORD +16 -0
- mtcli_risco-2.2.1.dist-info/entry_points.txt +3 -0
- mtcli_risco-2.1.2.dist-info/RECORD +0 -16
- mtcli_risco-2.1.2.dist-info/entry_points.txt +0 -3
- {mtcli_risco-2.1.2.dist-info → mtcli_risco-2.2.1.dist-info}/LICENSE +0 -0
- {mtcli_risco-2.1.2.dist-info → mtcli_risco-2.2.1.dist-info}/WHEEL +0 -0
mtcli_risco/commands/checar.py
CHANGED
|
@@ -1,67 +1,68 @@
|
|
|
1
|
-
"""Comando checar - verifica se o loss limit foi atingido."""
|
|
2
|
-
|
|
3
|
-
import click
|
|
4
|
-
from datetime import date
|
|
5
|
-
from mtcli.logger import setup_logger
|
|
6
|
-
from
|
|
7
|
-
from
|
|
8
|
-
from
|
|
9
|
-
carregar_estado,
|
|
10
|
-
salvar_estado,
|
|
11
|
-
risco_excedido,
|
|
12
|
-
encerrar_todas_posicoes,
|
|
13
|
-
cancelar_todas_ordens,
|
|
14
|
-
)
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
log = setup_logger()
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
@click.command("checar")
|
|
21
|
-
@click.
|
|
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
|
-
|
|
1
|
+
"""Comando checar - verifica se o loss limit foi atingido."""
|
|
2
|
+
|
|
3
|
+
import click
|
|
4
|
+
from datetime import date
|
|
5
|
+
from mtcli.logger import setup_logger
|
|
6
|
+
from mtcli_risco.conf import LOSS_LIMIT, STATUS_FILE
|
|
7
|
+
from mtcli_risco.models.trades_model import calcular_lucro_total_dia
|
|
8
|
+
from mtcli_risco.models.checar_model import (
|
|
9
|
+
carregar_estado,
|
|
10
|
+
salvar_estado,
|
|
11
|
+
risco_excedido,
|
|
12
|
+
encerrar_todas_posicoes,
|
|
13
|
+
cancelar_todas_ordens,
|
|
14
|
+
)
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
log = setup_logger()
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
@click.command("checar")
|
|
21
|
+
@click.version_option(package_name="mtcli-risco")
|
|
22
|
+
@click.option(
|
|
23
|
+
"--limite",
|
|
24
|
+
"-l",
|
|
25
|
+
default=LOSS_LIMIT,
|
|
26
|
+
help="Limite de perda diária (ex: -500), default -180.00.",
|
|
27
|
+
)
|
|
28
|
+
@click.option(
|
|
29
|
+
"--lucro",
|
|
30
|
+
is_flag=True,
|
|
31
|
+
default=False,
|
|
32
|
+
help="Exibe o lucro total do dia atualizado e sai.",
|
|
33
|
+
)
|
|
34
|
+
def checar(limite, lucro):
|
|
35
|
+
"""Verifica se o limite de prejuizo foi atingido."""
|
|
36
|
+
if lucro:
|
|
37
|
+
lucro = calcular_lucro_total_dia()
|
|
38
|
+
click.echo(f"Lucro total do dia: {lucro:.2f}")
|
|
39
|
+
log.info(f"Lucro total do dia: {lucro:.2f}")
|
|
40
|
+
return
|
|
41
|
+
|
|
42
|
+
estado = carregar_estado(STATUS_FILE)
|
|
43
|
+
hoje = date.today()
|
|
44
|
+
|
|
45
|
+
if estado["data"] != hoje.isoformat():
|
|
46
|
+
estado["bloqueado"] = False
|
|
47
|
+
|
|
48
|
+
if estado["bloqueado"]:
|
|
49
|
+
click.echo("Bloqueado hoje por risco. Nenhuma ordem deve ser enviada.")
|
|
50
|
+
return
|
|
51
|
+
|
|
52
|
+
if risco_excedido(limite):
|
|
53
|
+
click.echo(
|
|
54
|
+
f"Limite {limite:.2f} excedido. Encerrando posições e bloqueando novas ordens."
|
|
55
|
+
)
|
|
56
|
+
log.info(f"Risco {limite:.2f} excedido, iniciando encerramento de posições.")
|
|
57
|
+
encerrar_todas_posicoes()
|
|
58
|
+
cancelar_todas_ordens()
|
|
59
|
+
estado["bloqueado"] = True
|
|
60
|
+
else:
|
|
61
|
+
click.echo("Dentro do limite de risco.")
|
|
62
|
+
log.info(f"Risco dentro do limite {limite:.2f}")
|
|
63
|
+
|
|
64
|
+
salvar_estado(STATUS_FILE, hoje, estado["bloqueado"])
|
|
65
|
+
|
|
66
|
+
|
|
67
|
+
if __name__ == "__main__":
|
|
68
|
+
checar()
|
|
@@ -1,56 +1,57 @@
|
|
|
1
|
-
"""Comando monitorar - monitora o prejuízo do dia."""
|
|
2
|
-
|
|
3
|
-
import time
|
|
4
|
-
import click
|
|
5
|
-
from datetime import date
|
|
6
|
-
from mtcli.logger import setup_logger
|
|
7
|
-
from
|
|
8
|
-
from
|
|
9
|
-
carregar_estado,
|
|
10
|
-
salvar_estado,
|
|
11
|
-
risco_excedido,
|
|
12
|
-
encerrar_todas_posicoes,
|
|
13
|
-
cancelar_todas_ordens,
|
|
14
|
-
)
|
|
15
|
-
|
|
16
|
-
log = setup_logger()
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
@click.command("monitorar")
|
|
20
|
-
@click.
|
|
21
|
-
@click.option(
|
|
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
|
-
|
|
1
|
+
"""Comando monitorar - monitora o prejuízo do dia."""
|
|
2
|
+
|
|
3
|
+
import time
|
|
4
|
+
import click
|
|
5
|
+
from datetime import date
|
|
6
|
+
from mtcli.logger import setup_logger
|
|
7
|
+
from mtcli_risco.conf import LOSS_LIMIT, STATUS_FILE, INTERVALO
|
|
8
|
+
from mtcli_risco.models.checar_model import (
|
|
9
|
+
carregar_estado,
|
|
10
|
+
salvar_estado,
|
|
11
|
+
risco_excedido,
|
|
12
|
+
encerrar_todas_posicoes,
|
|
13
|
+
cancelar_todas_ordens,
|
|
14
|
+
)
|
|
15
|
+
|
|
16
|
+
log = setup_logger()
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
@click.command("monitorar")
|
|
20
|
+
@click.version_option(package_name="mtcli-risco")
|
|
21
|
+
@click.option("--limite", "-l", default=LOSS_LIMIT, help="Limite de perda diária.")
|
|
22
|
+
@click.option(
|
|
23
|
+
"--intervalo",
|
|
24
|
+
"-i",
|
|
25
|
+
default=INTERVALO,
|
|
26
|
+
help="Intervalo entre verificações (segundos), default 60.",
|
|
27
|
+
)
|
|
28
|
+
def monitorar(limite, intervalo):
|
|
29
|
+
"""Monitora continuamente o risco em tempo real."""
|
|
30
|
+
click.echo(f"Monitorando risco a cada {intervalo}s. Limite: {limite}")
|
|
31
|
+
try:
|
|
32
|
+
while True:
|
|
33
|
+
hoje = date.today()
|
|
34
|
+
estado = carregar_estado(STATUS_FILE)
|
|
35
|
+
|
|
36
|
+
if estado.get("data") != hoje.isoformat():
|
|
37
|
+
estado["bloqueado"] = False
|
|
38
|
+
salvar_estado(STATUS_FILE, hoje, False)
|
|
39
|
+
|
|
40
|
+
if not estado.get("bloqueado") and risco_excedido(limite):
|
|
41
|
+
click.echo(f"Limite {limite} excedido. Encerrando posições.")
|
|
42
|
+
log.info(f"Risco excedido em {limite}. Encerrando posições.")
|
|
43
|
+
encerrar_todas_posicoes()
|
|
44
|
+
cancelar_todas_ordens()
|
|
45
|
+
salvar_estado(STATUS_FILE, hoje, True)
|
|
46
|
+
elif estado.get("bloqueado"):
|
|
47
|
+
click.echo(f"O limite {limite} foi excedido. Sistema bloqueado hoje.")
|
|
48
|
+
else:
|
|
49
|
+
click.echo(f"Dentro do limite {limite}")
|
|
50
|
+
|
|
51
|
+
time.sleep(intervalo)
|
|
52
|
+
except KeyboardInterrupt:
|
|
53
|
+
click.echo("Monitoramento interrompido.")
|
|
54
|
+
|
|
55
|
+
|
|
56
|
+
if __name__ == "__main__":
|
|
57
|
+
monitorar()
|
|
@@ -1,53 +1,54 @@
|
|
|
1
|
-
"""Comando lucro - exibe os diferentes tipos de lucro."""
|
|
2
|
-
|
|
3
|
-
import click
|
|
4
|
-
from mtcli.logger import setup_logger
|
|
5
|
-
from
|
|
6
|
-
obter_lucro_aberto,
|
|
7
|
-
calcular_lucro_realizado,
|
|
8
|
-
calcular_lucro_total_dia,
|
|
9
|
-
)
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
log = setup_logger()
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
@click.command("
|
|
16
|
-
@click.
|
|
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
|
-
click.echo(f"lucro
|
|
49
|
-
click.echo(f"lucro
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
1
|
+
"""Comando lucro - exibe os diferentes tipos de lucro."""
|
|
2
|
+
|
|
3
|
+
import click
|
|
4
|
+
from mtcli.logger import setup_logger
|
|
5
|
+
from mtcli_risco.models.trades_model import (
|
|
6
|
+
obter_lucro_aberto,
|
|
7
|
+
calcular_lucro_realizado,
|
|
8
|
+
calcular_lucro_total_dia,
|
|
9
|
+
)
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
log = setup_logger()
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
@click.command("trades")
|
|
16
|
+
@click.version_option(package_name="mtcli-risco")
|
|
17
|
+
@click.option(
|
|
18
|
+
"--aberto", "-a", is_flag=True, default=False, help="Exibe o lucro aberto."
|
|
19
|
+
)
|
|
20
|
+
@click.option(
|
|
21
|
+
"--realizado",
|
|
22
|
+
"-r",
|
|
23
|
+
is_flag=True,
|
|
24
|
+
default=False,
|
|
25
|
+
help="Exibe o lucro realizado do dia.",
|
|
26
|
+
)
|
|
27
|
+
@click.option(
|
|
28
|
+
"--total", "-t", is_flag=True, default=False, help="Exibe o lucro total do dia."
|
|
29
|
+
)
|
|
30
|
+
def trades(aberto, realizado, total):
|
|
31
|
+
"""Exibe os lucros aberto, realizado e total do dia."""
|
|
32
|
+
lucro_aberto = obter_lucro_aberto()
|
|
33
|
+
lucro_realizado = calcular_lucro_realizado()
|
|
34
|
+
lucro_total = calcular_lucro_total_dia()
|
|
35
|
+
|
|
36
|
+
if aberto:
|
|
37
|
+
click.echo(f"{lucro_aberto:.2f}")
|
|
38
|
+
return
|
|
39
|
+
|
|
40
|
+
if realizado:
|
|
41
|
+
click.echo(f"{lucro_realizado:.2f}")
|
|
42
|
+
return
|
|
43
|
+
|
|
44
|
+
if total:
|
|
45
|
+
click.echo(f"{lucro_total:.2f}")
|
|
46
|
+
return
|
|
47
|
+
|
|
48
|
+
click.echo(f"lucro em aberto {lucro_aberto:.2f}")
|
|
49
|
+
click.echo(f"lucro realizado {lucro_realizado:.2f}")
|
|
50
|
+
click.echo(f"lucro total {lucro_total:.2f}")
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
if __name__ == "__main__":
|
|
54
|
+
trades()
|
|
@@ -1,100 +1,102 @@
|
|
|
1
|
-
"""
|
|
2
|
-
|
|
3
|
-
import MetaTrader5 as mt5
|
|
4
|
-
import json
|
|
5
|
-
import os
|
|
6
|
-
from datetime import date, datetime, time
|
|
7
|
-
from mtcli.logger import setup_logger
|
|
8
|
-
from .
|
|
9
|
-
from
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
log = setup_logger()
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
def carregar_estado(STATUS_FILE):
|
|
16
|
-
"""Carrega o estado do controle de risco."""
|
|
17
|
-
if os.path.exists(STATUS_FILE):
|
|
18
|
-
with open(STATUS_FILE, "r") as f:
|
|
19
|
-
log.info(f"Carregando dados do arquivo {STATUS_FILE}")
|
|
20
|
-
return json.load(f)
|
|
21
|
-
return {"data": None, "bloqueado": False}
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
def salvar_estado(STATUS_FILE, data, bloqueado):
|
|
25
|
-
"""Salva o estado do controle de risco."""
|
|
26
|
-
with open(STATUS_FILE, "w") as f:
|
|
27
|
-
json.dump({"data": data.isoformat(), "bloqueado": bloqueado}, f)
|
|
28
|
-
log.info(f"Salvando o estado: data {data} bloqueado {bloqueado}")
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
def encerrar_todas_posicoes():
|
|
32
|
-
"""Encerra todas as posições abertas."""
|
|
33
|
-
with mt5_conexao():
|
|
34
|
-
positions = mt5.positions_get()
|
|
35
|
-
if not positions:
|
|
36
|
-
log.info("Nenhuma posição aberta para fechar.")
|
|
37
|
-
return
|
|
38
|
-
|
|
39
|
-
with mt5_conexao():
|
|
40
|
-
for pos in positions:
|
|
41
|
-
tipo_oposto = (
|
|
42
|
-
mt5.ORDER_TYPE_SELL
|
|
43
|
-
if pos.type == mt5.ORDER_TYPE_BUY
|
|
44
|
-
else mt5.ORDER_TYPE_BUY
|
|
45
|
-
)
|
|
46
|
-
ordem_fechar = {
|
|
47
|
-
"action": mt5.TRADE_ACTION_DEAL,
|
|
48
|
-
"symbol": pos.symbol,
|
|
49
|
-
"volume": pos.volume,
|
|
50
|
-
"type": tipo_oposto,
|
|
51
|
-
"position": pos.ticket,
|
|
52
|
-
"deviation": 10,
|
|
53
|
-
"magic": 1000,
|
|
54
|
-
"comment": "Fechando posição por limite de risco",
|
|
55
|
-
}
|
|
56
|
-
log.info(
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
log.
|
|
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
|
-
|
|
1
|
+
"""Verifica dados de risco."""
|
|
2
|
+
|
|
3
|
+
import MetaTrader5 as mt5
|
|
4
|
+
import json
|
|
5
|
+
import os
|
|
6
|
+
from datetime import date, datetime, time
|
|
7
|
+
from mtcli.logger import setup_logger
|
|
8
|
+
from .trades_model import calcular_lucro_total_dia
|
|
9
|
+
from mtcli_risco.mt5_context import mt5_conexao
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
log = setup_logger()
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
def carregar_estado(STATUS_FILE):
|
|
16
|
+
"""Carrega o estado do controle de risco."""
|
|
17
|
+
if os.path.exists(STATUS_FILE):
|
|
18
|
+
with open(STATUS_FILE, "r") as f:
|
|
19
|
+
log.info(f"Carregando dados do arquivo {STATUS_FILE}")
|
|
20
|
+
return json.load(f)
|
|
21
|
+
return {"data": None, "bloqueado": False}
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
def salvar_estado(STATUS_FILE, data, bloqueado):
|
|
25
|
+
"""Salva o estado do controle de risco."""
|
|
26
|
+
with open(STATUS_FILE, "w") as f:
|
|
27
|
+
json.dump({"data": data.isoformat(), "bloqueado": bloqueado}, f)
|
|
28
|
+
log.info(f"Salvando o estado: data {data} bloqueado {bloqueado}")
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
def encerrar_todas_posicoes():
|
|
32
|
+
"""Encerra todas as posições abertas."""
|
|
33
|
+
with mt5_conexao():
|
|
34
|
+
positions = mt5.positions_get()
|
|
35
|
+
if not positions:
|
|
36
|
+
log.info("Nenhuma posição aberta para fechar.")
|
|
37
|
+
return
|
|
38
|
+
|
|
39
|
+
with mt5_conexao():
|
|
40
|
+
for pos in positions:
|
|
41
|
+
tipo_oposto = (
|
|
42
|
+
mt5.ORDER_TYPE_SELL
|
|
43
|
+
if pos.type == mt5.ORDER_TYPE_BUY
|
|
44
|
+
else mt5.ORDER_TYPE_BUY
|
|
45
|
+
)
|
|
46
|
+
ordem_fechar = {
|
|
47
|
+
"action": mt5.TRADE_ACTION_DEAL,
|
|
48
|
+
"symbol": pos.symbol,
|
|
49
|
+
"volume": pos.volume,
|
|
50
|
+
"type": tipo_oposto,
|
|
51
|
+
"position": pos.ticket,
|
|
52
|
+
"deviation": 10,
|
|
53
|
+
"magic": 1000,
|
|
54
|
+
"comment": "Fechando posição por limite de risco",
|
|
55
|
+
}
|
|
56
|
+
log.info(
|
|
57
|
+
f"Requizição para encerramento de posições pelo risco: {ordem_fechar}"
|
|
58
|
+
)
|
|
59
|
+
resultado = mt5.order_send(ordem_fechar)
|
|
60
|
+
|
|
61
|
+
if resultado is None:
|
|
62
|
+
log.error("Erro ao enviar ordem: resultado é None.")
|
|
63
|
+
continue
|
|
64
|
+
|
|
65
|
+
if resultado.retcode != mt5.TRADE_RETCODE_DONE:
|
|
66
|
+
log.error(f"Falha ao fechar posição {pos.ticket}: {resultado.retcode}")
|
|
67
|
+
else:
|
|
68
|
+
log.info(f"Posição {pos.ticket} fechada com sucesso.")
|
|
69
|
+
|
|
70
|
+
|
|
71
|
+
def cancelar_todas_ordens():
|
|
72
|
+
"""Cancela todas órdens pendentes."""
|
|
73
|
+
with mt5_conexao():
|
|
74
|
+
ordens = mt5.orders_get()
|
|
75
|
+
if not ordens:
|
|
76
|
+
log.info("Nenhuma ordem pendente para cancelar.")
|
|
77
|
+
return
|
|
78
|
+
|
|
79
|
+
with mt5_conexao():
|
|
80
|
+
for ordem in ordens:
|
|
81
|
+
resultado = mt5.order_delete(ordem.ticket)
|
|
82
|
+
if resultado.retcode != mt5.TRADE_RETCODE_DONE:
|
|
83
|
+
log.error(
|
|
84
|
+
f"Falha ao cancelar ordem {ordem.ticket}: {resultado.retcode}"
|
|
85
|
+
)
|
|
86
|
+
else:
|
|
87
|
+
log.info(f"Ordem {ordem.ticket} cancelada com sucesso.")
|
|
88
|
+
|
|
89
|
+
|
|
90
|
+
def risco_excedido(limite):
|
|
91
|
+
"""Verifica se o loss limit do dia foi atingido."""
|
|
92
|
+
try:
|
|
93
|
+
total_lucro = calcular_lucro_total_dia()
|
|
94
|
+
if total_lucro <= limite:
|
|
95
|
+
log.info(
|
|
96
|
+
f"Limite de prejuízo excedido! prejuízo: {total_lucro:.2f}, limite: {limite:.2f}"
|
|
97
|
+
)
|
|
98
|
+
return True
|
|
99
|
+
return False
|
|
100
|
+
except Exception as e:
|
|
101
|
+
log.error(f"Erro ao verificar risco: {e}")
|
|
102
|
+
return False
|
|
@@ -1,58 +1,77 @@
|
|
|
1
|
-
"""Cálculos do lucro do dia."""
|
|
2
|
-
|
|
3
|
-
import MetaTrader5 as mt5
|
|
4
|
-
from datetime import date, datetime, time
|
|
5
|
-
from mtcli.conecta import conectar, shutdown
|
|
6
|
-
from mtcli.logger import setup_logger
|
|
7
|
-
from
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
log = setup_logger()
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
def
|
|
14
|
-
"""
|
|
15
|
-
hoje = datetime.now().date()
|
|
16
|
-
inicio = datetime.combine(hoje, time(0, 0))
|
|
17
|
-
fim = datetime.combine(hoje, time(23, 59))
|
|
18
|
-
|
|
19
|
-
with mt5_conexao():
|
|
20
|
-
deals = mt5.history_deals_get(inicio, fim)
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
if deals
|
|
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
|
-
|
|
1
|
+
"""Cálculos do lucro do dia."""
|
|
2
|
+
|
|
3
|
+
import MetaTrader5 as mt5
|
|
4
|
+
from datetime import date, datetime, time
|
|
5
|
+
from mtcli.conecta import conectar, shutdown
|
|
6
|
+
from mtcli.logger import setup_logger
|
|
7
|
+
from mtcli_risco.mt5_context import mt5_conexao
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
log = setup_logger()
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
def contar_transacoes_realizadas() -> int:
|
|
14
|
+
"""Conta a quantidade de posições encerradas no dia (transações de fechamento)."""
|
|
15
|
+
hoje = datetime.now().date()
|
|
16
|
+
inicio = datetime.combine(hoje, time(0, 0))
|
|
17
|
+
fim = datetime.combine(hoje, time(23, 59))
|
|
18
|
+
|
|
19
|
+
with mt5_conexao():
|
|
20
|
+
deals = mt5.history_deals_get(inicio, fim)
|
|
21
|
+
|
|
22
|
+
if deals is None:
|
|
23
|
+
log.warning("Nenhum deal retornado.")
|
|
24
|
+
return 0
|
|
25
|
+
|
|
26
|
+
if not isinstance(deals, (list, tuple)):
|
|
27
|
+
deals = [deals]
|
|
28
|
+
|
|
29
|
+
# Types 1 = buy, 2 = sell → usados para fechar posições
|
|
30
|
+
transacoes = [d for d in deals if d.type in (1, 2)]
|
|
31
|
+
|
|
32
|
+
log.info(f"Transações realizadas hoje: {len(transacoes)}")
|
|
33
|
+
return len(transacoes)
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
def calcular_lucro_realizado() -> float:
|
|
37
|
+
"""Calcula o lucro realizado do dia."""
|
|
38
|
+
hoje = datetime.now().date()
|
|
39
|
+
inicio = datetime.combine(hoje, time(0, 0))
|
|
40
|
+
fim = datetime.combine(hoje, time(23, 59))
|
|
41
|
+
|
|
42
|
+
with mt5_conexao():
|
|
43
|
+
deals = mt5.history_deals_get(inicio, fim)
|
|
44
|
+
log.info(f"Deals recebidos: {len(deals) if deals else 0}")
|
|
45
|
+
|
|
46
|
+
if deals is None or len(deals) == 0:
|
|
47
|
+
log.warning("Nenhum deal retornado.")
|
|
48
|
+
return 0.0
|
|
49
|
+
|
|
50
|
+
lucro_realizado = sum(
|
|
51
|
+
deal.profit for deal in deals if deal.entry == mt5.DEAL_ENTRY_OUT
|
|
52
|
+
)
|
|
53
|
+
|
|
54
|
+
log.info(f"Lucro realizado do dia: {lucro_realizado:.2f}")
|
|
55
|
+
return lucro_realizado
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+
def obter_lucro_aberto() -> float:
|
|
59
|
+
"""Obtem o lucro em aberto."""
|
|
60
|
+
with mt5_conexao():
|
|
61
|
+
info = mt5.account_info()
|
|
62
|
+
|
|
63
|
+
lucro_aberto = info.profit if info else 0.0
|
|
64
|
+
log.info(f"lucro aberto: {lucro_aberto:.2f}")
|
|
65
|
+
|
|
66
|
+
return lucro_aberto
|
|
67
|
+
|
|
68
|
+
|
|
69
|
+
def calcular_lucro_total_dia() -> float:
|
|
70
|
+
"""Calcula o lucro total do dia."""
|
|
71
|
+
realizado = calcular_lucro_realizado()
|
|
72
|
+
aberto = obter_lucro_aberto()
|
|
73
|
+
total = realizado + aberto
|
|
74
|
+
log.info(
|
|
75
|
+
f"Lucro realizado: {realizado:.2f}, lucro aberto: {aberto:.2f}, total: {total:.2f}"
|
|
76
|
+
)
|
|
77
|
+
return total
|
mtcli_risco/plugin.py
CHANGED
|
@@ -1,22 +1,11 @@
|
|
|
1
|
-
"""
|
|
1
|
+
"""Registra os comandos do plugin."""
|
|
2
2
|
|
|
3
|
-
import click
|
|
4
3
|
from .commands.checar import checar
|
|
5
4
|
from .commands.monitorar import monitorar
|
|
6
|
-
from .commands.
|
|
5
|
+
from .commands.trades import trades
|
|
7
6
|
|
|
8
7
|
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
pass
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
cli.add_command(checar)
|
|
17
|
-
cli.add_command(monitorar)
|
|
18
|
-
cli.add_command(lucro)
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
if __name__ == "__main__":
|
|
22
|
-
cli()
|
|
8
|
+
def register(cli):
|
|
9
|
+
cli.add_command(checar, name="checar")
|
|
10
|
+
cli.add_command(monitorar, name="monitorar")
|
|
11
|
+
cli.add_command(trades, name="trades")
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
mtcli_risco/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
2
|
+
mtcli_risco/commands/__init__.py,sha256=0AvTSqwccXKX8r9CXkEaPWQ4bj87Bo8y1BFGpMhR9hk,60
|
|
3
|
+
mtcli_risco/commands/checar.py,sha256=nf7oo6DSU19SasWOLgkL0yKsQ6u-BCqob414URWMvDo,1949
|
|
4
|
+
mtcli_risco/commands/monitorar.py,sha256=0eiCaO4TtGmDz6EShnyR6SvIf9l6RJVQIXVXN0S5XeQ,1918
|
|
5
|
+
mtcli_risco/commands/trades.py,sha256=cxiGnWvZ_PNWSvE5ftSDJWBgHwLjeB5yAowRD9LV6bs,1380
|
|
6
|
+
mtcli_risco/conf.py,sha256=5T82aUS9k9zB1I94RGL5xud-0B4iYI6CwXJoUNDotAQ,365
|
|
7
|
+
mtcli_risco/models/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
8
|
+
mtcli_risco/models/checar_model.py,sha256=tv7r-lQdrXhtJo-4vlDUeMNN-FAQ2maUHR-IvccWAWg,3335
|
|
9
|
+
mtcli_risco/models/trades_model.py,sha256=4_kG0jCclKyb4HqLWLvEndvRp4kHexzJwk_0YKIGEog,2204
|
|
10
|
+
mtcli_risco/mt5_context.py,sha256=lA5ZppfRsCvzrxf0Jce1ewZtt2xFln5xnZ1sArk1P4k,190
|
|
11
|
+
mtcli_risco/plugin.py,sha256=gl7JzOyzcO3vHzoJuaZ9z-LBkq20Q5ICMviEuXW-yAk,309
|
|
12
|
+
mtcli_risco-2.2.1.dist-info/entry_points.txt,sha256=LV78AP1S7_X3zuy0HZjdRHh1kp8VM-RvpBCNf0dOiNY,57
|
|
13
|
+
mtcli_risco-2.2.1.dist-info/LICENSE,sha256=IwGE9guuL-ryRPEKi6wFPI_zOhg7zDZbTYuHbSt_SAk,35823
|
|
14
|
+
mtcli_risco-2.2.1.dist-info/METADATA,sha256=QDUUul9lT4N6oXrK4uuuYZv2RbiSvywttC06eQBVM4E,2482
|
|
15
|
+
mtcli_risco-2.2.1.dist-info/WHEEL,sha256=b4K_helf-jlQoXBBETfwnf4B04YC67LOev0jo4fX5m8,88
|
|
16
|
+
mtcli_risco-2.2.1.dist-info/RECORD,,
|
|
@@ -1,16 +0,0 @@
|
|
|
1
|
-
mtcli_risco/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
2
|
-
mtcli_risco/commands/__init__.py,sha256=0AvTSqwccXKX8r9CXkEaPWQ4bj87Bo8y1BFGpMhR9hk,60
|
|
3
|
-
mtcli_risco/commands/checar.py,sha256=GsOJ8jT8hZiLi0qNqUybH7T0bvuoaV1PRjgCyBLTK4o,1806
|
|
4
|
-
mtcli_risco/commands/lucro.py,sha256=rDrAjTpcieU1lBfglnDxgsclPk2Nhgsk3NROo7MsKM4,1256
|
|
5
|
-
mtcli_risco/commands/monitorar.py,sha256=DyKq9GRtvGH1F75Cqbq0ZO0MAeKp5ltBZYpfV7s0VBg,1784
|
|
6
|
-
mtcli_risco/conf.py,sha256=5T82aUS9k9zB1I94RGL5xud-0B4iYI6CwXJoUNDotAQ,365
|
|
7
|
-
mtcli_risco/models/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
8
|
-
mtcli_risco/models/lucro.py,sha256=j5fK7Pfi6MCIFGpy6bblG15lO7sHrm8hBAaHGW5NRwI,1615
|
|
9
|
-
mtcli_risco/models/risco.py,sha256=yQjA95zo9IgY8Rg1Fsjqf-l0014DdvajW-0UM0mSnck,3388
|
|
10
|
-
mtcli_risco/mt5_context.py,sha256=lA5ZppfRsCvzrxf0Jce1ewZtt2xFln5xnZ1sArk1P4k,190
|
|
11
|
-
mtcli_risco/plugin.py,sha256=ykFXflYFVDVoxTuhdqfxDLQpd8J9_SM9VE_Jxgt-A2Y,506
|
|
12
|
-
mtcli_risco-2.1.2.dist-info/entry_points.txt,sha256=pPmBNOystUidnZ4FoPta9-6sM9u7FCneA-4Cm2RMq4s,52
|
|
13
|
-
mtcli_risco-2.1.2.dist-info/LICENSE,sha256=IwGE9guuL-ryRPEKi6wFPI_zOhg7zDZbTYuHbSt_SAk,35823
|
|
14
|
-
mtcli_risco-2.1.2.dist-info/METADATA,sha256=2E6yDGeaOZSUcTHlwbUurbYF_S56b08AmE5do8R78og,2482
|
|
15
|
-
mtcli_risco-2.1.2.dist-info/WHEEL,sha256=b4K_helf-jlQoXBBETfwnf4B04YC67LOev0jo4fX5m8,88
|
|
16
|
-
mtcli_risco-2.1.2.dist-info/RECORD,,
|
|
File without changes
|
|
File without changes
|