robin_stocks icon indicating copy to clipboard operation
robin_stocks copied to clipboard

Currently not able to get expiration date from r.get_option_market_data_by_id(

Open millionarecoin opened this issue 1 year ago • 4 comments

Currently not able to get expirationDate from r.get_option_market_data_by_id(option_id). Can you make expirationdate as a field please.

Need this to issue sell order through order_sell_option_limit('close', 'credit', current_price_for_option, current_holding_stock, quantity, expirationDate, strike, # optionType='both', timeInForce='gtc', jsonify=True):

Screenshot from 2023-04-23 01-55-32

millionarecoin avatar Apr 23 '23 05:04 millionarecoin

Can we get strike also from the function r.get_option_market_data_by_id(option_id). Will save a lot of processing time trying to circumvent the data which is coming out from other fields.

millionarecoin avatar Apr 23 '23 16:04 millionarecoin

get_option_market_data_by_id didn't work for me. How did you get it work? I got this error: 403 Client Error: Forbidden for url..

blueslabs avatar Apr 24 '23 11:04 blueslabs

Here is my code. I was able to fix the problem by extracting the option contract expiration date and strike from a different field. But these fields should be available by default on all option functions.

If you cannot add those fields, you can use the below code as part of your examples.



import robin_stocks
import pyotp
import robin_stocks.robinhood as r
import robin_stocks.tda as t
import json

with open("secrets.txt") as f:
    lines = f.readlines()
    username = lines[0].strip()
    password = lines[1].strip()
    QR = lines[2].strip()
    # print(f"USERNAME={username}, PASSWORD={password}, QR={QR}")



totp = pyotp.TOTP(QR).now()
login = r.login(username, password, mfa_code=totp)



get_open_option_positions = r.get_open_option_positions()
print('current positions: ', get_open_option_positions)
current_position_symbols = []

get_all_open_option_orders = r.get_all_open_option_orders(info=None)
print('get_all_open_option_orders:   ', get_all_open_option_orders)

y = 1
for j in range(len(get_open_option_positions)):
    current_holding_stock = get_open_option_positions[j]['chain_symbol']
    print('current holding stock:  ', current_holding_stock)
    option_id = get_open_option_positions[j]['option_id']
    print('option id:  ', option_id)
    buy_price = get_open_option_positions[j]['average_price']
    quantity = get_open_option_positions[j]['quantity']
    quantity = int(float(quantity))

    chain_symbol = get_open_option_positions[j]['chain_symbol']
    print('current symbol: ', chain_symbol)
    current_position_symbols.append(chain_symbol)
    a = 1
    get_option_market_data_by_id = r.get_option_market_data_by_id(option_id)
    current_price_for_option = get_option_market_data_by_id[0]['bid_price']
    current_price_for_option = round(float(current_price_for_option) * 100, 3)
    occ_value = get_option_market_data_by_id[0]['occ_symbol']
    expirationdate = '20' + occ_value[6:8] + "-" + occ_value[8:10] + "-" + occ_value[10:12]
    strikeprice = int(float(occ_value[13:21])/1000)
    y = 1
    #book profit of 200 or greater begin
    if (current_price_for_option * quantity) - (float(buy_price) * quantity) > 200:
        sell_order = r.order_sell_option_limit('close', 'credit', current_price_for_option, current_holding_stock, quantity, expirationdate, strikeprice,
                           "call", timeInForce='gtc') #, jsonify=True)
        print('sell order response: ', sell_order)
    # book profit of 200 or greater end
a = 1
# print('current symbol list: ', current_position_symbols)

millionarecoin avatar Apr 24 '23 11:04 millionarecoin

Thanks. It seems I need to have a open option in order to make this work? I don't have any opening option in RH yet. And this code didn't work for me. Are you able to get market data for a random option which you don't own yet?

option_id = id_for_option('TSLA', '2023-04-28', 145, 'put') market_data = r.get_option_market_data_by_id(option_id)

blueslabs avatar Apr 25 '23 03:04 blueslabs