def change_position_limit_for_instrument_strategy(data): view_position_limit(data) data_position_limits = dataPositionLimits(data) strategy_name = get_valid_strategy_name_from_user(data, allow_all=False, source="positions") instrument_code = get_valid_instrument_code_from_user(data, allow_all=False) new_position_limit = get_and_convert( "New position limit?", type_expected=int, allow_default=True, default_value=-1, default_str="No limit", ) instrument_strategy = instrumentStrategy(instrument_code=instrument_code, strategy_name=strategy_name) if new_position_limit == -1: data_position_limits.delete_position_limit_for_instrument_strategy( instrument_strategy) else: new_position_limit = abs(new_position_limit) data_position_limits.set_position_limit_for_instrument_strategy( instrument_strategy, new_position_limit)
def set_position_limit_for_instrument( data, instrument_code: str, auto_parameters: parametersForAutoPopulation ): data_position_limits = dataPositionLimits(data) existing_position_limit = data_position_limits._get_position_limit_object_for_instrument(instrument_code) max_position_int = get_max_rounded_position_for_instrument( data, instrument_code=instrument_code, auto_parameters = auto_parameters ) if np.isnan(max_position_int): print( "Can't get standard position for %s, not setting max position" % instrument_code ) else: print("Update limit for %s from %s to %d" % (instrument_code, str(existing_position_limit.position_limit), max_position_int)) data_position_limits.set_abs_position_limit_for_instrument( instrument_code, max_position_int )
def set_position_limit_for_instrument(data, instrument_code, risk_multiplier): data_position_limits = dataPositionLimits(data) max_position_int = get_max_position_for_instrument(data, instrument_code, risk_multiplier) if np.isnan(max_position_int): print("Can't get standard position for %s, not setting max position" % instrument_code) else: data_position_limits.set_abs_position_limit_for_instrument( instrument_code, max_position_int)
def get_instrument_limits_as_df(data): data_position_limits = dataPositionLimits(data) instrument_limits = data_position_limits.get_all_instrument_limits_and_positions( ) instrument_limits_as_df = df_from_list_of_limits_and_positions( instrument_limits) return instrument_limits_as_df
def get_strategy_instrument_limits_as_df(data): data_position_limits = dataPositionLimits(data) strat_instrument_limits = data_position_limits.get_all_strategy_instrument_limits_and_positions( ) strat_instrument_limits_as_df = df_from_list_of_limits_and_positions( strat_instrument_limits) return strat_instrument_limits_as_df
def roll_state_was_closed_now_something_else(data: dataBlob, instrument_code: str): print( "Roll state is no longer closed, so removing temporary position limit of zero" ) data_position_limits = dataPositionLimits(data) data_position_limits.reset_position_limit_for_instrument_to_original_value( instrument_code)
def change_position_limit_for_instrument(data): data_position_limits = dataPositionLimits(data) instrument_code = get_valid_instrument_code_from_user(data, allow_all=False) new_position_limit = get_and_convert("New position limit?", type_expected=int, allow_default=True, default_str="No limit", default_value=-1) if new_position_limit==-1: data_position_limits.delete_position_limit_for_instrument(instrument_code) else: new_position_limit = abs(new_position_limit) data_position_limits.set_abs_position_limit_for_instrument(instrument_code, new_position_limit)
def view_position_limit(data): data_position_limits = dataPositionLimits(data) instrument_limits = data_position_limits.get_all_instrument_limits_and_positions() strategy_instrument_limits = data_position_limits.get_all_strategy_instrument_limits_and_positions() print("\nInstrument limits across strategies\n") for limit_tuple in instrument_limits: print(limit_tuple) print("\nInstrument limits per strategy\n") for limit_tuple in strategy_instrument_limits: print(limit_tuple)
def get_maximum_position_contracts_for_instrument_strategy( data: dataBlob, instrument_strategy: instrumentStrategy) -> int: override = get_override_for_instrument_strategy(data, instrument_strategy) if override == CLOSE_OVERRIDE: return 0 position_limit_data = dataPositionLimits(data) maximum = position_limit_data.get_maximum_position_contracts_for_instrument_strategy( instrument_strategy) if maximum is NO_LIMIT: return ARBITRARILY_LARGE_CONTRACT_LIMIT return maximum
def adjust_order_for_position_limits(self, order): log = order.log_with_attributes(self.log) data_position_limits = dataPositionLimits(self.data) cut_down_order = data_position_limits.cut_down_proposed_instrument_trade_okay(order) if cut_down_order.trade != order.trade: if cut_down_order.is_zero_trade(): ## at position limit, can't do anything log.warn("Can't trade at all because of position limits %s" % str(order)) else: log.warn("Can't do full trade of %s because of position limits, can only do %s" % (str(order), str(cut_down_order.trade))) return cut_down_order
def get_maximum_position_contracts_for_instrument_strategy( data: dataBlob, instrument_strategy: instrumentStrategy, previous_position: int = 0) -> int: override = get_override_for_instrument_strategy(data, instrument_strategy) if override == CLOSE_OVERRIDE: return 0 position_limit_data = dataPositionLimits(data) spare = int( position_limit_data.get_spare_checking_all_position_limits( instrument_strategy)) maximum = int(spare) + abs(previous_position) return maximum
def set_position_limit_for_instrument(data, instrument_code: str, risk_multiplier: float, max_leverage: float): data_position_limits = dataPositionLimits(data) max_position_int = get_max_position_for_instrument( data, instrument_code=instrument_code, risk_multiplier=risk_multiplier, max_leverage=max_leverage, ) if np.isnan(max_position_int): print("Can't get standard position for %s, not setting max position" % instrument_code) else: print("Update limit for %s with %d" % (instrument_code, max_position_int)) data_position_limits.set_abs_position_limit_for_instrument( instrument_code, max_position_int)
def roll_state_is_now_closing(data: dataBlob, instrument_code: str): print("Roll state is Close, so setting temporary position limit of zero") data_position_limits = dataPositionLimits(data) data_position_limits.temporarily_set_position_limit_to_zero_and_store_original_limit( instrument_code)