jmfernandes / robin_stocks

This is a library to use with Robinhood Financial App. It currently supports trading crypto-currencies, options, and stocks. In addition, it can be used to get real time ticker information, assess the performance of your portfolio, and can also get tax documents, total dividends paid, and more. More info at
http://www.robin-stocks.com
MIT License
1.69k stars 458 forks source link

Can anyone give an example of setting a stop loss price while either buying a new stock or an existing position? #113

Open bspallholtz opened 4 years ago

bspallholtz commented 4 years ago

Here is my example code

def set_slp():
  holdings = robin_stocks.build_holdings()
  for k in holdings:
      symbol = k
      quantity = float(holdings[k]['quantity'])
      quantity = round(quantity, 0)
      quantity = int(quantity)
      current_return = float(holdings[k]['percent_change'])
      if current_return < -10:
          cp = get_cp(symbol)
      if main(symbol) is True:
          stopPrice = cp * .9
      else:
          stopPrice = cp * .97
      stopPrice = round(stopPrice, 2)
      stopPrice = 6.55
      print("I would try and set SLP for %s at %f for quantity %i" % ( symbol, stopPrice, quantity))
      order = robin_stocks.orders.order_sell_stop_loss(symbol, quantity, stopPrice, timeInForce='gtc', extendedHours=True)
      order = robin_stocks.orders.order_buy_stop_loss(symbol, quantity, stopPrice, timeInForce='gtc', extendedHours=False)
      order = robin_stocks.orders.order_sell_stop_loss(symbol, quantity, stopPrice, timeInForce='gtc', extendedHours=False)
      print(order)

I have tried several variations and can't get any to work

Output I get

{'non_field_errors': ['Stop market sell order requested, but price provided.']}
jmfernandes commented 4 years ago

There was a mistake in the code. I fixed it on github but not on pypi. You can clone or fork the github repo, pull the changes, and install with pip install .

Don't forget the period after pip install