Skip to content

Commit

Permalink
#1 Iniciando implementação do relatório de preço médio
Browse files Browse the repository at this point in the history
  • Loading branch information
kl0ck committed Mar 26, 2021
1 parent 8560513 commit a5c6e02
Show file tree
Hide file tree
Showing 5 changed files with 100 additions and 19 deletions.
15 changes: 8 additions & 7 deletions database.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ def __init__(self):

con.commit()

def add(self, operacao):
def addOperacao(self, operacao):
con = self.con
c = con.cursor()

Expand All @@ -23,31 +23,32 @@ def add(self, operacao):

con.commit()

def list(self):
def listarOperacoes(self) -> list:
con = self.con
c = con.cursor()

c.execute("SELECT * FROM operacao")

rows = c.fetchall()

#for r in rows:
# print(r)
i = 0
for r in rows:
rows[i] = converters.toOperacao(r)
i+=1

return rows

def listarPorTicker(self, ticker):
def listarOperacoesPorTicker(self, ticker) -> list:
con = self.con
c = con.cursor()

c.execute("SELECT * FROM operacao WHERE ticker = ? ORDER BY data", (ticker,))
c.execute("SELECT * FROM operacao WHERE ticker = ?", (ticker,))

rows = c.fetchall()

i = 0
for r in rows:
rows[i] = converters.toOperacao(r)
#print(rows[i])
i+=1

return rows
20 changes: 13 additions & 7 deletions leitor.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from rn import RN
from operacao import Operacao
from database import Database
import relatorios

#print(sys.argv)

Expand Down Expand Up @@ -91,14 +92,19 @@ def isEmBranco(txt):
print("Leitura do arquivo " + arquivo + " concluída.")
print()

#print("Operações: ")
#print()
print("===========")
print("RESULTADOS:")
print("===========")

#for o in operacoes:
# print(o)
rn = RN(operacoes)

#pm = rn.precoMedio("EQTL3", "C")
#print("Preço médio = " + str(pm))
print()

rn = RN(operacoes)
pm = rn.precoMedio("B3SA3", "C")
print("Preço médio = " + str(pm))
print("Ativos = " + str(rn.ativos()))

relatorio = relatorios.RelatorioPrecoMedio(rn)
relatorio.txt()

#C:\Users\Carlos\Desktop\operacoes-2019-2021.txt
39 changes: 39 additions & 0 deletions relatorios.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
from rn import RN
from collections import defaultdict

class RelatorioPrecoMedio:

rn = None

def __init__(self, rn):
self.rn = rn

def txt(self):
rn = self.rn
ativos = rn.ativos()

mapOperacoes = defaultdict(list)

for ticker in ativos:
operacoes = rn.listarOperacoesPorTicker(ticker)

for o in operacoes:
mapOperacoes[ticker.upper()].append(o)

#print("mapOperacoes = " + str(mapOperacoes))

print("Preço médio:")
for ticker in ativos:
precoMedioCompra = rn.precoMedio(ticker, 'C')
if precoMedioCompra > 0:
print(f" C {ticker} {precoMedioCompra}")

precoMedioVenda = rn.precoMedio(ticker, 'V')
if precoMedioVenda > 0:
print(f" V {ticker} {precoMedioVenda}")

def pdf(self):
pass

def grafico(self):
pass
40 changes: 37 additions & 3 deletions rn.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from database import Database
from collections import defaultdict
import converters

class RN:
Expand All @@ -8,11 +9,12 @@ class RN:
def __init__(self, operacoes):
self.db = Database()
for o in operacoes:
self.db.add(o)
self.db.addOperacao(o)

def precoMedio(self, ticker, cv):
# Preço médio de compra ou de venda de um ativo.
def precoMedio(self, ticker, cv) -> float:
db = self.db
l = db.listarPorTicker(ticker)
l = db.listarOperacoesPorTicker(ticker)

somatorio = 0

Expand All @@ -25,3 +27,35 @@ def precoMedio(self, ticker, cv):
print(o)

return round(somatorio / l.__len__(), 2)

def listarOperacoes(self) -> list:
return self.db.listarOperacoes()

def listarOperacoesPorTicker(self, ticker) -> list:
return self.db.listarOperacoesPorTicker(ticker)

# Lista todos os ativos existentes nas operações.
def ativos(self) -> list:
operacoes = self.listarOperacoes()
myset = set()

for o in operacoes:
myset.add(o.ticker)

sortedList = list(myset)
sortedList.sort()

return sortedList

# Lista todas as operações feitas com um ativo.
def operacoes(self, ticker):
pass
#operacoes = self.listarOperacoes()
#map = defaultdict(list)
#for o in operacoes:
# map[ticker.upper()].append(o)


# Saldo do ativo (quantidade em carteira).
def saldo(self, ticker):
pass
5 changes: 3 additions & 2 deletions teste.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@
01/02/2021 C 25 B3SA3 50,00 # Clear
01/03/2021 C 25 B3SA3 51,00 # Clear
17/03/2021 C 50 B3SA3 52,68 # Clear
18/03/2021 V 100 LEVE3 19,24 # Mirae
18/03/2021 V 100 LEVE3 19,24 # Mirae

18/03/2021 V 100 LEVE3 19,24
18/03/2021 V 100 LEVE3 21,08 # Mirae

0 comments on commit a5c6e02

Please sign in to comment.