robin_stocks icon indicating copy to clipboard operation
robin_stocks copied to clipboard

Trailing Stop loss function - payload issue

Open Robiemaan opened this issue 1 year ago • 0 comments

Has anyone gotten the trailing stop loss function to work?

import robin_stocks as r
login = r.robinhood.login([uname],[pw],expiresIn=1000000)
sellorder = r.robinhood.orders.order_sell_trailing_stop(symbol='AAPL', quantity=1, trailAmount=5, trailType='percentage', timeInForce='gtc', jsonify=True)

It returns:

WARNING: priceType should be "ask_price" or "bid_price". You entered "True"
404 Client Error: Not Found for url: https://api.robinhood.com/accounts/gtc
{'account': ['This field may not be null.'],
 'time_in_force': ['"False" is not a valid choice.']}

Clearly there's something wrong with how the parameters are handled within the library and then sent to the RH endpoint.

I decided to dig in and try the REST API function manually by defining the payload dict myself:

payload = {
        'account': account['url'],
        'instrument': r.robinhood.orders.get_instruments_by_symbols(ticker, info='url')[0],
        'symbol' : ticker,
        'price' : 1,
        'quantity': quantity,
        'ref_id': str(uuid4()),
        'type': 'market',
        'trailing_peg' : {'type': 'percentage', 'percentage': str(5)},
        'stop_price': stopPrice,
        'time_in_force': 'gtc', 
        'trigger': 'stop', 
        'side': 'sell', 
        'order_form_version': 4, 
    }

    url = r.robinhood.orders.orders_url()
    print(payload)
    data = r.robinhood.request_post(url, payload, json=True, jsonify_data=True)
    return data

However, it returns:

{'non_field_errors': ['Stop market sell order requested, but price provided.']}

If I remove the stop_price it says:

{'non_field_errors': ['Stop limit order requested, but no stop price provided.']}

What am I doing wrong here?

Robiemaan avatar Feb 13 '24 18:02 Robiemaan