Print data obtained in listen

0

I have the following lib :

module Bitfinex
  module V1::TradesClient

    # Get a list of the most recent trades for the given symbol.
    #
    # @param symbol [IOTUSD] the name of the symbol
    # @param params :timestamp [time] Only show trades at or after this timestamp.
    # @param params :limit_trades [int] Limit the number of trades returned. Must be >= 1.
    # @return [Array]
    # @example:
    #   client.trades
    def trades(symbol="btcusd", params={})
      check_params(params, %i{timestamp limit_trades})
      get("trades/#{symbol}", params).body
    end

    # Listen to the trades using websocket.
    #
    # @param pair [string]
    # @param block [Block] The code to be executed when a new trade is executed
    # @example:
    #   client.listen_trades do |trade|
    #     puts trade.inspect
    #   end
    def listen_trades(pair="BTCUSD", &block)
      raise BlockMissingError unless block_given?
      register_channel pair:pair, channel: 'trades', &block
    end

  end
end

What should I do to:

  • puts client.listen

  • Go adding the amount of sell and buys .

  • gem: link

    doc: link

        
    asked by Jeison Perez 25.10.2017 в 13:39
    source

    1 answer

    0

    It's very simple if you look at the example in your repository as well as the API documentation .

    This is an example (basic) to achieve what you are looking for:

    require 'bitfinex-api-rb'
    
    # Configure the client with the proper KEY/SECRET, you can create a new one from:
    # https://www.bitfinex.com/api
    Bitfinex::Client.configure do |conf|
      conf.api_key = api_key      # sustituye por tu llave pública
      conf.secret  = api_secret   # sustituye por tu llave privada
      conf.websocket_api_endpoint = "wss://api.bitfinex.com/ws"
    end
    
    client = Bitfinex::Client.new
    pair   = "BTCUSD"
    total  = { buy: 0, sell: 0}
    
    client.listen_trades(pair) do |trade|
      if trade[1] == "tu"
        price  = trade[5]
        amount = trade[6]
        type   = amount >= 0 ? :buy : :sell
        total[type] += amount.abs
    
        puts "type: #{type}, amount: #{amount.abs}, price: #{price}, total acum: #{total[type]}"
      end
    end
    
    client.listen!
    

    Look at the documentation that I shared with you so that you can see exactly what the API returns and you can manipulate the information beyond this example.

        
    answered by 25.10.2017 / 17:06
    source