Skip to content

Instantly share code, notes, and snippets.

@prdn
Created September 13, 2019 09:55
Show Gist options
  • Save prdn/ef14821bbf8b0a5c5c288a6cd31bcefe to your computer and use it in GitHub Desktop.
Save prdn/ef14821bbf8b0a5c5c288a6cd31bcefe to your computer and use it in GitHub Desktop.
vwap-test-rb
# frozen_string_literal: true
def http_get(url)
HTTParty.get(url)
rescue => e
-1
end
# takes in a string pair and timeframe in minutes to use to calculate vwap
# example vwap_bittrex("BTC-LTC", 15)
# Note string pair must be a valid pair for the endpoint, example: https://api.bittrex.com/api/v1.1/public/getmarkethistory?market=BTC-LTC
def vwap_bittrex(pair, tfm)
begin
resp = http_get("https://api.bittrex.com/api/v1.1/public/getmarkethistory?market=#{pair}")
vwap = 0
status = false
tnow = Time.now
first = tnow
if resp['status']
tv = 0
tdt = 0
resp['result'].each do |trade|
ts = Time.parse(trade['TimeStamp'])
next unless ts >= tnow - tfm.minute
tv += trade['Quantity']
tdt += trade['Total']
first = ts if ts < first
end
vwap = tdt / tv
msg = 'complete'
status = true
else
msg = resp['message']
end
rescue => e
msg = "error calculating bittrex vwap: #{e.message}"
vwap = 0
end
{ 'status': status, 'vwap': vwap, 'msg': msg, 'int': tfm, 'first': first, 'last': tnow }
end
# takes in a string pair and timeframe in minutes to use to calculate vwap
# example vwap_kraken("LTCXBT", 15)
# Note string 'pair' must be a valid pair for the endpoint, example: https://api.kraken.com/0/public/Trades?pair=LTCXBT
# for complete listing of offered pairs see: https://api.kraken.com/0/public/AssetPairs
def vwap_kraken(pair, tfm)
begin
resp = http_get("https://api.kraken.com/0/public/Trades?pair=#{pair}")
vwap = 0
status = false
ltrade = Time.now
first = ltrade
if resp['error'].blank?
tv = 0
tdt = 0
ltrade = Time.at(resp['result']['last'].to_d / 1e9)
key = resp['result'].keys
key.delete('last')
key = key[0]
resp['result'][key].each do |trade|
price = trade[0].to_d
vol = trade[1].to_d
ts = Time.at(trade[2])
next unless ts >= ltrade - tfm.minute
tv += vol
tdt += (price * vol)
first = ts if ts < first
end
vwap = tdt / tv
msg = 'complete'
status = true
else
msg = resp['error']
end
rescue => e
msg = "error calculating kraken vwap: #{e.message}"
vwap = 0
end
{ 'status': status, 'vwap': vwap, 'msg': msg, 'int': tfm, 'first': first, 'last': ltrade }
end
# takes in a string pair and timeframe in minutes to use to calculate vwap
# example vwap_binance("LTCBTC", 15)
# Note string pair must be a valid pair for the endpoint, example: https://api.binance.com/api/v1/trades?symbol=LTCBTC
def vwap_binance(pair, tfm)
begin
resp = http_get("https://api.binance.com/api/v1/trades?symbol=#{pair}")
vwap = 0
status = false
tnow = Time.now
first = tnow
if resp.code == 200 && resp.parsed_response.is_a?(Array)
tv = 0
tdt = 0
resp.parsed_response.each do |trade|
price = trade['price'].to_d
vol = trade['qty'].to_d
ts = Time.at(trade['time'] / 1e3)
next unless ts >= tnow - tfm.minute
tv += vol
tdt += (price * vol)
first = ts if ts < first
end
vwap = tdt / tv
msg = 'complete'
status = true
else
msg = resp['msg']
end
rescue => e
msg = "error calculating bittrex vwap: #{e.message}"
vwap = 0
end
{ 'status': status, 'vwap': vwap, 'msg': msg, 'int': tfm, 'first': first, 'last': tnow }
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment