Advertisement
Guest User

Untitled

a guest
Jul 23rd, 2018
153
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.16 KB | None | 0 0
  1.  
  2. from Robinhood import Robinhood
  3. my_trader = Robinhood()
  4. logged_in = my_trader.login(username="xxxxxxx", password="xxxxxxxxx")
  5. stock_instrument = my_trader.instruments("GEVO")[0]
  6. quote_info = my_trader.quote_data("GEVO")
  7. buy_order = my_trader.submit_order(order_type="limit",trigger='immediate',side='buy',instrument_URL='',symbol='aapl',price=10.60,quantity=1)
  8.  
  9.  
  10.  
  11. objective: would like to place limit order with robinhood api wrapper
  12.  
  13. link: https://github.com/Jamonek/Robinhood/tree/master/Robinhood
  14.  
  15. error:
  16. File "rr.py", line 6, in <module>
  17. buy_order = my_trader.submit_order(order_type="limit",trigger='immediate',side='buy',instrument_URL='',symbol='aapl',price=10.60,quantity=1)
  18. File "/usr/local/lib/python3.6/dist-packages/Robinhood-1.0.1-py3.6.egg/Robinhood/Robinhood.py", line 1319, in submit_order
  19. File "/usr/local/lib/python3.6/dist-packages/Robinhood-1.0.1-py3.6.egg/Robinhood/Robinhood.py", line 585, in get_account
  20. IndexError: list index out of range
  21.  
  22.  
  23.  
  24.  
  25.  
  26. snippet from wrapper:
  27.  
  28. ######################################################################
  29. # PLACE ORDER
  30. ###########################################################################
  31.  
  32. def place_order(self,
  33. instrument,
  34. quantity=1,
  35. bid_price=0.0,
  36. transaction=None,
  37. trigger='immediate',
  38. order='market',
  39. time_in_force='gfd'):
  40. """Place an order with Robinhood
  41. Notes:
  42. OMFG TEST THIS PLEASE!
  43. Just realized this won't work since if type is LIMIT you need to use "price" and if
  44. a STOP you need to use "stop_price". Oops.
  45. Reference: https://github.com/sanko/Robinhood/blob/master/Order.md#place-an-order
  46. Args:
  47. instrument (dict): the RH URL and symbol in dict for the instrument to be traded
  48. quantity (int): quantity of stocks in order
  49. bid_price (float): price for order
  50. transaction (:enum:`Transaction`): BUY or SELL enum
  51. trigger (:enum:`Trigger`): IMMEDIATE or STOP enum
  52. order (:enum:`Order`): MARKET or LIMIT
  53. time_in_force (:enum:`TIME_IN_FORCE`): GFD or GTC (day or until cancelled)
  54. Returns:
  55. (:obj:`requests.request`): result from `orders` put command
  56. """
  57.  
  58. if isinstance(transaction, str):
  59. transaction = Transaction(transaction)
  60.  
  61. if not bid_price:
  62. bid_price = self.quote_data(instrument['symbol'])['bid_price']
  63.  
  64. payload = {
  65. 'account': self.get_account()['url'],
  66. 'instrument': unquote(instrument['url']),
  67. 'price': float(bid_price),
  68. 'quantity': quantity,
  69. 'side': transaction.name.lower(),
  70. 'symbol': instrument['symbol'],
  71. 'time_in_force': time_in_force.lower(),
  72. 'trigger': trigger,
  73. 'type': order.lower()
  74. }
  75.  
  76. #data = 'account=%s&instrument=%s&price=%f&quantity=%d&side=%s&symbol=%s#&time_in_force=gfd&trigger=immediate&type=market' % (
  77. # self.get_account()['url'],
  78. # urllib.parse.unquote(instrument['url']),
  79. # float(bid_price),
  80. # quantity,
  81. # transaction,
  82. # instrument['symbol']
  83. #)
  84.  
  85. res = self.session.post(endpoints.orders(), data=payload, timeout=15)
  86. res.raise_for_status()
  87.  
  88. return res
  89.  
  90.  
  91. def place_buy_order(self,
  92. instrument,
  93. quantity,
  94. bid_price=0.0):
  95. """Wrapper for placing buy orders
  96. Args:
  97. instrument (dict): the RH URL and symbol in dict for the instrument to be traded
  98. quantity (int): quantity of stocks in order
  99. bid_price (float): price for order
  100. Returns:
  101. (:obj:`requests.request`): result from `orders` put command
  102. """
  103.  
  104. transaction = Transaction.BUY
  105.  
  106. return self.place_order(instrument, quantity, bid_price, transaction)
  107.  
  108.  
  109. def place_sell_order(self,
  110. instrument,
  111. quantity,
  112. bid_price=0.0):
  113. """Wrapper for placing sell orders
  114. Args:
  115. instrument (dict): the RH URL and symbol in dict for the instrument to be traded
  116. quantity (int): quantity of stocks in order
  117. bid_price (float): price for order
  118. Returns:
  119. (:obj:`requests.request`): result from `orders` put command
  120. """
  121.  
  122. transaction = Transaction.SELL
  123.  
  124. return self.place_order(instrument, quantity, bid_price, transaction)
  125.  
  126. # Methods below here are a complete rewrite for buying and selling
  127. # These are new. Use at your own risk!
  128.  
  129. def place_market_buy_order(self,
  130. instrument_URL=None,
  131. symbol=None,
  132. time_in_force=None,
  133. quantity=None):
  134. """Wrapper for placing market buy orders
  135. Notes:
  136. If only one of the instrument_URL or symbol are passed as
  137. arguments the other will be looked up automatically.
  138. Args:
  139. instrument_URL (str): The RH URL of the instrument
  140. symbol (str): The ticker symbol of the instrument
  141. time_in_force (str): 'GFD' or 'GTC' (day or until cancelled)
  142. quantity (int): Number of shares to buy
  143. Returns:
  144. (:obj:`requests.request`): result from `orders` put command
  145. """
  146. return(self.submit_order(order_type='market',
  147. trigger='immediate',
  148. side='buy',
  149. instrument_URL=instrument_URL,
  150. symbol=symbol,
  151. time_in_force=time_in_force,
  152. quantity=quantity))
  153.  
  154. def place_limit_buy_order(self,
  155. instrument_URL=None,
  156. symbol=None,
  157. time_in_force=None,
  158. price=None,
  159. quantity=None):
  160. """Wrapper for placing limit buy orders
  161. Notes:
  162. If only one of the instrument_URL or symbol are passed as
  163. arguments the other will be looked up automatically.
  164. Args:
  165. instrument_URL (str): The RH URL of the instrument
  166. symbol (str): The ticker symbol of the instrument
  167. time_in_force (str): 'GFD' or 'GTC' (day or until cancelled)
  168. price (float): The max price you're willing to pay per share
  169. quantity (int): Number of shares to buy
  170. Returns:
  171. (:obj:`requests.request`): result from `orders` put command
  172. """
  173. return(self.submit_order(order_type='limit',
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement