Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from Robinhood import Robinhood
- my_trader = Robinhood()
- logged_in = my_trader.login(username="xxxxxxx", password="xxxxxxxxx")
- stock_instrument = my_trader.instruments("GEVO")[0]
- quote_info = my_trader.quote_data("GEVO")
- buy_order = my_trader.submit_order(order_type="limit",trigger='immediate',side='buy',instrument_URL='',symbol='aapl',price=10.60,quantity=1)
- objective: would like to place limit order with robinhood api wrapper
- link: https://github.com/Jamonek/Robinhood/tree/master/Robinhood
- error:
- File "rr.py", line 6, in <module>
- buy_order = my_trader.submit_order(order_type="limit",trigger='immediate',side='buy',instrument_URL='',symbol='aapl',price=10.60,quantity=1)
- File "/usr/local/lib/python3.6/dist-packages/Robinhood-1.0.1-py3.6.egg/Robinhood/Robinhood.py", line 1319, in submit_order
- File "/usr/local/lib/python3.6/dist-packages/Robinhood-1.0.1-py3.6.egg/Robinhood/Robinhood.py", line 585, in get_account
- IndexError: list index out of range
- snippet from wrapper:
- ######################################################################
- # PLACE ORDER
- ###########################################################################
- def place_order(self,
- instrument,
- quantity=1,
- bid_price=0.0,
- transaction=None,
- trigger='immediate',
- order='market',
- time_in_force='gfd'):
- """Place an order with Robinhood
- Notes:
- OMFG TEST THIS PLEASE!
- Just realized this won't work since if type is LIMIT you need to use "price" and if
- a STOP you need to use "stop_price". Oops.
- Reference: https://github.com/sanko/Robinhood/blob/master/Order.md#place-an-order
- Args:
- instrument (dict): the RH URL and symbol in dict for the instrument to be traded
- quantity (int): quantity of stocks in order
- bid_price (float): price for order
- transaction (:enum:`Transaction`): BUY or SELL enum
- trigger (:enum:`Trigger`): IMMEDIATE or STOP enum
- order (:enum:`Order`): MARKET or LIMIT
- time_in_force (:enum:`TIME_IN_FORCE`): GFD or GTC (day or until cancelled)
- Returns:
- (:obj:`requests.request`): result from `orders` put command
- """
- if isinstance(transaction, str):
- transaction = Transaction(transaction)
- if not bid_price:
- bid_price = self.quote_data(instrument['symbol'])['bid_price']
- payload = {
- 'account': self.get_account()['url'],
- 'instrument': unquote(instrument['url']),
- 'price': float(bid_price),
- 'quantity': quantity,
- 'side': transaction.name.lower(),
- 'symbol': instrument['symbol'],
- 'time_in_force': time_in_force.lower(),
- 'trigger': trigger,
- 'type': order.lower()
- }
- #data = 'account=%s&instrument=%s&price=%f&quantity=%d&side=%s&symbol=%s#&time_in_force=gfd&trigger=immediate&type=market' % (
- # self.get_account()['url'],
- # urllib.parse.unquote(instrument['url']),
- # float(bid_price),
- # quantity,
- # transaction,
- # instrument['symbol']
- #)
- res = self.session.post(endpoints.orders(), data=payload, timeout=15)
- res.raise_for_status()
- return res
- def place_buy_order(self,
- instrument,
- quantity,
- bid_price=0.0):
- """Wrapper for placing buy orders
- Args:
- instrument (dict): the RH URL and symbol in dict for the instrument to be traded
- quantity (int): quantity of stocks in order
- bid_price (float): price for order
- Returns:
- (:obj:`requests.request`): result from `orders` put command
- """
- transaction = Transaction.BUY
- return self.place_order(instrument, quantity, bid_price, transaction)
- def place_sell_order(self,
- instrument,
- quantity,
- bid_price=0.0):
- """Wrapper for placing sell orders
- Args:
- instrument (dict): the RH URL and symbol in dict for the instrument to be traded
- quantity (int): quantity of stocks in order
- bid_price (float): price for order
- Returns:
- (:obj:`requests.request`): result from `orders` put command
- """
- transaction = Transaction.SELL
- return self.place_order(instrument, quantity, bid_price, transaction)
- # Methods below here are a complete rewrite for buying and selling
- # These are new. Use at your own risk!
- def place_market_buy_order(self,
- instrument_URL=None,
- symbol=None,
- time_in_force=None,
- quantity=None):
- """Wrapper for placing market buy orders
- Notes:
- If only one of the instrument_URL or symbol are passed as
- arguments the other will be looked up automatically.
- Args:
- instrument_URL (str): The RH URL of the instrument
- symbol (str): The ticker symbol of the instrument
- time_in_force (str): 'GFD' or 'GTC' (day or until cancelled)
- quantity (int): Number of shares to buy
- Returns:
- (:obj:`requests.request`): result from `orders` put command
- """
- return(self.submit_order(order_type='market',
- trigger='immediate',
- side='buy',
- instrument_URL=instrument_URL,
- symbol=symbol,
- time_in_force=time_in_force,
- quantity=quantity))
- def place_limit_buy_order(self,
- instrument_URL=None,
- symbol=None,
- time_in_force=None,
- price=None,
- quantity=None):
- """Wrapper for placing limit buy orders
- Notes:
- If only one of the instrument_URL or symbol are passed as
- arguments the other will be looked up automatically.
- Args:
- instrument_URL (str): The RH URL of the instrument
- symbol (str): The ticker symbol of the instrument
- time_in_force (str): 'GFD' or 'GTC' (day or until cancelled)
- price (float): The max price you're willing to pay per share
- quantity (int): Number of shares to buy
- Returns:
- (:obj:`requests.request`): result from `orders` put command
- """
- return(self.submit_order(order_type='limit',
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement