Hello, community.
I am trying to convert myself from old procedures and shells programming to Julia.
I am strugling to learn objects and the Julia paradigm but a lot of old habits come across.
So, I developed a program to create a graphic of daily bitcoin rates and wish to submit to you all and wait for learn how to do it right.
The program is working, not completely commented but I guess most of the people will understand what I did.
By the way, how do I include the code here?
Thanks
Wilson
Update
Thanks to johnh
# First write by Wilson Costrino
# free for use and change, just keep the credits, please
using LibPQ;
#
v_postgres_host = "192.168.xx.yy"; # Input the hostname here
v_postgres_dbname = "testeDB"; # input the db name here
v_postgres_user = "wilson"; # input the postgres user
v_postgres_password = "password"; # input the password for the postgres user here
v_port ="5432"; # input the port number of postgres db here
conn_string = "host=" *v_postgres_host *" port=" *v_port *" dbname=" *v_postgres_dbname *" user=" *v_postgres_user *" password=" *v_postgres_password *"";
using HTTP, JSON, Dates, CSV, Printf, Plots
function pegavalor()
LinhaJson = HTTP.get("http://api.coindesk.com/v1/bpi/currentprice.json")
jsonstruct = String(LinhaJson.body)
jstruct = JSON.parse(jsonstruct)
return jstruct["bpi"]["USD"]["rate_float"]
end
minutos(x) = 60*hour(x) + minute(x) + 1
global iniciando = true
global ArqNomeCompleto = " "
while true
if minutos(now()) == 1 || iniciando
global pontos = [0.0 for i in 1:1440]
global var_ref = [0.0 for i in 1:1440]
global eixox = [i for i in 1:1440]
global variacao = [0.0 for i in 1:1440]
if ArqNomeCompleto != " "
close(arqsaida)
end
nomearq = "cotacoes_btc-"*string(Dates.today())*".csv"
global ArqNomeCompleto = homedir()*"\\"*nomearq
if isfile(ArqNomeCompleto)
dt_hoje = CSV.read(ArqNomeCompleto)
end
global arqsaida = open(ArqNomeCompleto,"a")
global referencia = pegavalor()
global inicio = minutos(now())
if second(now()) > 50
sleep(15)
end
global segundo = second(now())
@printf "\n\nValor de abertura = %8.2f em %d minutos com %d segundos\n\n" referencia inicio segundo
global iniciando = false
Plots.plot(2)
end
valor = pegavalor()
ponto = minutos(now())
datahora = Dates.format(now(),"\'yyyy-mm-dd HH:MM:SS\'")
pontos[ponto] = valor
if ponto != 1
variacao[ponto] = (pontos[ponto-1] != 0 ? 100*(valor-pontos[ponto-1])/pontos[ponto-1] : 0.0)
else
variacao[ponto] = 0.0
end
var_ref[ponto] = 100*(valor-referencia)/referencia
#abre conexĂŁo e insere
conn = LibPQ.Connection(conn_string);
linhasql = "INSERT INTO cotacaobt (datahora, valor) VALUES (" *datahora *"," *string(valor) *");"
LibPQ.execute(
conn,
linhasql
)
close(conn);
c_ponto = @sprintf("%4d",ponto)
c_valor = @sprintf("%8.2f",valor)
c_varia = @sprintf("%6.2f",variacao[ponto])
c_refer = @sprintf("%6.2f",var_ref[ponto])
print("Ponto ")
printstyled(c_ponto,color=:light_cyan)
print(", Valor ")
printstyled(c_valor,color=:light_yellow)
print(", Variacao ")
if variacao[ponto] >= 0.0
printstyled(c_varia,color=:light_green)
else
printstyled(c_varia, color=:light_red)
end
print(", Referencia ")
if var_ref[ponto] >= 0.0
printstyled(c_refer,color=:light_green)
else
printstyled(c_refer,color=:light_red)
end
println()
println(arqsaida, string(Dates.today()), " ",c_ponto," ", c_valor," ", c_varia," ", c_refer)
flush(arqsaida)
subeixox = eixox[inicio:ponto]
sub_ref = var_ref[inicio:ponto]
sub_var = variacao[inicio:ponto]
Plots.plot(subeixox,sub_var,label="Variacao %",linewidth=3,size=(800,450))
Plots.display(Plots.plot!(subeixox,sub_ref, label="Referencia %",linewidth=3,title="Bitcoin inicial a US\$ $referencia em $inicio"))
sleep(1)
while second(now()) != segundo
sleep(0.5)
end
end