def clear_completed_modifications_from_instrument_and_contract_stacks(data): stack_handler = stackHandler(data) ans = input("Clear all completed modifications from instrument and contract stacks, sure? (Y/other") if ans =="Y": stack_handler.clear_completed_modifications_from_instrument_and_contract_stacks() return None
def all_instrument_unlock(data): data_locks = dataLocks(data) list_of_locks = data_locks.get_list_of_locked_instruments() print("Locked %s" % list_of_locks) ans = input("Unlock everything [Y]es/no ?") if ans == "Y": stack_handler = stackHandler(data) stack_handler.clear_position_locks_no_checks()
def create_manual_trade(data): print( "Create a trade which will then be executed by the system (so don't use this if you are doing your trades manually)" ) print( "Use case is testing, or forcing an emergency close early (perhaps roll related)" ) instrument_order = enter_manual_instrument_order(data) ans = input( "Would you also like to create a contract order (if not stack generator will auto generate)? (y/other)" ) if ans == "y": contract_order = enter_manual_contract_order(data, instrument_order) else: contract_order = None print(instrument_order) print(contract_order) ans = input("Are you sure? (Y/other)") if ans != "Y": return None stack_handler = stackHandler(data) instrument_order_id = ( stack_handler.instrument_stack.put_manual_order_on_stack_and_return_order_id( instrument_order ) ) if not isinstance(instrument_order_id, int): print( "Error condition %s couldn't place instrument order; not doing contract order either" % str(instrument_order_id) ) return None if contract_order is not None: contract_order.parent = instrument_order_id contract_order_id = stack_handler.contract_stack.put_order_on_stack( contract_order ) if not isinstance(contract_order_id, int): print( "Error condition %s couldn't place contract order; see if you can spawn it manually" ) return None stack_handler.instrument_stack.add_children_to_order_without_existing_children( instrument_order_id, [contract_order_id] ) print( "For instant execution, you may want to do menu [1] create orders, menu [13] create broker orders" ) return None
def clear_algo_on_order(data): stack_handler = stackHandler(data) stack = stack_handler.contract_stack view_generic_stack(stack) order_id = get_and_convert("Order ID ", type_expected=int, allow_default=False) order = stack.get_order_with_id_from_stack(order_id) print("Controlled by %s; releasing now" % str(order.reference_of_controlling_algo)) stack.release_order_from_algo_control(order_id) print("Released")
def view_contract_stack(data): stack_handler = stackHandler(data) order_ids = stack_handler.contract_stack.get_list_of_order_ids() print("\nCONTRACT STACK \n") for order_id in order_ids: order = stack_handler.contract_stack.get_order_with_id_from_stack( order_id) print(order)
def generate_generic_manual_fill(data): stack = resolve_stack(data, exclude_instrument_stack=True) view_generic_stack(stack) order_id = get_and_convert("Enter order ID", default_str="Cancel", default_value="") if order_id == "": return None order = stack.get_order_with_id_from_stack(order_id) if order is missing_order: print("Order doesn't exist on stack") return None if len(order.trade) > 1: print( "Can't manually fill spread orders; delete and replace with legs") return None if not order.no_children(): print( "Don't manually fill order with children: can cause problems! Manually fill the child instead" ) return None print("Order now %s" % str(order)) fill_qty = get_and_convert( "Quantity to fill (must be less than or equal to %s)" % str(order.trade), type_expected=int, allow_default=True, default_value=order.trade, ) if isinstance(fill_qty, int): fill_qty = [fill_qty] filled_price = get_and_convert("Filled price", type_expected=float, allow_default=False) fill_datetime = get_datetime_input("Fill datetime", allow_default=True) order = stack.get_order_with_id_from_stack(order_id) order.fill_order(fill_qty=fill_qty, filled_price=filled_price, fill_datetime=fill_datetime) stack.mark_as_manual_fill_for_order_id(order_id) stack_handler = stackHandler() if type(order) is brokerOrder: ## pass up and change positions stack_handler.apply_broker_order_fills_to_database(order) else: stack_handler.apply_contract_order_fill_to_database(order) order = stack.get_order_with_id_from_stack(order_id) print("Order now %s" % str(order)) print( "If stack process not running, your next job will be to pass fills upwards" )
def view_contract_stack(data): stack_handler = stackHandler(data) order_ids = stack_handler.contract_stack.get_list_of_order_ids() print("\nCONTRACT STACK \n") broker_data = dataBroker(data) for order_id in order_ids: order = stack_handler.contract_stack.get_order_with_id_from_stack(order_id) IB_code = broker_data.get_brokers_instrument_code(order.instrument_code) print("%s:%s" % (IB_code, order.terse_repr()))
def complete_modification_for_contract(data): stack_handler = stackHandler(data) order_id = get_and_convert("Which contract order ID", default_value=0, default_str="CANCEL", type_expected=int) if order_id ==0: return None else: stack_handler.contract_stack.completed_modifying_order_on_stack(order_id) print("If you are trading manually, you will now want to pass the modification complete from contract to instruments")
def modify_instrument_order(data): stack_handler = stackHandler(data) order_id = get_and_convert("Enter order ID", type_expected=int, default_str="Cancel", default_value=0) if order_id ==0: return None order = stack_handler.instrument_stack.get_order_with_id_from_stack(order_id) print("Existing order %s" % str(order)) new_qty = get_and_convert("New quantity (zero to cancel)", type_expected=int, default_value = order.trade) stack_handler.instrument_stack.modify_order_on_stack(order_id, new_qty) print("You will probably want to push modifications down to contract orders next, if not running on auto")
def end_of_day(data): print( "Will cancel all broker orders, get outstanding fills, mark all orders as complete, update positions, remove everything from stack" ) ans = input("Are you sure? (Y/other)") if ans != "Y": return None stack_handler = stackHandler(data) stack_handler.safe_stack_removal() return None
def generate_force_roll_orders(data): stack_handler = stackHandler(data) print("This will generate force roll orders") instrument_code = input("Which instrument? <RETURN for default: All instruments>") ans = input("Are you sure? (Y/other)") if ans != "Y": return None if instrument_code == "": stack_handler.generate_force_roll_orders() else: stack_handler.generate_force_roll_orders_for_instrument(instrument_code)
def pass_modifications_down_to_contracts(data): print("This will pass a parent instrument order modification downwards") stack_handler = stackHandler(data) order_id = get_and_convert("Which instrument order ID", default_value="ALL", default_str="All", type_expected=int) check_ans = input("Are you sure? (Y/other)") if check_ans != "Y": return None if order_id =="ALL": stack_handler.pass_on_modification_from_instrument_to_contract_orders() else: stack_handler.pass_modification_from_parent_to_children(order_id) print("If you are trading manually, you will now want to mark contract modifications as complete")
def generate_ib_orders(data): stack_handler = stackHandler(data) print("This will create broker orders and submit to IB") contract_order_id = get_and_convert("Which contract order ID?", default_value="ALL", default_str="for all", type_expected=int) ans = input("Are you sure? (Y/other)") if ans !="Y": return None if contract_order_id=="ALL": stack_handler.create_broker_orders_from_contract_orders() else: stack_handler.create_broker_order_for_contract_order(contract_order_id) print("If stack process not running, your next job will be to get the fills from IB")
def get_list_of_timer_functions_for_stack_handler(): stack_handler_data = dataBlob(log_name="stack_handler") stack_handler = stackHandler(stack_handler_data) list_of_timer_names_and_functions = [ ("check_external_position_break", stack_handler), ("spawn_children_from_new_instrument_orders", stack_handler), ("generate_force_roll_orders", stack_handler), ("create_broker_orders_from_contract_orders", stack_handler), ("process_fills_stack", stack_handler), ("handle_completed_orders", stack_handler), ("safe_stack_removal", stack_handler), ] return list_of_timer_names_and_functions
def cancel_broker_order(data): view_broker_order_list(data) view_broker_stack(data) stack_handler = stackHandler(data) broker_order_id = get_and_convert( "Which order ID?", default_value="ALL", default_str="for all", type_expected=int ) ans = input("Are you sure? (Y/other)") if ans != "Y": return None if broker_order_id == "ALL": stack_handler.try_and_cancel_all_broker_orders_and_return_list_of_orders() else: stack_handler.cancel_broker_order_with_id_and_return_order(broker_order_id)
def get_fills_from_broker(data): stack_handler = stackHandler(data) print("This will get any fills from the broker, and write them to the broker stack") broker_order_id = get_and_convert("Which broker order ID?", default_value="ALL", default_str="for all", type_expected=int) ans = input("Are you sure? (Y/other)") if ans !="Y": return None if broker_order_id=="ALL": stack_handler.pass_fills_from_broker_to_broker_stack() else: stack_handler.apply_broker_fill_to_broker_stack(broker_order_id) print("If stack process not running, your next job will be to pass fills from broker to contract stack")
def pass_fills_upwards_from_broker(data): stack_handler = stackHandler(data) print("This will process any fills applied to broker orders and pass them up to contract orders") broker_order_id = get_and_convert("Which order ID?", default_value="ALL", default_str="for all", type_expected=int) ans = input("Are you sure? (Y/other)") if ans !="Y": return None if broker_order_id=="ALL": stack_handler.pass_fills_from_broker_up_to_contract() else: stack_handler.apply_broker_fill_to_parent_order(broker_order_id) print("If stack process not running, your next job will be to pass fills from contract to instrument")
def handle_completed_orders(data): stack_handler = stackHandler(data) print("This will process any completed orders (all fills present)") instrument_order_id = get_and_convert("Which instrument order ID?", default_str="All", default_value="ALL", type_expected=int) ans = input("Are you sure? (Y/other)") if ans != "Y": return None if instrument_order_id == "ALL": stack_handler.handle_completed_orders() else: stack_handler.handle_completed_instrument_order(instrument_order_id)
def spawn_contracts_from_instrument_orders(data): stack_handler = stackHandler(data) print("This will create contract orders for any instrument orders that don't have them") order_id = get_and_convert("Which instrument order ID", default_value="ALL", default_str="All", type_expected=int) check_ans = input("Are you sure? (Y/other)") if check_ans != "Y": return None if order_id =="ALL": stack_handler.spawn_children_from_new_instrument_orders() else: stack_handler.spawn_children_from_instrument_order_id(order_id) print("If you are trading manually, you should now view the contract order stack and trade.") print("Then create manual fills for contract orders")
def resolve_stack(data, exclude_instrument_stack = False): stack_handler = stackHandler(data) if exclude_instrument_stack: request_str = "Broker stack [1], or Contract stack [2]?" else: request_str = "Broker stack [1], Contract stack [2] or instrument stack [3]?" ans = get_and_convert(request_str, type_expected=int, default_str="Exit", default_value=0) if ans==1: stack = stack_handler.broker_stack elif ans==2: stack = stack_handler.contract_stack elif ans==3 and not exclude_instrument_stack: stack = stack_handler.instrument_stack else: return None return stack
def view_broker_stack(data): stack_handler = stackHandler(data) print("\nBROKER STACK \n") view_generic_stack(stack_handler.broker_stack)
def view_broker_stack(data): stack_handler = stackHandler(data) print("\nBroker stack (from database): \n") view_generic_stack(stack_handler.broker_stack)
def view_instrument_stack(data): stack_handler = stackHandler(data) print("\nINSTRUMENT STACK \n") view_generic_stack(stack_handler.instrument_stack)