this is my code: # Use ProcessPoolExecutor for parallel processing with…
- User
this is my code: # Use ProcessPoolExecutor for parallel processing with ProcessPoolExecutor(max_workers=num_processes) as executor: results = list(tqdm(executor.map(get_bpr, row_contract_pairs), total=len(row_contract_pairs), desc='Getting BPR')) that is located in a def.
the get_bpr is found outside that function.
find bpr
def get_bpr(index_row_contract): index, row, contract = index_row_contract # Blank variables for new Search placed_order_response = None data = {'symbol': None, 'short_bpr_margin': None, 'short_bpr_cash': None, 'long_bpr_margin': None, 'long_bpr_cash': None, 'fee_calculation_total_fees': None, 'price_candidate_used': None, 'sold_price': None, } # Set symbol to allow connection data['symbol'] = row['symbol']
# Convert back to Option and FutureOption Classes contract = contract_list[index] # Create Legs leg = contract.build_leg(Decimal('1'), order.OrderAction.SELL_TO_OPEN) buy_leg = contract.build_leg(Decimal('1'), order.OrderAction.BUY_TO_OPEN) # Try prices in price_candidates until results for candidate in row['price_candidates']: try: # Save Candidates data['price_candidate_used'] = candidate # Rounds to the nearest candidate price price_bid = round_mid_price(row['midPrice'], candidate) data['sold_price'] = price_bid # Sell Side order_to_fill = order.NewOrder( time_in_force=order.OrderTimeInForce.GTC, order_type=order.OrderType.LIMIT, price= price_bid, legs=[leg], price_effect=order.PriceEffect.CREDIT ) # Results of margin account for sell_leg placed_order_response_margin = margin_account.place_order(session, order_to_fill, dry_run=True) if placed_order_response_margin is not None: data['short_bpr_margin'] = placed_order_response_margin.buying_power_effect.change_in_buying_power data['fee_calculation_total_fees'] = placed_order_response_margin.fee_calculation.total_fees try: # Results of cash account for sell_leg placed_order_response_cash = cash_account.place_order(session, order_to_fill, dry_run=True) data['short_bpr_cash'] = placed_order_response_cash.buying_power_effect.change_in_buying_power except Exception as e: # Errors will occur when BPR is not enough in cash account or if trying to short call a ticker less then $10 in value data['short_bpr_cash'] = -float('inf') if row['instrument_type']=='FUTURE_OPTION': # Buy Side order_to_fill = order.NewOrder( time_in_force=order.OrderTimeInForce.GTC, order_type=order.OrderType.LIMIT, price= price_bid, legs=[buy_leg], price_effect=order.PriceEffect.DEBIT ) # Results of margin account for buy_leg placed_order_response_margin = margin_account.place_order(session, order_to_fill, dry_run=True) if placed_order_response_margin is not None: data['long_bpr_margin'] = placed_order_response_margin.buying_power_effect.change_in_buying_power try: # Results of margin account for buy_leg placed_order_response_cash = cash_account.place_order(session, order_to_fill, dry_run=True) data['long_bpr_cash'] = placed_order_response_cash.buying_power_effect.change_in_buying_power except Exception as e: # Errors will occur when BPR is not enough in cash account or if trying to short call a ticker less then $10 in value data['long_bpr_cash'] = -float('inf') else: data['long_bpr_margin'] = price_bid * row['notional_multiplier'] data['long_bpr_cash'] = price_bid * row['notional_multiplier'] if data['long_bpr_cash']>= float(cash_balance['derivative_buying_power'].values[0]): data['long_bpr_cash'] = -float('inf') return data except Exception as e: # Log the exception logging.error(f"Error processing symbol {row['symbol']}: {str(e)}") return data - Assistant
The error message indicates that the name 'contract_list' is not defined in the scope where the function
get_bpris called.Since
contract_listis used in theget_bprfunction, you need to make sure that it is defined before calling the function. You can definecontract_listbefore the code block with thewith ProcessPoolExecutorstatement, or pass it as an argument to theget_bprfunction when calling it in the parallel processing section.Here's an example fix where
contract_listis defined before the parallel processing section:# Define contract_list here contract_list = ... # Use ProcessPoolExecutor for parallel processing with ProcessPoolExecutor(max_workers=num_processes) as executor: results = list(tqdm(executor.map(get_bpr, row_contract_pairs), total=len(row_contract_pairs), desc='Getting BPR'))Make sure to replace
...with the actual code to definecontract_listbased on your requirements.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.