class Robinhood

Attributes

errors[RW]

Public Class Methods

new() click to toggle source

<<< NOTES

# File lib/robinhood_rails/robinhood.rb, line 20
def initialize
end

Public Instance Methods

accounts() click to toggle source
# File lib/robinhood_rails/robinhood.rb, line 52
def accounts
  raw_response = HTTParty.get(endpoints[:accounts], headers: headers)
  JSON.parse(raw_response.body)
end
buy(symbol, instrument_id, price, quantity) click to toggle source
# File lib/robinhood_rails/robinhood.rb, line 72
def buy(symbol, instrument_id, price, quantity)
  raw_response = HTTParty.post(
    endpoints[:orders],
    body: {
      'account' => "https://api.robinhood.com/accounts/#{ENV['ROBINHOOD_ACCOUNT_NUMBER']}/",
      'instrument' => "https://api.robinhood.com/instruments/#{instrument_id}/",
      'price' => price,
      'quantity' => quantity,
      'side' => "buy",
      'symbol' => symbol,
      'time_in_force' => 'gfd',
      'trigger' => 'immediate',
      'type' => 'market'
    }.as_json,
    headers: headers
  )
end
cancel_order(order_id) click to toggle source
# File lib/robinhood_rails/robinhood.rb, line 162
def cancel_order(order_id)
  raw_response = HTTParty.post("https://api.robinhood.com/orders/#{order_id}/cancel/", headers: headers)
  raw_response.code == 200
end
instruments(symbol) click to toggle source
# File lib/robinhood_rails/robinhood.rb, line 62
def instruments(symbol)
  raw_response = HTTParty.get(endpoints[:instruments], query: {'query' => symbol.upcase}, headers: headers)
  JSON.parse(raw_response.body)
end
investment_profile() click to toggle source
# File lib/robinhood_rails/robinhood.rb, line 41
def investment_profile
  raw_response = HTTParty.get(endpoints[:investment_profile], headers: headers)
  JSON.parse(raw_response.body)
end
limit_buy(symbol, instrument_id, price, quantity) click to toggle source
# File lib/robinhood_rails/robinhood.rb, line 90
def limit_buy(symbol, instrument_id, price, quantity)
  raw_response = HTTParty.post(
    endpoints[:orders],
    body: {
      'account' => "https://api.robinhood.com/accounts/#{ENV['ROBINHOOD_ACCOUNT_NUMBER']}/",
      'instrument' => "https://api.robinhood.com/instruments/#{instrument_id}/",
      'price' => price,
      'quantity' => quantity,
      'side' => "buy",
      'symbol' => symbol,
      'time_in_force' => 'gfd',
      'trigger' => 'immediate',
      'type' => 'limit'
    }.as_json,
    headers: headers
  )
end
limit_sell(symbol, instrument_id, price, quantity) click to toggle source
# File lib/robinhood_rails/robinhood.rb, line 126
def limit_sell(symbol, instrument_id, price, quantity)
  raw_response = HTTParty.post(
    endpoints[:orders],
    body: {
      'account' => "https://api.robinhood.com/accounts/#{ENV['ROBINHOOD_ACCOUNT_NUMBER']}/",
      'instrument' => "https://api.robinhood.com/instruments/#{instrument_id}/",
      'price' => price,
      'quantity' => quantity,
      'side' => "sell",
      'symbol' => symbol,
      'time_in_force' => 'gfd',
      'trigger' => 'immediate',
      'type' => 'limit'
    }.as_json,
    headers: headers
  )
end
login(username, password) click to toggle source
# File lib/robinhood_rails/robinhood.rb, line 23
def login(username, password)
  raw_response = HTTParty.post(
    endpoints[:login],
    body: {
      'password' => password,
      'username'=> username
    }.as_json,
    headers: headers
  )
  response = JSON.parse(raw_response.body)
  if response["non_field_errors"]
    puts response["non_field_errors"]
    false
  elsif response["token"]
    puts response['token']
  end
end
orders(order_id=nil) click to toggle source
# File lib/robinhood_rails/robinhood.rb, line 46
def orders(order_id=nil)
  url = order_id ? "#{endpoints[:orders]}#{order_id}" : endpoints[:orders]
  raw_response = HTTParty.get(url, headers: headers)
  JSON.parse(raw_response.body)
end
portfolio() click to toggle source
# File lib/robinhood_rails/robinhood.rb, line 57
def portfolio
  raw_response = HTTParty.get("https://api.robinhood.com/accounts/#{ENV['ROBINHOOD_ACCOUNT_NUMBER']}/portfolio/", headers: headers)
  JSON.parse(raw_response.body)
end
positions(instrument_id) click to toggle source
# File lib/robinhood_rails/robinhood.rb, line 167
def positions(instrument_id)
  raw_response = HTTParty.get("https://api.robinhood.com/accounts/#{ENV['ROBINHOOD_ACCOUNT_NUMBER']}/positions/#{instrument_id}/", headers: headers)
  JSON.parse(raw_response.body)
end
quote(symbol) click to toggle source
# File lib/robinhood_rails/robinhood.rb, line 67
def quote(symbol)
  raw_response = HTTParty.post("https://api.robinhood.com/quotes/#{symbol}/", headers: headers)
  JSON.parse(raw_response.body)
end
sell(symbol, instrument_id, price, quantity) click to toggle source
# File lib/robinhood_rails/robinhood.rb, line 108
def sell(symbol, instrument_id, price, quantity)
  raw_response = HTTParty.post(
    endpoints[:orders],
    body: {
      'account' => "https://api.robinhood.com/accounts/#{ENV['ROBINHOOD_ACCOUNT_NUMBER']}/",
      'instrument' => "https://api.robinhood.com/instruments/#{instrument_id}/",
      'price' => price,
      'quantity' => quantity,
      'side' => "sell",
      'symbol' => symbol,
      'time_in_force' => 'gfd',
      'trigger' => 'immediate',
      'type' => 'market'
    }.as_json,
    headers: headers
  )
end
stop_loss_sell(symbol, instrument_id, price, quantity) click to toggle source
# File lib/robinhood_rails/robinhood.rb, line 144
def stop_loss_sell(symbol, instrument_id, price, quantity)
  raw_response = HTTParty.post(
    endpoints[:orders],
    body: {
      'account' => "https://api.robinhood.com/accounts/#{ENV['ROBINHOOD_ACCOUNT_NUMBER']}/",
      'instrument' => "https://api.robinhood.com/instruments/#{instrument_id}/",
      'stop_price' => price,
      'quantity' => quantity,
      'side' => "sell",
      'symbol' => symbol,
      'time_in_force' => 'gtc',
      'trigger' => 'stop',
      'type' => 'market'
    }.as_json,
    headers: headers
  )
end

Private Instance Methods

endpoints() click to toggle source
# File lib/robinhood_rails/robinhood.rb, line 174
def endpoints
  {
    login:  'https://api.robinhood.com/api-token-auth/',
    investment_profile: 'https://api.robinhood.com/user/investment_profile/',
    accounts: 'https://api.robinhood.com/accounts/',
    ach_iav_auth: 'https://api.robinhood.com/ach/iav/auth/',
    ach_relationships:  'https://api.robinhood.com/ach/relationships/',
    ach_transfers:'https://api.robinhood.com/ach/transfers/',
    applications: 'https://api.robinhood.com/applications/',
    dividends:  'https://api.robinhood.com/dividends/',
    edocuments: 'https://api.robinhood.com/documents/',
    instruments:  'https://api.robinhood.com/instruments/',
    margin_upgrade:  'https://api.robinhood.com/margin/upgrades/',
    markets:  'https://api.robinhood.com/markets/',
    notifications:  'https://api.robinhood.com/notifications/',
    orders: 'https://api.robinhood.com/orders/',
    password_reset: 'https://api.robinhood.com/password_reset/request/',
    quotes: 'https://api.robinhood.com/quotes/',
    document_requests:  'https://api.robinhood.com/upload/document_requests/',
    user: 'https://api.robinhood.com/user/',
    watchlists: 'https://api.robinhood.com/watchlists/'
  }
end
headers() click to toggle source
# File lib/robinhood_rails/robinhood.rb, line 198
def headers
  @headers ||= {
    'Accept' => '*/*',
    'Accept-Encoding' => 'gzip, deflate',
    'Accept-Language' => 'en;q=1, fr;q=0.9, de;q=0.8, ja;q=0.7, nl;q=0.6, it;q=0.5',
    'Content-Type' => 'application/x-www-form-urlencoded; charset=utf-8',
    'X-Robinhood-API-Version' => '1.0.0',
    'Connection' => 'keep-alive',
    'User-Agent' => 'Robinhood/823 (iPhone; iOS 7.1.2; Scale/2.00)',
    "Authorization" => "Token #{ENV["ROBINHOOD_SECRET_TOKEN"]}"
  }
end