Anyone have a clue why the code below generates the error "UnboundLocalError: local variable 'order_id' referenced before assignment" on the line:
filled = bool(get_order(order_id)['filled'])
I'm running on minute data, if it matters.
def initialize(context):
context.first = True
context.stock = sid(24)
def handle_data(context, data):
record(Price = data[context.stock].close_price)
record(OpenOrders = 1 if bool(get_open_orders(context.stock)) else 0)
record(Notional = context.portfolio.positions[context.stock].amount * data[context.stock].close_price)
if context.first:
order_id = order(context.stock, 100, limit_price= 450)
context.first = False
filled = bool(get_order(order_id)['filled'])
if filled:
order(context.stock, -100, stop_price = 400)