busca possibilidades de arbitragem entre corretoras
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 

156 lines
3.7 KiB

require "json"
require "http/client"
require "openssl/hmac"
require "terminal_table"
require "digest/md5"
require "colorize"
require "./order"
require "./config"
module Helper
def f(el)
el.to_s.to_f64
end
def z(time)
return time < 10 ? "0#{time}" : time
end
def ago(utc)
date = Time.utc unless utc
unix = utc.to_s.scan(/\d{10}/)
if !unix.empty? # unix time!
date = Time.unix(unix.[0][0].to_i64)
else
date = Time.parse(utc, "%Y-%m-%dT%H:%M:%S.%6N", Time::Location::UTC)
end
span = (Time.utc - date)
days = span.days > 0 ? "#{span.days}d" : ""
"#{days}#{z span.hours}h#{z span.minutes}m#{z span.seconds}s#{span.milliseconds}"
end
puts line
puts "\t Arbitro".colorize(:blue)
puts line
def line
"=================================".colorize(:blue)
end
def log(str)
pp str if Config.params[:log]
end
# finish! if interrupted
# [Signal::QUIT, Signal::ABRT, Signal::INT, Signal::KILL, Signal::TERM].each do |s|
# s.trap do
# puts "=> Interrupted!".colorize(:red)
# finish!
# end
# end
def get_json(url, path)
# puts "http://51.15.63.128:2020/?url=#{url}#{path}"
log "=> #{url}#{path}".colorize(:yellow)
md5 = Digest::MD5.hexdigest("#{url}#{path}")
cache = "cache/#{md5}.json"
json = ""
begin
if Config.params[:cache] && File.exists?(cache)
json = File.read(cache)
log md5
else
json = HTTP::Client.get("#{url}#{path}").body
File.write(cache, json)
end
rescue e
puts "error: #{e.message}".colorize(:red)
end
JSON.parse(json)
end
def get_last_eur_brl
get_json("https://economia.awesomeapi.com.br", "/eur")[0]["ask"].to_s.to_f64
# pctChange
end
def to_eur(f64)
f64 / f(Config.params[:eur_brl])
end
def test_colors
colors = [:black, :red, :green, :yellow, :blue, :magenta, :cyan, :light_gray, :dark_gray, :light_red, :light_green, :light_yellow, :light_blue, :light_magenta, :light_cyan, :white]
colors.each do |c1|
puts "color #{c1}".colorize(c1)
colors.each do |c2|
puts "fore #{c1} | back #{c2}".colorize.fore(c1).back(c2)
end
end
end
def profit_perct(x, por)
(x.to_f64 * por.to_f64/100).round(2).to_s
end
def percentage(x, y)
x, y = x.to_f64, y.to_f64
pct = x < y ? ((y - x) / x) * 100.0 : - ((x - y) / y) * 100.0
pct = 0.0 if pct.infinite? || pct.nan?
pct.round(4)
end
def as_time(time, tmz=false)
parsed_time = Time.parse(time.to_s, "%Ft%T.%L", Time::Location::UTC)
tmz ? parsed_time + 2.hours : parsed_time # TMZ = 2.hours # athens
end
def elapsed_time
as_time_ago(Time.utc - as_time(Config.params[:start_time]))
end
def as_time_ago(span)
h = span.hours > 0 ? "#{span.hours}h" : ""
m = span.minutes > 0 ? "#{span.minutes}m" : ""
"#{h}#{m}#{span.seconds}s#{span.milliseconds}"
end
def as_dolar(value)
"$#{value.round(2)}"
end
# def finish!
# elapsed = Time.utc - as_time(Config.params[:start_time])
# line
# puts "=> #{Broker.requests_count} requests in #{as_time_ago(elapsed)}".colorize(:yellow)
# line
# exit
# end
def color(float : Float64)
float > 0 ? float.to_s.colorize(:green) : float.to_s.colorize(:red)
end
def now
Time.utc.to_s("%Ft%T.%L")
end
def save_json(json, file)
File.open("log/#{file.to_s}.json", "w") do |f|
f.write("#{json.to_pretty_json}\n".to_slice)
end
end
end
include Helper
class Logger
def self.log(msg, file=:renda, error=false)
File.open("log/#{file.to_s}.log", "a+") do |f|
stamped = "#{now} #{msg}"
puts stamped.colorize(error ? :red : :yellow) if Config.params[:log] || error
f.write("#{stamped}\n".to_slice)
end
end
end