def get_roll_state_required(
        roll_data: RollDataWithStateReporting) -> RollState:
    invalid_input = True
    while invalid_input:
        roll_data.display_roll_query_banner()
        roll_state_required_as_str = print_menu_of_values_and_get_response(
            roll_data.allowable_roll_states_as_list_of_str)

        if roll_state_required_as_str != roll_data.original_roll_status_as_string:
            # check if changing
            print("")
            okay_to_change = true_if_answer_is_yes(
                "Changing roll state for %s from %s to %s, are you sure y/n to try again/<RETURN> to exit: "
                % (
                    roll_data.instrument_code,
                    roll_data.original_roll_status_as_string,
                    roll_state_required_as_str,
                ),
                allow_empty_to_return_none=True)
            print("")
            if okay_to_change is None:
                return no_change_required

            if okay_to_change:
                # happy
                return RollState[roll_state_required_as_str]
            else:
                print("OK. Choose again.")
                # back to top of loop
                continue
        else:
            print("No change")
            return no_change_required
def _get_roll_adjusted_multiple_prices_object_ffill_option(data: dataBlob,
                                                 instrument_code: str) \
            -> rollingAdjustedAndMultiplePrices:

    ## returns failure if goes wrong
    try_forward_fill = \
        true_if_answer_is_yes("Do you want to try forward filling prices first (less accurate, but guarantees roll)? [y/n]")

    if not try_forward_fill:
        print("OK, nothing I can do")
        return failure

    try:
        rolling_adj_and_mult_object = rollingAdjustedAndMultiplePrices(
            data, instrument_code, allow_forward_fill=True)
        ## We do this as getting the object doesn't guarantee it works
        _unused_ = rolling_adj_and_mult_object.updated_multiple_prices

    except Exception as e:
        print(
            "Error %s when trying to calculate roll prices, even when forward filling"
            % str(e))
        return failure

    return rolling_adj_and_mult_object
Example #3
0
def interactively_get_config_overrides_for_cleaning(data) -> priceFilterConfig:
    default_config = get_config_for_price_filtering(data)
    print("Current data cleaning configuration: %s" % str(default_config))
    make_changes = true_if_answer_is_yes("Make changes?")
    if make_changes:
        new_config = get_field_names_for_named_tuple(default_config)
        print("New config %s" % str(new_config))
        return new_config
    else:
        return default_config
def account_curve_report(data: dataBlob):
    run_full_report = true_if_answer_is_yes('Run normal full report? (alternative is customise dates)')
    if run_full_report:
        start_date = arg_not_supplied
        end_date = arg_not_supplied
    else:
        start_date, end_date = get_report_dates()

    report_config = email_or_print_or_file(account_curve_report_config)
    report_config.modify_kwargs(
         start_date=start_date, end_date=end_date
    )
    run_report(report_config, data = data)
Example #5
0
def adjust_capital_for_delta(data: dataBlob):
    data_capital = dataCapital(data)

    capital_delta = get_and_convert(
        "What change have you made to brokerage account that will not change capital +ve deposit, -ve withdrawal",
        type_expected=float,
    )
    old_capital = data_capital.get_series_of_broker_capital()[-1]
    new_capital = old_capital + capital_delta
    user_wants_adjustment = true_if_answer_is_yes(
        "New brokerage capital will be %f, are you sure? " % new_capital)
    if user_wants_adjustment:
        data_capital.total_capital_calculator.adjust_broker_account_for_delta(
            capital_delta)
def roll_adjusted_and_multiple_prices(
        data: dataBlob,
        instrument_code: str,
        confirm_adjusted_price_change: bool = True) -> status:
    """
    Roll multiple and adjusted prices

    THE POSITION MUST BE ZERO IN THE PRICED CONTRACT! WE DON'T CHECK THIS HERE

    :param data: dataBlob
    :param instrument_code: str
    :return:
    """
    print(landing_strip(80))
    print("")
    print("Rolling adjusted prices!")
    print("")
    rolling_adj_and_mult_object = get_roll_adjusted_multiple_prices_object(
        data=data, instrument_code=instrument_code)

    if rolling_adj_and_mult_object is failure:
        print("Error %s when trying to calculate roll prices" % str(e))
        return failure

    rolling_adj_and_mult_object.compare_old_and_new_prices()

    if confirm_adjusted_price_change:
        is_okay_to_roll = true_if_answer_is_yes(
            "Confirm roll adjusted prices for %s are you sure y/n:" %
            instrument_code)
        if not is_okay_to_roll:
            print(
                "\nUSER DID NOT WANT TO ROLL: Setting roll status back to previous state"
            )
            return failure
    else:
        print_with_landing_strips_around(
            "AUTO ROLLING - NO USER CONFIRMATION REQUIRED")

    try:
        rolling_adj_and_mult_object.write_new_rolled_data()
    except Exception as e:
        data.log.warn(
            "%s went wrong when rolling: Going to roll-back to original multiple/adjusted prices"
            % e)
        rolling_adj_and_mult_object.rollback()
        return failure

    return success
Example #7
0
def setup_initial_capital(data: dataBlob):
    (
        broker_account_value,
        total_capital,
        maximum_capital,
        acc_pandl,
    ) = get_initial_capital_values_from_user(data)
    user_agrees_to_do_this = true_if_answer_is_yes(
        "Are you *REALLY* sure about this? Will delete all existing capital (not for individual strategies)?"
    )
    if user_agrees_to_do_this:
        data_capital = dataCapital(data)
        data_capital.total_capital_calculator.create_initial_capital(
            broker_account_value,
            total_capital=total_capital,
            maximum_capital=maximum_capital,
            acc_pandl=acc_pandl,
            are_you_really_sure=True,
        )
Example #8
0
def modify_any_value(data: dataBlob):
    data_capital = dataCapital(data)

    (
        broker_account_value,
        total_capital,
        maximum_capital,
        acc_pandl,
    ) = get_values_from_user_to_modify(data=data)

    ans_is_yes = true_if_answer_is_yes(
        "Sure about this? May cause subtle weirdness in capital calculations?")
    if ans_is_yes:
        data_capital.total_capital_calculator.modify_account_values(
            broker_account_value=broker_account_value,
            total_capital=total_capital,
            maximum_capital=maximum_capital,
            acc_pandl=acc_pandl,
            propagate=True)
def get_auto_roll_parameters() -> autoRollParameters:
    min_volume = get_and_convert(
        "Minimum relative volume before rolling",
        type_expected=float,
        allow_default=True,
        default_value=0.1,
    )

    manual_prompt_for_position = true_if_answer_is_yes(
        "Manually prompt for state if have position? (y/n)")

    if manual_prompt_for_position:
        state_when_position_held = no_change_required
    else:
        state_when_position_held = get_state_to_use_for_held_position()

    auto_parameters = autoRollParameters(
        min_volume=min_volume,
        manual_prompt_for_position=manual_prompt_for_position,
        state_when_position_held=state_when_position_held,
    )

    return auto_parameters
Example #10
0
def update_capital_from_ib(data: dataBlob):

    data_capital = dataCapital(data)
    broker_account_value = get_broker_account_value(data)
    try:
        total_capital = (
            data_capital.
            update_and_return_total_capital_with_new_broker_account_value(
                broker_account_value))
        print("New total capital is %s" % total_capital)

    except LargeCapitalChange:
        ans_is_yes = true_if_answer_is_yes(
            "Do you want to try again, without checking for large capital changes??"
        )
        if ans_is_yes:
            total_capital = data_capital.update_and_return_total_capital_with_new_broker_account_value(
                broker_account_value, check_limit=A_VERY_LARGE_NUMBER)
        else:
            print("Capital not updated")
            return failure

    print("New total capital is %s" % str(total_capital))
def build_and_write_roll_calendar(
    instrument_code,
    output_datapath=arg_not_supplied,
    check_before_writing=True,
    input_prices=arg_not_supplied,
    input_config=arg_not_supplied,
):

    if output_datapath is arg_not_supplied:
        print(
            "*** WARNING *** This will overwrite the provided roll calendar. Might be better to use a temporary directory!"
        )
    else:
        print("Writing to %s" % output_datapath)

    if input_prices is arg_not_supplied:
        prices = arcticFuturesContractPriceData()
    else:
        prices = input_prices

    if input_config is arg_not_supplied:
        rollparameters = mongoRollParametersData()
    else:
        rollparameters = input_config

    csv_roll_calendars = csvRollCalendarData(output_datapath)

    dict_of_all_futures_contract_prices = prices.get_all_prices_for_instrument(
        instrument_code)
    dict_of_futures_contract_prices = dict_of_all_futures_contract_prices.final_prices(
    )

    roll_parameters_object = rollparameters.get_roll_parameters(
        instrument_code)

    # might take a few seconds
    print("Prepping roll calendar... might take a few seconds")
    roll_calendar = rollCalendar.create_from_prices(
        dict_of_futures_contract_prices, roll_parameters_object)

    # checks - this might fail
    roll_calendar.check_if_date_index_monotonic()

    # this should never fail
    roll_calendar.check_dates_are_valid_for_prices(
        dict_of_futures_contract_prices)

    # Write to csv
    # Will not work if an existing calendar exists

    if check_before_writing:
        check_happy_to_write = true_if_answer_is_yes(
            "Are you ok to write this csv to path %s/%s.csv? [might be worth writing and hacking manually]?"
            % (csv_roll_calendars.datapath, instrument_code))
    else:
        check_happy_to_write = True

    if check_happy_to_write:
        print("Adding roll calendar")
        csv_roll_calendars.add_roll_calendar(instrument_code,
                                             roll_calendar,
                                             ignore_duplication=True)
    else:
        print("Not writing")

    return roll_calendar